//ELABEL
// ELabel.js 
//
//   This Javascript is provided by Mike Williams
//   Community Church Javascript Team
//   http://www.bisphamchurch.org.uk/   
//   http://econym.org.uk/gmap/
//
//   This work is licenced under a Creative Commons Licence
//   http://creativecommons.org/licenses/by/2.0/uk/
//
// Version 0.2      the .copy() parameters were wrong
// version 1.0      added .show() .hide() .setContents() .setPoint() .setOpacity() .overlap
// version 1.1      Works with GMarkerManager in v2.67, v2.68, v2.69, v2.70 and v2.71
// version 1.2      Works with GMarkerManager in v2.72, v2.73, v2.74 and v2.75
// version 1.3      add .isHidden()
// version 1.4      permit .hide and .show to be used before addOverlay()
// version 1.5      fix positioning bug while label is hidden
// version 1.6      added .supportsHide()
// version 1.7      fix .supportsHide()
// version 1.8      remove the old GMarkerManager support due to clashes with v2.143

function ELabel(point, html, classname, pixelOffset, percentOpacity, overlap) {
// Mandatory parameters
this.point = point;
this.html = html;

// Optional parameters
this.classname = classname||"";
this.pixelOffset = pixelOffset||new GSize(0,0);
if (percentOpacity) {
  if(percentOpacity<0){percentOpacity=0;}
  if(percentOpacity>100){percentOpacity=100;}
}        
this.percentOpacity = percentOpacity;
this.overlap=overlap||false;
this.hidden = false;
} 

ELabel.prototype = new GOverlay();

ELabel.prototype.initialize = function(map) {
var div = document.createElement("div");
div.style.position = "absolute";
div.innerHTML = '<div class="' + this.classname + '">' + this.html + '</div>' ;
map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div);
this.map_ = map;
this.div_ = div;
if (this.percentOpacity) {        
  if(typeof(div.style.filter)=='string'){div.style.filter='alpha(opacity:'+this.percentOpacity+')';}
  if(typeof(div.style.KHTMLOpacity)=='string'){div.style.KHTMLOpacity=this.percentOpacity/100;}
  if(typeof(div.style.MozOpacity)=='string'){div.style.MozOpacity=this.percentOpacity/100;}
  if(typeof(div.style.opacity)=='string'){div.style.opacity=this.percentOpacity/100;}
}
if (this.overlap) {
  var z = GOverlay.getZIndex(this.point.lat());
  this.div_.style.zIndex = z;
}
if (this.hidden) {
  this.hide();
}
}

ELabel.prototype.remove = function() {
this.div_.parentNode.removeChild(this.div_);
}

ELabel.prototype.copy = function() {
return new ELabel(this.point, this.html, this.classname, this.pixelOffset, this.percentOpacity, this.overlap);
}

ELabel.prototype.redraw = function(force) {
var p = this.map_.fromLatLngToDivPixel(this.point);
var h = parseInt(this.div_.clientHeight);
this.div_.style.left = (p.x + this.pixelOffset.width) + "px";
this.div_.style.top = (p.y +this.pixelOffset.height - h) + "px";
}

ELabel.prototype.show = function() {
if (this.div_) {
  this.div_.style.display="";
  this.redraw();
}
this.hidden = false;
}

ELabel.prototype.hide = function() {
if (this.div_) {
  this.div_.style.display="none";
}
this.hidden = true;
}

ELabel.prototype.isHidden = function() {
return this.hidden;
}

ELabel.prototype.supportsHide = function() {
return true;
}

ELabel.prototype.setContents = function(html) {
this.html = html;
this.div_.innerHTML = '<div class="' + this.classname + '">' + this.html + '</div>' ;
this.redraw(true);
}

ELabel.prototype.setPoint = function(point) {
this.point = point;
if (this.overlap) {
  var z = GOverlay.getZIndex(this.point.lat());
  this.div_.style.zIndex = z;
}
this.redraw(true);
}

