function getHeight()
{
	var y = 600;
	if (self.innerHeight) // all except Explorer
	{
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		y = document.body.clientHeight;
	}
	return y;
}

function getWidth()
{
	var x = 700;
	if (self.innerWidth) // all except Explorer
	{
		x = self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
	}
	return x;
}

function checkHeight()
{
	var newHeight = getHeight() - 102;
	$("map").style.height = newHeight + "px";
	var newHeight2 = newHeight - 24;
	$("about").style.height = newHeight2 + "px";
	$("contact").style.height = newHeight2 + "px";
	
	var newWidth = getWidth() - 196;
	$("about").style.width = newWidth + "px";
	$("contact").style.width = newWidth + "px";
}

function getStates()
{
	var info = new Array();
	
	info[0] = new Array();
	info[0]["state"] = "California";
	info[0]["abbr"] = "ca";
	info[0]["ctrlon"] = -119.6;
	info[0]["ctrlat"] = 36.5;
	
	info[1] = new Array();
	info[1]["state"] = "Colorado";
	info[1]["abbr"] = "co";
	info[1]["ctrlon"] = -105.55;
	info[1]["ctrlat"] = 38.9;
	
	info[2] = new Array();
	info[2]["state"] = "Florida";
	info[2]["abbr"] = "fl";
	info[2]["ctrlon"] = -81.5;
	info[2]["ctrlat"] = 27.1;
	
	info[3] = new Array();
	info[3]["state"] = "Maryland";
	info[3]["abbr"] = "md";
	info[3]["ctrlon"] = -76.35;
	info[3]["ctrlat"] = 38.9;
	
	info[4] = new Array();
	info[4]["state"] = "North Carolina";
	info[4]["abbr"] = "nc";
	info[4]["ctrlon"] = -79.2;
	info[4]["ctrlat"] = 35.5;

	info[5] = new Array();
	info[5]["state"] = "Texas";
	info[5]["abbr"] = "tx";
	info[5]["ctrlon"] = -99.1;
	info[5]["ctrlat"] = 31.6;
	
	info[6] = new Array();
	info[6]["state"] = "Virginia";
	info[6]["abbr"] = "va";
	info[6]["ctrlon"] = -78.35;
	info[6]["ctrlat"] = 37.4;
	
	return(info);
}

function mapUS(doRecenter)
{
	if (doRecenter == true)
	{
		map.setCenter(new GLatLng(37.9, -96.1), 4);
	}
	
	// add state markers
	for (var i = 0; i < states.length; i++)
	{
		var newPoint = new GLatLng(states[i]["ctrlat"], states[i]["ctrlon"]);
		var marker = new GMarker(newPoint, {title:"Map " + states[i]["state"] + " schools"});
		marker.state = states[i]["state"];
		marker.abbr = states[i]["abbr"];
		map.addOverlay(marker);
		bounds.extend(newPoint);
		
		// update tooltip
		//var topElement = marker.iconImage;
		//if (marker.transparentIcon) {topElement = marker.transparentIcon;}
		//if (marker.imageMap) {topElement = marker.imageMap;}
		//topElement.setAttribute( "title" , "Map " + marker.state + " schools" );
	}
	
	if (doRecenter == true)
	{
		bounds.extend(new GLatLng(bounds.getNorthEast().lat() + 2.5, bounds.getNorthEast().lng() + 2.5));
		bounds.extend(new GLatLng(bounds.getSouthWest().lat() - 2.5, bounds.getSouthWest().lng() - 2.5));
		
		map.setZoom(map.getBoundsZoomLevel(bounds));
		map.setCenter(bounds.getCenter());
	}
	
}

