//var geoXml = new GGeoXml("http://www.redpalm-kl.com/location/RedPalmKL.kml");
//http://www.backpackingmalaysia.com/images/site-images/sign-puduraya.png
//http://www.backpackingmalaysia.com/images/site-images/sign-twintowers.png
//http://www.backpackingmalaysia.com/images/site-images/sign-gecko-small-green.png
//http://www.backpackingmalaysia.com/images/site-images/sign-gecko-small-orange.png

var	htMapPointsIndex = new Hash();
var	markerPoints = [];
var	markerIcons = [];
var	markerIconsWidth = [];
var	markerIconsHeight = [];
var	markerToolTips = [];
var	markerLinks = [];
var	markerFlagsType = [];
var	markerFlagsIsPermanent = [];
var markerZIndexes = [];
var markerFlagsVisible = [];
var	markerFlagsVisibleDefault = [];
var markerFlagsOutBounds = [];
var PoI_Markers = [];
var iPoint = 0;

var	locLat;
var	locLng;
var	locZoom;
var	locMapType;
var locBounds;

var typeAll = 0;
var typeHostel = 1;
var typeSight = 2;

var hasPermanentPoIs = false;

function initAndLoadMap(tmpLocLat, tmpLocLng, tmpLocZoom, tmpLocMapType, tmpMapType) {
	if (GBrowserIsCompatible()) {
		
		// * store given values for later use *
		locLat = tmpLocLat;
		locLng = tmpLocLng;
		locZoom = tmpLocZoom;
		locMapType = tmpLocMapType;
		
		// * init map  function... to be called later*
		mapInit = function() {
			GMap.init(tmpMapType, locLat, locLng, locZoom, locMapType);
			GMap.clear();
			// * add all previously stored point to the map *
			//for (var iZIndex = 0; iZIndex <= 7; iZIndex++) {
				for (var i = 0; i < htMapPointsIndex.length; i++) {
					//if (markerZIndexes[i] == iZIndex) {
						PoI_Markers[i] = GMap.addPoI(htMapPointsIndex[i], markerPoints[i], markerToolTips[i], markerLinks[i], markerIcons[i], markerIconsWidth[i], markerIconsHeight[i], markerFlagsType[i], markerFlagsIsPermanent[i], markerFlagsVisibleDefault[i]);
					//};
				};
			//};
			if (!hasPermanentPoIs) {GMap.setBounds(GMap.map().getBounds());};

			locBounds = GMap.bounds();
			GMap.setCenterAndZoom(locZoom, false);
		};

		// * init map *
		mapInit();
	};
};

function addMapPoint(entry_id, lat, lng, tooltip, isPermanent, type, iconpic, iconwidth, iconheight, url, isVisible) {
	if (isPermanent == 'yes') {isPermanent = true;};
	if (isPermanent == 'no') {isPermanent = false;};
	if (!iconwidth) {iconwidth = 24};
	if (!iconheight) {iconheight = 28};

	var markerZIndex = 0;
	if (!iconpic) {
		switch (type) {
			case typeHostel:
				iconpic = "http://www.backpackingmalaysia.com/images/site-images/sign-gecko-small-orange.png";
				break;
			case typeSight:
				iconpic = "http://www.backpackingmalaysia.com/images/site-images/sign-gecko-small.png"
				break
			default:
				iconpic = "http://www.backpackingmalaysia.com/images/site-images/sign-gecko-small.png";
		};
	} else {
		markerZIndex += 4;
	};
	if (isPermanent) {markerZIndex += 2; hasPermanentPoIs = true;};
	if (type == typeSight) {markerZIndex += 1;};
	
	htMapPointsIndex.setItem(entry_id,iPoint);
	markerPoints[iPoint] = new GLatLng(lat,lng);
	markerIcons[iPoint] = iconpic;
	markerIconsWidth[iPoint] = iconwidth;
	markerIconsHeight[iPoint] = iconheight;
	markerIcons[iPoint] = iconpic;
	markerToolTips[iPoint] = tooltip;
	markerLinks[iPoint] = url;
	markerFlagsType[iPoint] = type;
	markerFlagsIsPermanent[iPoint] = isPermanent;
	markerZIndexes[iPoint] = markerZIndex;
	markerFlagsVisibleDefault[iPoint] = isVisible;
	markerFlagsVisible[iPoint] = isVisible;
	iPoint++;
};

