//Funciones del mapa del buscador de cercanía

	var mapa; 
	var iconoDefecto;
	var iconoAmarillo;
	var vectorPuntos;
	var parametrosBusqueda = "";
	
	var geoXmlParkings;
	var geoXmlBusUrbano;
	var geoXmlPuntosInteres;
	
	function load() {
				
		  if (GBrowserIsCompatible()) {			
				
			inicializarMapa();							
			mapa.setCenter(new GLatLng(41.071587,-0.187626), 8);
			var punto = new GMarker(new GLatLng(41.071587,-0.187626), iconoAmarillo);
			var tooltip = new Tooltip(punto,'MotorLand Aragón',-50);
			punto.tooltip = tooltip;
					
			GEvent.addListener(punto,'mouseover',function(){if (!punto.isInfoWindowOpen) {this.tooltip.show();}});
			GEvent.addListener(punto,'mouseout',function(){this.tooltip.hide();});		
												
			mapa.addOverlay(punto);
			mapa.addOverlay(tooltip);

			//marcarTodas();
			inicializarBusqueda();									
			}			
	}	
	
	function marcarTodas(){
		var checkButtons = document.getElementsByName("categorias");		
      	for (var x = 0; x < checkButtons.length; x ++) {
			checkButtons[x].checked = true;
		  	}
	}
	
	function inicializarBusqueda (){
	
		parametrosBusqueda = "";
		var checkButtons = document.getElementsByName("categorias");		
      	for (var x = 0; x < checkButtons.length; x ++) {
			if (checkButtons[x].checked) 
				{
			  	parametrosBusqueda += checkButtons[x].value + ' ';
				} 
		  	}	  
		cargarPuntos();
	
	}
	
		function inicializarMapa(){
		
		//Define los iconos y el tipo de mapa de arranque
	
		iconoDefecto = new GIcon();
		iconoDefecto.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
		iconoDefecto.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		iconoDefecto.iconSize = new GSize(12, 20);
		iconoDefecto.shadowSize = new GSize(22, 20);
		iconoDefecto.iconAnchor = new GPoint(6, 20);
		iconoDefecto.infoWindowAnchor = new GPoint(5, 1);
		
		iconoAmarillo = new GIcon();
		iconoAmarillo.image = "http://labs.google.com/ridefinder/images/mm_20_yellow.png";
		iconoAmarillo.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		iconoAmarillo.iconSize = new GSize(12, 20);
		iconoAmarillo.shadowSize = new GSize(22, 20);
		iconoAmarillo.iconAnchor = new GPoint(6, 20);
		iconoAmarillo.infoWindowAnchor = new GPoint(5, 1);

        
		mapa = new GMap2(document.getElementById("mapa")); 
		mapa.addControl(new GLargeMapControl());
		mapa.addControl(new GHierarchicalMapTypeControl());
		mapa.addMapType(G_PHYSICAL_MAP); 
    	mapa.addMapType(G_SATELLITE_3D_MAP); 
		
		mapa.enableContinuousZoom();
		mapa.enableScrollWheelZoom();
	
	}
		
	function cargarPuntos() {
	
		var etiquetaCarga = document.getElementById("etiquetaCargando")
		
		etiquetaCarga.style.display = '';
		cargandoListadoPuntos();
		
		limpiarPuntosActuales();
		
		var parametrosAdicionales = "";
		vectorPuntos = new objetoPuntos();	
		
		if (parametrosBusqueda != ""){
			parametrosAdicionales = "?mapa=1&filtro=" + parametrosBusqueda;
		
			GDownloadUrl("ajaxAlojamientos.aspx" + parametrosAdicionales, function (doc) { 
				var xmlDoc = GXml.parse(doc);			
				var servicios = xmlDoc.documentElement.getElementsByTagName("alojamiento"); 
				
				vectorPuntos = new objetoPuntos(); 
				
				for (var i = 0; i < servicios.length; i++) 
					{ 		
						incluirPunto(servicios[i]);									
					}
					etiquetaCarga.style.display = 'none';
					actualizarListadoPuntos();
				});
			}	
		else
			{
				etiquetaCarga.style.display = 'none';
				actualizarListadoPuntos();
			}	
	}
	
	function incluirPunto(servicio){
		
		var lat = parseFloat(servicio.getAttribute("lat")); 
        var lng = parseFloat(servicio.getAttribute("long")); 
		var nombre = servicio.getAttribute("nombre"); 
		var direccion = servicio.getAttribute("direccion"); 
		var cp = servicio.getAttribute("cp"); 
		var poblacion = servicio.getAttribute("poblacion"); 
		var telefono = servicio.getAttribute("telefono"); 
		var web = servicio.getAttribute("web"); 
		var email = servicio.getAttribute("email"); 
		var distancia = servicio.getAttribute("distancia");
		var codigo = servicio.getAttribute("codigo");
		
		var punto = new GMarker(new GLatLng(lat,lng), iconoDefecto);
		punto.isInfoWindowOpen = false;
		var tooltip = new Tooltip(punto,nombre,-50);
		punto.tooltip = tooltip;
				
		GEvent.addListener(punto,'mouseover',function(){if (!punto.isInfoWindowOpen) {this.tooltip.show();}});
		GEvent.addListener(punto,'mouseout',function(){this.tooltip.hide();});
		GEvent.addListener(punto,'infowindowopen',function() {punto.isInfoWindowOpen = true;});
		GEvent.addListener(punto,'infowindowclose',function() {punto.isInfoWindowOpen = false;});
		GEvent.addListener(punto, "click", function() {this.tooltip.hide(); 
  	    punto.openInfoWindowHtml("<div class='bocadillo'><table width='230'><tr><td><b>" + nombre + "</b><br/>" + direccion + "<br/>" + cp + " " + poblacion + " (a "+ distancia + " Km. de MotorLand)<br/>Teléfono: " + telefono + "<br/><a target='_blank' href='" + web + "'>" + web + "</a><br/><a target='_blank' href='mailto:" + email + "'>" + email + "</a><br/></td></tr><tr><td><a class='comollegar' href='http://maps.google.com/maps?hl=es&daddr=" + lat + "," + lng + "' target='_blank'>Cómo llegar >>></a></td></tr></table></div>");
  		});		
											
		mapa.addOverlay(punto);
		mapa.addOverlay(tooltip);

		vectorPuntos.nuevoPunto(codigo,nombre,poblacion, telefono, distancia, punto);
		
		}

	function limpiarPuntosActuales(){
		
			//Recorremos el vector de hoteles y eliminamos todos los puntos del mapa
			if (vectorPuntos != undefined)			
				for (i=0; i< vectorPuntos.punto.length; i++)
				{
					mapa.removeOverlay(vectorPuntos.punto[i].marker);
				}			
	}
