//<![CDATA[
    var map;
    var geocoder;

    var iconBlue = new GIcon(); 
    iconBlue.image = 'images/mapicon2.png';
    iconBlue.shadow = 'images/mapicon2.png';
    iconBlue.iconSize = new GSize(25, 25);
    iconBlue.shadowSize = new GSize(25, 25);
    iconBlue.iconAnchor = new GPoint(6, 20);
    iconBlue.infoWindowAnchor = new GPoint(5, 1);

    var iconRed = new GIcon(); 
    iconRed.image = 'images/mapicon2.png';
    iconRed.shadow = 'images/mapicon2.png';
    iconRed.iconSize = new GSize(25, 25);
    iconRed.shadowSize = new GSize(25, 25);
    iconRed.iconAnchor = new GPoint(6, 20);
    iconRed.infoWindowAnchor = new GPoint(5, 1);

    var customIcons = [];
    customIcons["store"] = iconBlue;
    customIcons["store"] = iconRed;
	
    function load() {
      if (GBrowserIsCompatible()) {
        geocoder = new GClientGeocoder();
        map = new GMap2(document.getElementById('map'));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(42.519687, -71.057854), 6);
      }
	  
    }
	function startLocations() {
     var startAddress = "01880";
     geocoder.getLatLng(startAddress, function(latlng) {
       if (!latlng) {
         alert(address + ' not found');
       } else {
         searchLocationsNear(latlng);
       }
	       });
   } 
   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) {
     var radius = document.getElementById('radiusSelect').value;
	 var lot = "0";
	 		if (document.searchForm.lottery.checked) {
				lot = "1";
			}
	 var self = "0";
	 		if (document.searchForm.selfserv.checked) {
				self = "1";
			}
	 var full = "0";
	 		if (document.searchForm.fullserv.checked) {
				full = "1";
			}
	 var conv ="0";
	 		if (document.searchForm.conv.checked) {
				conv = "1";
			}
	 var gar = "0";
	 		if (document.searchForm.garage.checked) {
				gar = "1";
			}	
	 var dies = "0";
	 		if (document.searchForm.diesel.checked) {
				dies = "1";
			}
     var searchUrl = 'phpsqlsearch_genxml.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius + '&L=' + lot + '&s=' + self + '&f=' + full + '&c=' + conv + '&g=' + gar + '&d=' + dies;
	 GDownloadUrl(searchUrl, function(data) {
       var xml = GXml.parse(data);
       var markers = xml.documentElement.getElementsByTagName('marker');
       map.clearOverlays();

       var sidebar = document.getElementById('sidebar');
       sidebar.innerHTML = '';
       if (markers.length == 0) {
         sidebar.innerHTML = 'No results found.';
         map.setCenter(new GLatLng(42.519687, -71.057854), 6);
         return;
       }

       var bounds = new GLatLngBounds();
       for (var i = 0; i < markers.length; i++) {
		   var id = markers[i].getAttribute('id');
         var name = markers[i].getAttribute('name');
         var address = markers[i].getAttribute('address');
		 var type = markers[i].getAttribute('type');
         var distance = parseFloat(markers[i].getAttribute('distance'));
         var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
                                 parseFloat(markers[i].getAttribute('lng')));
         
         var marker = createMarker(point, id, name, address, type);
         map.addOverlay(marker);
         var sidebarEntry = createSidebarEntry(marker, id, name, address, distance);
         sidebar.appendChild(sidebarEntry);
         bounds.extend(point);
       }
       map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
     });
   }

    function createMarker(point, id, name, address, type) {
      var marker = new GMarker(point, customIcons[type]);
      var html = "<b>" + name + "</b> <br/>" + address + "<br><a href='store.php?loc=" + id + "'>More Info</a>";
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }

    function createSidebarEntry(marker, id, name, address, distance) {
      var div = document.createElement('div');
      var html = '<b>' + name + '</b> (' + distance.toFixed(1) + ' Miles)<br/>' + address + "<br><a href='store.php?loc=" + id + "'>More Info</a>";
      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 = '#ffff00';
      });
      GEvent.addDomListener(div, 'mouseout', function() {
        div.style.backgroundColor = '#ffffff';
      });
      return div;
    }
    //]]>
