﻿    //<![CDATA[
    var my_undefined = "undefined"; 

	function googleApiUnload()
	{
	   	if(document.getElementById("googlemaps_storelocator") != null)
    	{
			GUnload();
		}
	}
    function googleApiLoad() 
    {
    	if(document.getElementById("googlemaps_storelocator") != null)
    	{
        /*  HACK: Have to check because IE will throw an error because a child container HTML element 
            contains script code that tries to modify the parent container element of the child container.
            SOURCE: http://support.microsoft.com/default.aspx/kb/927917
            
            This error is thrown if we attempt to remove the load() call from the body onload event and 
            place it into a RegisterStartupScript. The google JS script attempts to modify part of the DOM and this 
            error is then thrown, in turn, shutting down the APP in IE (version 7 at least).
            
            The homeSpatialInfo and ProximityLocations variables are not loaded until the "Go" button is clicked. 
            Therefore, on the first intial load, they are of type "undefined", therefore the load does not occur. 
            After a post back occurs from the "Go" button, these JSON variables are written to the client and the 
            conditional will pass because they are defined and the method call will continue. 
            
            Thiis is VERY HACKY, but it works. Please modify it if you see a better way to handle this and let 
            me know! :) 
        */ 
        //alert(homeSpatialInfo);        alert(ProximityLocations);
        if(typeof(homeSpatialInfo) != my_undefined && typeof(ProximityLocations) != my_undefined)
        {
          if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map"));
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());
            map.setCenter(new GLatLng(homeSpatialInfo.latitude, homeSpatialInfo.longitude), 10);

            // Create a base icon for all of our markers that specifies the
            // shadow, icon dimensions, etc.
            var baseIcon = new GIcon();
            baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
            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.infoShadowAnchor = new GPoint(18, 25);
            
            // Creates a marker whose info window displays the letter corresponding// to the given index.
            function createMarker(point, name, address, urladdress, locationNumber) {
              var icon = new GIcon(baseIcon);  
              icon.image = "/images/markers/marker" + locationNumber + ".png";
              var marker = new GMarker(point, icon);
              GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(name + '<br>' + address + '<br>' + '<a target="_blank" href="http://maps.google.com/maps?f=q&hl=en&q=from:'+homeSpatialInfo.fromAddress+'+to:'+urladdress+'">Directions</a>');
              });
              return marker;
            }

            // Load all the markers from the JSON ProximityLocations variable
            var bounds = map.getBounds();
            var southWest = bounds.getSouthWest();
            var northEast = bounds.getNorthEast();
            var lngSpan = northEast.lng() - southWest.lng();
            var latSpan = northEast.lat() - southWest.lat();
            for (var i = 0; i < ProximityLocations.locations.length; i++) {
              var point = new GLatLng(ProximityLocations.locations[i].latitude,ProximityLocations.locations[i].longitude);
              map.addOverlay(createMarker(point, ProximityLocations.locations[i].name, ProximityLocations.locations[i].address, ProximityLocations.locations[i].urladdress, i+1));
            }
          }
        }
        }
    }
    
    //]]>