/* Funciones del vector de empresas */

	function objetoPuntos() { 
		this.punto=new Array(); 
	}
	
	function punto(codigo,nombre,direccion,telefono,distancia,marker) { 
		this.codigo=codigo;
		this.nombre=nombre;
		this.direccion=direccion;
		this.telefono=telefono; 
		this.distancia=distancia; 
		this.marker=marker;
	}
	
	objetoPuntos.prototype.nuevoPunto=function(codigo,nombre,direccion,telefono,distancia,marker) { 
		this.punto[this.punto.length]=new punto(codigo,nombre,direccion,telefono,distancia,marker); 
	} 	

function actualizarListadoPuntos(){
			
			//Limpiamos la tabla actual de empresas
			var capaPuntos = document.getElementById('listaAlojamientos');	
			var stringAux;
			capaPuntos.innerHTML='';			
			
			if (vectorPuntos.punto.length > 0)
			{
				//Pintamos los elementos en la tabla
				stringAux = "<table id='empresasParque' cellpadding='10' cellspacing='5' width='100%'><tr><th height='15'>Alojamiento<br/>(Click para ver en mapa)</th><th>Población</th><th>Teléfono(s)</th><th>Distancia (Km)</th></tr>";
				for (i=0; i< vectorPuntos.punto.length; i++)
				{
					stringAux += nuevoPuntoCapa(vectorPuntos.punto[i], i);
				}			
				stringAux += "</table>"
				capaPuntos.innerHTML=stringAux;
		
			}	else {
				capaPuntos.innerHTML= '<br><h3 align="center">Por favor, seleccione al menos una categoría para<br/>visualizar los alojamientos en el mapa</h3>'
				}			
	}
function cargandoListadoPuntos(){
			
			var capaPuntos = document.getElementById('listaAlojamientos');	
			capaPuntos.innerHTML='<br/><h3 align="center">Actualizando el listado de alojamientos...</h3>';							
	}


function nuevoPuntoCapa (nuevoPunto, indexPunto){

		return "<tr><td height='15'><a class='rojo' href='javascript:seleccionarPuntoMapa(" + indexPunto + ")'>" + nuevoPunto.nombre + "</a></td><td>" + nuevoPunto.direccion + "</td><td>" + nuevoPunto.telefono + "</td><td>" + nuevoPunto.distancia + "</td></tr>"		
	}

function seleccionarPuntoMapa(indexPunto){
		
			//Disparamos el evento Click del punto en cuestión
			GEvent.trigger(vectorPuntos.punto[indexPunto].marker, "click");
		}
/*Funciones del elemento ToolTip*/
	
	function Tooltip(marker, text, padding){
		this.marker_ = marker;
		this.text_ = text;
		this.padding_ = padding;
	}
	
	Tooltip.prototype = new GOverlay();
	
	Tooltip.prototype.initialize = function(map){
		var div = document.createElement("div");
		div.appendChild(document.createTextNode(this.text_));
		div.className = 'tooltip';
		div.style.position = 'absolute';
		div.style.visibility = 'hidden';
		map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
		this.map_ = map;
		this.div_ = div;
	}
	
	Tooltip.prototype.remove = function(){
		this.div_.parentNode.removeChild(this.div_);
	}
	
	Tooltip.prototype.copy = function(){
		return new Tooltip(this.marker_,this.text_,this.padding_);
	}
	
	Tooltip.prototype.redraw = function(force){
		if (!force) return;
		var markerPos = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
		var iconAnchor = this.marker_.getIcon().iconAnchor;
		var xPos = Math.round(markerPos.x - this.div_.clientWidth / 2);
		var yPos = markerPos.y - iconAnchor.y - this.div_.clientHeight - this.padding_;
		this.div_.style.top = yPos + 'px';
		this.div_.style.left = xPos + 'px';
	}
	
	Tooltip.prototype.show = function(){
		this.div_.style.visibility = 'visible';
	}
	
	Tooltip.prototype.hide = function(){
		this.div_.style.visibility = 'hidden';
	}	
