var map = null;
var keyGoogle="ABQIAAAA9CwGdNj4VjBgvIf9xMnDeBRNuEHTnbY63e6hwwvtny2Q83D_txQlfRt7abBhUYmeiuagqsAdZaIErw"; //www.argentinaski.com
//var keyGoogle="ABQIAAAA9CwGdNj4VjBgvIf9xMnDeBRsJdpmwUaUSavpnEn40Pz-VVbCSBRY4MnC5t2xNImGEI4kLrafrkWOOg"; //explore-argentina

/****************************************** FUNCIONES ******************************************/
function doReplaceExp(expresion, reemplazo, valor) {
  	var re = new RegExp(expresion, "g");
	return valor.replace(re, reemplazo);
}

function crearBotonReset(x_, y_, z, l){
	var donde_reset = document.getElementById("btnsT");
	var a_reset = document.createElement("a");
	a_reset.setAttribute('href', 'javascript:recenterMap(' + x_ + ', ' + y_ + ', ' + z + ')');
	a_reset.setAttribute('id', 'btnReset');
	if(l == "esp") a_reset.innerHTML = "Restaurar Mapa";
	else a_reset.innerHTML = "Reset Map";
	donde_reset.appendChild(a_reset);
}

function crearMapa(x_, y_, z, d){
	map = new GMap2(document.getElementById(d));
	map.setCenter(new GLatLng(x_, y_), z);
	map.addControl(new GLargeMapControl3D());
	map.addControl(new GMapTypeControl());
	map.addMapType(G_PHYSICAL_MAP);
	map.setMapType(G_HYBRID_MAP);
	map.disableDoubleClickZoom();
	map.clearOverlays();
}

function crearLoadingDest(d){
	var div_mapa = document.getElementById(d);
	var loading = document.createElement("div");
	loading.setAttribute('id', 'loading');
	div_mapa.appendChild(loading);
}

function crearLoadingHotel(d){
	var div_mapa2 = document.getElementById(d);
	var loading2 = document.createElement("div");
	loading2.setAttribute('id', 'loading_h');
	div_mapa2.appendChild(loading2);
}

function crearLoadingTour(d){
	var div_mapa2 = document.getElementById(d);
	var loading2 = document.createElement("div");
	loading2.setAttribute('id', 'loading_t');
	div_mapa2.appendChild(loading2);
}

function moveendBounds(id_hotel, moveH, id_destino, moveD, id_tour, moveT, lang, id_pais){
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var maxx = northEast.lat()
	var minx = southWest.lat()
	var maxy = northEast.lng()
	var miny = southWest.lng();
	if(moveD) obtener_destinos_bound(minx, miny, maxx, maxy, lang, id_destino, id_pais);
	if(moveH) obtener_hoteles_bound(minx, miny, maxx, maxy, lang, id_hotel, id_pais);
	if(moveT) obtener_tours_bound(minx, miny, maxx, maxy, lang, id_tour, id_pais);
}

function recenterMap(x_, y_, zoom){
	if(x_ != 0){
		map.setMapType(G_HYBRID_MAP);
		map.setCenter(new GLatLng(x_, y_), zoom);
	}
}

function obtenerZoomBound(x1, y1, x2, y2){
	var bound = new GLatLngBounds();
	bound.extend(new GLatLng(x1, y1));
	bound.extend(new GLatLng(x2, y2));
	return map.getBoundsZoomLevel(bound);
}
/*********************************************************************************************/

/****************************************** HOTELES ******************************************/
function mostrar_hoteles(lista_hoteles, donde, id_pais, otros){
	if(otros != false) otros = true;
	var temp = location.pathname.split('/');
	base_url = 'http://' + location.host + '/' + temp[1] + '/' + temp[2]
	var lang = temp[1];
	if (GBrowserIsCompatible()){
		new Request({
			url:'/mapas/obtener_hoteles.php',
			data:'hoteles=' + lista_hoteles + '&id_pais=' + id_pais,
			method: 'get',
			onSuccess: function(responseText){
				var temp = JSON.decode(responseText);
				extend = temp.extend;
				crearMapa(extend.x, extend.y, 15, donde);
				var zoom_level = obtenerZoomBound(extend.xmin, extend.ymin, extend.xmax, extend.ymax);
				if(zoom_level>15) zoom_level = 15
 				crearBotonReset(extend.x, extend.y, zoom_level, lang);
				recenterMap(extend.x, extend.y, zoom_level);

				temp.hoteles.each(function(hotel){
					if(hotel.x != 0) dibujarHotelNumber(hotel.n, hotel.x, hotel.y, hotel.tt, hotel.il, lang);
				});
				crearLoadingDest(donde);
				crearLoadingHotel(donde);
				crearLoadingTour(donde);
				moveendBounds(lista_hoteles, otros, "", true, "", true, lang, id_pais);

				GEvent.bind(map, "moveend", this, function(){
					moveendBounds(lista_hoteles, otros, "", true, "", true, lang, id_pais);
				});
			}
		}).send();
	}else{
		$(donde).setStyle('display','none');
	}
}

