    var map;
    var geocoder;

    var gKey = '';
    if(location.host.toUpperCase().indexOf("MODSPACE.CA",0) != -1 || location.host.toUpperCase().indexOf("CANADA.MODSPACE.COM",0) != -1) {
		gKey = "ABQIAAAAJJ-nKONref8I2IOJI6D5LRSayYO7I99kf0NlKOWponDZxj6boBS0Pa-m4jhrPQO37WkoYga5O71XBg";

	} else {
		gKey = "ABQIAAAAJJ-nKONref8I2IOJI6D5LRSx5Ci9sePaMr9clxzMMDQ8q3UjvBQ8yfOWqDMJ-EAKqoGQGrQJjYGgzQ";
	}

     google.loader.ApiKey = gKey;     
     google.loader.KeyVerified = true;
     google.load("maps", "2.150");

    function load() {
      if (GBrowserIsCompatible()) {
        geocoder = new GClientGeocoder();
        map = new GMap2(document.getElementById('map'));
        map.addControl(new GMapTypeControl());
		svOverlay = new GStreetviewOverlay();
		if(location.host.toUpperCase().indexOf("MODSPACE.CA",0) != -1 || location.host.toUpperCase().indexOf("CANADA.MODSPACE.COM",0) != -1) {
			map.setCenter(new GLatLng(53, -95), 3);
		} else {
			map.setCenter(new GLatLng(40, -100), 3);
		}
		map.setUIToDefault();
      }
	  searchLocationsNear(0);
    }
 
    
   function searchLocations() {
     var address = document.getElementById('addressInput').value;
     geocoder.getLatLng(address, function(latlng) {
       if (!latlng) {
         alert(address + ' not found');

       } else {
         searchLocationsNear(latlng);
       }
     });
   }

   function searchLocationsNear(center) {

     if(center != 0 ) {
		 var radius = document.getElementById('radiusSelect').value;
		 var searchUrl = '../locations/XmlServlet?lattitude=' + center.lat() + '&longitude=' + center.lng() + '&radius=' + radius;
		// var searchUrl = 'http://rh5-devl.modspace.com/locations/XmlServlet?lattitude=' + center.lat() + '&longitude=' + center.lng() + '&radius=' + radius;

		 
	} else {
			 var searchUrl = '../locations/XmlServlet';
			//var searchUrl = 'http://rh5-dev1.modspace.com:12080/locations/XmlServlet';

	}
     GDownloadUrl(searchUrl, function(data) {
       var xml = GXml.parse(data);
        var markers = xml.documentElement.getElementsByTagName('BranchInfo');
       map.clearOverlays();
       var sidebar = document.getElementById('sidebar');
       sidebar.innerHTML = '';
       if (markers.length == 0) {
         sidebar.innerHTML = 'No Branches Found for your search.';
         map.setCenter(new GLatLng(40, -100), 3);
         return;
       }
       var bounds = new GLatLngBounds();
        for (var i = 0; i < markers.length; i++) {
          var name = markers[i].getElementsByTagName("NAME")[0].firstChild.nodeValue;
          var address = markers[i].getElementsByTagName("ADDRESS1")[0].firstChild.nodeValue;
          var city = markers[i].getElementsByTagName("CITY")[0].firstChild.nodeValue;
          var state = markers[i].getElementsByTagName("STATE")[0].firstChild.nodeValue;
           var postalcode = markers[i].getElementsByTagName("POSTAL_CODE")[0].firstChild.nodeValue;
          var phone = markers[i].getElementsByTagName("PHONE_NUMBER")[0].firstChild.nodeValue;
          var fax = markers[i].getElementsByTagName("FAX_NUMBER")[0].firstChild.nodeValue;
          var email = markers[i].getElementsByTagName("EMAIL_ADDRESS")[0].firstChild.nodeValue;
         /* if(markers[i].getElementsByTagName("DISTANCE")[0] != null) {
          	var distance =  markers[i].getElementsByTagName("DISTANCE")[0].firstChild.nodeValue;
			distance = '(' + distance + ' miles)';
          } else {
              var distance = "";
          } */
          var point = new GLatLng(parseFloat(markers[i].getElementsByTagName("LATTITUDE")[0].firstChild.nodeValue),
                                 parseFloat(markers[i].getElementsByTagName("LONGITUDE")[0].firstChild.nodeValue));
         var marker = createMarker(point, name, address, city, state, postalcode, phone, fax, email);
         map.addOverlay(marker);
         var sidebarEntry = createSidebarEntry(marker, name, address, city, state, postalcode, phone, fax, email);
         sidebar.appendChild(sidebarEntry);
         bounds.extend(point);


       }
     });
   }

    function createMarker(point, name, address, city, state, postalcode, phone, fax, email) {

    var icon = new GIcon();
   /* icon.image = "/fileadmin/templates/locations/logo.png";
    icon.shadow = "/fileadmin/templates/locations/shadow-logo.png";
    icon.iconSize = new GSize(60, 40);
    icon.shadowSize = new GSize(50, 40);
    icon.iconAnchor = new GPoint(0, 45.0);
    icon.infoWindowAnchor = new GPoint(0, 45.0);  */

    icon.image = "/fileadmin/templates/locations/blue-pushpin_01.png";

    icon.iconSize = new GSize(30, 30);
    icon.iconAnchor = new GPoint(18, 25); 
    icon.infoWindowAnchor = new GPoint(15, 25);

     var marker = new GMarker(point, icon);

     // var marker = new GMarker(point);
	        var html = '<span style="color:#1d2675;"><b>' + name + '</b></span> <br> '  + address+ '<br/>' + city + " " +   state 
	  + " " + postalcode +" <br/>Phone: "+ phone;

	   html = html + '<br>Directions:' +
           '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" style="margin:0 0 5px 0;" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +		  						 
           '<input type="hidden" name="daddr" value="' + address + ', ' + city + ', ' + state + ', ' + postalcode +  '"/>';


      GEvent.addListener(marker, 'click', function() {
   		map.setCenter(point, 6);
        map.panTo(new GLatLng(point.lat()+1.5, point.lng()));
        marker.openInfoWindowHtml(html);
      });		
       return marker;
    }

    function createSidebarEntry(marker, name, address, city, state, postalcode, phone, fax, email) {
      var div = document.createElement('div');
      var html = '<span style="color:#1d2675;"><b>' + name + '</b></span> <br/>' + address+ '<br/>' + city + " " +   state 
	  + " " + postalcode +" <br/>Phone: "+ phone;
      div.innerHTML = html;
      div.style.cursor = 'pointer';
      div.style.marginBottom = '5px'; 
      GEvent.addDomListener(div, 'click', function() {
        GEvent.trigger(marker, 'click');
      });
      GEvent.addDomListener(div, 'mouseover', function() {
        div.style.backgroundColor = '#F2F3F4';
      });
      GEvent.addDomListener(div, 'mouseout', function() {
        div.style.backgroundColor = '#fff';
      });
      return div;
    }
 