ELabel.prototype.setOpacity = function(percentOpacity) {
if (percentOpacity) {
  if(percentOpacity<0){percentOpacity=0;}
  if(percentOpacity>100){percentOpacity=100;}
}        
this.percentOpacity = percentOpacity;
if (this.percentOpacity) {        
  if(typeof(this.div_.style.filter)=='string'){this.div_.style.filter='alpha(opacity:'+this.percentOpacity+')';}
  if(typeof(this.div_.style.KHTMLOpacity)=='string'){this.div_.style.KHTMLOpacity=this.percentOpacity/100;}
  if(typeof(this.div_.style.MozOpacity)=='string'){this.div_.style.MozOpacity=this.percentOpacity/100;}
  if(typeof(this.div_.style.opacity)=='string'){this.div_.style.opacity=this.percentOpacity/100;}
}
}

ELabel.prototype.getPoint = function() {
return this.point;
}

// JavaScript Document
var map, myPano;
var directionsPanel;
var directions;
var DIR = "http://www.hoteis.pt/";
/*
var icon0 = new GIcon(); 
    icon0.image 				= DIR + 'images/gmaps/hotel0star.png';
    icon0.shadow				= DIR + 'images/gmaps/shadow.png';
    icon0.iconSize 				= new GSize(32, 37);
    icon0.shadowSize 			= new GSize(61, 31);
    icon0.iconAnchor 			= new GPoint(6, 37);
    icon0.infoWindowAnchor 	= new GPoint(5, 1);
	
var icon1 = new GIcon(); 
    icon1.image 				= DIR + 'images/gmaps/hotel1star.png';
    icon1.shadow				= DIR + 'images/gmaps/shadow.png';
    icon1.iconSize 				= new GSize(32, 37);
    icon1.shadowSize 			= new GSize(61, 31);
    icon1.iconAnchor 			= new GPoint(6, 37);
    icon1.infoWindowAnchor 		= new GPoint(5, 1);
	
var icon2 = new GIcon(); 
    icon2.image 				= DIR + 'images/gmaps/hotel2stars.png';
    icon2.shadow				= DIR + 'images/gmaps/shadow.png';
    icon2.iconSize 				= new GSize(32, 37);
    icon2.shadowSize 			= new GSize(61, 31);
    icon2.iconAnchor 			= new GPoint(6, 37);
    icon2.infoWindowAnchor 		= new GPoint(5, 1);
	
var icon3 = new GIcon(); 
    icon3.image 				= DIR + 'images/gmaps/hotel3stars.png';
    icon3.shadow				= DIR + 'images/gmaps/shadow.png';
    icon3.iconSize 				= new GSize(32, 37);
    icon3.shadowSize 			= new GSize(73, 48);
    icon3.iconAnchor 			= new GPoint(6, 37);
    icon3.infoWindowAnchor 		= new GPoint(5, 1);
	
var icon4 = new GIcon(); 
    icon4.image 				= DIR + 'images/gmaps/hotel4stars.png';
    icon4.shadow				= DIR + 'images/gmaps/shadow.png';
    icon4.iconSize 				= new GSize(32, 37);
    icon4.shadowSize 			= new GSize(61, 31);
    icon4.iconAnchor 			= new GPoint(6, 37);
    icon4.infoWindowAnchor 		= new GPoint(5, 1);
	
var icon5 = new GIcon(); 
    icon5.image 				= DIR + 'images/gmaps/hotel5stars.png';
    icon5.shadow				= DIR + 'images/gmaps/shadow.png';
    icon5.iconSize 			= new GSize(32, 37);
    icon5.shadowSize 			= new GSize(61, 31);
    icon5.iconAnchor 			= new GPoint(6, 37);
    icon5.infoWindowAnchor 	= new GPoint(5, 1);
*/

var icon0 = new GIcon(); 
    icon0.image 				= DIR + 'images/gmaps/bulletDistrict.png';
   // icon0.shadow				= DIR + 'images/gmaps/shadow.png';
    icon0.iconSize 				= new GSize(32, 32);
    icon0.shadowSize 			= new GSize(61, 31);
    icon0.iconAnchor 			= new GPoint(6, 37);
    icon0.infoWindowAnchor 	= new GPoint(5, 1);
	
var icon1 = new GIcon(); 
    icon1.image 				= DIR + 'images/gmaps/bulletDistrict.png';
    //icon1.shadow				= DIR + 'images/gmaps/shadow.png';
    icon1.iconSize 				= new GSize(32, 32);
    icon1.shadowSize 			= new GSize(61, 31);
    icon1.iconAnchor 			= new GPoint(6, 37);
    icon1.infoWindowAnchor 		= new GPoint(5, 1);
	
