﻿function loadGoogleMaps()
{
  if(typeof(homeSpatialInfo) != "undefined" && typeof(ProximityLocations) != "undefined")
  {
    if (GBrowserIsCompatible()) 
    {
			/* ################################## */
			// Determines if the user agent is MSIE 6. If it is, then displays the map icon images as .gif, else .png.
			// Added by JYEAROUS on 3/17/2008
			var UA = navigator.userAgent;
			var fileType = ".png";
			
			if (UA.indexOf("MSIE 6.0") > -1)
				fileType = ".gif";
			/* ################################## */
			
      //Create the map
      var map = new GMap2(document.getElementById("map"));
      
      //add the controls to the map
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      
      //we don't know our zoom level yet so set some arbitrary center for now. we'll change it later.
      map.setCenter(new GLatLng(0,0),0);
      //map.setCenter(new GLatLng(homeSpatialInfo.latitude, homeSpatialInfo.longitude), 9);
      
      //define our icons
      var baseIcon        = new GIcon(G_DEFAULT_ICON);      
      var centerIcon      = new GIcon(G_DEFAULT_ICON);
      centerIcon.iconSize = new GSize(32,32);
      centerIcon.image    = "../graphics/storelocator/yellow-trans" + fileType;
      
      // Creates a marker whose info window displays the letter corresponding// to the given index.
      function createMarker(point, name, address, city, state, zip, urladdress, storeType, locationNumber, phone) 
      {
        var url  = "<a target=\"_blank\" class=\"newwindow\" href=\"http://maps.google.com/maps?saddr=" +
                   homeSpatialInfo.fromAddress + "&daddr=" + urladdress + "\">Directions</a>";
        var icon = new GIcon(baseIcon);
        var iconPrefix = "../graphics/storelocator/icon";
        var iconSuffix = "-trans" + fileType;
        
        if (storeType == "Kwik Trip")
          icon.image = iconPrefix + "ktlogo" + ((locationNumber > 0)?locationNumber:"") + iconSuffix;
        else if (storeType == "Kwik Star")
          icon.image = iconPrefix + "kslogo" + ((locationNumber > 0)?locationNumber:"") + iconSuffix;
        else if (storeType == "TOP")
          icon.image = iconPrefix + "toplogo" + ((locationNumber > 0)?locationNumber:"") + iconSuffix;
        else if (storeType == "Hearty Platter")
          icon.image = iconPrefix + "hplogo" + ((locationNumber > 0)?locationNumber:"") + iconSuffix;
        else
          icon.image = iconPrefix + "otherlogo" + ((locationNumber > 0)?locationNumber:"") + iconSuffix;
          
        var marker = new GMarker(point, icon);
        
        GEvent.addListener(marker, "click", function() 
            {
              marker.openInfoWindowHtml(name + '<br>' + address + '<br>' + 
                                        city + ', ' + state + ' ' + zip + '<br>' + 
                                        phone + '<br>' + url);
            });

        return marker;
      }
      
      // Start with an empty GLatLngBounds object
      var bounds = new GLatLngBounds();
      
      // Load all the markers from the JSON ProximityLocations variable
      var locNum      = 0;
      var clusterer   = new Clusterer(map);
      var boolCluster = false;
      var clusterIcon;

      if (ProximityLocations.locations.length > 20)
      {
        locNum = -1*ProximityLocations.locations.length;
        boolCluster = true;
        clusterIcon = new GIcon(baseIcon);
      }			
        
      for (var i = 0; i < ProximityLocations.locations.length; i++) 
      {
        var lat     = ProximityLocations.locations[i].latitude;
        var lng     = ProximityLocations.locations[i].longitude;
        var name    = ProximityLocations.locations[i].name;
        var addr    = ProximityLocations.locations[i].address;
        var city    = ProximityLocations.locations[i].city;
        var state   = ProximityLocations.locations[i].state;
        var zip     = ProximityLocations.locations[i].zip;
        var urlAddr = ProximityLocations.locations[i].urladdress;
        var phone   = ProximityLocations.locations[i].phone;
        var point   = new GLatLng(lat,lng);
        var marker  = createMarker(point, name, addr, city, state, zip, urlAddr, name, ++locNum, phone);
				//map.addOverlay(marker);
        var title = "<strong>" + name + "</strong> " + city + ", " + state;
        clusterer.AddMarker(marker,title);
        
        //each time a point is found, extend the bounds to include it
        bounds.extend(point);
      }
      
      //set the marker for your start location
      var searchPoint = new GLatLng(homeSpatialInfo.latitude,homeSpatialInfo.longitude);
      bounds.extend(searchPoint);
      map.addOverlay(new GMarker(searchPoint,centerIcon));
      
      //determine the zoom level from the bounds
      map.setZoom(map.getBoundsZoomLevel(bounds));
      
      //determine the center from the bounds
      map.setCenter(bounds.getCenter());
      
    } //end GBrowserIsCompatible()    
  } //end undefined check
} //end function