//Must be activated otherwise the map is not rendered.
var GMAPS_ACTIVE = 0;

//Is set to 1 when the GMap2 object has been initiated.
var GMAPS_CREATED = 0;

var googleMap;

//Map center coordinates
var LONGITUDE = 0;
var LATITUDE = 0;

var mapKey = "";

var showThisText = "Show this";

//Holds the markers to be plotted on the map
var markers = new Array();
//Holds the point of interest markers to be plotted on the map
var poiMarkers = new Array();

var activeStr = "false";

var zoomLevel = 11;

var satelliteView = 0;

function setActive(mActiveStr) {
	activeStr = mActiveStr;	
}

//Sets the "Show this" text to be used on hotel markers.
function setShowThisText(mText) {
	showThisText = mText;
}

//Sets the Google Map key. If the map key is invalid maps will not be set to active.
function setMapKey(key) {
	mapKey = key;	
}

//Activate the maps. Maps is only activated if one or more valid markers has been added
//and the map key is not an empty string.
function activateMaps() {
	if (markers.length > 0 && mapKey != "" && activeStr == "true" && GBrowserIsCompatible()) {
		GMAPS_ACTIVE = 1;
		$('hotelsMap').show();
		
		var gMapBoxEl = document.getElementById("gMapBox");
		if (gMapBoxEl != null) {
			$("gMapBox").show();
		}
		
		loadMaps();
		showId('showAllHFMap');
	} else {
	   $('hotelsNoMap').innerHTML = "Sorry, we couldn\'t find a map for this location.";
	   if($("gMapBox")) $("gMapBox").show();
	}
	$('hotelsMapLoading').hide();
}

//Re-loads the map
function refreshMaps() {
	if (GMAPS_CREATED == 1) {
		loadMaps();
	}	
}

//Sets the zoomlevel for this map
function setZoomLevel(mZoomLevel) {
	zoomLevel = mZoomLevel;
}

//Sets the center for this map.
function setMapCenter(longitude, latitude) {
	LONGITUDE = longitude;
	LATITUDE = latitude;
}

//Sets that the map should be default to satellite view
function setSatelliteView() {
	satelliteView = 1;
}

//Adds a new marker to the map if the data is correct.
function addMarker(longitude, latitude, info, identifier) {
	if (longitude == null || longitude == "") return;
	if (latitude == null || latitude == "") return;
	if (identifier == null || identifier == "") return;
	if (info == null || info == "") return;
	
	markers[markers.length] = new HotelMarker(longitude, latitude, info, identifier);
}

//Adds a new point of interest marker to the map if the data is correct.
function addPOIMarker(longitude, latitude, info) {
	if (longitude == null || longitude == "") return;
	if (latitude == null || latitude == "") return;
	if (info == null || info == "") return;
	
	poiMarkers[poiMarkers.length] = new POIMarker(longitude, latitude, info);
}

//Hides the marker with the seleced id (if found)
function hideMarker(mIdentifier) {
	for (var i = 0; i < markers.length; i++) {	
		if (markers[i].identifier == mIdentifier) {
			markers[i].hideMarker();
		}
	}
}

//Shows the marker with the seleced id (if found)
function showMarker(mIdentifier) {
	for (var i = 0; i < markers.length; i++) {	
		if (markers[i].identifier == mIdentifier) {
			markers[i].showMarker();
		}
	}
}

//Cneters the map to the marker with the seleced id (if found)
function centerMarker(mIdentifier) {
	for (var i = 0; i < markers.length; i++) {	
		if (markers[i].identifier == mIdentifier) {
			markers[i].centerMarker();
		}
	}
}

//Creates the GMap2 object and adds all markers
function loadMaps() {
	if (GMAPS_ACTIVE == 1) {
		googleMap = new GMap2(document.getElementById("hotelsMap"));
		GMAPS_CREATED = 1;
		googleMap.addControl(new GSmallMapControl());
		googleMap.addControl(new GMapTypeControl());
		googleMap.setCenter(new GLatLng(LATITUDE, LONGITUDE), zoomLevel);
		if (satelliteView == 1) {
			googleMap.setMapType(G_SATELLITE_MAP);	
		}
		
		for (var i = 0; i < markers.length; i++) {
			markers[i].shown = 0;
			markers[i].showMarker();
		}
		
		for (var i = 0; i < poiMarkers.length; i++) {
			poiMarkers[i].shown = 0;
			poiMarkers[i].showMarker();
		}
	}	
}

//Unload maps to avoid memory leaks.
function unloadMaps() {
	if (GMAPS_ACTIVE == 1) {
		GMAPS_ACTIVE = 0;
		GUnload();	
	}
}

//Shows a specific hotel. Needs to clear the filter to be sure the hotel is shown.
function showSpecificHotel(hotelName) {
	hf.fl('hotelname', hotelName, true, FULL_UPD);
}

