//  The following JavaScript files may also be required to allow the 
//	functions in this file to operate correctly:
//  ajUIUtils.js:  Misc javascript functions
//  ajGMapUtils.js:  Google mapping functions
//  prototype.js:  AJAX & other functions

function loadMapNeighborhoods(cityId){	
	new Ajax.Request(
		"ajaxDestinationsForCityAsJSON.do", {
	 		onSuccess  : loadNeighborhoodsSuccessful,
	 		onFailure  : loadNeighborhoodsFailed,
			parameters : "cityId=" + cityId + "&type=neighborhoods&isActiveOnly=true&isDescriptionsReturned=true"
		}
	);
}

function zoomMap() {
	var zoomImage = $("zoomMapImg");
	var mapStyle  = $("gNeighborhoodMap").style;  
	if (zoomImage.src.indexOf("map_expand") < 0) {
		zoomImage.src = replaceAll(zoomImage.src,"reduce","expand");
		zoomMapImg.title = "Enlarge the map";
		mapStyle.height = "250px";
	} else {
		zoomImage.src = replaceAll(zoomImage.src,"expand","reduce");
		zoomMapImg.title = "Reduce the map";
		mapStyle.height = "400px";
	}
}

var isMapInited = false;
function loadNeighborhoodsSuccessful(xhrObject)
{
	if ( xhrObject.responseText.indexOf("fail") >= 0) {
		// Was really a failure on the server-side but HTTP request processed OK so this method was still called...
		loadNeighborhoodsFailed(xhrObject);
	} else  {
		var cityNeighborhoods = new CityDestinations(xhrObject.responseText);
		if ( !isMapInited )
		{
			//  Call ajGMapUtils to init the map
			initMap("gNeighborhoodMap",true);
			isMapInited = true;
		}	
		createGMarkers(cityNeighborhoods);

		//  Call ajGMapUtils again
		updateGoogleMap(nMarkers, false, 13, cityCenter);
	}
}
	
function loadNeighborhoodsFailed(xhrObject)
{
	//  First try to display any message embedded in the response text
	var index = xhrObject.responseText.indexOf("failure:");
	if ( index >= 0) {
		alert(xhrObject.responseText.substring(index+8,xhrObject.responseText.length));
	} else {
		alert("An error occurred while loading the map of neighborhoods for this city.\n\n"
		  + "Please refresh your page and contact us if the problem persists.");
	}

}

var destPopupHtmls = []; // hold html used by the neighborhoods
var cityCenter;
var nMarkers = [];  // hold markers used by the neighborhood popups
function createGMarkers(cityNeighborhoods)
{	
	//  First marker will be the City center so pull that out first
	cityName   = cityNeighborhoods.city.name;
	cityCenter = cityNeighborhoods.city.location;
	//var popupHtml = '<iframe src="neighborhoodPopup.do?destinationId=%s" scrolling="no" frameborder="0" height="140" width="450"></iframe>';
	var popupHtml = "<div class='ajHeading'>%s</div>"
					+ "<div class='ajText' style='margin-top:3px;width:400px'>%s</div>"
					+ "<div class='ajLogoText' style='margin-top:7px;margin-bottom:5px'>"
					+ "<img src='images/arrowDown.gif' border='0'/>"
					+ "<span class='ajLogoText' style='vertical-align:top'>"
					+ "View tours in this neighborhood below the map"
					+ "</span>"
					+ "</div>";
	
    for (var i = 0; i < cityNeighborhoods.destinations.length; i++)
    {
		var icon = getNeighborhoodIcon(cityName, cityNeighborhoods.destinations[i].name);
		nMarkers[i] = new GMarker(cityNeighborhoods.destinations[i].location, icon); //  store markers for later use by popups
		nMarkers[i].neighborhoodId = cityNeighborhoods.destinations[i].id; 

		//var nPopupHtml = sprintf(popupHtml, cityNeighborhoods.destinations[i].id);
		var nPopupHtml = sprintf(popupHtml, cityNeighborhoods.destinations[i].name, cityNeighborhoods.destinations[i].description);
	    destPopupHtmls[i] = nPopupHtml;
	    addListeners(nMarkers[i],cityNeighborhoods.destinations[i].id);
	}
}

// Show neighborhood info popup on map
function showNeighborhoodPopup(neighborhoodId) 
{
	//  First fire off the call to display the popup
    for (var i = 0; i < nMarkers.length; i++)
    {
    	if ( nMarkers[i].neighborhoodId == neighborhoodId)
			nMarkers[i].openInfoWindowHtml(destPopupHtmls[i]);
	}
}

// Called when a neighborhood is selected on the map so (1) a neighborhood info popup can
// be displayed and (2) the neighborhood checkbox can be selected in the filter.
function processNeighborhoodSelection(neighborhoodId) 
{
	//  First fire off the call to display the popup
	showNeighborhoodPopup(neighborhoodId);
	
	//  Then select the neighborhood checkbox to update the journey listing 
	selectNeighborhood(neighborhoodId);
}

//  Must create listener is a separate method or else the index used
//  by showNeighborhoodPopup() isn't stored properly
function addListeners(marker,neighborhoodId)
{	
    GEvent.addListener(marker, "click", function() { processNeighborhoodSelection(neighborhoodId) });
}