var icon2 = new GIcon(); 
    icon2.image 				= DIR + 'images/gmaps/bulletDistrict.png';
   // icon2.shadow				= DIR + 'images/gmaps/shadow.png';
    icon2.iconSize 				= new GSize(32, 32);
    icon2.shadowSize 			= new GSize(61, 31);
    icon2.iconAnchor 			= new GPoint(6, 37);
    icon2.infoWindowAnchor 		= new GPoint(5, 1);
	
var icon3 = new GIcon(); 
    icon3.image 				= DIR + 'images/gmaps/bulletDistrict.png';
  //  icon3.shadow				= DIR + 'images/gmaps/shadow.png';
    icon3.iconSize 				= new GSize(32, 32);
    icon3.shadowSize 			= new GSize(73, 48);
    icon3.iconAnchor 			= new GPoint(6, 37);
    icon3.infoWindowAnchor 		= new GPoint(5, 1);
	
var icon4 = new GIcon(); 
    icon4.image 				= DIR + 'images/gmaps/bulletDistrict.png';
   // icon4.shadow				= DIR + 'images/gmaps/shadow.png';
    icon4.iconSize 				= new GSize(32, 32);
    icon4.shadowSize 			= new GSize(61, 31);
    icon4.iconAnchor 			= new GPoint(6, 37);
    icon4.infoWindowAnchor 		= new GPoint(5, 1);
	
var icon5 = new GIcon(); 
    icon5.image 				= DIR + 'images/gmaps/bulletDistrict.png';
  //  icon5.shadow				= DIR + 'images/gmaps/shadow.png';
    icon5.iconSize 				= new GSize(32, 32);
    icon5.shadowSize 			= new GSize(61, 31);
    icon5.iconAnchor 			= new GPoint(6, 37);
    icon5.infoWindowAnchor 		= new GPoint(5, 1);
	
var customIcons = [];
    customIcons["0"] = icon0;
    customIcons["1"] = icon1;
	customIcons["2"] = icon2;
	customIcons["3"] = icon3;
	customIcons["4"] = icon4;
	customIcons["5"] = icon5;


function initialize( LAT, LONG, ZOOM, MULTI, WIDTH, HEIGHT, ID) {

  if (GBrowserIsCompatible()) {
	  
	    if(WIDTH !='' && HEIGHT!='' )
			map = new GMap2(document.getElementById("map_canvas"),{ size: new GSize(WIDTH,HEIGHT)});
		else
			map = new GMap2(document.getElementById("map_canvas"));
		
		map.addMapType(G_PHYSICAL_MAP);
		map.addControl(new GScaleControl());
		map.addControl(new GLargeMapControl3D());	
		map.setCenter(new GLatLng(LAT, LONG), ZOOM);
		map.setMapType( G_HYBRID_MAP );
		
		
		if (MULTI == 1){
			map.addControl(new GMenuMapTypeControl());
			multiMarkers();
			directionsPanel = document.getElementById("route");
			directions = new GDirections(map, directionsPanel);

		}else{
			try{
				map.addControl(new GMapTypeControl());
				monoMarker(ID);
				directionsPanel = document.getElementById("innerRoute");
				directions = new GDirections(map, directionsPanel);
				directionsPanel.style.height = (document.getElementById('map_canvas').clientHeight - 185);
			}catch(e){}
		}
  }

}

function monoMarker(ID) {

	GDownloadUrl(DIR + "template/componentes/maps/monoMarker.php?id="+ID, function(data) {
	  var xml = GXml.parse(data);
	  var markers = xml.documentElement.getElementsByTagName("marker");
	  
	  for (var i = 0; i < markers.length; i++) {
		  var xmlCAT 	= markers[i].getAttribute("type");
		  var point 	= new GLatLng(parseFloat(markers[i].getAttribute("lat")),
								      parseFloat(markers[i].getAttribute("lng")));
		  
		  var marker 	= monoCreateMarker(point, 
									   xmlCAT, 
									   markers[i].getAttribute("id"),
									   markers[i].getAttribute("nome"),
									   markers[i].getAttribute("G1"),
									   markers[i].getAttribute("G2"),
									   markers[i].getAttribute("G3"),
									   markers[i].getAttribute("logo"),
									   parseFloat(markers[i].getAttribute("lat")),
									   parseFloat(markers[i].getAttribute("lng"))
									   );
		  map.addOverlay(marker);	
	  }
	});		
}



