var centerLatitude = 44.833500;
var centerLongitude = -93.119730;
var startZoom = 9;
var description = "<img src='/images/gopher_logo.gif'><br><br><p class='bodyintro'>3385 South Highway 149<br>Eagan, MN 55121</p>";
var directionsPanel;
var directions;

var map;

function init() {
	if(GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		directionsPanel = document.getElementById("directions");
		map.addControl(new GSmallMapControl());
		var location = new GLatLng(centerLatitude, centerLongitude);
		map.setCenter(location, startZoom);
		addMarker(centerLatitude, centerLongitude, description);
		directions = new GDirections(map, directionsPanel);
		GEvent.addListener(directions, "load", onGDirectionsLoad);
		GEvent.addListener(directions, "error", handleErrors);
	}
}

function addMarker(latitude, longitude, description) {
	var marker = new GMarker(new GLatLng(latitude, longitude));
	GEvent.addListener(marker, "click", 
		function() {
			marker.openInfoWindowHtml(description);
		}
	);
	map.addOverlay(marker);
}

function setDirections(fromAddress, toAddress, locale) {
	map.clearOverlays();
	directions.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
	map.openInfoWindowHtml(new GLatLng(centerLatitude, centerLongitude), description);
}

function handleErrors() {
	if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
		alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + directions.getStatus().code);
	} else if (directions.getStatus().code == G_GEO_SERVER_ERROR) {
		alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + directions.getStatus().code);
	} else if (directions.getStatus().code == G_GEO_MISSING_QUERY) {
		alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + directions.getStatus().code);
	} else if (directions.getStatus().code == G_GEO_BAD_KEY) {
		alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + directions.getStatus().code);
	} else if (directions.getStatus().code == G_GEO_BAD_REQUEST) {
		alert("A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code);
	} else {
		alert("An unknown error occurred.");
	}
}

function onGDirectionsLoad() {
	// Use this function to access information about the latest load() results.
}

window.onload = init;
window.onunload = GUnload;