function infoMarker(marker, id, lang, principal){
	var txt = (lang == 'esp')?'M&aacute;s Informaci&oacute;n':'More Information';
	new Request({
		url:'/mapas/obtener_info_hotel.php',
		data:'id_hotel=' + id + '&lang=' + lang,
		method: 'get',
		onSuccess: function(responseText){
			//alert(responseText)
			var hotel = JSON.decode(responseText);
			if(hotel){
				link_ = (!principal)?"<a class=\"info_tt\" href=\"" + hotel.r + "\" target=\"_self\">" + txt + "</a>":"";
				if (hotel.imagen == '') marker.openInfoWindowHtml("<div style=\"margin-right:10px;width:350px;\" class=\"clearfix\"><span class=\"nombre_hotel\">" + hotel.n + "</span><br /><hr /><span class=\"direccion_hotel\">" + hotel.d + "</span><br /><img class=\"img_tt\" src=\"/images/vc-hot-" + hotel.e + "star.gif\"/>" + link_ + "</div>");
				else marker.openInfoWindowHtml("<div style=\"margin-right:10px;width:350px;\" class=\"clearfix\"><img class=\"img_hotel\" height=\"100\" align=\"left\" src=\"" + hotel.i + "\" /><span class=\"nombre_hotel\">" + hotel.n + "</span><br /><hr /><span class=\"direccion_hotel\">" + hotel.d + "</span><br /><img class=\"img_tt\" src=\"/images/vc-hot-" + hotel.e + "star.gif\"/>" + link_ + "</div>");
			}
		}
	}).send();
}

function dibujarHotelNumber(numero, x_, y_, tooltip, id, lang){
	x_ = Number(x_);
	y_ = Number(y_);

	var marker = new GMarker(new GLatLng(x_, y_), {icon: getIconNumberHotel(numero), draggable: false, title:tooltip});
	GEvent.addListener(marker, "click", function(){
		infoMarker(marker, id, lang, false);
	});
	map.addOverlay(marker);
}

function getIconNumberHotel(numero) {
	var icon = new GIcon();
	icon.image = "/images/mapas/numbers/" + numero + ".png";
	icon.shadow = "/images/mapas/numbers/s.png";
	icon.iconSize = new GSize(17, 19);
	icon.iconAnchor = new GPoint(0, 19);
	icon.infoWindowAnchor = new GPoint(8, 9);
	return icon;
}

function mostrar_hotel(id_hotel, donde, id_pais){
	var temp = location.pathname.split('/');
	base_url = 'http://' + location.host + '/' + temp[1] + '/' + temp[2]
	var lang = temp[1];
	if (GBrowserIsCompatible()){
		new Request({
			url:'/mapas/obtener_hotel.php',
			data:"hotel=" + id_hotel + "&lang=" + lang + "&id_pais=" + id_pais,
			method:'get',
			onSuccess: function(responseText){
				var hotel = JSON.decode(responseText);
				if(hotel.x != 0){
					crearBotonReset(hotel.y, hotel.x, 15, lang);
					crearMapa(hotel.y, hotel.x, 15, donde);
					dibujarHotelBound(hotel.ig, hotel.y, hotel.x, id_hotel, hotel.t, true, lang);
					crearLoadingDest(donde);
					crearLoadingHotel(donde);
					crearLoadingTour(donde)
					moveendBounds(id_hotel, true, "", true, "", true, lang, id_pais);
					GEvent.bind(map, "moveend", this, function(){
						moveendBounds(id_hotel, true, "", true, "", true, lang, id_pais);
					});
				}
			}
		}).send();
	} else
		$(donde).setStyle('display','none');
}
/****************************************************************************************************/

/*//***************************************** TOURS BOUND *******************************************/
var time2 = null;
var AjaxToursBound = null;
var tours_dibujados = new Array();
var tours_dibujados_id = new Array();

function loadingT(lang){
	$('loading_t').setStyles({visibility:'visible'});
	if(lang == "esp") $('loading_t').innerHTML = "<p style=\"color:#fff;padding:1px 2px;margin:0\"><img src=\"/images/mapas/loading_t.gif\" style=\"border:0\" align=\"absmiddle\" /> Obteniendo Tours/Circuitos</p>";
	else $('loading_t').innerHTML = "<p style=\"color:#fff;padding:1px 2px;margin:0\"><img src=\"/images/mapas/loading_t.gif\" align=\"absmiddle\" style=\"border:0\" /> Retrieving Tours/Circuits</p>";
}

