var routeHTML = "<p style=\"text-align:center;font-weight:bold;\">Enter Your Address in the Map Above.</p><p style=\"text-align:center;font-size:8pt;\">(ex. 3566 Highland Dr., Hudsonville, MI 49426)</p>";
var map;
var directionsPanel;
var directions;
var apexLocation;
var gdir;			

function initialize() {
  	if (GBrowserIsCompatible()) {      
    	map = new GMap2(document.getElementById("map_canvas"));
	    apexLocation = new GLatLng(42.852892, -85.86943);
	    map.setCenter(apexLocation, 15);
	    map.addControl(new GSmallMapControl());
		//map.addControl(new GMapTypeControl());
	    		      
	    var marker = new GMarker(apexLocation);
	    map.addOverlay(marker);
		
		marker.openInfoWindowHtml("<img src=\"images/ApexLogo.png\" alt=\"APEX Controls, Inc.\" style=\"width:100px;height:29px;margin-bottom:5px;\"/><br/><table><tr><td><label for=\"address\">Get Directions From:</label></td><td></td></tr><tr><td><input type=\"text\" id=\"address\" name=\"address\" style=\"width:200px;\"/></td><td><button id=\"getDirections\" onclick=\"setDirections(document.getElementById('address').value);\">Go</button></td></tr></table><table style=\"font-weight:normal;margin-top:5px;\"><tr><td style=\"width:30px;\"><b>To:</b></td><td>3566 Highland Dr.</td></tr><tr><td></td><td>Hudsonville, MI 49426</td></tr></table>");
  	}
}

function setDirections(fromAddress) {
	var locale = "en_US";
	
	document.getElementById("route").innerHTML = "";
	
	map = new GMap2(document.getElementById("map_canvas"));
	gdir = new GDirections(map, document.getElementById("route"));
	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	GEvent.addListener(gdir, "error", handleErrors);
	map.addControl(new GSmallMapControl());
	//map.addControl(new GMapTypeControl());
    	
  	gdir.load("from: " + fromAddress + " to: 3566 Highland Dr., Hudsonville, MI 49426", { "locale": locale });
}

function handleErrors(){
   	if (gdir.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: " + gdir.getStatus().code);
   	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 Error code: " + gdir.getStatus().code);			   
   	else if (gdir.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: " + gdir.getStatus().code);			     
   	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 Error code: " + gdir.getStatus().code);
   	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
     	alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);			    
   	else alert("An unknown error occurred.");			   
}

function onGDirectionsLoad(){ 

}