//<![CDATA[
var errorReporting = false;
var draggableMarkers = false;
var geocoder;
var map;
var address, city, state, zip, latitude, longitude;
var resultStatus = new Array();
resultStatus[0] = "We were unable to accurately determine your location. To find your location please click and drag the map or use the navigation controls to the left. You may change map views by using the buttons in the top right corner. When you have found your location click the \"Place Marker\" button below.";
resultStatus[1] = "We were unable to accurately determine your location. To find your location please click and drag the map or use the navigation controls to the left. You may change map views by using the buttons in the top right corner. When you have found your location click the \"Place Marker\" button below.";
resultStatus[2] = "We were unable to accurately determine your location. To find your location please click and drag the map or use the navigation controls to the left. You may change map views by using the buttons in the top right corner. When you have found your location click the \"Place Marker\" button below.";
resultStatus[3] = "We were unable to accurately determine your location. To find your location please click and drag the map or use the navigation controls to the left. You may change map views by using the buttons in the top right corner. When you have found your location click the \"Place Marker\" button below.";
resultStatus[4] = "Your address could not be found, but you have been taken to the town which you entered above. To find your location please click and drag the map or use the navigation controls to the left. You may change map views by using the buttons in the top right corner. When you have found your location click the \"Place Marker\" button below.";
resultStatus[5] = "Your address could not be found, but you have been taken to the zip code which you entered above. To find your location please click and drag the map or use the navigation controls to the left. You may change map views by using the buttons in the top right corner. When you have found your location click the \"Place Marker\" button below.";
resultStatus[6] = "Your address could not be found, but you have been taken to the street which you entered above. To find your location please click and drag the map or use the navigation controls to the left. You may change map views by using the buttons in the top right corner. When you have found your location click the \"Place Marker\" button below.";
resultStatus[7] = "Your address could not be found, but you have been taken to an intersection close to your location. Please click and drag the marker to your exact location. You may change map views by using the buttons in the top right corner.";
resultStatus[8] = "Your address has been found according to the information you entered above. Please click and drag the marker to your exact location. You may change map views by using the buttons in the top right corner.";
resultStatus[500] = "An error has occurred. Please check that your address was entered properly.";
resultStatus[601] = "An error has occurred. Please check that your address was entered properly.";
resultStatus[602] = "No corresponding geographic location could be found for the specified address. This may be due to the fact that the address is relatively new, or it may be incorrect.";
resultStatus[603] = "An error has occurred. Please check that your address was entered properly.";
resultStatus[610] = "An error has occurred. Please check that your address was entered properly.";

function MakeCopyrightSmaller()
{
	for(var i = 0; i < map.getContainer().childNodes.length; ++i)
		if(map.getContainer().childNodes[i].innerHTML.indexOf(String.fromCharCode(169)) !== -1)
		{
			map.getContainer().childNodes[i].style.fontSize = '6px';
			break;
		}
}

function getLocationFields(addressField, cityField, stateField, zipField, fullAddressField, cityStateZipField, latitudeField, longitudeField)
{
	address = addressField;
	city = cityField;
	state = stateField;
	zip = zipField;
	fullAddress = fullAddressField;
	cityStateZip = cityStateZipField;
	latitude = latitudeField;
	longitude = longitudeField;
}

function load()
{
	// Create new map object
	map = new GMap2(document.getElementById("map"));
	setTimeout('MakeCopyrightSmaller();', 2000);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());

	// Create new geocoding object
	geocoder = new GClientGeocoder();

	map.setCenter(new GLatLng(39.0750, -87.0800), 12);
	mapActions();
	map.setMapType(G_HYBRID_MAP);
}

function addLocation(placeName, location, showLabelOnLoad)
{
	//Clear all markers and alerts from map.
	map.clearOverlays();
	if( errorReporting ) hideMapInfoAlert();

	if (errorReporting && (!address.value || !city.value || state[state.selectedIndex].defaultSelected || !zip.value))
	{
		showMapInfoAlert("", "Please provide an address, city, state, and zip code before indicating your location on the map.");
		return;
	}

	if(typeof(location) == "string")
		geocoder.getLocations(location, addToMap);
	else
		addToMap(location, true);

	function addToMap(locationInfo, latlng)
	{
		if( errorReporting ) try
		{
			if(locationInfo.Status.code && locationInfo.Status.code != 200)
			{
				//alert(locationInfo.Status.code);
				showMapInfoAlert("", resultStatus[locationInfo.Status.code]);
				return;
			}
		}catch(error){} 

		if(latlng == true)
		{
			// Retrieve the object
			place = locationInfo;

			// Retrieve the latitude and longitude
			point = new GLatLng(locationInfo[0], locationInfo[1]);

			// Center the map on this point
			map.setCenter(point, 17);
		}
		else
		{
			// Retrieve the object
			place = locationInfo.Placemark[0];

			// Retrieve the latitude and longitude
			point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);

			// Center the map on this point
			map.setCenter(point, 9 + place.AddressDetails.Accuracy);

			if( errorReporting ) showMapInfoAlert("", resultStatus[place.AddressDetails.Accuracy]);
		}

		try
		{
			if(place.AddressDetails.Accuracy && place.AddressDetails.Accuracy < 7)
				return;
		}catch(error){}

		// Create a marker
		var marker = new GMarker(point, {draggable: draggableMarkers}); //, bouncy: false, dragCrossMove: true});

		// Add address information to marker
		try
		{
			var streetAddress = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
			var city = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
			var state = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
			var zip = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
			formattedAddress = streetAddress + '<br />' + city + ', ' + state + ' ' + zip;
		}
		catch (error1)
		{
			try
			{
				formattedAddress = address.value + "<br />" + city.value + ", " + state.value + " " + zip.value;
			}
			catch (error2)
			{
				formattedAddress = "Latitude: " + locationInfo[0] + "<br />Longitude: " + locationInfo[1];
			}
		}

		// Add address information to marker
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml('<b>' + placeName + '</b>' + '<br />' + formattedAddress);
		});

		GEvent.addListener(marker, "dragstart", function() {
			map.closeInfoWindow();
		});

		GEvent.addListener(marker, "dragend", function() {
			marker.openInfoWindowHtml('<b>' + placeName + '</b>' + '<br />' + formattedAddress);
			if( errorReporting ) showMapInfoAlert(marker, "");
			map.panTo(marker.getPoint());
		});

		// Add the marker to map
		map.addOverlay(marker);

		if(showLabelOnLoad)
		{
			marker.openInfoWindowHtml('<b>' + placeName + '</b>' + '<br />' + formattedAddress);
		}
	}
}

