var map = null;
var drawMap = false;
var mapmarkers = [];
var enableZoomToolFlag = false;
var enableTypeToolFlag = false;
var bName = "", bAddress = "";

// Create a base icon for all of our markers that specifies the shadow, icon
// dimensions, etc.
var baseIcon = new GIcon();
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);

function enableMap() {
	drawMap = true;
}

function enableZoomTool() {
	enableZoomToolFlag = true;
}

function enableTypeTool() {
	enableTypeToolFlag = true;
}

function pushMapMarker(marker) {
	mapmarkers.push(marker);
}
	
function initMap(markers) {
  if (! drawMap) return;

  var mapDiv = document.getElementById("pgmap");

  if (mapDiv != null && GBrowserIsCompatible()) {
    map = new GMap(mapDiv);
    map.centerAndZoom(new GPoint(-74.00523, 40.749155), 2);
    var lastPoint = addMarkers(markers);
    if (enableZoomToolFlag) {
      map.addControl(new GLargeMapControl());
    }
    if (enableTypeToolFlag) {
      map.addControl(new GMapTypeControl());
    }
    if (lastPoint != null) {
      map.centerAndZoom(lastPoint, 2);
    }
  }
  else {
    mapDiv.innerHTML = 'Map not available<br>for this browser';
  }

}

function createMarker(point, i) {
  var letter = String.fromCharCode("A".charCodeAt(0) + i);
  var icon = new GIcon(baseIcon);
  icon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
  var marker = new GMarker(point, icon);
  //GEvent.addListener(marker, "infowindowopen", function() {highlightLegend(index, 1);});
  //GEvent.addListener(marker, "infowindowclose", function() {highlightLegend(index, 0);});
  return marker;
}

function addMarkers(markers) {
  if (markers == null) return null;
  var firstPoint = null;
  for (i = 0; i < markers.length; i++) {
    var longcoord = markers[i][0];
    var latcoord = markers[i][1];
    var index = markers[i][2];
    var point = new GPoint(longcoord, latcoord);
    if (i == 0) {
      firstPoint = point;
    }
    var marker = null;
    if (index > -1) {
      marker = createMarker(point, index);
    }
    else {
      marker = new GMarker(point);
    }
    map.addOverlay(marker);
  }
  return firstPoint;
}

   function addToMap(response)
   {
	  if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable find the address: " + bAddress);
      }
      else{
      // Retrieve the object
      place = response.Placemark[0];

      // Retrieve the latitude and longitude
      point = new GLatLng(place.Point.coordinates[1],
                          place.Point.coordinates[0]);

      // Center the map on this point
      map.setCenter(point, 13);

      // Create a marker
      marker = new GMarker(point);
      //marker = createMarker(point, 0);
      // Add the marker to map
      map.addOverlay(marker);

      // Add address information to marker
      //marker.openInfoWindowHtml(place.address);
      marker.openInfoWindowHtml(bName.replace("[", "").replace("]", "") + "<br/>" + processAddress(bAddress));
	  map.addControl(new GLargeMapControl());
      }
   }
   
   function load(address, businessname)
   {
      // Create new map object
      map = new GMap2(document.getElementById("pgmap"));
      bName = businessname;
      bAddress = address;
      // Create new geocoding object
      geocoder = new GClientGeocoder();

      // Retrieve location information, pass it to addToMap()
      geocoder.getLocations(cleanAddress(bAddress), addToMap);
   }
   
   function processAddress(address){
   	var result = address;
   	if(address != null){
   		var str = result.split(",")
   		if(str[0] == "Serving Your Area")
   			result = str[1] + "," + str[2];
   		else
   			result = str[0] + "<br/>" + str[1] + "," + str[2];
   	}
   	return result;
   }
   
   function cleanAddress(address){
   		var result = address;   		
   		if(result != null){
   			var str = result.split(",");
   			if(str[0] == "Serving Your Area")
   				result = str[1] + "," + str[2];
   			else
   				result = address;
    	}
   		return result;
   }