var map;
var gdir;
var geocoder = null;
var marker;

function load() {
	if (GBrowserIsCompatible()) {      
    	map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		gdir = new GDirections(map, document.getElementById("directions"));
		geocoder = new GClientGeocoder();
		GEvent.addListener(gdir, "error", handleErrors);
		showLocation();
	}
}

function showLocation() {
	address = "801 N Main St, Enterprise, AL 36330";
	geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				alert(address + " not found");
			}
			else {
				map.setCenter(point, 13);
				marker = new GMarker(point);
				map.addOverlay(marker);
				marker.openInfoWindowHtml("Sessions Company<br />801 N Main St<br />Enterprise, AL 36330<br />334-393-0200");
				GEvent.addListener(marker, "click", showInfoWindow);
			}
		}
	);
}

function showInfoWindow() {
	marker.openInfoWindowHtml("Sessions Company<br />801 N Main St<br />Enterprise, AL 36330<br />334-393-0200");
}
    
function setDirections(fromAddress, locale) {
	if (fromAddress != "") {
		toAddress = "801 N Main St, Enterprise, AL 36330";
		gdir.load("from: " + fromAddress + " to: " + toAddress, {"locale": locale});
		map.removeOverlay(marker);
	}
}

function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("No location could be found for the specified address.  Make sure the address is correct and try making it more specific.\n");
	else if (gdir.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");
	else if (gdir.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");
	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		alert("A directions request could not be successfully parsed.\n");
	else alert("An unknown error occurred.");
}

