var map;
var geocoder = new GClientGeocoder();
zat.bound = null;
zat.mapoffX = -6; zat.mapoffY = 0; // offset of map in client window
zat.movelistener = null;
// translate geocode address accuracy to map scale
//        unknown country state county city postcode street intersection address
zat.scales = [1,     4,     6,    8,    11,    13,     14,       16,        17  ]; 
// zat.minMapSize = 300; // must match #map min-height in zat.css
zat.marker = null;

// functions to resize the map:
// calc height of browser client area
function getWindowHeight() {
	if (window.self && self.innerHeight) {return self.innerHeight;}
	if (document.documentElement && document.documentElement.clientHeight) {return document.documentElement.clientHeight;}
	return 0;
}
function getWindowWidth() {
	if (window.self && self.innerWidth) {return self.innerWidth;}
	if (document.documentElement && document.documentElement.clientWidth) {return document.documentElement.clientWidth;}
	return 0;
}
// resize map when browser window resized
function resizeMap() {
	var offTop = 0;
	var mapElem = $("map");
	for (var elem = mapElem; elem != null; elem = elem.offsetParent) { offTop += elem.offsetTop; }
	zat.mapoffY = offTop;
	var height = getWindowHeight() - offTop - 27;
	// if (height < zat.minMapSize) {
	//	height = getWindowHeight() - 27;
	//	document.body.style.overflow = 'scroll';
	// } else document.body.style.overflow = 'hidden';
	mapElem.style.height = height + "px";
	var tabsElem = $("tabs");
	if (tabsElem != undefined) { // sidebar for search
		tabsElem.style.height = (height + 16) + "px";
		var width = getWindowWidth();
		tabsElem.style.width = "170px";
		mapElem.style.width = (width - 184) + "px";
		zat.mapoffX = 190;
	}
	if (map) map.checkResize();
	if (zat.movelistener !== null) zat.movelistener();
}

// functions for Geocoding:
// test response from Google geocoder
function geoResponseOk(response) {
	if (response && response.Status.code != undefined && response.Status.code == G_GEO_SUCCESS) return true;
	switch(response.Status.code) {
		case G_GEO_SERVER_ERROR: alert('Google Geocoder Server Error'); break;
		case G_GEO_MISSING_ADDRESS: alert('Missing Address'); break;
		case G_GEO_UNKNOWN_ADDRESS: alert('Unknown Address'); break;
		case G_GEO_UNAVAILABLE_ADDRESS: alert('Unavailable Address!'); break;
		default: alert('Unknown Google Geocoder Error ('+response.Status.code+')');
	}
	return false;
}

// move map from Google Geocoder
function gcback(response) {
	if (!geoResponseOk(response)) return;
	if (undefined == response.Placemark[0]) alert("invalid geocode placemark");
	var place = response.Placemark[0];
	if (undefined == place.AddressDetails.Accuracy) alert("invalid geocode accuracy");
	var scale = place.AddressDetails.Accuracy;
	if (scale == 0) {
		alert("Unknown Address");
		return;
	}
	if (undefined == place.Point.coordinates[1]) alert("invalid geocode latitude coordinate");
	if (undefined == place.Point.coordinates[0]) alert("invalid geocode longitude coordinate");
	var lat = place.Point.coordinates[1];
	var lng = place.Point.coordinates[0];
	if (lat > 90 || lat < -90) alert("invalid latitude: " + lat);
	if (lng > 180 || lat < -180) alert("invalid longitude: " + lng);
	var point = new GLatLng(lat, lng);
	map.setCenter(point, zat.scales[scale]);
	alert(scale);
	if (scale >= 7) { // intersection or address-level accuracy
		if (zat.marker == null) {
			zat.marker = new GMarker(point, {draggable: true});
			map.addOverlay(zat.marker);
		} else zat.marker.setPoint(point);
		$('lat').value = lat;
		$('lng').value = lng;
	}
}

// invoke Google geocoder
function geocode(addr) {
	addr = prompt('Enter location to find', addr)
	if (addr != null) geocoder.getLocations(addr, gcback);
}

// zoom and pan map to show an Area, specified by boundary
function showArea() {
	var poh = $('partof_hidden').value;
	if (poh.length == 0) {
		alert("must specify an existing area");
		return;
	}
	poh = poh.split(':', 5);
	var b = new GLatLngBounds(new GLatLng(poh[2], poh[4]), new GLatLng(poh[1], poh[3]));
	var p = b.getCenter();
	if (isNaN(p.lat())) { alert('Invalid view: '+poh); return; }
	map.setZoom(map.getBoundsZoomLevel(b));
	map.panTo(p);
}

// save current map view
function saveView() {
	var ll = map.getCenter();
	document.cookie = 'zatview='+getmaptype()+'|'+map.getZoom()+'|'+ll.lat()+'|'+ll.lng();
	// if (zat.movelistener !== null) zat.movelistener();
};

// zoom map using mouse wheel
function wheelZoom(a) { 
	if ((a.detail || -a.wheelDelta) < 0) map.zoomIn(); 
    else map.zoomOut();
}

// return integer map type (only works for built-in types)
function getmaptype() {
	var t = map.getCurrentMapType();
	for (var i = G_DEFAULT_MAP_TYPES.length; i > 0; i--) if (G_DEFAULT_MAP_TYPES[i] == t) break;
	return i;
}