function obtener_tours_bound(xmin, ymin, xmax, ymax, lang, id_tour, id_pais){
	if(AjaxToursBound != null) AjaxToursBound.cancel();
	var ids = tours_dibujados_id.join(",");
	ids = doReplaceExp(",+", ",", ids);
	ids = doReplaceExp("^,", "", ids);
	ids = doReplaceExp(",$", "", ids);
	AjaxToursBound = new Request({
		url:'/mapas/obtener_tours_bound.php',
		data:'xmin=' + xmin + '&ymin=' + ymin + '&xmax=' + xmax + '&ymax=' + ymax + '&not=' + ids + '&id_tour=' + id_tour + '&id_pais=' + id_pais,
		method: 'get',
		encoding:'ISO-8859-1',
		onRequest: function(){
			time2 = setTimeout("loadingT('"+lang+"')",500);
		},
		onSuccess: function(responseText){
			clearTimeout(time2);
			var temp = JSON.decode(responseText);
			var total = temp.tours.length;
			var num = 1;

			temp.tours.each(function(tour){
				if(!tours_dibujados[tour.ig]){
					if(tour.x != 0){
						if(tour.il != "-1"){
							if(lang == "esp"){
								//$('loading_t').innerHTML = "Loading Tours (" + num + "/" + total + ")";
								dibujarDestinoBound(tour.ig, tour.y, tour.x, tour.t, tour.te, tour.il, lang, false, tour.tp, tour.c);
								num++;
							}else{
								//$('loading_t').innerHTML = "Cargando Tours (" + num + "/" + total + ")";
								dibujarDestinoBound(tour.ig, tour.y, tour.x, tour.t, tour.ti, tour.il, lang, false, tour.tp, tour.c);
								num++;
							}
						}
					}
				}
			});
			$('loading_t').setStyles({visibility:'hidden'});
		}
	}).send();
}

/****************************************************************************************************/

/****************************************** DESTINOS BOUND ******************************************/
var timeO = null;
var AjaxDestinosBound = null;
var destinos_dibujados = new Array();
var destinos_dibujados_id = new Array();
var geoms_dibujadas = new Array();
var geoms_borrar = new Array();
var ajaxDestinoBound = new Array();
var destinos_markers = new Array();
var ajaxDestinoBoundPoligono = new Array();
var ajaxDBound = null;

function loadingD(lang){
	$('loading').setStyles({visibility:'visible'});
	if(lang == "esp") $('loading').innerHTML = "<p style=\"color:#fff;padding:1px 2px;margin:0\"><img src=\"/images/mapas/loading.gif\" style=\"border:0\" align=\"absmiddle\" /> Obteniendo Destinos</p>";
	else $('loading').innerHTML = "<p style=\"color:#fff;padding:1px 2px;margin:0\"><img src=\"/images/mapas/loading.gif\" align=\"absmiddle\" style=\"border:0\" /> Retrieving Destinations</p>";
}

function obtener_destinos_bound(xmin, ymin, xmax, ymax, lang, id_destino, id_pais){
	if(AjaxDestinosBound != null) AjaxDestinosBound.cancel();
	var ids = destinos_dibujados_id.join(",");
	ids = doReplaceExp(",+", ",", ids);
	ids = doReplaceExp("^,", "", ids);
	ids = doReplaceExp(",$", "", ids);
	AjaxDestinosBound = new Request({
		url:'/mapas/obtener_destinos_bound.php',
		data:'xmin=' + xmin + '&ymin=' + ymin + '&xmax=' + xmax + '&ymax=' + ymax + '&not=' + ids + '&id_destino=' + id_destino + '&id_pais=' + id_pais,
		method: 'get',
		encoding:'ISO-8859-1',
		onRequest: function(){
			timeO = setTimeout("loadingD('"+lang+"')",500);
		},
		onSuccess: function(responseText){
			clearTimeout(timeO);
			var temp = JSON.decode(responseText);
			var total = temp.destinos.length;
			var num = 1;

			temp.destinos.each(function(destino){
				if(!destinos_dibujados[destino.ig]){
					if(destino.x != 0){
						if(destino.il != "-1"){
							if(lang == "esp"){
								$('loading').innerHTML = "Loading Destinations (" + num + "/" + total + ")";
								dibujarDestinoBound(destino.ig, destino.x, destino.y, destino.t,  destino.te, destino.il, lang, true, "P", 0);
								num++;
							}else{
								$('loading').innerHTML = "Cargando Destinos (" + num + "/" + total + ")";
								dibujarDestinoBound(destino.ig, destino.x, destino.y, destino.t, destino.ti, destino.il, lang, true, "P", 0);
								num++;
							}
						}
					}
				}
			});
			$('loading').setStyles({visibility:'hidden'});
		}
	}).send();
}

