//overlayMessage.js

OverlayMessage = function ( container )
    {
    var parent = container.parentNode;
    var wrapper = document.createElement( 'div' );
    wrapper.style.cssText = container.style.cssText;
    parent.insertBefore( wrapper, container );
    parent.removeChild( container );
    wrapper.appendChild( container );
    container.style.cssText = 'position: relative; width: 100%; height: 100%;';
    this.overlay = document.createElement( 'div' );
    wrapper.appendChild( this.overlay );
    this.visibleStyle = 'background-color:white;border:2px solid black;position: relative; top: -55%; width: 40%; text-align: center; margin-left: auto; margin-right: auto; padding: 2em; z-index: 100; opacity: .75; filter: alpha(opacity=75);';
    this.invisibleStyle = 'display: none;';
    this.overlay.style.cssText = this.invisibleStyle;
    };

OverlayMessage.backgroundColor = '#9999cc';
OverlayMessage.borderColor = '#666699';

OverlayMessage.prototype.Set = function ( message )
    {
    this.overlay.innerHTML = message;
    this.overlay.style.cssText = this.visibleStyle;
    };

OverlayMessage.prototype.Clear = function ()
    {
    this.overlay.style.cssText = this.invisibleStyle;
    };

OverlayMessage.SetBackgroundColor = function ( color )
    {
    OverlayMessage.backgroundColor = color;
    };

OverlayMessage.SetBorderColor = function ( color )
    {
    OverlayMessage.borderColor = color;
    };


//Emap

function EGMap(lang, city_id) {
	this.lang = lang;
	this.city_id = city_id;
}

EGMap._instance = null;
EGMap.prototype.lat = 0;
EGMap.prototype.lng = 0;
EGMap.prototype.zoom = 0;
EGMap.prototype.lbl_loading = "";
EGMap.prototype.lang = "en";
EGMap.prototype.city_id = 0;
EGMap.prototype.lnk_poi = 1; // if to link poi's
EGMap.prototype.map_type = G_NORMAL_MAP;
EGMap.prototype.onreceivexml = null;
EGMap.prototype.show_active_estab = true;
EGMap.prototype.show_inactive_estab = true;
EGMap.prototype.url_layout = 1; // website layout
EGMap.prototype.pk_order = 0; // format for a custom estab ur
EGMap.prototype.url_format = ''; // url format that should be sent to the
									// mapFunctions.php. This is used by the
									// offer list.