function showPoIInfo(entry_id) {
	var lat = markerPoints[htMapPointsIndex.getItem(entry_id)].lat();
	var lng = markerPoints[htMapPointsIndex.getItem(entry_id)].lng();
	var bounds = GMap.map().getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	if (!(((northEast.lng() > lng) && (lng > southWest.lng())) && ((northEast.lat() > lat) && (lat > southWest.lat())))) {
		//alert('(((' + northEast.lng() + ' > ' + lng + ') && (' + lng + ' > ' + southWest.lng() + ')) && ((' + northEast.lat() + ' > ' + lat + ') && (' + lat + ' > ' + southWest.lat() + ')))')
		markerFlagsOutBounds[htMapPointsIndex.getItem(entry_id)] = true;
		GMap.setBounds(locBounds);
		GMap.setLocMapCenter(locLat, locLng, locZoom, locMapType, locMapType);
		GMap.bounds().extend(markerPoints[htMapPointsIndex.getItem(entry_id)]);
		GMap.setCenterAndZoom(locZoom, true);
	};
	markerFlagsVisible[htMapPointsIndex.getItem(entry_id)] = !PoI_Markers[htMapPointsIndex.getItem(entry_id)].isHidden();
	PoI_Markers[htMapPointsIndex.getItem(entry_id)].show();
	PoI_Markers[htMapPointsIndex.getItem(entry_id)].setImage('http://www.backpackingmalaysia.com/images/site-images/sign-gecko-small-green.png');
	//PoI_Markers[htMapPointsIndex.getItem(entry_id)].showTooltip();
};

function hidePoIInfo(entry_id) {
	if (markerFlagsOutBounds[htMapPointsIndex.getItem(entry_id)]) {
		GMap.setBounds(locBounds);
		GMap.setLocMapCenter(locLat, locLng, locZoom, locMapType, locMapType);
		GMap.setCenterAndZoom(locZoom, false);
	};
	if (!markerFlagsVisible[htMapPointsIndex.getItem(entry_id)]) {PoI_Markers[htMapPointsIndex.getItem(entry_id)].hide();};
	PoI_Markers[htMapPointsIndex.getItem(entry_id)].setImage(markerIcons[htMapPointsIndex.getItem(entry_id)]);
	PoI_Markers[htMapPointsIndex.getItem(entry_id)].hideTooltip();
};

function showPoIsOfType(typeOfPoI) {
	for (var i = 0; i < htMapPointsIndex.length; i++) {
		if (typeOfPoI == markerFlagsType[i] || typeOfPoI == typeAll) {
			markerFlagsVisible[i] = true;
			PoI_Markers[i].show();
		};
	};
};

function hidePoIsOfType(typeOfPoI) {
	for (var i = 0; i < htMapPointsIndex.length; i++) {
		if ((typeOfPoI == markerFlagsType[i] || typeOfPoI == typeAll) && (!markerFlagsIsPermanent[i])) {
			markerFlagsVisible[i] = false;
			PoI_Markers[i].hide();
		};
	};
};

function setVisbilityPoIsOfType(typeOfPoI) {
	for (var i = 0; i < htMapPointsIndex.length; i++) {
		if (typeOfPoI == markerFlagsType[i] || typeOfPoI == typeAll) {
			if (markerFlagsVisibleDefault[i]) {
				PoI_Markers[i].show();
			} else {
				PoI_Markers[i].hide();
			};
		};
	};
};

