///////////////
// LOAD MAP //
//////////////
var map;
function init()
{
 if(GBrowserIsCompatible())
 {
  map = new GMap2(document.getElementById("map"));
  geocoder = new GClientGeocoder();
  if(to_address_latlng) { setto_address(to_address_latlng); }
  else { setto_address(to_address); }
  map.addControl(new GSmallMapControl());
  map.addControl(new GMapTypeControl());
  document.getElementById("to").value = to_address;
  if(administrationMode=='on')
  {
   GEvent.addListener(map,'click',function(overlay,latlng){ addMarker(overlay,latlng); });
   document.getElementById("mapAdminControl").innerHTML = '<b>Admin Controls:</b><br>Click map to get latitude longitude cordinates <span style="font-size:12px;">(displays in bubble &amp; below map)</span><br><input type="button" value="Clear map" onClick="map.clearOverlays();">';
   document.getElementById("mapAdminControl").style.display = 'block';
  }
 }
}
window.onload = init;
window.onunload = GUnload;

/////////////////////////////
// GET LAT LNG CORDINATES //
////////////////////////////
function addMarker(overlay,latlng)
{
 var marker = new GMarker(latlng);
 map.addOverlay(marker);
 marker.openInfoWindowHtml('Latitude Longitude of this Marker: <br>'+latlng);
 document.getElementById("directions").innerHTML = '<div class="">Latitude Longitude of Last Marker On Map:</div>'+latlng;
}

//////////////////////////
// SET CENTER LOCATION //
/////////////////////////
    function setto_address(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, zoom_level);
              var marker = new GMarker(point);
              map.addOverlay(marker);
            }
          }
        );
      }
    }

/////////////////////
// GET DIRECTIONS //
////////////////////
function getDirections()
{
 var from = document.getElementById("from").value;
 var to;
 var locale = 'en_US';
 if(to_address_latlng) { to = to_address_latlng; }
 else { to = to_address; }
 if(window.gDirections) { window.gDirections.clear(); }
 window.gDirections = new GDirections(map, document.getElementById("directions"));
 window.gDirections.load("from: "+from+" to: "+to,{ "locale": locale });
 GEvent.addListener(window.gDirections, "error", handleErrors);
}

////////////////////
// HANDLE ERRORS //
////////////////////
/* code from Google example */
    function handleErrors(){
	   if (window.gDirections.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.\n\nHINT: Make sure you put in at least the address and state in the from text box.\n\nError code: " + window.gDirections.getStatus().code);
	   else if (window.gDirections.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: " + window.gDirections.getStatus().code);
	   
	   else if (window.gDirections.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: " + window.gDirections.getStatus().code);

	//   else if (window.gDirections.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + window.gDirections.getStatus().code);
	     
	   else if (window.gDirections.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: " + window.gDirections.getStatus().code);

	   else if (window.gDirections.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + window.gDirections.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}