function dibujarDestinoBound(id_g, x_, y_, tipo_geom, tooltip, id_lugar, lang, destino_, tipo_prod, circuito){
	geoms_borrar[id_g] = true;
	
	if(!destino_){
		switch(tipo_prod){
			case "C":{
				if(lang == "esp") pre_tt = "Crucero:";
				else pre_tt = "Cruise: ";
				break;
			}
			case "P":{
				switch(circuito){
					case 1:{
						if(lang == "esp") pre_tt = "Crucero: ";
						else  pre_tt = "Cruise: ";
						break;
					}
					case 2:{
						if(lang == "esp") pre_tt = "Circuito: ";
						else  pre_tt = "Circuit: ";
						break;
					}
					case 3:{
						if(lang == "esp") pre_tt = "Tour: ";
						else  pre_tt = "Tour: ";
						break;
					}
					case 4:{
						if(lang == "esp") pre_tt = "Tour: ";
						else  pre_tt = "Tour: ";
						break;
					}
					case 5:{
						if(lang == "esp") pre_tt = "Excursión: ";
						else  pre_tt = "Excursión: ";
						break;
					}
					case 6:{
						if(lang == "esp") pre_tt = "Tracking: ";
						else  pre_tt = "Tracking: ";
						break;
					}
				}
				break;
			}
			default: pre_tt = "Tour: ";
		}
	}else pre_tt = "";
	tooltip = pre_tt + tooltip;
	var marker = new GMarker(new GLatLng(x_, y_), {icon: getIconDestinoBound(destino_, tipo_prod, circuito), draggable: false, title:tooltip});
	if(tipo_geom == 3 || tipo_geom == 2){
		var MarkerMouseOver = GEvent.bind(marker, "mouseover", this, function(){
			if(ajaxDestinoBound[id_g]){
				geoms_dibujadas[id_g].show();
				if(ajaxDestinoBoundPoligono[id_g]){
					if(destino_) geoms_dibujadas[id_g].setFillStyle({"color":"#FF0000", "opacity":0.2});
					else{
						switch(tipo_prod){
							case "P":{
								geoms_dibujadas[id_g].setFillStyle({"color":"#8800FF", "opacity":0.2});
								break;
							}
							case "C":{
								geoms_dibujadas[id_g].setFillStyle({"color":"#83C4DF", "opacity":0.2});
								break;
							}
						}

					}
				}
			}else dibujarPoligonoBound(id_g, tipo_geom, destino_, tipo_prod);
		});
		var MarkerMouseOut = GEvent.bind(marker, "mouseout", this, function(){
			if(geoms_borrar[id_g]){
				if(ajaxDBound != null) ajaxDBound.cancel();
				try{
					geoms_dibujadas[id_g].hide();
				}catch(e){}
			}
		});
		var MarkerInfoClose = GEvent.bind(marker, "infowindowclose", this, function(){
			geoms_borrar[id_g] = true;
			geoms_dibujadas[id_g].hide();
		});
	}
	var MarkerClick = GEvent.bind(marker, "click", this, function(){
		geoms_borrar[id_g] = false;
		if(destino_) link_ = "destino"
		else link_ = "tour"
		new Request({
			url:"/mapas/obtener_info_" + link_ + ".php",
			data:"id_lugar=" + id_lugar + "&lang=" + lang + "&circuito=" + circuito,
			method: 'get',
			onSuccess: function(responseText){
				if(lang == "esp") desc_ruta = "M&aacute;s Informaci&oacute;n"
				else desc_ruta = "More Information"
				var destino = JSON.decode(responseText);
				if(destino_) txtTar = "";
				else txtTar = "<br /><span class=\"tarifa_tt\">" + destino.t + "</span><br /><span class=\"hab_tt\">" + destino.h + "</span>";
				if(link_ == "destino"){
					if (destino.foto == '')
						marker.openInfoWindowHtml("<div style=\"margin-right:10px;width:350px;\"><span class=\"nombre_hotel\">" + destino.nombre + "</span><br><hr><span class=\"direccion_hotel\">" + destino.descripcion + "</span>" + txtTar + "<p align=\"left\" style=\"margin:2px 0 0 0;\" ></p></div>");
					else
						marker.openInfoWindowHtml("<div style=\"margin-right:10px;width:350px;\"><img class=\"img_hotel\" height=\"100\" align=\"left\" src=\"" + destino.foto + "\" /><span class=\"nombre_hotel\">" + destino.nombre + "</span><br><hr><span class=\"direccion_hotel\">" + destino.descripcion + "</span>" + txtTar + "<p style=\"margin:2px 0 0 0;\" align=\"left\"></p></div>");
				}else if(link_ == "tour"){
					if (destino.foto == '')
						marker.openInfoWindowHtml("<div style=\"margin-right:10px;width:350px;\"><span class=\"nombre_hotel\">" + destino.nombre + "</span><br><hr><span class=\"direccion_hotel\">" + destino.descripcion + "</span>" + txtTar + "<p align=\"left\" style=\"margin:2px 0 0 0;\" ><a class=\"info_tt\" style=\"right:90px;\" href=\"" + destino.ruta + "\" target=\"_self\">M&aacute;s informaci&oacute;n</a></p></div>");
					else
						marker.openInfoWindowHtml("<div style=\"margin-right:10px;width:350px;\"><img class=\"img_hotel\" height=\"100\" align=\"left\" src=\"" + destino.foto + "\" /><span class=\"nombre_hotel\">" + destino.nombre + "</span><br><hr><span class=\"direccion_hotel\">" + destino.descripcion + "</span>" + txtTar + "<p style=\"margin:2px 0 0 0;\" align=\"left\"><a class=\"info_tt\" style=\"right:90px;\" href=\"" + destino.ruta + "\" target=\"_self\">M&aacute;s informaci&oacute;n</a></p></div>");
				}
			}
		}).send();
	});
	map.addOverlay(marker);
	if(destino_){
		destinos_dibujados[id_g] = true;
		destinos_markers[id_g] = marker;
		destinos_dibujados_id[id_g] = id_g;
	}else{
		tours_dibujados[id_g] = true;
		tours_dibujados_id[id_g] = id_g;
	}
}

