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.77732663079842, -91.44796371459961), 13);
      map.addControl(new GLargeMapControl());   
      var point = new GLatLng(44.76382975136448, -91.44789397716522);

      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/eccha_view.jpg' /></td><td><b>Eau Claire County Humane Association</b><br/>3900 Old Town Hall Road<br/>Eau Claire, WI 54701-8949<br/><br/>Phone: 715.839.4747<br/>E-mail: <a href='mailto:petquestions@eccha.org'>petquestions@eccha.org</a></td></tr></table>");
      GEvent.addListener(marker, "mouseover", function() {marker.openInfoWindowHtml("<table style='margin: 0px;'><tr><td><img src='/images/map/eccha_view.jpg' /></td><td><b>Eau Claire County Humane Association</b><br/>3900 Old Town Hall Road<br/>Eau Claire, WI 54701-8949<br/><br/>Phone: 715.839.4747<br/>E-mail: <a href='mailto:petquestions@eccha.org'>petquestions@eccha.org</a></td></tr></table>") } );          
      GEvent.addListener(gdir, "error", handleErrors);
   }
}


function setDirections(fromAddress) {
   document.getElementById("directions").style.width = "200px";
   document.getElementById("map").style.width = "440px";
   document.getElementById("map").style.borderWidth = "1px";
   map.checkResize();
   gdir.load("from: " + fromAddress + " to: 44.76382975136448, -91.44789397716522");
   map.closeInfoWindow();
}


function removeDirections() {
   document.getElementById("directions").style.width = "0px";
   document.getElementById("map").style.width = "640px";
   document.getElementById("map").style.borderWidth = "1px";
   map.checkResize();
   gdir.clear();   
   map.setCenter(new GLatLng(44.77732663079842, -91.44796371459961), 13);
   marker.openInfoWindowHtml("<table style='margin: 0px;'><tr><td><img src='/images/map/eccha_view.jpg' /></td><td><b>Eau Claire County Humane Association</b><br/>3900 Old Town Hall Road<br/>Eau Claire, WI 54701-8949<br/><br/>Phone: 715.839.4747<br/>E-mail: <a href='mailto:petquestions@eccha.org'>petquestions@eccha.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.");	   
}


function loadsales() {
   if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("map"));
      map.setCenter(new GLatLng(44.827853,-91.491567), 11);
      map.addControl(new GLargeMapControl());   
      var point1 = new GLatLng(44.817853,-91.491567);
      var point2 = new GLatLng(44.713442,-91.458737);
      var point3 = new GLatLng(44.872376,-91.447827);
      
      var baseIcon = new GIcon(G_DEFAULT_ICON);       
      baseIcon.iconSize = new GSize(20, 27);
      baseIcon.shadowSize = new GSize(37, 27);
      baseIcon.iconAnchor = new GPoint(9, 27);
      baseIcon.infoWindowAnchor = new GPoint(9, 2);        
      baseIcon.image = "images/Events/ThriftSale/marker.png"
                    
      var baseIcon1 = new GIcon(G_DEFAULT_ICON);       
      baseIcon1.iconSize = new GSize(257, 159);
      baseIcon1.shadowSize = new GSize(0,0);
      baseIcon1.iconAnchor = new GPoint(257, 159);
      baseIcon1.image = "images/Events/ThriftSale/overlay1.gif"
      
      var baseIcon2 = new GIcon(G_DEFAULT_ICON);       
      baseIcon2.iconSize = new GSize(291, 185);
      baseIcon2.shadowSize = new GSize(0,0);
      baseIcon2.iconAnchor = new GPoint(103, 185);       
      baseIcon2.image = "images/Events/ThriftSale/overlay2.gif"      
               
      var baseIcon3 = new GIcon(G_DEFAULT_ICON);       
      baseIcon3.iconSize = new GSize(249, 117);
      baseIcon3.shadowSize = new GSize(0,0);
      baseIcon3.iconAnchor = new GPoint(82, 117);       
      baseIcon3.image = "images/Events/ThriftSale/overlay3.gif"   
                                  
      // Set up our GMarkerOptions object
      markerOptions = { icon:baseIcon };
      marker1 = new GMarker(point1, markerOptions);
      marker2 = new GMarker(point2, markerOptions);
      marker3 = new GMarker(point3, markerOptions);
      
      markerOptions = { icon:baseIcon1 };
      marker4 = new GMarker(point1, markerOptions);
      
      markerOptions = { icon:baseIcon2 };
      marker5 = new GMarker(point2, markerOptions);
      
      markerOptions = { icon:baseIcon3 };
      marker6 = new GMarker(point3, markerOptions);
            
      map.addOverlay(marker1);    
      map.addOverlay(marker2); 
      map.addOverlay(marker3);  
      map.addOverlay(marker4);   
      map.addOverlay(marker5);  
      map.addOverlay(marker6);       
      map.addControl(new GMapTypeControl());   
      
      GEvent.addListener(gdir, "error", handleErrors);
   }
}