//Class for holding HotelMarker info
function HotelMarker(mLongitude, mLatitude, mInfo, mIdentifier) {
	this.longitude = mLongitude;
	this.latitude = mLatitude;	
	this.info = mInfo;
	this.identifier = mIdentifier;
	this.obj;
	this.shown = 0;
	this.created = 0;
	
	this.createMarker = hm_createMarker;
	this.showMarker = hm_showMarker;
	this.hideMarker = hm_hideMarker;
	this.centerMarker = hm_centerMarker;
}

//Creates a GMarker object from the data in the HotelMarker object.
function hm_createMarker() {
	var point = new GLatLng(this.latitude, this.longitude);
	var marker = new GMarker(point);
	var mInfo = this.info;
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(mInfo);
	});
	
	if (showThisText != "") {
		GEvent.addListener(marker, "infowindowclose", function() {
			hf.fl('hotelname', '', true, FULL_UPD);
		});
	}
	
	this.obj = marker;
	this.created = 1;
}

function hm_showMarker() {
	if (GMAPS_CREATED == 1) {
		if (this.created == 0) {
			this.createMarker();	
		}
		if (this.shown == 0) {
			googleMap.addOverlay(this.obj);	
			this.shown = 1;
		}
	}
}

function hm_hideMarker() {
	if (GMAPS_CREATED == 1) {
		if (this.created == 0) {
			this.createMarker();	
		}
		if (this.shown == 1) {
			googleMap.removeOverlay(this.obj);
			this.shown = 0;	
		}
	}
}

function hm_centerMarker() {
	googleMap.setCenter(new GLatLng(this.latitude, this.longitude), zoomLevel);
	var mInfo = this.info;
	this.obj.openInfoWindowHtml(mInfo);
}

//Generates HTML for a hotel marker.
function generateHotelMarkerText(stars, hotelName, totalFare, totalCurrency, thumbUrl) {
	var html = "<table><tr>";
	html += "<td width=\"85\"><img src=\"" + thumbUrl + "\" align=\"left\" width=\"80\" height=\"80\"/></td>";
	html += "<td valign=\"top\">" + stars + "<br/>";
	html += hotelName + "<br/>";
	html += "<b>" + totalFare + "&nbsp;" + totalCurrency + "</b><br/>";
	if (showThisText != "") {
		html += "<a href=\'javascript:showSpecificHotel(\"" + escape(hotelName) + "\");\'>" + showThisText + "</a>";
	}
	html += "<td></tr></table>";
	
	return html;
}
 
function generateOneHotelMarkerText(hotelName, hotelAdress, hotelCity, hotelPostalCode, hotelCountry) {
	var html = "<div class='hotelMarkerText'>";
	html += "<h2>" + hotelName + "</h2>";
	html += "<p>" + hotelAdress + "</p>";
	html += "<p>" + hotelCity + " " + hotelPostalCode + "</p>";
	html += "<p>" + hotelCountry + "</p>";
	
	
	if (showThisText != "") {
		html += "<a href=\'javascript:showSpecificHotel(\"" + escape(hotelName) + "\");\'>" + showThisText + "</a>";
	}
	html += "</div>";
	
	return html;
	
}


//Class for holding Point of interest marker info
function POIMarker(mLongitude, mLatitude, mInfo) {
	this.longitude = mLongitude;
	this.latitude = mLatitude;	
	this.info = mInfo;
	this.obj;
	this.shown = 0;
	this.created = 0;
	
	this.createMarker = poi_createMarker;
	this.showMarker = poi_showMarker;
	this.centerMarker = poi_centerMarker;
}

//Creates a GMarker object from the data in the POIMarker object.
function poi_createMarker() {
	var point = new GLatLng(this.latitude, this.longitude);
	
	var icon = new GIcon();
	icon.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
	icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	icon.iconSize = new GSize(12, 20);
	icon.shadowSize = new GSize(22, 20);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);

	var mInfo = this.info;
	
	var marker = new PdMarker(point, icon);
	marker.setDetailWinHTML(mInfo);
	
	this.obj = marker;
	this.created = 1;
}

function poi_showMarker() {
	if (GMAPS_CREATED == 1) {
		if (this.created == 0) {
			this.createMarker();	
		}
		if (this.shown == 0) {
			googleMap.addOverlay(this.obj);	
			this.shown = 1;
		}
	}
}

function poi_centerMarker() {
	googleMap.setCenter(new GLatLng(this.latitude, this.longitude), zoomLevel);
	var mInfo = this.info;
	this.obj.openInfoWindowHtml(mInfo);
}

function generatePOIMarkerText(interestName) {
	var html = "<b>" + interestName + "</b>";
	return html;
}














