/* Contantes de las posiciones de la información dentro del array de hoteles */

	var HOTEL_CODIGO    = 0;

	var HOTEL_NOMBRE    = 1;

	var HOTEL_CATEGORIA = 2;

	var HOTEL_PAIS      = 3;

	var HOTEL_CIUDAD    = 4;

	var HOTEL_DIRECCION = 5;

	var HOTEL_LATITUD   = 6;

	var HOTEL_LONGITUD  = 7;

	var HOTEL_MARCA     = 8;

	var HOTEL_PRECIO    = 9;

	var HOTEL_PATH_FOTO = 10;

		

	/* Valores para el mapa */

	var existenGeocodes       = false;   // False si no existe ningun hotel con geocodes en la busqueda

	var centroLatitud         = 39.57;   // Por defecto. Palma de Mallorca. Si aparece ha habido un error.

	var centroLongitud        = 2.65;    // Por defecto. Palma de Mallorca. Si aparece ha habido un error.

	var mapaProfundidad       = 16;      // Profundidad por defecto.

	var mapaAltura            = 0.54;    // Amplitud de altura permitido para la profundidad por defecto

	var mapaAnchura           = 0.30;    // Amplitud de altura permitido para la profundidad por defecto

	var mapaUmbral            = 0.01;    // Umbral minimo para la amplitud de la altura o anchura

	var mapaProfundidadMaxima = 14;      // Profundidad maxima permitida

	var mapaProfundidadMinima = 5;

	

	/* Devuelve el texto que se mostrara dentro de la ventana de información de cada marcador */

	function formatearContenido(indice) {

		var texto = new String();

		

		texto    =		"<table bgcolor='#ffffff' border='0' cellpadding='0' cellspacing='0' width='305'>";

		texto    +=			"<tr>";

		texto    +=				"<td width='8'></td>";

		texto    +=				"<td width='160'></td>";

		texto    +=				"<td width='5'></td>";

		texto    +=				"<td width='132'></td>";

		texto    +=			"</tr>";

		texto    +=			"<tr bgcolor='#EEEEEE'>";

		texto    +=				"<td align='right' class='v_333333_11n' height='15' valign='top'><img src='http://www.solmelia.com/solNew/comun/img/fle_gui/msg_fle_trans.gif' width='3' height='10' alt='' border='0'></td>";

		texto    +=				"<td colspan='3' class='v_333333_11b'>&nbsp;"+contenedorHoteles[indice][HOTEL_NOMBRE]+" "+contenedorHoteles[indice][HOTEL_CATEGORIA]+"</td>";

		texto    +=			"</tr>";

				

		texto    +=			"<tr>";

		texto    +=				"<td colspan='4'></td>";

		texto    +=			"</tr>";

				   

		texto    +=			"<tr>";

		texto    +=				"<td valign='top'></td>";

		texto    +=				"<td valign='top'>";	

						

		texto    +=					"<table bgcolor='#ffffff' border='0' cellpadding='0' cellspacing='0' width='160'>";

		texto    +=						"<tr>";

		texto    +=							"<td height='5'></td>";

		texto    +=						"</tr>";

		texto    +=						"<tr>";

		texto    +=							"<td class='v_333333_11n'>";

		texto    +=								"Claudio Coello, 139<br/>";

		texto    +=								"28006 - Madrid<br/>";

		texto    +=								"España<br/><br/>";

		texto    +=								"Tel: +34 91 5626600<br/>";

		texto    +=								"Fax: +34 91 5627519<br/>";

		texto    +=							"</td>";

		texto    +=						"</tr>";

		texto    +=					"</table>";		

						

		texto    +=				"</td>";

		texto    +=				"<td>&nbsp;</td>";

		texto    +=				"<td><img src='http://www.solmelia.com/solNew/img/hoteles/"+contenedorHoteles[indice][HOTEL_CODIGO]+"/"+contenedorHoteles[indice][HOTEL_PATH_FOTO]+"' border='1' width='130' height='90' alt='"+contenedorHoteles[indice][HOTEL_NOMBRE]+"' class='mapPopImg'></td>";

		texto    +=			"</tr>";

					

		texto    +=			"<tr>";

		texto    +=				"<td colspan='4'></td>";

		texto    +=			"</tr>"; 	

		texto    +=		"</table>";

		

		return texto;

	}



	/* 

	Crea un marcador para un hotel.

	La imagen depende de la marca del hotel

	Se añaden los eventos relaciones con el marcador.

	*/

	function crearMarcador(map, geocode, indice, icono) {

	

		/* Icono */

			icono.image  = "../img/logo_mapa.gif";

        	//icono.image  = pathIconos+"sm_icono_"+contenedorHoteles[indice][HOTEL_MARCA]+".png";

		var marcador = new GMarker(geocode, icono);

		

		/* Eventos */

		GEvent.addListener(marcador, "click", function() {

			marcador.openInfoWindowHtml(formatearContenido(indice));

		});

		/*GEvent.addListener(marcador, "mouseout", function() {

			map.closeInfoWindow();

		});*/

		

		return marcador;

	}

			

	/* Carga el mapa si el navegador es compatible */

	function load() {

		if (GBrowserIsCompatible()) {



			/* Inicializacion del mapa */

			var map = new GMap2(document.getElementById("map"));

			map.setCenter(new GLatLng(39.57, 2.65), 15);

			

			/* Icono base para las imagenes de los marcadores */

		        var icono              = new GIcon();

		        icono.iconSize         = new GSize(150, 35);

		        icono.shadowSize       = new GSize(0, 0);

		        icono.iconAnchor       = new GPoint(75, 1);

		        icono.infoWindowAnchor = new GPoint(75, 2);

			

			/* Carga de los marcadores e infos de los hoteles */

			for (var i=0; i<contenedorHoteles.length; i++) {

				if (contenedorHoteles[i][HOTEL_LATITUD]!="" && contenedorHoteles[i][HOTEL_LONGITUD]!="") {

					var geocode  = new GLatLng(contenedorHoteles[i][HOTEL_LATITUD],contenedorHoteles[i][HOTEL_LONGITUD]);

					map.addOverlay(crearMarcador(map, geocode, i, icono));

				}

			}



			/* Variables que controlan la mayor amplitud entre los hoteles */

			var mayorLatitud  = new Number();

			var mayorLongitud = new Number();

			var menorLatitud  = new Number();

			var menorLongitud = new Number();

			

			/* Busqueda de la mayor amplitud en alto y ancho en todos los hoteles */	

			for (var i=0; i<contenedorHoteles.length; i++) {

			

				/* Si el hotel hotel tiene geocode */

				if (contenedorHoteles[i][HOTEL_LATITUD]!="" && contenedorHoteles[i][HOTEL_LONGITUD]!="") {

					

					/* Si es el primer hotel con geocode */

					if (existenGeocodes==false) {

						existenGeocodes = true;

						mayorLatitud    = contenedorHoteles[i][HOTEL_LATITUD];

						menorLatitud    = contenedorHoteles[i][HOTEL_LATITUD];

						mayorLongitud   = contenedorHoteles[i][HOTEL_LONGITUD];

						menorLongitud   = contenedorHoteles[i][HOTEL_LONGITUD];

					} else {

						if (contenedorHoteles[i][HOTEL_LATITUD]>mayorLatitud)   {mayorLatitud=contenedorHoteles[i][HOTEL_LATITUD];}

						if (contenedorHoteles[i][HOTEL_LATITUD]<menorLatitud)   {menorLatitud=contenedorHoteles[i][HOTEL_LATITUD];}

						if (contenedorHoteles[i][HOTEL_LONGITUD]>mayorLongitud) {mayorLongitud=contenedorHoteles[i][HOTEL_LONGITUD];}

						if (contenedorHoteles[i][HOTEL_LONGITUD]<menorLongitud) {menorLongitud=contenedorHoteles[i][HOTEL_LONGITUD];}

					}

					



				}

			}

			//alert("("+mayorLatitud+","+menorLatitud+","+mayorLongitud+","+menorLongitud+")");

			

			/* Calculo del geocode central del mapa */

			centroLatitud  = (new Number(mayorLatitud) + new Number(menorLatitud)) / 2;

			centroLongitud = (new Number(mayorLongitud) + new Number(menorLongitud)) / 2; 

			

			/* Punto central del mapa. Si no hay geocodes se elimina el mapa */

			if (existenGeocodes) {

			

				var ok = false;

				while (!ok && mapaProfundidad>mapaProfundidadMinima) {

					map.setCenter(new GLatLng(centroLatitud, centroLongitud), mapaProfundidad);

					var tmpBounds = map.getBounds();

					var indice = 0;

					var algunoFuera = false;

					while (indice<contenedorHoteles.length && !algunoFuera) {

						if (contenedorHoteles[indice][HOTEL_LATITUD]!="" && contenedorHoteles[indice][HOTEL_LONGITUD]!="") {

							var geocode = new GLatLng(contenedorHoteles[indice][HOTEL_LATITUD],contenedorHoteles[indice][HOTEL_LONGITUD]);;

							if (!tmpBounds.contains(geocode)) {

								algunoFuera = true;

							}

						}

						indice++;

					}

					if (algunoFuera) {

						mapaProfundidad--;

					} else {

						ok = true;

					}

				}

				

				map.setCenter(new GLatLng(centroLatitud, centroLongitud), mapaProfundidad);

			} else {

				GUnload();

				document.getElementById("map").style.visibility = "hidden";

				document.getElementById("map").style.height     = 0;

				document.getElementById("map").style.width      = 0;

				document.getElementById("estadoVisualizacionMapa").style.visibility = "hidden";

				document.getElementById("estadoVisualizacionMapa").style.height     = 0;

				document.getElementById("estadoVisualizacionMapa").style.width      = 0;

			}

			

			/* Añadimos los controles al mapa */

			map.addControl(new GCentrarHotelControl(centrar_mapa,new GLatLng(centroLatitud, centroLongitud), mapaProfundidad));

			map.addControl(new GSmallMapControl());

			map.addControl(new GMapTypeControl());			

		}

	}

	

		/* VISUALIZACION DEL MAPA */

	

	/* Estado de visualizacion del mapa */

	var visualizacionMapa = false;

	

	/* Controla el estado de visualizacion del mapa */

	function estadoVisualizacionMapa() {

		if (visualizacionMapa) {

			mostrarMapa();

		} else {

			ocultarMapa();

		}

		visualizacionMapa = !visualizacionMapa;

	}

	

	/* Oculta el mapa en el navegador */

	function ocultarMapa() {

		document.getElementById("map").style.visibility = "hidden";

		document.getElementById("map").style.height     = 0;

		document.getElementById("map").style.width      = 0;

		// mostrar_mapa declarado fuera

		document.getElementById("estadoVisualizacionMapa").innerHTML = mostrar_mapa;

	}



	/* Muestra el mapa (oculto) en el navegador */

	function mostrarMapa() {

		document.getElementById("map").style.visibility = "visible";

		document.getElementById("map").style.height     = "270px";

		document.getElementById("map").style.width      = "554px";

		// ocultar_mapa declarado fuera

		document.getElementById("estadoVisualizacionMapa").innerHTML = ocultar_mapa;	

	}

	

	/* AL CAMBIAR O CERRAR LA PAGINA */

	

	document.onunload=window.onunload = function() {

		GUnload();

	}