var  map, gdir, fromAddress, toAddress;
/*
google maps api with turn-by-turn directions.
version: 1.0
By: Caleb Peters
Description:
The original idea for this script can be found on Google's page devoted to their map API.
My major contribution to this piece of code was to change the location of the form to inside the map itself,
as well as, adding controls and a few variables.
*/
function initialize() {
  if (GBrowserIsCompatible()) {
    //setup elements
    map  = new GMap2(document.getElementById("Gmap"));
	map.addControl(new GLargeMapControl3D());
	map.addControl(new GScaleControl());
	map.addControl(new GMapTypeControl());
    gdir  = new GDirections(map, document.getElementById("directions"));
	
	//settings
    var marker = new GMarker(new GLatLng(43.8912,-85.8517));
			
		// this following line is the HTML that makes up the direction overlay form	
	var html = "<h3 Style='margin:5px;'>How to get here</h3> <form action='map.php' onsubmit='overlayDirections();return false;' method='post'>     <div class='address-form-column'><label for='street'>Street Address</label><input id='street' name='street_address' type='text' /></div>   <br />   <div class='address-form-column'>  <label for='city'>City</label><input id='city' name='city' type='text' /></div>   <br />   <div class='address-form-column'><label for='state'>State</label><select id='state' name='state'><option value='AL'>Alabama</option><option value='AK'>Alaska</option><option value='AZ'>Arizona</option><option value='AR'>Arkansas</option><option value='CA'>California</option><option value='CO'>Colorado</option><option value='CT'>Connecticut</option><option value='DE'>Delaware</option><option value='DC'>District Of Columbia</option><option value='FL'>Florida</option><option value='GA'>Georgia</option><option value='HI'>Hawaii</option><option value='ID'>Idaho</option><option value='IL'>Illinois</option><option value='IN'>Indiana</option><option value='IA'>Iowa</option><option value='KS'>Kansas</option><option value='KY'>Kentucky</option><option value='LA'>Louisiana</option><option value='ME'>Maine</option><option value='MD'>Maryland</option><option value='MA'>Massachusetts</option><option value='MI' selected='selected'>Michigan</option><option value='MN'>Minnesota</option><option value='MS'>Mississippi</option><option value='MO'>Missouri</option><option value='MT'>Montana</option><option value='NE'>Nebraska</option><option value='NV'>Nevada</option><option value='NH'>New Hampshire</option><option value='NJ'>New Jersey</option><option value='NM'>New Mexico</option><option value='NY'>New York</option><option value='NC'>North Carolina</option><option value='ND'>North Dakota</option><option value='OH'>Ohio</option><option value='OK'>Oklahoma</option><option value='OR'>Oregon</option><option value='PA'>Pennsylvania</option><option value='RI'>Rhode Island</optionoption value='SD'>South Dakota</option><option value='TN'>Tennessee</option><option value='TX'>Texas</optionoption value='VT'>Vermont</option><option value='VA'>Virginia</option><option value='WA'>Washington</option><option value='WV'>West Virginia</option><option value='WI'>Wisconsin</option><option value='WY'>Wyoming</option></select></div>   <div class='zip-form-column'><label for='zip'>Zip Code</label><input id='zip' name='zip_code' type='text' maxlength='5' size='5' /></div>   <br />   <div class='button'><input name='submit' type='submit' value='Get Directions' />";
		
	map.addOverlay(marker);
	marker.openInfoWindowHtml(html);
			  
	GEvent.addListener(marker, "click", function() {
	marker.openInfoWindowHtml(html);
	});
	
	toAddress = "510 Michigan Ave Baldwin, MI 49304";
    
    var defaultZoomLevel  = 13;
    //end settings
    
	//error handler  
	GEvent.addListener(gdir, "error", handleErrors);
    
    //set map center
    map.setCenter(new GLatLng(43.9019, -85.8508),13);
  }
  else {
		alert("Sorry, the Google Maps API is not compatible with this browser");
		}
}

/*
Looks up the directions, overlays route on map,
and prints turn-by-turn to #directions.
This is the cool part!
*/
function overlayDirections(){	
    fromAddress =
      document.getElementById("street").value
      + " " + document.getElementById("city").value
      + " " + document.getElementById("state").options[document.getElementById("state").selectedIndex].value
      + " " + document.getElementById("zip").value;
    
    var language  = "en";
    gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": language });
}
/*
Display error to user
*/
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.");
}