function getIconDestinoBound(destino, tipo_prod, circuito){
	var icon = new GIcon();
	if(destino) icon.image = "/images/mapas/mini_icon_red.png";
	else{
		switch(tipo_prod){
			case "P":{
				switch(circuito){
					case 2:{
						icon.image = "/images/mapas/mini_icon_lblue.png";
						break;
					}
					case 3:{
						icon.image = "/images/mapas/mini_icon_purple.png";
						break;
					}
					default: icon.image = "/images/mapas/mini_icon_purple.png";
				}
				break;
			}
			case "C":{
				icon.image = "/images/mapas/mini_icon_lblue.png";
				break;
			}
		}
	}
	icon.shadow = "/images/mapas/shadow.png";
	icon.iconSize = new GSize(12, 20);
	icon.shadowSize = new GSize(19, 20);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(6, 20);
	return icon;
}

function destinos_hide(){
	destinos_markers.each(function(mar){
		mar.show();							   
	})
}

function destinos_show(){
	destinos_markers.each(function(mar){
		mar.hide();							   
	})
}

function dibujarPoligonoBound(id_g, tipo, destino, tipo_prod){
	ajaxDBound = new Request({
		url:'/mapas/obtener_geom.php',
		data:'id_g=' + id_g,
		method: 'get',
		encoding:'ISO-8859-1',
		onRequest: function(){
		},
		onSuccess: function(responseText){
			var the_geom = this.response.text;
			var puntos = the_geom.split(",");
			var puntos_poligono = new Array();
			var i=0;
			puntos.each(function(punto){
				var point = punto.split(" ");
				puntos_poligono[i++] = new GLatLng(point[0], point[1]);
			});
			if(destino) color = "#FF0000";
			else{
				switch(tipo_prod){
					case "P":{
						color = "#8800FF";
						break;
					}
					case "C":{
						color = "#83C4DF";
						break;
					}
					default: color = "#FF0000";
				}
			}
			switch(tipo){
				case 2:{
					var polygon = new GPolyline(puntos_poligono, color, 5, 0.7)
					var poligono = false;
					break;
				}
				case 3:{
					var polygon = new GPolygon(puntos_poligono, color, 2, 0.7, color, 0.2);
					var poligono = true;
					break;
				}
			}
			geoms_dibujadas[id_g] = polygon;
			map.addOverlay(polygon);
			ajaxDestinoBound[id_g] = true;
			ajaxDestinoBoundPoligono[id_g] = poligono;
			$('loading').setStyles({visibility:'hidden'});
		}
	}).send();
}
/***************************************************************************************************/

/****************************************** HOTELES BOUND ******************************************/
var hoteles_dibujados = new Array();
var hoteles_dibujados_id = new Array();
var AjaxHotelesBound = null;
var time1 = null;

function loadingH(lang){
	$('loading_h').setStyles({visibility:'visible'});
	if(lang == "esp") $('loading_h').innerHTML = "<p style=\"color:#fff;padding:1px 2px;margin:0\"><img src=\"/images/mapas/loading_h.gif\" style=\"border:0\" align=\"absmiddle\" /> Obteniendo Holetes</p>";
	else $('loading_h').innerHTML = "<p style=\"color:#fff;padding:1px 2px;margin:0\"><img src=\"/images/mapas/loading_h.gif\" align=\"absmiddle\" style=\"border:0\" /> Retrieving Hotels</p>";
}

