//Funciones del mapa del buscador de cercanía

	var mapa; 
	var iconoDefecto;
	var vectorPuntos;
	var parametrosBusqueda = "";
	
	var geoXmlParkings;
	var geoXmlBusUrbano;
	var geoXmlPuntosInteres;
	
	function load() {
				
		  if (GBrowserIsCompatible()) {			
				
			inicializarMapa();							
			mapa.setCenter(new GLatLng(41.65507835114933, -0.8876609802246094), 17);
			//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 = "images/iconoAranda.png";
		iconoDefecto.shadow = "images/shadow-iconoAranda.png";
		iconoDefecto.iconSize = new GSize(32.0, 32.0);
		iconoDefecto.shadowSize = new GSize(49.0, 32.0);
		iconoDefecto.iconAnchor = new GPoint(10.0, 24.0);
		iconoDefecto.infoWindowAnchor = new GPoint(16.0, 16.0);

        
		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 = '';
		limpiarPuntosActuales();
		var parametrosAdicionales = "";
		
		if (parametrosBusqueda != ""){
			parametrosAdicionales = "?filtro=" + parametrosBusqueda;
		
			GDownloadUrl("ajaxTiendas.aspx" + parametrosAdicionales, function (doc) { 
				var xmlDoc = GXml.parse(doc);			
				var servicios = xmlDoc.documentElement.getElementsByTagName("tienda"); 
				
				vectorPuntos = new Array(); 
				
				for (var i = 0; i < servicios.length; i++) 
					{ 		
						incluirPunto(servicios[i]);									
					}
					etiquetaCarga.style.display = 'none';
				});
			}	
		else
			{
				etiquetaCarga.style.display = 'none';
			}	
	}
	
	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 telefono = servicio.getAttribute("telefono"); 
		var codigo = servicio.getAttribute("codigo"); 
		var archivo = servicio.getAttribute("archivo"); 
		
		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 + " Zaragoza<br/>Teléfono: " + telefono + "<br/><a class='masInfo' href='catalogo.aspx?idArea=70&idParrafo=" + codigo + "'>más información</a></td><td width='100' align='left'><img class='miniatura' src='image.aspx?fil_ID=" + archivo + "&iW=85&iH=75&tImg=D'/></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[vectorPuntos.length]=punto;
		
		}

	function limpiarPuntosActuales(){
		
			//Recorremos el vector de hoteles y eliminamos todos los puntos del mapa
			if (vectorPuntos != undefined)			
				for (i=0; i< vectorPuntos.length; i++)
				{
					mapa.removeOverlay(vectorPuntos[i]);
				}			
	}

/*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';
	}
	
	function cambiarEstadoParkings(){	
			
		if (document.getElementById('chkParkings').checked) 
		{
			//Carga asíncronamente la capa KML con las líneas del metro 		
			geoXmlParkings = new GGeoXml("http://www.winpicks.es/parking.kml");
			mapa.addOverlay(geoXmlParkings);
				
		} else {
			//Borra la capa KML con las líneas del metro 		
			mapa.removeOverlay(geoXmlParkings);
		}
	}
	
	function cambiarEstadoBusUrbano(){		
			
		if (document.getElementById('chkBusUrbano').checked) 
		{
			//Carga asíncronamente la capa KML con las líneas del metro 		
			geoXmlBusUrbano = new GGeoXml("http://www.winpicks.es/lineasMetro.kml");
			mapa.addOverlay(geoXmlBusUrbano);
				
		} else {
			//Borra la capa KML con las líneas del metro 		
			mapa.removeOverlay(geoXmlBusUrbano);
		}
	}
	
	function cambiarEstadoPuntosInteres(){		
			
		if (document.getElementById('chkPuntosInteres').checked) 
		{
			//Carga asíncronamente la capa KML con las líneas del metro 		
			geoXmlPuntosInteres = new GGeoXml("http://www.winpicks.es/lineasMetro.kml");
			mapa.addOverlay(geoXmlPuntosInteres);
				
		} else {
			//Borra la capa KML con las líneas del metro 		
			mapa.removeOverlay(geoXmlPuntosInteres);
		}
	}
	
