
	var map = null; 
		
    var geocoder = null;
    
    function getQueryVariable(variable) {
	  var query = window.location.search.substring(1);
	  var vars = query.split("&");
	  for (var i=0;i<vars.length;i++) {
	    var pair = vars[i].split("=");
	    if (pair[0] == variable) {
	      return pair[1];
	    }
	  } 
	  
	}
    
    function loadMyMaps() {
		map = new GMap2(document.getElementById("map"));
		if (GBrowserIsCompatible()) {
			//map.setCenter(new GLatLng(42.296426,-83.674567), 11); // original
			map.setCenter(new GLatLng(locCoords['Global'].lat,locCoords['Global'].lng), locCoords['Global'].zoom);
			map.addControl(new GSmallMapControl());
			geocoder = new GClientGeocoder();
			loadIcon();
			if (getQueryVariable('Type') != undefined)
		    {	if (getQueryVariable('Pop') != undefined) {
		    		showJust(getQueryVariable('Type'), getQueryVariable('Pop'));
		    	} else {
		    		showJust(getQueryVariable('Type'));
		    	}
		    } else {
			    showAllAddresses();
			}
		} else {
	      alert("Sorry, the Google Maps API is not compatible with this browser");
    	}
		
	
	}
    
    function showJust(_type, _idToPop) {
    	map.clearOverlays();
    	showAllAddresses(_type, _idToPop);
    	if (locCoords[_type] != undefined) {
    	     map.setCenter(new GLatLng(locCoords[_type].lat,locCoords[_type].lng), locCoords[_type].zoom);
    	} else {
    	     map.setCenter(new GLatLng(locCoords['Global'].lat,locCoords['Global'].lng), locCoords['Global'].zoom);
    	}
    }
    
    function loadIcon() {
      
      for (var i in IMAGES) {
        var icon = new GIcon();
        icon.image = "google/" + IMAGES[i] + ".png";
        icon.iconAnchor = new GPoint(16, 16);
        icon.infoWindowAnchor = new GPoint(16, 0);
        icon.iconSize = new GSize(23, 35);
        icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        icon.shadowSize = new GSize(41, 32);
        ICONS[i] = icon;
      }
      return ICONS[i];
    }
    
    /**
     *
     *	@param	p_type		string		[optional] 		Lease,Restoration,Ect...
     *	@param	_idToPop	numeric		[opt only if p_type is set] id of item to popup
     */
    function showAllAddresses(p_type, _idToPop) 
    {
	    for(var _type in locMaps) {
	    	if (p_type != undefined && _type != p_type) {
	    		continue;
	    	}
	    	for (var _id in locMaps[_type]) {
	    		if (p_type != undefined && _id == _idToPop) {
	    			showAddress(locMaps[_type][_id]['Address'], _type, _id, true);
	    		} else {
	    			if (locMaps[_type][_id]['lat'] != undefined) {
	    			var _x = new Object();
		       	  	  _x._lat = 0;
		       	  	  _x._lng = 0;
		       	  	  _x.lat = function () {
		       	  	  	return this._lat;
		       	  	  }
		       	  	  _x.lng = function() {
		       	  	  	return this._lng;
		       	  	  }
		       	  	  _x._lat = locMaps[_type][_id]['lat'];
		       	  	  _x._lng = locMaps[_type][_id]['lng'];
		       	  	  showAddress(_x, _type, _id);
	    			} else {
	    				showAddress(locMaps[_type][_id]['Address'], _type, _id);
	    			}
	    		}
    		}
		}
    }
    
    
    function createMarker(point, _type, _id) 
    {
    	if (_type == 'Lease') {
    		
    		var temp = LocIcons[_type+'_'+locMaps[_type][_id]['Status']];
    		var marker = new GMarker(point, {icon:ICONS[temp]});
    	
    	} else {
    		var marker = new GMarker(point, {icon:ICONS[LocIcons[_type]]});
		}
		
		GEvent.addListener(marker, "click", function() {
			/**
			 *	Pops up when a marker is clicked
			 *  @Marker 1
			 */
			marker.openInfoWindowHtml('<table cellpadding="0" cellspacing="2" border="0" width="235" style="font-family: verdana; font-size: 11px;"><tr><td valign="top"><b style="font-size: 12px; color: #900; padding-bottom: 3px;">'+locMaps[_type][_id]['Name']+'</b><br><img style="padding:3px 0 5px 0;" src="google/'+locMaps[_type][_id]['Image']+'" border="0" width="235" height="153"></td></tr><tr><td valign="top">'+locMaps[_type][_id]['Info']+'<hr style="margin-bottom: 2px;" size="1" noshade><a style="font-size: 11px;" href="'+locMaps[_type][_id]['Link']+'"> More information</a></td></tr></table>');
		});
		
	return marker;
	}


	function showAddress(address, _type, _id, _flPop) 
	{
      if (geocoder) {
      	
      	if (typeof address != 'string') {
      	  	
       	  	  var marker = createMarker(address, _type, _id);
              map.addOverlay(marker);
              if (_flPop === true) {
             	marker.openInfoWindowHtml('<table cellpadding="0" cellspacing="2" border="0" width="235" style="font-family: verdana; font-size: 11px;"><tr><td valign="top"><b style="font-size: 12px; color: #900; padding-bottom: 3px;">'+locMaps[_type][_id]['Name']+'</b><br><img style="padding:3px 0 5px 0;" src="google/'+locMaps[_type][_id]['Image']+'" border="0" width="235" height="153"></td></tr><tr><td valign="top">'+locMaps[_type][_id]['Info']+'<hr style="margin-bottom: 2px;" size="1" noshade><a style="font-size: 11px;" href="'+locMaps[_type][_id]['Link']+'"> More information</a></td></tr></table>');
              }
              
      	} else {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
            	/*
            	 * alert(address + 'Lng: '+point.lng()+' Lat: '+point.lat());
            	 */
              var marker = createMarker(point, _type, _id);
              map.addOverlay(marker);
              if (_flPop === true) {
             	/**
				 *	Pops up when a Pop variable is passed in the query string
				 *  @Marker 2
				 */
             	marker.openInfoWindowHtml('<table cellpadding="0" cellspacing="2" border="0" width="235" style="font-family: verdana; font-size: 11px;"><tr><td valign="top"><b style="font-size: 12px; color: #900; padding-bottom: 3px;">'+locMaps[_type][_id]['Name']+'</b><br><img style="padding:3px 0 5px 0;" src="google/'+locMaps[_type][_id]['Image']+'" border="0" width="235" height="153"></td></tr><tr><td valign="top">'+locMaps[_type][_id]['Info']+'<hr style="margin-bottom: 2px;" size="1" noshade><a style="font-size: 11px;" href="'+locMaps[_type][_id]['Link']+'"> More information</a></td></tr></table>');
              }
            }
          }
        );
      	}
      }
    }
	