function obtener_hoteles_bound(xmin, ymin, xmax, ymax, lang, id_hotel, id_pais){
	if(AjaxHotelesBound != null) AjaxHotelesBound.cancel();
	var ids = hoteles_dibujados_id.join(",");
	ids = doReplaceExp(",+", ",", ids);
	ids = doReplaceExp("^,", "", ids);
	ids = doReplaceExp(",$", "", ids);
	new Request({
		url:'/mapas/obtener_hoteles_bound.php',
		data:'xmin=' + xmin + '&ymin=' + ymin + '&xmax=' + xmax + '&ymax=' + ymax + '&not=' + ids + '&id_hotel=' + id_hotel + '&lang=' + lang + '&id_pais=' + id_pais,
		method: 'get',
		encoding:'ISO-8859-1',
		onRequest: function(){
			time1 = setTimeout("loadingH('"+lang+"')",500);
		},
		onSuccess: function(responseText){
			clearTimeout(time1);
			var temp = JSON.decode(responseText);
			var total = temp.hoteles.length;
			var num = 1;

			temp.hoteles.each(function(hotel){
				if(!hoteles_dibujados[hotel.ig]){
					if(hotel.x != 0){
						dibujarHotelBound(hotel.ig, hotel.x, hotel.y, hotel.il, hotel.tt, false, lang);
						num++;
					}
				}
			});
			$('loading_h').setStyles({visibility:'hidden'});
		}
	}).send();
}

function dibujarHotelBound(id_geom, x_, y_, id, tooltip, principal, lang){
	if(principal){ var marker = new GMarker(new GLatLng(x_, y_), {icon: getIconHotel(), draggable: false, title:tooltip});
	}else{ var marker = new GMarker(new GLatLng(x_, y_), {icon: getIconHotelBound(), draggable: false, title:tooltip});
	}
	GEvent.addListener(marker, "click", function(){
		infoMarker(marker, id, lang, principal);
	});
	map.addOverlay(marker);
	hoteles_dibujados_id[id_geom] = id_geom;
}

function getIconHotelBound() {
	var icon = new GIcon();
	icon.image = "/images/mapas/numbers/h.png";
	icon.shadow = "/images/mapas/numbers/s.png";
	icon.iconSize = new GSize(17, 19);
	icon.iconAnchor = new GPoint(1, 19);
	icon.infoWindowAnchor = new GPoint(1, 19);
	return icon;
}

function getIconHotel() {
	var icon = new GIcon();
	icon.image = "/images/mapas/pin1.png";
	icon.iconSize = new GSize(32, 32);
	icon.iconAnchor = new GPoint(16, 31);
	icon.infoWindowAnchor = new GPoint(16, 31);
	return icon;
}

/***************************************************************************************************/

/********************************************* DESTINOS ********************************************/
function dibujarDestinoFijo(x_, y_, nombre, tipo_geom, the_geom, tooltip, destino){
	if(tipo_geom != 1){
		var puntos = the_geom.split(",");
		var puntos_poligono = new Array();
		var i=0;
		puntos.each(function(punto){
			var point = punto.split(" ");
			puntos_poligono[i++] = new GLatLng(point[0], point[1]);
		});
		switch(tipo_geom){
			case 2:{
				var polygon = new GPolyline(puntos_poligono, "#00E13C", 6, 0.6, {title:tooltip})
				break;
			}
			case 3:{
				var polygon = new GPolygon(puntos_poligono, "#00E13C", 2, 0.7, "#00E13C", 0.2, {title:tooltip});
				break;
			}
		}
		map.addOverlay(polygon);
	}else{
		var marker = new GMarker(new GLatLng(x_, y_), {icon: getIconHotel(), draggable: false, title:tooltip});
		map.addOverlay(marker);
	}
}

function mostrar_tour(id_tour, donde, id_pais, destinos, tours, hoteles){
	mostrar_destino(id_tour, donde, false, id_pais, destinos, tours, hoteles);
}

function mostrar_circuito(id_circuito, donde, id_pais){
	mostrar_destino(id_circuito, donde, false, id_pais);
}

