var map;
    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;
	var counter = 0;
	
	
		  var addy = ['2485 High School Ave, Suite 227 Concord, CA 94520-1819',
					  '1895 Mowry Ave, Suite 116 Fremont, CA 94538-1737',
					  '2322 Butano Dr, Suite 205 Sacramento, CA 95825-0629',
					  '2107 OFarrell St San Francisco, CA 94115-3419',
					  '2365 Montpelier Dr San Jose, CA 95116-1614'];

    function initialize() {
		if (GBrowserIsCompatible()) {
		  map = new GMap2(document.getElementById("google_map"));
		  map.setCenter(new GLatLng(37.98538, -122.034176), 7);
		  
		  var lat = [37.98538, 37.5589833, 38.6066999, 37.783515, 37.365499];
		  var long = [-122.034176, -121.9800062, -121.3815254, -122.433277, -121.847606];
		  map.addControl(new GSmallMapControl());
		  map.addControl(new GMapTypeControl());
		  
		  
		  geocoder = new GClientGeocoder();
		  gdir = new GDirections(map, document.getElementById("directions"));
		  GEvent.addListener(gdir, "error", handleErrors);
		  
		  for (var i=0; i<addy.length; i++) {
			address = addy[i];
      		geocoder.getLocations(address, addAddressToMap2);
			/*var point = new GLatLng(lat[i],
									 long[i]);
			//alert(point);
			var marker = new GMarker(point);
			map.addOverlay(marker);
			GEvent.addListener(marker, "click", function() {
				var myHtml = "Empty";
				map.openInfoWindowHtml(point, myHtml);
			});*/
		  }
		  
		}
    }
	
	function createMarker(point,html) {
        var marker = new GMarker(point);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml('<div style="width:270px"><h3 class="map-title"><a href="javascript:void(0)" onclick="findLocation(\'' + html + '\');return false;">' + html + '<br \/><span>Get Directions<\/span><\/a><\/h3><\/div>');
        });
        return marker;
      }

    // addAddressToMap() is called when the geocoder returns an
    // answer.  It adds a marker to the map with an open info window
    // showing the nicely formatted version of the address and the country code.
    function addAddressToMap(response) {
      if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
      } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
		//alert(point);
        marker = new GMarker(point);
        map.addOverlay(marker);
		marker.openInfoWindowHtml('<div style="width:270px"><h3 class="map-title">' + place.address + '<br \/><span>Enter your address, city, or zip code below to get directions to this clinic.<\/span><\/h3><\/div>');
      }
    }
	
    function addAddressToMap2(response) {
      if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
      } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
		//alert(point);
		displayMsg = addy[counter];
		var marker = createMarker(point,displayMsg);
        map.addOverlay(marker);
		counter++;
		
      }
    }

    // showLocation() is called when you click on the Search button
    // in the form.  It geocodes the address entered into the form
    // and adds a marker to the map at that location.
    function showLocation() {
      address = document.forms[0].q.value;
      geocoder.getLocations(address, addAddressToMap);
    }
	

   // findLocation() is used to enter the sample addresses into the form.
    function findLocation(address) {
      document.forms[0].q.value = address;
	  $('#map_directions').show('slow');
      showLocation();
    }	
	
	function setDirections(fromAddress, toAddress) {
      gdir.load("from: " + fromAddress + " to: " + address,
                { "locale": "en" });
    }

    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("We are having difficulties finding that address. Please contact our clinic via phone to get personal drections to the clinic.");
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}