function createMarkerAtCenter ()
{
	//Clear all markers and alerts from map.
	map.clearOverlays();
	if( errorReporting ) hideMapInfoAlert();

	if (errorReporting && (!address.value || !city.value || state[state.selectedIndex].defaultSelected || !zip.value))
	{
		showMapInfoAlert("", "Please provide an address, city, state, and zip code before indicating your location on the map.");
		return;
	}

	var placeName = "Install wireless Internet here!";

	// Create a marker
	var marker = new GMarker(map.getBounds().getCenter(), {draggable: draggableMarkers}); //, bouncy: false, dragCrossMove: true});

	formattedAddress = address.value + "<br />" + city.value + ", " + state.value + " " + zip.value;

	// Add address information to marker
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml('<b>' + placeName + '</b>' + '<br />' + formattedAddress);
	});

	GEvent.addListener(marker, "dragstart", function() {
		map.closeInfoWindow();
//		GEvent.addListener(marker, "dragend", dragging == false);
	});

	GEvent.addListener(marker, "dragend", function() {
		marker.openInfoWindowHtml('<b>' + placeName + '</b>' + '<br />' + formattedAddress);
		if( errorReporting ) showMapInfoAlert(marker, "");
		map.panTo(marker.getPoint());
	});

	// Add the marker to map
	map.addOverlay(marker);

	marker.openInfoWindowHtml('<b>Click and drag this marker to where you would like wireless Internet installed.</b><br />Please be as accurate as possible. To place the marker more accurately you may use the navigation controls to the left or change map views by using the buttons in the top right corner.');
}

function showMapInfoAlert (target, msg)
{
	document.getElementById('mapInfoAlert').style.display = "block";
	document.getElementById('mapInfoAlertMsg').innerHTML = "";
/*	if(msg != "" && accuracy == "")
	{
		document.getElementById('mapInfoAlertMsg').innerHTML = msg;
		return;
	}
	var coords = new Array(target.getPoint().lat(), target.getPoint().lng());
	if (accuracy) document.getElementById('mapInfoAlertMsg').innerHTML += "We were able to resolve the address you provided to an accuracy of " + parseInt(accuracy) * 10 + "%.<br /><br />";
	document.getElementById('mapInfoAlertMsg').innerHTML += "Is the current location of the marker where you would like wireless Internet installed?" + 
		"<br /><br /><input type=\"button\" value=\"Yes\" onClick=\"hideMapInfoAlert(); storeLatLng(" + coords[0] + ", " + coords[1] + ");\" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"button\" value=\"No\" onClick=\"createMarkerAtCenter()\" />";
*/
	if(target == "" && msg != "")
		document.getElementById('mapInfoAlertMsg').innerHTML = msg + "<br /><input type=\"button\" value=\"OK\" onClick=\"hideMapInfoAlert();\" />";
	else if(target != "" && msg == "")
	{
		var coords = new Array(target.getPoint().lat(), target.getPoint().lng());
		document.getElementById('mapInfoAlertMsg').innerHTML = "Please click OK when you are finished moving the marker.<br /><input type=\"button\" value=\"OK\" onClick=\"hideMapInfoAlert(); storeLatLng(" + coords[0] + ", " + coords[1] + ");\" />";
	}
		
//	document.getElementById('mapInfoAlertMsg').innerHTML = msg;
//	document.getElementById('mapInfoAlertMsg').innerHTML += "<br /><input type=\"button\" value=\"OK\" onClick=\"hideMapInfoAlert();\" />";
}

function hideMapInfoAlert()
{
	document.getElementById('mapInfoAlert').style.display = "none";
}

function storeLatLng(lat, lng, target)
{
	map.panTo(new GLatLng(lat, lng));
	latitude.value = lat;
	longitude.value = lng;
}

function setFullAddress()
{
	fullAddress.value = address.value + ", " + city.value + ", " + state[state.selectedIndex].text + " " + zip.value;
	cityStateZip.value = city.value + ", " + " " + state.value + " " + zip.value;
}
//]]>