function createMarker(point,html)
{
	var marker = new GMarker(point, icon05);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

function mapRecent()
{
	if (map.overlays && map.overlays.length && map.overlays.length > 100)
	{
		// show existing markers
		for (var i = 0; i < map.overlays.length; i++)
		{
			map.overlays[i].display(true);
		}
	}
	else
	{
		// create new markers		
		var requestUrl = "getrecent.php";
		var request = GXmlHttp.create();
		request.open("GET", requestUrl, true);
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				var xmlDoc = request.responseXML;
				var centers = xmlDoc.documentElement.getElementsByTagName("center");
				for (var i = 0; i < centers.length; i++)
				{
					var newlat = parseFloat(centers[i].getElementsByTagName("lat")[0].firstChild.data);
					var newlon = parseFloat(centers[i].getElementsByTagName("lon")[0].firstChild.data);
					var marker = createMarker(new GLatLng(newlat, newlon), '<strong>Lat</strong>: ' + newlat + '<br /><strong>Lon</strong>: ' + newlon);
					// GEvent.addListener(marker, "click", function() {
					//	marker.openInfoWindowHtml('<strong>Lat</strong>: ' + newlat + '<br /><strong>Lon</strong>: ' + newlon);
					// });
					map.addOverlay(marker);
				}
			}
		}
		request.send(null);
	}
	
	$("recentlink").href = "javascript:hideRecent();";
	$("recentlink").innerHTML = "Hide recent map centers";
}

function hideRecent()
{
	/*
	if (map.overlays && map.overlays.length)
	{
		alert(map.overlays.length);
		for (var i = 0; i < map.overlays.length; i++)
		{
			if (!map.overlays[i].abbr)
			{
				map.removeOverlay(overlays[i]);
			}
		}
	}
	*/

	map.getInfoWindow().hide();
	map.clearOverlays();
	mapUS(false);

	$("recentlink").href = "javascript:mapRecent();";
	$("recentlink").innerHTML = "Show 100 recent map centers";
}

function about()
{
	$("about").style.display = "block";
	$("contact").style.display = "none";
}

function closeAbout()
{
	$("about").style.display = "none";
}

function contact()
{
	$("contact").style.display = "block";
	$("about").style.display = "none";
}

function closeContact()
{
	$("contact").style.display = "none";
}

function checkZipKey(e)
{
	var key = window.event ? e.keyCode : e.which;
	if (key == 13 || key == 10) {
		// alert("checking zip");
		checkZipJS();
		return false;
	} else {
		return String.fromCharCode(key);
	}
}

function checkZip()
{
	// see if we can take the user to a map location based on zip code entered
	var zipurl = "getzipinfo.php?zip=" + encodeURI($F("zip"));
	
	new Ajax.Request(zipurl, {
		method: 'get',
		onSuccess: function(transport) {
			var state = transport.responseText.documentElement.getElementsByTagName("state")[0].firstChild.data;
			var lat = transport.responseText.documentElement.getElementsByTagName("lat")[0].firstChild.data;
			var lon = transport.responseText.documentElement.getElementsByTagName("lon")[0].firstChild.data;
			// alert("successful request\n" + state + "\n" + lat + "\n" + lon);
			switch(state) {
				case "MD":
					location.href = "/md/#" + lat + "," + lon + ",12,3";
					break;
				case "CA":
				case "CO":
				case "FL":
				case "NC":
				case "TX":
				case "VA":
					location.href = "/" + state.toLowerCase() + "/#" + lat + "," + lon + ",12,1";
					break;
				case "XX":
					alert("Sorry, that zip code isn't in our database.");
					break;
				default:
					alert("Sorry, " + state + " schools are not yet available in School Performance Maps.");
					break;
			}
		},
		onFailure: function() {
			alert('Sorry, the zip code lookup produced an error.\n\nPlease use the Select... menu or map markers to navigate to the state you want to map.');
		}
	});
	return false;
}

function checkZipJS()
{
	// see if we can take the user to a map location based on zip code entered
	var zipurl = "getzipjs.php?zip=" + encodeURI($F("zip"));
	
	new Ajax.Request(zipurl, {
		method: 'get',
		onFailure: function() {
			alert('Sorry, the zip code lookup produced an error.\n\nPlease use the Select... menu or map markers to navigate to the state you want to map.');
		}
	});
	return false;
}