EGMap.prototype.init = function() {
	if (GBrowserIsCompatible() == false) {
		return;
	}
	// keep track of it self
	EGMap._instance = this;

	// WAIT SCREEN
	om = new OverlayMessage(document.getElementById('map'));
	om.Set(this.lbl_loading);
	info_windows = new Array();
	marker_arr = new Array();
	pk_hotel_arr = new Array(); // keeps track of where
	poi_arr = new Array();

	// ---------------------------------
	// LOAD MAP
	// ---------------------------------
	EGMap.map = new GMap2(document.getElementById("map"));
	EGMap.map.setCenter(new GLatLng(this.lat, this.lng), this.zoom,
			this.map_type);
	EGMap.map.addControl(new GLargeMapControl(), new GControlPosition(
			G_ANCHOR_TOP_LEFT, new GSize(5, 5)));
	EGMap.map.addControl(new GMapTypeControl());

	ewindow = new EWindow(EGMap.map, E_STYLE_1);
	EGMap.map.addOverlay(ewindow);

	// Close the EWindow if theres a map click
	GEvent.addListener(EGMap.map, "click", function(marker, point) {
		if (point) {
			ewindow.hide();
		}
	});

	GMarker.prototype.pk = 0;
	GMarker.prototype.type = '';

	map_tooltip = document.createElement("div");
	map_tooltip.className = "map-tooltip";
	EGMap.map.getPane(G_MAP_FLOAT_PANE).appendChild(map_tooltip);
	map_tooltip.style.visibility = "hidden";

	// ---------------------------------
	// INIT ICONS
	// ---------------------------------
	red_icon = new GIcon();
	red_icon.image = "http://www.engrande.com/images/map/marker-red.png";
	red_icon.iconSize = new GSize(20, 34);
	red_icon.iconAnchor = new GPoint(6, 20);
	red_icon.infoWindowAnchor = new GPoint(4, 11);

	gray_icon = new GIcon();
	gray_icon.image = "http://www.engrande.com/images/map/marker-grey.png";
	gray_icon.iconSize = new GSize(20, 34);
	gray_icon.iconAnchor = new GPoint(6, 20);
	gray_icon.infoWindowAnchor = new GPoint(4, 11);

	blue_icon = new GIcon();
	blue_icon.image = "http://www.engrande.com/images/map/marker-blue.png";
	blue_icon.iconSize = new GSize(20, 34);
	blue_icon.iconAnchor = new GPoint(6, 20);
	blue_icon.infoWindowAnchor = new GPoint(5, 11);

	green_icon = new GIcon();
	green_icon.image = "http://www.engrande.com/images/map/marker-green.png";
	green_icon.iconSize = new GSize(20, 34);
	green_icon.iconAnchor = new GPoint(6, 20);
	green_icon.infoWindowAnchor = new GPoint(5, 11);

	green_tick_icon = new GIcon();
	green_tick_icon.image = "http://www.engrande.com/images/map/marker-green-tick.png";
	green_tick_icon.iconSize = new GSize(20, 34);
	green_tick_icon.iconAnchor = new GPoint(6, 20);
	green_tick_icon.infoWindowAnchor = new GPoint(5, 11);

	restaurants_icon = new GIcon();
	restaurants_icon.image = "http://www.barcelona30.com/images-generic/icon-guide-resto.gif";
	restaurants_icon.iconSize = new GSize(20, 34);
	restaurants_icon.iconAnchor = new GPoint(6, 20);
	restaurants_icon.infoWindowAnchor = new GPoint(5, 11);

	bars_icon = new GIcon();
	bars_icon.image = "http://www.barcelona30.com/images-generic/icon-guide-bar.gif";
	bars_icon.iconSize = new GSize(20, 34);
	bars_icon.iconAnchor = new GPoint(6, 20);
	bars_icon.infoWindowAnchor = new GPoint(5, 11);

	clubs_icon = new GIcon();
	clubs_icon.image = "http://www.barcelona30.com/images-generic/icon-guide-club.gif";
	clubs_icon.iconSize = new GSize(20, 34);
	clubs_icon.iconAnchor = new GPoint(6, 20);
	clubs_icon.infoWindowAnchor = new GPoint(5, 11);

}

EGMap.prototype.checkResize = function() {
	EGMap.map.checkResize();
}

/*
 * EGMap.prototype.load()
 *
 * load markers on map.
 *
 * pk_web: website to load poi's of interest for show_poi: if to show point's of
 * interest is_linked: if there should be a link to the establishment
 * active_estab: comma separated string with estabs with red marker to load
 * (pk's of hotels) inactive_estab: comma separated string with estabs with gray
 * marker to load (pk's of hotels) selected_marker_pk: pk of the marker that
 * should be selected (OPTIONAL) selected_marker_type: the type of the marker to
 * select, poi or estab (OPTIONAL)
 */
EGMap.prototype.load = function(show_poi, lnk_estab, active_estab,
		inactive_estab, selected_marker_pk, selected_marker_type) {
	var param = new Object();
	var url = "/ajax/mapFunctions.php";

	param.pk_web = this.pk_web;
	if(this.city_id) {
		param.city_id = this.city_id;
	}
	param.show_poi = show_poi;
	param.lang = this.lang;
	param.lnk_estab = lnk_estab;
	param_lnk_poi = this.lnk_poi;
	param.url_layout = this.url_layout;

	if (active_estab != "" && this.show_active_estab ) {
		param.active_estab = active_estab;
	}
	if (inactive_estab != "" && this.show_inactive_estab) {
		param.inactive_estab = inactive_estab;
	}

	if (this.pk_order != 0) {
		param.pk_order = this.pk_order;
	}
	if (this.url_format != '') {
		param.url_format = this.url_format;
	}
	if (this.match_estab != '') {
		param.match_estab = this.match_estab;
	}

	// prepare variables that can be used in GDownloadUrl
	var custom_estab_url = this.custom_estab_url;

	// GET MARKERS
	EGMap.selected_marker_pk = selected_marker_pk;
	EGMap.selected_marker_type = selected_marker_type;

	// GDownloadUrl(url, this.receiveXML);
	$.post(url, param, this.receiveXML, 'xml');
}