function createMarker(point, t, n, id,m,cp,loc,co,d,p,lat,long) {
  var marker = new GMarker(point, customIcons[t]);
  var html = infoToolTipo(t, n, id,m,cp,loc,co,d);
  GEvent.addListener(marker, 'mouseover', function() {
  	marker.openInfoWindowHtml(html,{maxWidth:300});
  }); 
  GEvent.addListener(marker, 'click', function() {
	$j("#to").val(lat+', '+long);
  });  
  
  return marker;
}


function monoCreateMarker(point, t,id , n, g1, g2, g3,logo, LAT, LONG){
  var marker = new GMarker(point, customIcons[t]);
  var html = monoToolTipo(t,id, n, g1, g2, g3,logo, LAT, LONG);
  
  GEvent.addListener(marker, 'mouseover', function() {
  	marker.openInfoWindowHtml(html,{maxWidth:300});
  });  
  return marker;
}

function directionPoint (F,T,L,M){
	document.getElementById("route").style.display='block';
	GEvent.addListener(directions, "error", handleErrors);
	directions.load("from: "+ F +" to: "+ T,{ "locale": L ,"travelMode": M=='C' ? G_TRAVEL_MODE_DRIVING : G_TRAVEL_MODE_WALKING });
}

function directionPointMono (F,T,L,M){
	document.getElementById("innerRoute").style.display='block';
	document.getElementById("printer").style.display='block';
	GEvent.addListener(directions, "error", handleErrors);
	directions.load("from: "+ F +" to: "+ T,{ "locale": L ,"travelMode": M=='C' ? G_TRAVEL_MODE_DRIVING : G_TRAVEL_MODE_WALKING });
}

/********************* FUNCOE AUXILIARES ******************************/
function multiMarkers() {
	GDownloadUrl(DIR + "template/componentes/maps/marker.php", function(data) {
	  var xml = GXml.parse(data);
	  var markers = xml.documentElement.getElementsByTagName("marker");
	  for (var i = 0; i < markers.length; i++) {
		  var xmlCAT 	= markers[i].getAttribute("type");
		  var point 	= new GLatLng(parseFloat(markers[i].getAttribute("lat")),
								  parseFloat(markers[i].getAttribute("lng")));
		  
		  
		  var marker 	= createMarker(point, 
									   xmlCAT, 
									   markers[i].getAttribute("nome"),
									   markers[i].getAttribute("id"),
									   markers[i].getAttribute("morada"),
									   markers[i].getAttribute("cod_postal"),
									   markers[i].getAttribute("localidade"),
									   markers[i].getAttribute("concelho"),
									   markers[i].getAttribute("distrito"),
									   markers[i].getAttribute("pais"),
									   parseFloat(markers[i].getAttribute("lat")),
									   parseFloat(markers[i].getAttribute("lng"))
									   );
		  map.addOverlay(marker);	
		 // var  label  =createLabel( point,markers[i].getAttribute("nome") );
		  //map.addOverlay(label);
	  }
	});		
}


function createLabel(point , m ){
	var label = new ELabel(point, '<div style="background-color:#ccccff;border:1px solid black; padding:2px"><nobr>'+m+'<\/nobr><\/div>', null, new GSize(9,-10), 0);
	return label;
}
function infoToolTipo(t, n, id,m,cp,loc,co,d){
	var Info="<div class='hotelHeader'>";
			Info +="<div id='hotelInfo'>";
				Info+="<table border='0' cellspacing='0' cellpadding='0'><tr>";
					Info+="<td><div class='hotelname'>"+ n +"</div></td>";
					if(t>0){
						Info+="<td>";
						Info+="<div class='hotelStars'>";
							for (var i = 0; i < t; i++) {
								Info+="<li><img src='/images/icons/star_2.png'></li>";
							}
						Info+="</div></td>";
					}
				Info+="</tr></table>";
			Info+="</div>";
		Info+="</div>";
		Info+='<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>';
		Info+='<td class="logotipo" width="100" height="70" align="center" valign="middle">';
			Info+='<div class="zoneLogotipo">';
				Info+="<img src='/cliente/" + id + "/logotipo/logo64.gif' />";
			Info+="</div>";
		Info+="</td>";
		Info+='<td align="left" valign="top"  ><div class="moradaContact">';
			Info+='<div class="morada">'+m +'</div>';
			Info+='<div class="codPosLoc">'+cp +'&nbsp;'+loc +'</div>';
			Info+='<div class="concDis">'+ co+' -'+d +'</div>';
			Info+='<div align="right"><a href="/'+id+'" ><div id="btInfo" >+ info</div></a></div>';
		  Info+="</div></td>";
	  Info+="</tr>";
	Info+="</table>";

return Info;
}

