var map;
var gdir;
var geocoder = null;
var marker;

function load() {
   if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("map"));
      gdir = new GDirections(map, document.getElementById("directions"));
      map.setCenter(new GLatLng(44.683347,-91.458267), 13);
      map.addControl(new GLargeMapControl());   
      var point = new GLatLng(44.683347,-91.458267);

      var baseIcon = new GIcon(G_DEFAULT_ICON);       
      baseIcon.iconSize = new GSize(20, 34);
      baseIcon.shadowSize = new GSize(37, 34);
      baseIcon.iconAnchor = new GPoint(9, 34);
      baseIcon.infoWindowAnchor = new GPoint(9, 2);        
      baseIcon.image = "/images/map/marker.png"
                      
      // Set up our GMarkerOptions object
      markerOptions = { icon:baseIcon };
      marker = new GMarker(point, markerOptions);

      map.addOverlay(marker);    
      map.addControl(new GMapTypeControl());        
      marker.openInfoWindowHtml("<table style='margin: 0px;'><tr><td><img src='/images/map/logo.jpg' /></td><td><b>Bob's House For Dogs</b><br/>E3015 County Road HH<br/>Eleva, WI 54738-9017<br/><br/>Phone: 715.878.4505<br/>E-mail: <a href='mailto:info@bobshousefordogs.org'>info@bobshousefordogs.org</a></td></tr></table>");
      GEvent.addListener(marker, "mouseover", function() {marker.openInfoWindowHtml("<table style='margin: 0px;'><tr><td><img src='/images/map/logo.jpg' /></td><td><b>Bob's House For Dogs</b><br/>E3015 County Road HH<br/>Eleva, WI 54738-9017<br/><br/>Phone: 715.878.4505<br/>E-mail: <a href='mailto:info@bobshousefordogs.org'>info@bobshousefordogs.org</a></td></tr></table>") } );          
      GEvent.addListener(gdir, "error", handleErrors);
   }
}

function setDirections(fromAddress) {
   document.getElementById("directions").style.width = "170px";
   document.getElementById("map").style.width = "460px";
   map.checkResize();
   gdir.load("from: " + fromAddress + " to: 44.683347,-91.458267");
   map.closeInfoWindow();
}

function removeDirections() {
   document.getElementById("directions").style.width = "0px";
   document.getElementById("map").style.width = "630px";
   map.checkResize();
   gdir.clear();   
   map.setCenter(new GLatLng(44.683347,-91.458267), 13);
   marker.openInfoWindowHtml("<table style='margin: 0px;'><tr><td><img src='/images/map/logo.jpg' /></td><td><b>Bob's House For Dogs</b><br/>E3015 County Road HH<br/>Eleva, WI 54738-9017<br/><br/>Phone: 715.878.4505<br/>E-mail: <a href='mailto:info@bobshousefordogs.org'>info@bobshousefordogs.org</a></td></tr></table>");
}

function handleErrors() {
   removeDirections();
   
   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.");	   
}