/*
 * loads the ecg map
 */

/*
 * EGMap.prototype.loadECG = function(pk_ecg){ url =
 * "/ajax/mapFunctions.php?pk_ecg=" + pk_ecg + "&lang=" + this.lang;
 */

EGMap.prototype.loadECG = function(pk_ecg, ecg_type) {
	var url = "/ajax/mapFunctions.php";
	var param = new Object();
	param.pk_ecg = pk_ecg;
	param.lang = this.lang;
	param.ecg_type = ecg_type;
	EGMap.selected_marker_pk = pk_ecg;
	EGMap.selected_marker_type = 'ecg';
	$.post(url, param, this.receiveXML);
}


EGMap.prototype.loadEvent = function(event_id, event_type, active_estab, city_id) {
  var url = "/ajax/mapFunctions.php";
  var inactive_estab = '';
  var param = new Object();
  param.event_id = event_id;
  param.lang = this.lang;
  param.city_id = city_id;
  param.active_estab = active_estab;
  param.inactive_estab = inactive_estab;
  param.event_type = event_type;
  EGMap.selected_marker_pk = event_id;
  EGMap.selected_marker_type = 'event';
  $.post(url, param, this.receiveXML);
}

var marker_index = 0;

EGMap.prototype.receiveXML = function(doc) {
	EGMap.mgr = new GMarkerManager(EGMap.map);
	$("marker",doc).each(function(){

		// READ XML DATA
		var lat = parseFloat($(this).attr("lat"));
		var lng = parseFloat($(this).attr("lng"));
		var icon = $(this).attr("icon");
		var type = $(this).attr("type"); // type of marker, poi,
													// estab
		var pk = parseInt($(this).attr("pk"));
		// var info_html = GXml.value(markers[i].getElementsByTagName("info")[0]);
		var info_html = $('info:first-child',this).text();

		info_windows.push(info_html);
		var marker_event = 'click';

		// CREATE MAP MARKER
		var marker = null;
		switch (icon) {
			case 'red':
				marker = new GMarker(new GLatLng(lat, lng), red_icon);
				break;

			case 'green':
				marker = new GMarker(new GLatLng(lat, lng), green_icon);
				break;

			case 'green-tick':
				marker = new GMarker(new GLatLng(lat, lng), green_tick_icon);
				break;

			case 'gray':
				marker = new GMarker(new GLatLng(lat, lng), gray_icon);
				marker_event = 'mouseover';
				break;

			case 'blue':
				marker = new GMarker(new GLatLng(lat, lng), blue_icon);
				break;

			case 'restaurants_icon':
				marker = new GMarker(new GLatLng(lat, lng), restaurants_icon);
				break;

			case 'bars_icon':
				marker = new GMarker(new GLatLng(lat, lng), bars_icon);
				break;

			case 'clubs_icon':
				marker = new GMarker(new GLatLng(lat, lng), clubs_icon);
				break;
		}

		// EVENT HANDLING OF MARKER
		if (marker != null) {
			marker.index = marker_index;
			marker.type = type;
			marker.pk = pk;
			EGMap.mgr.addMarker(marker, 1);
			marker_arr.push(marker);

			if (marker_event == 'click') {
				GEvent.addListener(marker, "click", function() {
					EGMap.clickOnMarker(this);
				});
			} else if (marker_event == 'mouseover') {
				GEvent.addListener(marker, "mouseover",
						function() {
							EGMap._instance.showTooltip(this,
									info_windows[this.index]);
							// ewindow.openOnMarker(this,
							// info_windows[this.index] );
					});

				if (marker_event == 'mouseover') {
					GEvent.addListener(marker, "mouseout", function() {
						map_tooltip.style.visibility = "hidden";
					});
				}
			}

			// IS THIS THE SELECTED MARKER?
			if (EGMap.selected_marker_pk == marker.pk
					&& EGMap.selected_marker_type == marker.type) {
				EGMap.clickOnMarker(marker);
			}
		}
		marker_index++;
	});
	EGMap.mgr.refresh();
	if( EGMap._instance.onreceivexml != null ){
		EGMap._instance.onreceivexml();
	}

	om.Clear();
}