function mostrar_destino(id_destino, donde, destino_, id_pais, destinos, tours, hoteles){
	var temp = location.pathname.split('/');
	base_url = 'http://' + location.host + '/' + temp[1] + '/' + temp[2]
	var lang = temp[1];
	if (GBrowserIsCompatible()){
		if(destino_) link_ = "destino";
		else link_ = "tour";
		new Request({
			url:"/mapas/obtener_" + link_ + ".php",
			data:link_ + "=" + id_destino + "&lang=" + lang + "&id_pais=" + id_pais,
			method:'get',
			encoding:'ISO-8859-1',
			onSuccess: function(responseText){
				var destino = JSON.decode(responseText);
				if(destino.x != 0){
					crearMapa(destino.x, destino.y, 15, donde);

					if(destino.tipo_geom != 1){
						var zoom_level = obtenerZoomBound(Number(destino.xmin), Number(destino.ymin), Number(destino.xmax), Number(destino.ymax));
						$('img_destino').setProperty('src', "/images/mapas/destino.gif");
					}else{
						var zoom_level = 15;
						$('img_destino').setProperty('src', "/images/mapas/pin1.gif");
					}
					recenterMap(destino.x, destino.y, zoom_level);
					crearBotonReset(destino.x, destino.y, zoom_level, lang);

					if(lang == "eng"){
						dibujarDestinoFijo(destino.x, destino.y, destino.nombre_esp, destino.tipo_geom, destino.the_geom, destino.nombre_esp_tt, destino);
					}else{
						dibujarDestinoFijo(destino.x, destino.y, destino.nombre_ing, destino.tipo_geom, destino.the_geom, destino.nombre_ing_tt, destino)
					}

					crearLoadingDest(donde);
					crearLoadingHotel(donde);
					crearLoadingTour(donde)
					moveendBounds("", hoteles, id_destino, destinos, id_destino, tours, lang, id_pais);

					GEvent.bind(map, "moveend", this, function(){
						moveendBounds("", hoteles, id_destino, destinos, id_destino, tours, lang, id_pais);
					});
				}
			}
		}).send();
	}else{
		$(donde).setStyle('display','none');
	}
}
/***************************************************************************************************/
function mostrar_destino_staticImg(id_destino, donde, id_pais){
	var temp = location.pathname.split('/');
	//base_url = 'http://' + location.host + '/' + temp[1] + '/' + temp[2]
	var lang = temp[1];
	if (GBrowserIsCompatible()){
		link_ = "destino";		
		new Request({
			url:'/mapas/obtener_' + link_ + '.php', 
			data:link_ + '=' + id_destino + "&lang=" + lang + "&id_pais=" + id_pais,
			method:'get',
			encoding:'ISO-8859-1',
			onSuccess: function(responseText){
				var destino = JSON.decode(responseText);
				if(destino.x != 0){	
					if(lang == "eng") var nombreDest = 'Location Map of '+destino.nombre_ing;
					if(lang == "esp") var nombreDest = 'Mapa de Ubicacion de '+destino.nombre_esp;
					var zoom_level = 8;//obtenerZoomBound(Number(destino.xmin), Number(destino.ymin), Number(destino.xmax), Number(destino.ymax));
					var staticMap='<img src="http://maps.google.com/staticmap?center='+destino.x+','+destino.y+'&zoom='+zoom_level+'&size=180x195&maptype=terrain&markers='+destino.x+','+destino.y+'&key='+keyGoogle+'" alt="'+nombreDest+'" />';
					$(donde).set('html', staticMap);					
				}
			}
		}).send();
	}else{
		$(donde).setStyle('display','none');
	}
}
/***************************************************************************************************/

