

// Variable for the map
var map;
var mapPinDir = "/images/mapPinsV262387";

var listingIds  = new Array();
var markers     = new Array();

// Variable for mapicons
var mapBlackIcons = new Array();
var mapGreyIcons = new Array();


// Creates a marker at the given point with the given number label
function createGMarker(point, html, icon, markerNumber, listingId, showDirections) {
	var marker;
	
	if (icon) {
		marker = new GMarker(point, {icon: icon});   
	}
	else {
		marker = new GMarker(point);   	
	}
	
	if (html) {
	    GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html); 
			requestBubble('bubble-' + markerNumber, listingId, showDirections, markerNumber);       	
	    });
	}
	
    return marker;
}

function createGMarkers(markerNumber, latitude, longitude, listingId, businessName, paid, address, suburb, state, postCode, phoneNumber, seoURL, showDirections) {
	var point = new GLatLng(latitude, longitude);

   	var blackIcons = paid;
	var markerIcon = getMarkerIcon(markerNumber, blackIcons);	
	html = '<div id="bubble-' + markerNumber + '"></div>';
	var marker = createGMarker(point, html, markerIcon, markerNumber, listingId, showDirections);
	return marker;			
}

var currentBubbleContent;

function requestBubble(element, listingId, showDirections, markerNumber) {
	var url = Truelocal.Rnr.url('googleMapsBubble.do');
    var params = 'id=' + listingId + '&showDirections=' + showDirections + '&markerNumber=' + markerNumber;
 	currentBubbleContent = element;
	var options = {
		onComplete: updateBubble
	};

    Truelocal.Rnr.requestAjax(
    	element,
        url,
        params,
        options
    );
   
}

function updateBubble() {
	var content = $(currentBubbleContent).innerHTML;
	var tabs = new Array();
	tabs.push(new GInfoWindowTab('',  content));
	map.updateInfoWindow(tabs);
}

function showDirectionInput(toFrom, markerNumber) {
	if (toFrom == 'to') {
		$('fromHereInput' + markerNumber).hide();
		$('fromHereLink' + markerNumber).className = 'fromHere';
		$('toHereLink' + markerNumber).className = 'toHere noLink';
		Element.show('toHereInput' + markerNumber);
 		map.updateInfoWindow(map.getInfoWindow().getTabs())
	}
	else {
		$('toHereInput' + markerNumber).hide();
		$('toHereLink' + markerNumber).className = 'toHere';
		$('fromHereLink' + markerNumber).className = 'fromHere noLink';
		Element.show('fromHereInput' + markerNumber);
		map.updateInfoWindow(map.getInfoWindow().getTabs())
	}
}

function backDirections(markerNumber) {
	$('fromHereInput' + markerNumber).hide();
	$('toHereInput' + markerNumber).hide();
	$('fromHereLink' + markerNumber).className = 'fromHere';
	$('toHereLink' + markerNumber).className = 'toHere';
	map.updateInfoWindow(map.getInfoWindow().getTabs())
}