EGMap.prototype.showMarker = function(pk, type) {
	for (i = 0; i < marker_arr.length; i++) {
		marker = marker_arr[i];
		if (marker.pk > 0 && marker.pk == pk && marker.type == type) {
			EGMap.clickOnMarker(marker);
		}
	}
}

EGMap.prototype.showTooltip = function(marker, html) {
	map_tooltip.innerHTML = html;
	var point = EGMap.map.getCurrentMapType().getProjection()
			.fromLatLngToPixel(
					EGMap.map.fromDivPixelToLatLng(new GPoint(0, 0), true),
					EGMap.map.getZoom());
	var offset = EGMap.map.getCurrentMapType().getProjection()
			.fromLatLngToPixel(marker.getPoint(), EGMap.map.getZoom());
	var anchor = marker.getIcon().iconAnchor;
	var width = marker.getIcon().iconSize.width;
	var height = map_tooltip.clientHeight;
	var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x
			- point.x - anchor.x + width, offset.y - point.y - anchor.y
			- height));
	pos.apply(map_tooltip);
	map_tooltip.style.visibility = "visible";
}

EGMap.clickOnMarker = function(marker) {
	ewindow.openOnMarker(marker, info_windows[marker.index]);
	EGMap.map.panTo(marker.getPoint());
}

//ewindows.js

function EStyle(stemImage, stemSize, boxClass, boxOffset) {
	this.stemImage = stemImage;
	this.stemSize = stemSize;
	this.boxClass = boxClass;
	this.boxOffset = boxOffset;
	//this.border = border;

	// Known fudge factors are:
	// Firefox (1.0.6 and 1.5)    5, -1
	// IE 6.0                     0, -1
	// Opera 8.54                 3, -1
	// Opera 9 prev               4, -1
	// Netscape (7.2, 8.0)        5, -1
	// Safari                     5, -1

	var agent = navigator.userAgent.toLowerCase();
var fudge = 5;  // assume Netscape if no match found
if (agent.indexOf("opera") > -1) {
 fudge = 3;
}
if (agent.indexOf("firefox") > -1) {
 fudge = 0;
}
if (agent.indexOf("safari") > -1) {
 fudge = 5;
}
if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){
 fudge = 0;
}
this.fudge = fudge;
}

var E_STYLE_1 = new EStyle("http://www.engrande.com/photos/map_poi/Stem1.png", new GSize(33,35), "estyle1", new GPoint(-120,37-3));
var E_STYLE_2 = new EStyle("http://www.engrande.com/photos/map_poi/Stem2.png", new GSize(81,87),  "estyle2", new GPoint(-30,87-1));
var E_STYLE_3 = new EStyle("http://www.engrande.com/photos/map_poi/Stem3.png", new GSize(81,87),  "estyle3", new GPoint(-30,87-10));
var E_STYLE_4 = new EStyle("http://www.engrande.com/photos/map_poi/Stem4.png", new GSize(81,87),  "estyle4", new GPoint(-30,87-10));
var E_STYLE_5 = new EStyle("http://www.engrande.com/photos/map_poi/Stem5.png", new GSize(81,87),  "estyle5", new GPoint(-30,87-3));
var E_STYLE_6 = new EStyle("http://www.engrande.com/photos/map_poi/Stem6.png", new GSize(100,50), "estyle6", new GPoint(100-2,20));
var E_STYLE_7 = new EStyle("http://www.engrande.com/photos/map_poi/Stem7.png", new GSize(24,24),  "estyle2", new GPoint(-10,23));


function EWindow(map,estyle) {
	// parameters
	this.map=map;
	this.estyle=estyle;
	// internal variables
	this.visible = false;
	// browser - specific variables
	this.ie = false;
	var agent = navigator.userAgent.toLowerCase();
	if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){ this.ie = true} else {this.ie = false}
}

EWindow.prototype = new GOverlay();

EWindow.prototype.initialize = function(map) {
	var div1 = document.createElement("div");
	div1.style.position = "absolute";
	map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div1);
	var div2 = document.createElement("div");
	div2.style.position = "absolute";
	div2.style.width = this.estyle.stemSize.width+"px";
	map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div2);
	this.div1 = div1;
	this.div2 = div2;
}