function handleErrors(){
	if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS){
	 	//alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + directions.getStatus().code);
		$j("#from").focus().css({"border":"1px solid #FF0000"});
		$j("#route, #innerRoute").hide();
	
	}else if (directions.getStatus().code == G_GEO_SERVER_ERROR){
	 	//alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + directions.getStatus().code);
		$j("#from").focus().css({"border":"1px solid #FF0000"});
		$j("#route,#innerRoute ").hide();
	}else if (directions.getStatus().code == G_GEO_MISSING_QUERY){
	 	//alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + directions.getStatus().code);
		$j("#from").focus().css({"border":"1px solid #FF0000"});
		$j("#route,#innerRoute ").hide();
	}else if (directions.getStatus().code == G_GEO_BAD_KEY){
	 	//alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + directions.getStatus().code);
		$j("#from").focus().css({"border":"1px solid #FF0000"});
		$j("#route,#innerRoute ").hide();
	}else if (directions.getStatus().code == G_GEO_BAD_REQUEST){
	 	//alert("A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code);
		$j("#from").focus().css({"border":"1px solid #FF0000"});
		$j("#route,#innerRoute ").hide();
	}else{ //alert("An unknown error occurred."); 
	}
}


function handleNoFlash(errorCode) {
  if (errorCode == FLASH_UNAVAILABLE) {
	alert("Error: Flash doesn't appear to be supported by your browser");
	return;
  }
}  

function monoToolTipo(t, id, n,  g1, g2, g3,logo, LAT, LONG){
		var info = "";
		if(logo != ''){
			info+='<div style="margin-bottom:10px;" align="center">';
				info+="<img src='/cliente/" + id + "/logotipo/logo64.gif' />";
			info+="</div>";
		}
		
	  info+="<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
	  info+="<tr><td colspan='3' align='center' style='font-size:14px; font-weight:bolder; padding-bottom:5px;'>"+ n +"</td></tr>";
	  info+="<tr>";
		info+="<th align='right'>&nbsp;</th>";
		info+="<td align='center' id='GPS' onClick='mudaGps(this)' style='cursor:pointer;'>"+
				"<div id='gps3' style='display:inline;'><table border='0' cellspacing='0' cellpadding='0'><tr><td>"+ g3 +"&nbsp;&nbsp;</td> <td><span  class='ui-icon ui-icon-refresh'></span></td></tr></table></div>"+
				"<div id='gps2' style='display:none;'><table border='0' cellspacing='0' cellpadding='0'><tr><td>"+ g2 +"&nbsp;&nbsp;</td> <td><span  class='ui-icon ui-icon-refresh'></span></td></tr></table></div>"+
				"<div id='gps1' style='display:none;'><table border='0' cellspacing='0' cellpadding='0'><tr><td>"+ g1 +"&nbsp;&nbsp;</td> <td><span  class='ui-icon ui-icon-refresh'></span></td></tr></table></span></div>"+
		    "</td>";
	  info+="</tr>";
	info+="</table>";
	
	return info;
}

function mudaGps(obj){
	var obj1 = obj.firstChild;
	var obj2 = obj.firstChild.nextSibling;
	var obj3 = obj.firstChild.nextSibling.nextSibling;
	var fstyle=obj1.style.display;
	obj1.style.display = obj2.style.display;
	obj2.style.display = obj3.style.display;
	obj3.style.display = fstyle;
}

function printMap(div) {
	var a = window.open('','','width=450,height=400');
	a.document.open("text/html");
	a.document.write($j('#'+div).html());		
	a.document.close();
	a.print();
};