// a small hash table class
function Hash() {
	this.length = 0;
	this.items = new Array();
	for (var i = 0; i < arguments.length; i += 2) {
		if (typeof(arguments[i + 1]) != 'undefined') {
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}
	this.removeItem = function(in_key) {
		var tmp_value;
		if (typeof(this.items[in_key]) != 'undefined') {
			this.length--;
			var tmp_value = this.items[in_key];
			delete this.items[in_key];
		}
		return tmp_value;
	}
	this.getItem = function(in_key) {
		return this.items[in_key];
	}
	this.setItem = function(in_key, in_value) {
		if (typeof(in_value) != 'undefined') {
			if (typeof(this.items[in_key]) == 'undefined') {
				this.length++;
			}
			this.items[in_key] = in_value;
		}
		return in_value;
	}
	this.hasItem = function(in_key) {
		return typeof(this.items[in_key]) != 'undefined';
	}
}

// overwrite some GxMarker classes
function GxMarkerNamespace() {
	var n4=(document.layers);
	var n6=(document.getElementById&&!document.all);
	var ie=(document.all);
	var o6=(navigator.appName.indexOf("Opera") != -1);
	var safari=(navigator.userAgent.indexOf("Safari") != -1);
	var currentSpan = new GBounds();

	function setCursor(container, cursor) {
		try {
			container.style.cursor = cursor;
		} catch (c) {
			if (cursor == "pointer") {setCursor("hand");};
  		};
	};

	function GxMarker(a, b, tooltipHtml) {
		this.inheritFrom = GMarker;
		this.inheritFrom(a,b);
		if (!currentSpan.minX || a.x < currentSpan.minX) currentSpan.minX = a.x;
		if (!currentSpan.maxX || a.x > currentSpan.maxX) currentSpan.maxX = a.x;
		if (!currentSpan.minY || a.y < currentSpan.minY) currentSpan.minY = a.y;
		if (!currentSpan.maxY || a.y > currentSpan.maxY) currentSpan.maxY = a.y;
		if (typeof tooltipHtml != "undefined") {
			this.setTooltip( tooltipHtml );
		};
	};
	
	GxMarker.prototype = new GMarker(new GLatLng(1, 1));

	GxMarker.prototype.setTooltip = function(string) {
		this.removeTooltip();
		this.tooltip = new Object();
		this.tooltip.opacity = 100;
		this.tooltip.contents = string;
	};

	GxMarker.prototype.initialize = function(a) {
		try {
			GMarker.prototype.initialize.call(this, a);
			GEvent.addListener(this, "mouseover", this.onMouseOver);
			GEvent.addListener(this, "mouseout", this.onMouseOut);
			this.map = a;
		} catch(e) {
			console.error(e);
		};
	};

	GxMarker.prototype.setCursor = function(cursor) {
		var c = this.iconImage;
		// Use the image map for Firefox/Mozilla browsers
		if (n6 && this.icon.imageMap && !safari) {
			c = this.imageMap;
		}
		// If we have a transparent icon, use that instead of the main image
		else if (this.transparentIcon && typeof this.transparentIcon != "undefined") {
			c = this.transparentIcon;
		};
	};

	GxMarker.prototype.removeTooltip = function() {
		if (this.tooltipElement) {
			try {
				document.body.removeChild(this.tooltipElement);
			} catch(e) {
				console.error(e);
			};
			this.tooltipElement = null;
		};
	};

	GxMarker.prototype.onMouseOver = function() {
		this.showTooltip();
	};

	GxMarker.prototype.onMouseOut = function() {
		this.hideTooltip();
	};

	GxMarker.prototype.showTooltip = function() {
		if (this.tooltip) {
			if (!this.tooltipElement) {
				var opacity = this.tooltip.opacity / 100;
				this.tooltipElement = document.createElement("div");
				this.tooltipElement.style.display    = "none";
				this.tooltipElement.style.position   = "absolute";
				this.tooltipElement.style.background = "#fff";
				this.tooltipElement.style.padding    = "0";
				this.tooltipElement.style.margin     = "0";
				this.tooltipElement.style.MozOpacity = opacity;
				this.tooltipElement.style.filter     = "alpha(opacity=" + this.tooltip.opacity + ")";
				this.tooltipElement.style.opacity    = opacity;
				this.tooltipElement.style.zIndex     = 1000;
				this.tooltipElement.innerHTML        = this.tooltip.contents;
				document.body.appendChild(this.tooltipElement);
			};
			markerPosition = calculateMarkerPosition(this.map, this.getPoint());
			try {
				this.tooltipElement.style.top  = markerPosition.top  + ( this.getIcon().iconSize.height / 2 ) - 10 + "px";
				this.tooltipElement.style.left = markerPosition.left - 100 + "px";
				this.tooltipElement.style.display = "block";
			} catch(e) {
				console.error(e);
			};
		};
	};

	function calculateMarkerPosition(map, markerLatLngPoint) {
		gmapTopLeftLatLng = map.fromContainerPixelToLatLng(new GPoint(0,0));
		gmapTopLeftPixel = map.fromLatLngToDivPixel(gmapTopLeftLatLng);
		currentMarkerDivPixel = map.fromLatLngToDivPixel(markerLatLngPoint);
		markerTop = getAbsoluteTop(map.getContainer()) - gmapTopLeftPixel.y + currentMarkerDivPixel.y;
		markerLeft = getAbsoluteLeft(map.getContainer()) - gmapTopLeftPixel.x + currentMarkerDivPixel.x;
		return {top:markerTop, left:markerLeft};
	};

	GxMarker.prototype.hideTooltip = function() {
		if ( this.tooltipElement ) {
			this.tooltipElement.style.display = "none";
		};
	};

	function makeInterface(a) {
		var b = a || window;
		b.GxMarker = GxMarker;
	};

	makeInterface();
};

GxMarkerNamespace();

var GMap = {
	the_map: null,
	the_bounds: null,
	hasFixPointsOutOffRange: false,

	map: function() {
	    if (this.the_map == null) this.the_map = new GMap2(document.getElementById("map"));
		return this.the_map;
	},

	bounds: function() {
		if (this.the_bounds == null) this.the_bounds = new GLatLngBounds();
		return this.the_bounds;
	},
  
	init: function(mapType, locLat, locLng, locZoom, locMapType) {
		switch (mapType) {
			case 'cityOverView':
				this.map().addControl(new GLargeMapControl ());
				this.map().addControl(new GScaleControl ());
				this.map().addControl(new GMapTypeControl ());
				break;
			case 'citySmall':
				this.map().addControl(new GSmallMapControl());
				break;
			default:
				this.map().addControl(new GLargeMapControl());
				this.map().addControl(new GMapTypeControl());
		};
		if (!locMapType) { locMapType = G_DEFAULT_MAP_TYPES; };
		this.setLocMapCenter(locLat, locLng, locZoom, locMapType, locMapType);
	},

	setLocMapCenter: function(locLat, locLng, locZoom, locMapType, locMapType) {
		if (locLat && locLng && locZoom) {
			var centerPoint = new GLatLng(locLat, locLng)
			this.map().setCenter(centerPoint, locZoom, locMapType);
			this.bounds().extend(centerPoint);
		} else {
			var centerPoint = new GLatLng(0, 0);
			this.map().setCenter(centerPoint);
		};
	},
  
	addPoI: function(mapPointIndex, markerPoint, markerToolTip, markerLink, markerIcon, markerIconsWidth, markerIconsHeight, markerType, markerIsPermanent, markerVisible) {
		var PoI_Marker = this.createMarker(markerPoint, markerToolTip, markerLink, markerIcon, markerIconsWidth, markerIconsHeight);
		this.map().addOverlay(PoI_Marker);
		if (markerIsPermanent) {
			if (!this.map().getBounds().contains(markerPoint)) {this.hasFixPointsOutOffRange = true;};
			this.bounds().extend(markerPoint);
		};
		if (!markerVisible) { PoI_Marker.hide(); };
		return PoI_Marker;
	},
  
	createMarker: function(markerPoint, markerToolTip, markerLink, markerIcon, markerIconsWidth, markerIconsHeight) {
		var icon = new GIcon();
		icon.image = markerIcon;
		//icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon.iconSize = new GSize(markerIconsWidth, markerIconsHeight);
		//icon.shadowSize = new GSize(37, 34);
		icon.iconAnchor = new GPoint(9, 34);
		icon.infoWindowAnchor = new GPoint(9, 2);
		//icon.infoShadowAnchor = new GPoint(18, 25);
		var marker = new GxMarker(markerPoint, icon, markerToolTip);
		if (markerLink) {
			GEvent.addListener(marker, "click", function() {
				window.location = markerLink;
			});
		};
		return marker;
	},

	setCenter: function(latitude, longitude, zoom) {
 		this.map().setCenter(new GLatLng(latitude,longitude),zoom);
	},
  
	setCenterAndZoom: function(zoom, reCenter) {
		var bounds = this.bounds();
		if (reCenter || this.hasFixPointsOutOffRange) {
			var center = bounds.getCenter();
			this.map().setCenter(center);
		};
		var zoom = Math.min(this.map().getBoundsZoomLevel(this.bounds()), zoom);
		this.map().setZoom(zoom);
	},

	setBounds: function(locBounds) {
		this.the_bounds = new GLatLngBounds(locBounds.getSouthWest(),locBounds.getNorthEast());
	},
	
	clear: function() {
		this.map().clearOverlays();
		this.the_bounds = null;
	}
};