function mostrar_circuitos(lista_circuitos, donde, circuito){
	mostrar_tours(lista_circuitos, donde, circuito);
}
function mostrar_tours(lista_tours, donde, circuito){
	var temp = location.pathname.split('/');
	//base_url = 'http://' + location.host + '/' + temp[1] + '/' + temp[2]
	var lang = temp[1];
	if (GBrowserIsCompatible()){
		new Request({
			url:'/mapas/obtener_tours.php',
			data:'tours=' + lista_tours + '&lang=' + lang + '&cir=' + circuito,
			method: 'get',
			encoding:'ISO-8859-1',
			onSuccess: function(responseText){
				var temp = JSON.decode(responseText);
				extend = temp.extend;
				crearMapa(extend.x, extend.y, 15, donde);
				var zoom_level = obtenerZoomBound(extend.xmin, extend.ymin, extend.xmax, extend.ymax);
				if(zoom_level>15) zoom_level = 15
 				crearBotonReset(extend.x, extend.y, zoom_level, lang);
				recenterMap(extend.x, extend.y, zoom_level);

				temp.tours.each(function(tour){
					if(tour.x != 0) dibujarTourN(tour.ig, tour.x, tour.y, tour.t, tour.ti, tour.il, lang, false, tour.tp, tour.c, tour.n);
				});
				crearLoadingDest(donde);
				crearLoadingHotel(donde);
				crearLoadingTour(donde);
				moveendBounds("", false, "", true, "", false, lang);

				GEvent.bind(map, "moveend", this, function(){
					moveendBounds("", false, "", true, "", false, lang);
				});
			}
		}).send();
	}else{
		$(donde).setStyle('display','none');
	}
}
function dibujarTourN(id_g, x_, y_, tipo_geom, tooltip, id_lugar, lang, destino_, tipo_prod, circuito, numero){
	geoms_borrar[id_g] = true;
	
	if(!destino_){
		switch(tipo_prod){
			case "C":{
				if(lang == "esp") pre_tt = "Crucero:";
				else pre_tt = "Cruise: ";
				break;
			}
			case "P":{
				switch(circuito){
					case 1:{
						if(lang == "esp") pre_tt = "Crucero: ";
						else  pre_tt = "Cruise: ";
						break;
					}
					case 2:{
						if(lang == "esp") pre_tt = "Circuito: ";
						else  pre_tt = "Circuit: ";
						break;
					}
					case 3:{
						if(lang == "esp") pre_tt = "Tour: ";
						else  pre_tt = "Tour: ";
						break;
					}
					case 4:{
						if(lang == "esp") pre_tt = "Tour: ";
						else  pre_tt = "Tour: ";
						break;
					}
					case 5:{
						if(lang == "esp") pre_tt = "Excursión: ";
						else  pre_tt = "Excursión: ";
						break;
					}
					case 6:{
						if(lang == "esp") pre_tt = "Tracking: ";
						else  pre_tt = "Tracking: ";
						break;
					}
				}
				break;
			}
			default: pre_tt = "Tour: ";
		}
	}else pre_tt = "";
	tooltip = pre_tt + tooltip;
	var marker = new GMarker(new GLatLng(x_, y_), {icon: getIconNumberHotel(numero), draggable: false, title:tooltip});
	if(tipo_geom == 3 || tipo_geom == 2){
		var MarkerMouseOver = GEvent.bind(marker, "mouseover", this, function(){
			if(ajaxDestinoBound[id_g]){
				geoms_dibujadas[id_g].show();
				if(ajaxDestinoBoundPoligono[id_g]){
					if(destino_) geoms_dibujadas[id_g].setFillStyle({"color":"#FF0000", "opacity":0.2});
					else{
						switch(tipo_prod){
							case "P":{
								geoms_dibujadas[id_g].setFillStyle({"color":"#8800FF", "opacity":0.2});
								break;
							}
							case "C":{
								geoms_dibujadas[id_g].setFillStyle({"color":"#83C4DF", "opacity":0.2});
								break;
							}
						}

					}
				}
			}else dibujarPoligonoBound(id_g, tipo_geom, destino_, tipo_prod);
		});
		var MarkerMouseOut = GEvent.bind(marker, "mouseout", this, function(){
			if(geoms_borrar[id_g]){
				if(ajaxDBound != null) ajaxDBound.cancel();
				try{
					geoms_dibujadas[id_g].hide();
				}catch(e){}
			}
		});
		var MarkerInfoClose = GEvent.bind(marker, "infowindowclose", this, function(){
			geoms_borrar[id_g] = true;
			geoms_dibujadas[id_g].hide();
		});
	}
	var MarkerClick = GEvent.bind(marker, "click", this, function(){
		geoms_borrar[id_g] = false;
		if(destino_) link_ = "destino"
		else link_ = "tour"
		new Request({
			url:"/mapas/obtener_info_" + link_ + ".php",
			data:"id_lugar=" + id_lugar + "&lang=" + lang + "&circuito=" + circuito,
			method: 'get',
			onSuccess: function(responseText){
				if(lang == "esp") desc_ruta = "M&aacute;s Informaci&oacute;n"
				else desc_ruta = "More Information"
				var destino = JSON.decode(responseText);
				if(destino_) txtTar = "";
				else txtTar = "<br /><span class=\"tarifa_tt\">" + destino.t + "</span><br /><span class=\"hab_tt\">" + destino.h + "</span>";
				if (destino.foto == '')
					marker.openInfoWindowHtml("<div style=\"margin-right:10px;width:350px;\"><span class=\"nombre_hotel\">" + destino.nombre + "</span><br><hr><span class=\"direccion_hotel\">" + destino.descripcion + "</span>" + txtTar + "<p align=\"left\" style=\"margin:2px 0 0 0;\" ><a class=\"info_tt\" style=\"right:90px;\" href=\"" + destino.ruta + "\" target=\"_self\">M&aacute;s informaci&oacute;n</a></p></div>");
				else
					marker.openInfoWindowHtml("<div style=\"margin-right:10px;width:350px;\"><img class=\"img_hotel\" height=\"100\" align=\"left\" src=\"" + destino.foto + "\" /><span class=\"nombre_hotel\">" + destino.nombre + "</span><br><hr><span class=\"direccion_hotel\">" + destino.descripcion + "</span>" + txtTar + "<p style=\"margin:2px 0 0 0;\" align=\"left\"><a class=\"info_tt\" style=\"right:90px;\" href=\"" + destino.ruta + "\" target=\"_self\">M&aacute;s informaci&oacute;n</a></p></div>");
			}
		}).send();
	});
	map.addOverlay(marker);
	tours_dibujados[id_g] = true;
	tours_dibujados_id[id_g] = id_g;
}