EWindow.prototype.openOnMap = function(point, html, offset) {
	this.offset = offset||new GPoint(0,0);
	this.point = point;
	this.div1.innerHTML = '<div class="' + this.estyle.boxClass + '"><nobr>' + html + '</nobr></div>';
	if (this.ie && this.estyle.stemImage.toLowerCase().indexOf(".png")>-1) {
		var loader = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.estyle.stemImage+"', sizingMethod='scale');";
		this.div2.innerHTML = '<div style="height:' +this.estyle.stemSize.height+ 'px; width:'+this.estyle.stemSize.width+'px; ' +loader+ '" ></div>';
	} else {
		this.div2.innerHTML = '<img src="' + this.estyle.stemImage + '" width="' + this.estyle.stemSize.width +'" height="' + this.estyle.stemSize.height +'">';
	}
	var z = GOverlay.getZIndex(this.point.lat());
	this.div1.style.zIndex = z;
	this.div2.style.zIndex = z+1;
	this.visible = true;
	this.show();
	this.redraw(true);
}

EWindow.prototype.openOnMarker = function(marker,html) {
	var vx = marker.getIcon().iconAnchor.x - marker.getIcon().infoWindowAnchor.x;
	var vy = marker.getIcon().iconAnchor.y - marker.getIcon().infoWindowAnchor.y;
	this.openOnMap(marker.getPoint(), html, new GPoint(vx,vy));
}


EWindow.prototype.redraw = function(force) {
	if (!this.visible) {return;}
	var p = this.map.fromLatLngToDivPixel(this.point);
	this.div2.style.left   = (p.x + this.offset.x) + "px";
	this.div2.style.bottom = (-p.y + this.offset.y -this.estyle.fudge) + "px";
	this.div1.style.left   = (p.x + this.offset.x + this.estyle.boxOffset.x) + "px";
	this.div1.style.bottom = (-p.y + this.offset.y + this.estyle.boxOffset.y) + "px";
}

EWindow.prototype.remove = function() {
	this.div1.parentNode.removeChild(this.div1);
	this.div2.parentNode.removeChild(this.div2);
	this.visible = false;
}

EWindow.prototype.copy = function() {
	return new EWindow(this.map, this.estyle);
}

EWindow.prototype.show = function() {
	this.div1.style.display="";
	this.div2.style.display="";
	this.visible = true;
}

EWindow.prototype.hide = function() {
	this.div1.style.display="none";
	this.div2.style.display="none";
	this.visible = false;
}

EWindow.prototype.isHidden = function() {
	return !this.visible;
}

EWindow.prototype.supportsHide = function() {
	return true;
}

EWindow.prototype.zindex = function(zin) {
	var z = GOverlay.getZIndex(this.point.lat());
	this.div1.style.zIndex = z+zin;
	this.div2.style.zIndex = z+1+zin;
}

//showOnMap.js
function centerAndOpenMessage(pkAcc, type) {
    	window.scrollTo(0,document.getElementById('map').offsetTop-40);
    	if ($("#map_area").is(':hidden')) {
    		if (oMap) { // map was loaded but it's currently hidden
    			showhide();
    			setTimeout("oMap.showMarker("+pkAcc+", '"+type+"')", 2000);
    		}
    		else { // map was not loaded
    			showhide();
    			$("#showhidecmd").click();
    			setTimeout("oMap.showMarker("+pkAcc+", '"+type+"')", 2000);
    		}
    	}
    	else {
    		oMap.showMarker(pkAcc, type);
    	}
    }

function checkMap(pkAcc, type){
	  if (!oMap){
			 map_load();
			 window.onload = map_load;
			 window.onunload=GUnload;
	  }
	  var hide_map;
	  hide_map = $("#map_area").is(":visible");

	  if(!hide_map){
			  $("#map_area").slideDown('slow', function(){
			    $("#map").show();
			  });
			  setTimeout("oMap.checkResize()", 2000);
		  }
	  window.scrollTo(0,document.getElementById('map').offsetTop-40);
	  setTimeout("oMap.showMarker("+pkAcc+", '"+type+"')", 2000);
}

function showlegend(){
	hide_map = $("#map_area").is(":visible");
	  if(hide_map){
		  	$("#map_legend").show();
		  }
}
