//////////////////////////////////////////////////////////////////////
// This file contains JavaScript functions to support embedded 
// Google Maps.
//////////////////////////////////////////////////////////////////////

var sectionNameList = [];
var markerManagerList = {};
var establishments = [];
var markerList = {};

function displayDirections(address) {
 var encodedAddress = encodeURI(address);
 var url = "http://maps.google.com/maps?daddr=" + encodedAddress;
 window.open(url);
}

function displayMap(address) {
 var encodedAddress = encodeURI(address);
 var url = "http://maps.google.com/maps?q=" + encodedAddress;
 window.open(url);
}

function initializeEmbeddedMap(entry) {
 if (GBrowserIsCompatible()) {
	
	// Create the map in the "map_canvas" DIV
    var map = new GMap2(document.getElementById("map_canvas"));

	// Create center point for the Entry
	var point = createEntryCenterPoint(entry);
	map.setCenter(point, 13);
	
	// Create a Marker at the center point for the Entry
	var marker = createEntryMarkerAtPoint(entry, point, false);
	map.addOverlay(marker);

	// Create a popup Info window for the Entry Marker
	addEntryInfoToMarker(entry, marker, false);
	
	// Use default map UI
    map.setUIToDefault();
  }
}

function createEntryCenterPoint(entry) {
	var geocoding = entry.geocoding;
	var geocode_array = geocoding.split(",");
	var latitude = geocode_array[2];
	var longitude = geocode_array[3];
	var point = new GLatLng(latitude, longitude);	
	return point;
}

function createEntryMarkerAtPoint(entry, point, hide) {
	var icon = new GIcon();
	var section = entry.navSectionName;
	// Define icon image
	var image = entry.icon;
	icon.image = "/images/" + image;
	icon.iconSize = new GSize(25, 29);
	icon.iconAnchor = new GPoint(13, 14);
	icon.infoWindowAnchor = new GPoint(13.0, 14.0);
	
	// Define shadow if it exists
	var shadowImage = entry.iconShadow;
	if (shadowImage) {
		icon.shadow = "/images/" + shadowImage;
		icon.shadowSize = new GSize(41, 29);
	}
	
	// Define Marker
	var marker = new GMarker(point, {hide: hide, icon: icon});
	
	return marker;
}

function openEntryInfoWindowOnMap (map,entry) {
	var infoText = getEntryInfoWindowContent(entry,true);
	map.openInfoWindow(map.getCenter(),	document.createTextNode(infoText));
}

function toggleCategoryMarkers(checkbox,category) {
	for (var i = 0; i < establishments.length; i++) {
		var establishment = establishments[i];
		if (establishment.sectionName == category) {
			if (checkbox.checked) {
				establishment.marker.show();
				if (! sectionNameList.contains(category)) {sectionNameList.push(category);}
			} else {
				establishment.marker.hide();
				removeByElement(sectionNameList,category);
			}
		}
	}
}
				   
function focusOnMapEntry (map,establishment) {
	var point = createEntryCenterPoint(establishment);
	map.setCenter(point, 13);
	openEntryInfoWindowOnMap (map,establishment);
}
				   
function getEntryInfoWindowContent(entry, displayEntryLink) {
//	if (!displayEntryLink) { var displayEntryLink = true; }
	  var infoText = "";
	  infoText += "<table cellspacing=0 cellpadding=0 width='200px'><tr><td style=\"font-size: 9pt; font-style: Arial, sans-serif\" align=left>";
	  infoText += "<b>" + entry.estName + "</b><br>";
	  infoText += entry.estStreet + "<br>";
	  infoText += entry.estCity + ", " + entry.estState + " " + entry.estZip + "<br>";
	  infoText += entry.estPhone + "<br>";
	  var parsedUrl = entry.parsedUrl;
	  if (! parsedUrl) {
	   parsedUrl = encodeURI(entry.estUrl);
	  }
	  infoText += "<a target=_blank href='" + parsedUrl + "'>" + entry.estUrl + "</a><br>";
	  infoText += "</td></tr></table>";
	  infoText += "<hr noshade size=1 vspace=0 >";
	  infoText += "<table cellspacing=0 cellpadding=0 width='200px'><tr><td style=\"font-size: 8pt; font-style: Arial, sans-serif\" align=left>";
	  infoText += "<a href=\"javascript:void(0)\" onClick=\"displayMap('" + entry.address + "');\">Goto Map</a>";
	  infoText += " | ";
	  infoText += "<a href=\"javascript:void(0)\" onClick=\"displayDirections('" + entry.address + "');\">Get Directions</a>";
	  if (displayEntryLink == true) { 
	  infoText += " | ";
	  infoText += "<a target=_blank href=\"" + entry.articleUrl + "\">View Article</a>";
		}
	  infoText += "</td></tr></table>";
	  return infoText;
}

function addEntryInfoToMarker(entry, marker, displayEntryLink) {
	GEvent.addListener(marker, "click", function() {
		var infoText = getEntryInfoWindowContent(entry, displayEntryLink);
		marker.openInfoWindowHtml(infoText);
    });
}

function toggleCategorySelection(element, category) {
	toggleCategoryMarkers(element,category);
	toggleEstablishmentList(element,category);
}

function removeByElement(arrayName,arrayElement)
 {
    for(var i=0; i<arrayName.length;i++ )
     { 
        if(arrayName[i]==arrayElement)
            arrayName.splice(i,1); 
      } 
  }
  
function toggleEstablishmentList (checkbox, section) {
	var div = document.getElementById('map_list');
	var html = '<ul>\n';
	for (var i = 0; i < establishments.length; i++) {
	 var establishment = establishments[i];
	 if (sectionNameList.contains(establishment.sectionName)) {
		html += "<li><a href=\"javascript: focusOnMapEntry(map,establishments[" + i + "]);\">" + establishment.estName + "</a></li><BR>\n";
	 }
	}
	div.innerHTML=html;
}

function addEstMarkersToMap(markerList) {
	var mgrOptions = { borderPadding: 50, maxZoom: 15, trackMarkers: true, hide: true };
	for (var key in markerList) {
		var mgr = new MarkerManager(map, mgrOptions);
		var batch = markerList[key];
		mgr.addMarkers(batch, 10);
		markerManagerList[key] = mgr;
		mgr.refresh();
	}
}

function addMarkerToMarkerList(sectionName, marker) {
	markerArray = markerList[sectionName];
	if (! markerArray) { markerArray = []; markerList[sectionName] = markerArray; }
	markerArray.push(marker);
}

if (!Array.prototype.contains){
    Array.prototype.contains = function(obj){
    var len = this.length;
    for (var i = 0; i < len; i++){
      if(this[i]===obj){return true;}
    }
    return false;
  };
}

