var map;
var gdir;
var geocoder = new GClientGeocoder();
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.80284,-91.48165), 11);
      map.addControl(new GLargeMapControl());  
      
      addSaleLocation("1706 Fairway St, Eau Claire, WI 54701", "<b>1706 Fairway St.<br />Eau Claire, WI 54701</b><br /><br /><div style='width: 200px;'></div>");
      addSaleLocation("W4355 Jene Rd, Eau Claire, WI 54701", "<b>W4355 Jene Rd.<br />Eau Claire, WI 54701</b><br /><br /><div style='width: 200px;'></div>");
      addSaleLocation("2509 Peebles St, Eau Claire, WI 54703", "<b>2509 Peebles St.<br />Eau Claire, WI 54703</b><br /><br /><div style='width: 200px;'></div>");
      
      addSaleLocation("E5813 Hickory Rd, Eau Claire, WI 54701", "<b>E5813 Hickory Rd.<br />Eau Claire, WI 54701</b><br /><br /><div style='width: 200px;'></div>");

     
      
      
      addSaleLocation("2908 S Lexington Blvd, Eau Claire, WI 54701", "<b>2908 S. Lexington Blvd.<br />Eau Claire, WI 54701</b><br /><br /><div style='width: 200px;'></div>");
      
         

      addSaleLocation("535 Lake Rd, Altoona, WI 54720", "<b>535 Lake Rd.<br />Altoona, WI 54720</b><br /><br /><div style='width: 200px;'>Saturday 7:00am - 5:00pm<br />Lots of household, trinkets, Christmas, twin bedding, jewelry, womens shoes, furniture, and other items too numerous to mention. All priced to sell.</div>");
      addSaleLocation("1828 Lake Rd, Altoona, WI 54720", "<b>1828 Lake Rd.<br />Altoona, WI 54720</b><br /><br /><div style='width: 200px;'>Saturday 8:00am - 4:00pm<br />Entertainment center, Toro snowblower, Craftsman rototiller, chain saws, Pokemon cards, name brand clothing, girls Schwinn bike.</div>");
      addSaleLocation("3550 Halsey St, Eau Claire, WI 54701", "<b>3550 Halsey St.<br />Eau Claire, WI 54701</b><br /><br /><div style='width: 200px;'>Thrurday &amp; Friday 8:00am-6:00pm<br />Saturday 8:00am-12:00pm<br />Table saw, accordian, Womens bike, sewing machine, air compressor, books, household items & clothes.</div>");
      addSaleLocation("1824 Fenwick Ave, Eau Claire, WI 54701", "<b>1824 Fenwick Ave.<br />Eau Claire, WI 54701</b><br /><br /><div style='width: 200px;'>Friday 8:00am - 6:00pm<br />Saturday 8:00am - ?<br />Toys, books, games, puzzles, boys & girls clothes, stroller, kitchen items & much more!</div>");
      addSaleLocation("1204 Omaha St, Eau Claire, WI 54703", "<b>1204 Omaha St.<br />Eau Claire, WI 54703</b><br /><br /><div style='width: 200px;'>Friday 8:00am - 4:00pm<br />Saturday 8am - 1pm<br>Womens 3X clothes, lots of books, DVD's, & decor.</div>");
      addSaleLocation("5135 Rivercrest Dr, Eau Claire, WI 54703", "<b>5135 Rivercrest Dr.<br />Eau Claire, WI 54703</b><br /><br /><div style='width: 200px;'>Friday - Saturday, 9:00am to 5:00pm<br />Birdhouses, wood crafts, collectible signs, strawberry plants, houseplants, yarn, fabric, kits, books, Kitchen-Aid mixer, bike golf clubs, & more!</div>");
      addSaleLocation("3754 Sundet Rd, Chippewa Falls, WI 54729", "<b>3754 Sundet Rd.<br />Chippewa Falls, WI 54729</b><br /><br /><div style='width: 200px;'>By Hallie Golf entrance Mother moved to assisted living. Kitchenware, household, books, seasonal decorations, hose reel, May 20th - 22nd 7:00am.</div>");
      addSaleLocation("925 Water St, Eau Claire, WI 54703", "<b>925 Water St.<br />Eau Claire, WI 54703</b><br /><br /><div style='width: 200px;'>Saturday & Sunday 10:00am - ?<br />Maple bdrm set, potty, wheelchair, yarn, toys, oak podium, baskets, steins, cook books, Holiday Barbies, sport, Packer, cookie jars, fish poles, old lures, jewelry, cedar chest.</div>");
      
      
               
      map.addControl(new GMapTypeControl());   
      
      GEvent.addListener(gdir, "error", handleErrors);
   }
}

function addSaleLocation(address, text)
{
    var icon = new GIcon(G_DEFAULT_ICON);       
    icon.iconSize = new GSize(27, 30);
    icon.shadowSize = new GSize(42, 30);
    icon.iconAnchor = new GPoint(13, 30);   
    icon.infoAnchor = new GPoint(0,0);   
    icon.image = "images/Events/ThriftSale/marker.png"
    icon.shadow = "images/Events/ThriftSale/shadow.png"
      
    // Set up our GMarkerOptions object
    geocoder.getLatLng(address, 
        function(point) { 
            if (!point) { 
                addSaleLocation(address, text);
            } else { 
                var markerOptions = { icon:icon };
                var marker = new GMarker(point, markerOptions);

                map.addOverlay(marker);  
                GEvent.addListener(marker, "mouseover", function() { marker.openInfoWindowHtml("<div style='padding: 0px; margin: 0px; font-size: 10px; text-align: left; color: #000;'>" + text + "</div>") } );                  
            } 
        } 
    );
}