// Gets a marker loading on demand
function getMarkerIcon(iconNumber, blackIcons) {
	var iconSet;
	var iconFilePrefix;

	if (blackIcons == true) {
		iconSet = mapBlackIcons;
		iconFilePrefix = mapPinDir + "/MPB";
	} 
	else {
		iconSet = mapGreyIcons;
		iconFilePrefix = mapPinDir + "/MPG";
	}
	
	// if no iconNumber we want star icon
	if (!iconNumber) {
		var newIcon = new GIcon();
		newIcon.image = iconFilePrefix + "star.png";
		newIcon.iconSize = new GSize(26, 35);
		newIcon.shadow = mapPinDir + "/MapPin-Shadow.png";
		newIcon.shadowSize = new GSize(46, 35);
		newIcon.iconAnchor = new GPoint(13, 32);
		newIcon.infoWindowAnchor = new GPoint(12, 4);
		newIcon.printImage = iconFilePrefix + "star.gif";
		newIcon.mozPrintImage = iconFilePrefix + "star.gif";
		return newIcon;
	}
	
		// if iconNumber is G or R we want Green Star or Red Star (directions)
	if (iconNumber == 'R' || iconNumber == 'G') {
		var newIcon = new GIcon();
		newIcon.image = mapPinDir + "/MP" + iconNumber + "star.png";
		newIcon.iconSize = new GSize(26, 35);
		newIcon.shadow = mapPinDir + "/MapPin-Shadow.png";
		newIcon.shadowSize = new GSize(46, 35);
		newIcon.iconAnchor = new GPoint(13, 32);
		newIcon.infoWindowAnchor = new GPoint(12, 4);
		newIcon.printImage = mapPinDir + "/MP" + iconNumber + "star.gif";
		newIcon.mozPrintImage = mapPinDir + "/MP" + iconNumber + "star.gif";
		return newIcon;
	}
	
	var icon = iconSet[iconNumber];
	// Create and load the icon
	if (!icon) {
		var newIcon = new GIcon();
		newIcon.image = iconFilePrefix + iconNumber + ".png";
		newIcon.iconSize = new GSize(26, 35);
		newIcon.shadow = mapPinDir + "/MapPin-Shadow.png";
		newIcon.shadowSize = new GSize(46, 35);
		newIcon.iconAnchor = new GPoint(13, 32);
		newIcon.infoWindowAnchor = new GPoint(12, 4);
		newIcon.printImage = iconFilePrefix  + iconNumber + ".gif";
		newIcon.mozPrintImage = iconFilePrefix  + iconNumber + ".gif";
		iconSet[iconNumber] = newIcon;
	} 
	
	return iconSet[iconNumber];	
}

// Closes any markers which are open
function closeMarker() {
	map.closeInfoWindow();
}

// Zooms (maximum zoom) to the marker and sets the centre of the map to it
function zoomMarker(listingId) {
	map.closeInfoWindow();
    var marker;
   	var num;
   
   	for (var i = 0; i < listingIds.length; i++) {
   		var id = listingIds[i];
   		if (id == listingId) {
   			num = i;
   			break;
   		}
	} 
	
   	if (num != undefined) {
   		map.setCenter(markers[num].getPoint(), 16);
   		GEvent.trigger(markers[num],  "click");  		
   		return;
   	}    	
   	//Clicks on a listing-id with no marker so close all info windows
   	//map.closeInfoWindow();

	// Code below does not actually detect currently available max zoom
	// - instead hard-code to one above the max to be safe
	//var zoom = map.getCurrentMapType().getMaximumResolution(point);
	//map.closeInfoWindow();
    //map.setCenter(marker.getPoint(), 16);
    //GEvent.trigger(marker,  "click");
}

function openMarker(listingId) {
   	var marker;
   	var num;
   	for (var i = 0; i < listingIds.length; i++) {
   		var id = listingIds[i];
   		if (id == listingId) {
   			num = i;
   			break;
   		}
	} 
   	if (num != undefined) {
   		GEvent.trigger(markers[num],  "click");  		
   		return;
   	}    	
   	//Clicks on a listing-id with no marker so close all info windows
   	if (map) {
   		map.closeInfoWindow();
   	}
}

function getAddress(placemark) {
 	var searchAddress;
	if (placemark.AddressDetails.Accuracy > 2) { // above state level
		if (placemark.AddressDetails.Country.AdministrativeArea) {
			state  = placemark.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
			suburb = placemark.AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
			searchAddress = suburb + ',' + state;
		}
		else {
			suburb = placemark.AddressDetails.Country.PostalCode.PostalCodeNumber;
			searchAddress = suburb;
		}
	}
	else if (placemark.AddressDetails.Accuracy == 2) { // state level
		var state  = placemark.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
		searchAddress = state;
	}
	else { // country level
		searchAddress = 'australia';
	}		
	return searchAddress;
}
