function load(latitud,longitud,zoom,tipo_mapa)
{

      if (GBrowserIsCompatible())
      {


      	var punto_inicial = eval("new GLatLng("+latitud+","+longitud+");");
      	map = new GMap2(document.getElementById("mapa"));
        map.setCenter(punto_inicial, zoom, tipo_mapa);


        /*var myPano = new GStreetviewPanorama(document.getElementById("pano"));
        svOverlay = new GStreetviewOverlay();
        map.addOverlay(svOverlay);
        GEvent.addListener(map,"click", function(overlay,latlng) {
        	  myPano.setLocationAndPOV(latlng);
        	});*/

        var icon= new GIcon();
			icon.image="./img/arrow.png";
			icon.iconSize=new GSize(39,34);
			icon.iconAnchor=new GPoint(11,34);

       	ma = new GxMarker(punto_inicial,icon,"");
			GEvent.addListener(ma,'click',function() {
			map.setCenter(punto_inicial,13);});

		map.addOverlay(ma);

        map.enableContinuousZoom();
        map.enableDoubleClickZoom();
        map.enableScrollWheelZoom();

        //var ovcontrol = new GOverviewMapControl(new GSize(150,150));
      	//map.addControl(ovcontrol);

     	var escala=new GScaleControl();
      	map.addControl(escala);
      	//map.addControl(new GLargeMapControl(),new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10)));
      	//map.addControl(new GLargeMapControl());
      	//map.addControl(new GMapTypeControl());

      	//ovmap = ovcontrol.getOverviewMap();
      	//setTimeout("ovmap.setMapType(G_HYBRID_MAP);",1);
      	//setTimeout("ovmap.setMapType(G_HYBRID_MAP);",1);

      	//GEvent.addListener(map,'click',function(overlay,point){if(!overlay)tratar_punto(point,"");});
		generaClusters();

		GEvent.addListener(map,"zoomend",calculaZoomMarcar);
		calculaZoomMarcar(-1,map.getZoom());

		//recogemos la capa del buscador por codigo postal
		if (tipo!='ahorro')
		{
			tratarCapaCodPostal();
		}


      }
      else
      {
      	mostrarError('Tu navegador no soporta google maps');
      }
}

function mostrar_puntos(idioma,tipo)
{
	ponerCargandoPuntos();
	limpia_mapa();
	parametros="";
	primero=true;
	for (var i=0;i < document.formulario.elements.length;i++)
	{
		var elemento = document.formulario.elements[i];
		//if (elemento.type == "checkbox" && elemento.checked)
		if (elemento.type == "radio" && elemento.name.indexOf('categoria')!=-1 && elemento.checked)
		{
			if (primero)
			{
				parametros = elemento.value;
				primero=false;
			}
			else
			{
				parametros += ","+elemento.value;
			}

		}
	}

	tipoEmpresa="";
	if (getRadioButtonSelectedValue(document.formulario.tipoEmpresa)){
		tipoEmpresa = getRadioButtonSelectedValue(document.formulario.tipoEmpresa);
	}

	loadDatosPuntos('./include/puntos.jsp?tipo='+tipo+'&idioma='+idioma+'&parametros='+escape(parametros)+"&tipoEmpresa="+tipoEmpresa,
		function ()
		{
			pintarPuntos(tipo, tipoEmpresa)
		});
}

function loadDatosPuntos(url,procesoReqCambio)
{
	// Puede que no se soporte nativamente el objeto XMLHttpRequest
    if(window.XMLHttpRequest)
    {
    	req_puntos=new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
    	req_puntos= new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(req_puntos)
    {
    	req_puntos.onreadystatechange=procesoReqCambio;
    	req_puntos.open("POST",url,true);
    	req_puntos.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	req_puntos.send("id=3");
    }
}

function mostrarPunto(nombrePunto,coordenadaPunto,iconoPunto,iconWidth,iconHeight,pk_rec,fk_cat)
{

	variable = 'marker'+pk_rec;

	if (typeof(window[variable])=='undefined')
	{
		//No esta el marker, por tanto lo pintamos
		var icon= new GIcon();
   		icon.image='./img/'+iconoPunto;
   		icon.iconSize=new GSize(iconWidth,iconHeight);
		icon.iconAnchor=new GPoint((iconWidth/2),(iconHeight/2));
		var point=eval('new GLatLng'+coordenadaPunto);
		//eval('marker'+pk_rec+' = new GxMarker(point,icon,nombrePunto)');
		eval('marker'+pk_rec+' = new GMarker(point,{icon:icon,title:nombrePunto});');
		eval('marker'+pk_rec+'.pk_rec=pk_rec;')
		eval('GEvent.addListener(marker'+pk_rec+',\'click\',function(){abrir_bocadillo(\''+pk_rec+'\')});');
		//eval('GEvent.addListener(marker'+pk_rec+',\'click\',abrir_bocadillo);');
		eval('GEvent.addListener(marker'+pk_rec+',\'remove\',ocultarMarker);');
		eval('map.addOverlay(marker'+pk_rec+');');
		//clusterer.AddMarker(marker,arrayDatos[i].nombre);
		//nombreCluster="clusterer_"+fk_cat;
		//eval(nombreCluster+'.AddMarker(marker'+pk_rec+',nombrePunto)');

	}
	else
	{
		//Esta el marker, vemos si esta oculto o no
		if (window[variable].isHidden())
		{
			//Si esta oculto, lo ponemos a show y pintamos el punto
			//La function show() no pinta el punto solo pone el isHidden a true;
			window[variable].show();
			var icon= new GIcon();
	   		icon.image='./img/'+iconoPunto;
   			icon.iconSize=new GSize(iconWidth,iconHeight);
			icon.iconAnchor=new GPoint((iconWidth/2),(iconHeight/2));
			var point=eval('new GLatLng'+coordenadaPunto);
			//eval('marker'+pk_rec+' = new GxMarker(point,icon,nombrePunto)');
			eval('marker'+pk_rec+' = new GMarker(point,{icon:icon,title:nombrePunto});');
			eval('marker'+pk_rec+'.pk_rec=pk_rec;')
			eval('GEvent.addListener(marker'+pk_rec+',\'click\',abrir_bocadillo);');
			eval('GEvent.addListener(marker'+pk_rec+',\'remove\',ocultarMarker);');
			//map.addOverlay(marker);
			//clusterer.AddMarker(marker,arrayDatos[i].nombre);

			eval('map.addOverlay(marker'+pk_rec+');');
			//nombreCluster="clusterer_"+fk_cat;
			//eval(nombreCluster+'.AddMarker(marker'+pk_rec+',nombrePunto)');
		}
	}

}
function pintarPuntos(tipo, tipoEmpresa)
{
	if(req_puntos.readyState==4)
    {
    	// Comprobamos si ha habido exito
    	if(req_puntos.status==200)
    	{
    		arrayDatos = eval(req_puntos.responseText);
    		for (i=0;i<arrayDatos.length;i++)
    		{
    			var icon= new GIcon();
		   		icon.image='./img/'+arrayDatos[i].icono;
		   		icon.iconSize=new GSize(arrayDatos[i].iconWidth,arrayDatos[i].iconHeight);
  				icon.iconAnchor=new GPoint((arrayDatos[i].iconWidth/2),(arrayDatos[i].iconHeight/2));
				var point=eval('new GLatLng('+arrayDatos[i].latitud+','+arrayDatos[i].longitud+')');
				var nombre = "";

				if (typeof(arrayDatos[i].nombre)!='undefined')
				{
					nombre=arrayDatos[i].nombre
				}
				else
				{
					nombre="";
				}

				//eval('marker'+arrayDatos[i].pk_rec+' = new GxMarker(point,icon,nombre)');
				eval('marker'+arrayDatos[i].pk_rec+' = new GMarker(point,{icon:icon,title:nombre});');
				eval('marker'+arrayDatos[i].pk_rec+'.pk_rec=arrayDatos[i].pk_rec;')
				//eval('GEvent.addListener(marker'+arrayDatos[i].pk_rec+',\'click\',abrir_bocadillo);');
				eval('GEvent.addListener(marker'+arrayDatos[i].pk_rec+',\'click\',function(){abrir_bocadillo(\''+arrayDatos[i].pk_rec+'\')});');
				eval('GEvent.addListener(marker'+arrayDatos[i].pk_rec+',\'remove\',ocultarMarker);');
				//map.addOverlay(marker);
				//clusterer.AddMarker(marker,arrayDatos[i].nombre);
				//nombreCluster="clusterer_"+arrayDatos[i].fk_cat;
				if (tipo=="ahorro")
				{
					eval('map.addOverlay(marker'+arrayDatos[i].pk_rec+')');
				}
				else
				{
					//nombreCluster="clusterer_"+arrayDatos[i].fk_catPadre;
					nombreCluster="clusterer_"+tipoEmpresa;
					eval(nombreCluster+'.AddMarker(marker'+arrayDatos[i].pk_rec+',nombre)');
				}
			}
			quitarCargandoPuntos();
		}
		else
    	{
    		mostrarError("Problema al recoger los puntos seleccionados:\n"+req_puntos.statusText);
    	}
	}
}
function ocultarMarker()
{
	this.hide();
}
function abrir_bocadillo(clave)
{

	if (clave==null)
	{
		pk_rec=this.pk_rec;
		panTo=false;
	}
	else
	{
		pk_rec=clave;
		panTo=true;
	}
	//loadDatosBocadillo('./include/bocadillo.jsp?pk_rec='+escape(pk_rec),pinta_bocadillo);
	var idioma = document.formulario.idioma.value;
	loadDatosBocadillo('./include/bocadillo.jsp?tipo='+tipo+'&idioma='+idioma+'&pk_rec='+escape(pk_rec),function(){pinta_bocadillo(panTo);});

}
function pinta_bocadillo(panTo)
{
	if(req_bocadillo.readyState==4)
    {
    	// Comprobamos si ha habido exito
    	if(req_bocadillo.status==200)
    	{
			//alert(req_bocadillo.responseText)
			datosBocadillo = eval(req_bocadillo.responseText);
			var point=eval('new GLatLng'+datosBocadillo[0].punto);
  			efectoBocata(point,datosBocadillo[0].codigoHtml,panTo);

  			//Otras formas de abrir el bocadillo
  			//map.openInfoWindowHtml(point,datosBocadillo[0].codigoHtml);
  			/* capa_bocadillo = document.getElementById('cuerpo_bocadillo');
  			capa_bocadillo.innerHTML = datosBocadillo[0].codigoHtml;
  			map.openInfoWindow(point,capa_bocadillo);*/

		}
		else
    	{
    		mostrarError("Problema al recoger los datos del bocadillo:\n"+req_bocadillo.statusText);
    	}
	}

}
function loadDatosBocadillo(url,procesoReqCambio)
{
	// Puede que no se soporte nativamente el objeto XMLHttpRequest
    if(window.XMLHttpRequest)
    {
    	req_bocadillo=new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
    	req_bocadillo= new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(req_bocadillo)
    {
    	req_bocadillo.onreadystatechange=procesoReqCambio;
    	//req_bocadillo.onsuccess=procesoReqCambio;
    	req_bocadillo.open("POST",url,true);
    	req_bocadillo.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	req_bocadillo.send("id=3");
    }
}
function zoomMaximo(puntox,puntoy)
{
    	// Ponemos un zoom por cada tipo de mapa

	var nombre_Actual=map.getCurrentMapType();
    zooma = nombre_Actual.getMaximumResolution(new GLatLng(puntox,puntoy));
    /*var nombre_Actual=map.getCurrentMapType().getName();

    	if(nombre_Actual==G_NORMAL_MAP.getName())
 		{
 			zooma=17;
 		}
 		else if(nombre_Actual==G_HYBRID_MAP.getName())
 		{
 			zooma=15;
 		}
 		else if(nombre_Actual==G_SATELLITE_MAP.getName())
		{
			zooma=15;
		}
	*/
	//alert(zooma);
	map.setCenter(new GLatLng(puntox,puntoy), zooma-2);
}
function loadDatosFicha(url,procesoReqCambio)
{
	// Puede que no se soporte nativamente el objeto XMLHttpRequest
    if(window.XMLHttpRequest)
    {
    	req_ficha=new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
    	req_ficha= new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(req_ficha)
    {
    	req_ficha.onreadystatechange=procesoReqCambio;
    	//req_bocadillo.onsuccess=procesoReqCambio;
    	req_ficha.open("POST",url,true);
    	req_ficha.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	req_ficha.send("id=3");
    }
}
function abrirFicha(pk_rec) {

        var idioma = document.formulario.idioma.value;
        /*
        miPopup = window.open('./include/ficha.jsp?idioma='+idioma+'&pk_rec='+pk_rec,"_blank","width=900,height=700,toolbar=no,directories=no,menubar=no,scrollbars=yes,left=5,top=5");

		if (!miPopup.opener)
		{
        	miPopup.opener = self;
        }
        */
        loadDatosFicha('./include/ficha.jsp?idioma='+idioma+'&pk_rec='+pk_rec,pintaFicha);

}
function pintaFicha()
{
	if(req_ficha.readyState==4)
    {
    	// Comprobamos si ha habido exito
    	if(req_ficha.status==200)
    	{
			datosFicha = eval(req_ficha.responseText);
			var point=eval('new GLatLng'+datosFicha[0].punto);
  			efectoFicha(point,datosFicha[0].codigoHtml);
		}
		else
    	{
    		mostrarError("Problema al recoger los datos del bocadillo:\n"+req_ficha.statusText);
    	}
	}
/*
         var contenidoFicha=document.getElementById("contenidoFicha");

        contenidoFicha*/
}
 function ponerCargandoPuntos()
{
	var obj=document.getElementById('cargandoPuntos');
	obj.style.display='block';
	desactivaRadioButtons();

}
function quitarCargandoPuntos()
{
	var obj=document.getElementById('cargandoPuntos');
	obj.style.display='none';
	activaRadioButtons();
}
function bloquearMapa()
{
	if (map.draggingEnabled())
	{
		map.disableDragging();
	}
}
function desbloquearMapa()
{
	map.enableDragging();
}
function cambia_tipo_mapa(tipo_mapa)
{
	map.setMapType(tipo_mapa);
	//ovmap.setMapType(tipo_mapa);
	marca_tipo_mapa();
}
function marca_tipo_mapa()
{
	var idioma = document.formulario.idioma.value;
	if(G_NORMAL_MAP.getName()==map.getCurrentMapType().getName())
	{
		document.getElementById('pestana_mapa').src=imgFolder+idioma+'pestana_mapa_selec.gif';
		document.getElementById('pestana_satelite').src=imgFolder+idioma+'pestana_satelite.gif';
		document.getElementById('pestana_mixto').src=imgFolder+idioma+'pestana_mixto.gif';
	}
	else if(G_SATELLITE_MAP.getName()==map.getCurrentMapType().getName())
	{
		document.getElementById('pestana_mapa').src=imgFolder+idioma+'pestana_mapa.gif';
		document.getElementById('pestana_satelite').src=imgFolder+idioma+'pestana_satelite_selec.gif';
		document.getElementById('pestana_mixto').src=imgFolder+idioma+'pestana_mixto.gif';
	}
	else
	{
		document.getElementById('pestana_mapa').src=imgFolder+idioma+'pestana_mapa.gif';
		document.getElementById('pestana_satelite').src=imgFolder+idioma+'pestana_satelite.gif';
		document.getElementById('pestana_mixto').src=imgFolder+idioma+'pestana_mixto_selec.gif';
	}
}

function desactivaRadioButtons()
{
	for (var i=0;i < document.formulario.elements.length;i++)
	{
		var elemento = document.formulario.elements[i];
		if (elemento.type == "radio" || elemento.type=="select-one")
		{
			elemento.disabled=true;
		}
	}


}
function activaRadioButtons()
{
	for (var i=0;i < document.formulario.elements.length;i++)
	{
		var elemento = document.formulario.elements[i];
		if (elemento.type == "radio" || elemento.type=="select-one")
		{
			elemento.disabled=false;
		}
	}


}

function abrirImagen(pk_rec) {

        var idioma = document.formulario.idioma.value;
        /*
        miPopup = window.open('./include/ficha.jsp?idioma='+idioma+'&pk_rec='+pk_rec,"_blank","width=900,height=700,toolbar=no,directories=no,menubar=no,scrollbars=yes,left=5,top=5");

		if (!miPopup.opener)
		{
        	miPopup.opener = self;
        }
        */
        loadDatosFicha('./include/imagen.jsp?idioma='+idioma+'&pk_rec='+pk_rec,pintaImagen);

}
function pintaImagen()
{
	if(req_ficha.readyState==4)
    {
    	// Comprobamos si ha habido exito
    	if(req_ficha.status==200)
    	{
			datosFicha = eval(req_ficha.responseText);
			var point=eval('new GLatLng'+datosFicha[0].punto);
  			efectoFicha(point,datosFicha[0].codigoHtml);
		}
		else
    	{
    		mostrarError("Problema al recoger los datos del bocadillo:\n"+req_ficha.statusText);
    	}
	}
}
function generaClusters(){
	clusterer_simplificado = new Clusterer(map,'clusterer_simplificado');
	var icon= new GIcon();
	icon.image="./img/icono_emp_instaladoras.png";
	icon.iconSize=new GSize(52,39);
	icon.iconAnchor=new GPoint(26,19);
	icon.shadow='';
	icon.shadowSize = new GSize( 0, 0 );
	icon.infoWindowAnchor = new GPoint( 26, 19 );
	icon.infoShadowAnchor = new GPoint( 0, 0);
	clusterer_simplificado.SetIcon(icon);
	clusterer_simplificado.SetMaxVisibleMarkers(1);

	clusterer_prosol = new Clusterer(map,'clusterer_prosol');
	var icon= new GIcon();
	icon.image="./img/icono_emp_instaladoras.png";
	icon.iconSize=new GSize(52,39);
	icon.iconAnchor=new GPoint(26,19);
	icon.shadow='';
	icon.shadowSize = new GSize( 0, 0 );
	icon.infoWindowAnchor = new GPoint( 26, 19 );
	icon.infoShadowAnchor = new GPoint( 0, 0);
	clusterer_prosol.SetIcon(icon);
	clusterer_prosol.SetMaxVisibleMarkers(1);

	clusterer_concesionarios = new Clusterer(map,'clusterer_concesionarios');
	var icon= new GIcon();
	icon.image="./img/icono_emp_concesionarios.png";
	icon.iconSize=new GSize(52,39);
	icon.iconAnchor=new GPoint(26,19);
	icon.shadow='';
	icon.shadowSize = new GSize( 0, 0 );
	icon.infoWindowAnchor = new GPoint( 26, 19 );
	icon.infoShadowAnchor = new GPoint( 0, 0);
	clusterer_concesionarios.SetIcon(icon);
	clusterer_concesionarios.SetMaxVisibleMarkers(1);
	
	clusterer_ventana = new Clusterer(map,'clusterer_ventana');
	var icon= new GIcon();
	icon.image="./img/icono_emp_instaladoras.png";
	icon.iconSize=new GSize(52,39);
	icon.iconAnchor=new GPoint(26,19);
	icon.shadow='';
	icon.shadowSize = new GSize( 0, 0 );
	icon.infoWindowAnchor = new GPoint( 26, 19 );
	icon.infoShadowAnchor = new GPoint( 0, 0);
	clusterer_ventana.SetIcon(icon);
	clusterer_ventana.SetMaxVisibleMarkers(1);
	
	clusterer_ilumina = new Clusterer(map,'clusterer_ilumina');
	var icon= new GIcon();
	icon.image="./img/icono_emp_instaladoras.png";
	icon.iconSize=new GSize(52,39);
	icon.iconAnchor=new GPoint(26,19);
	icon.shadow='';
	icon.shadowSize = new GSize( 0, 0 );
	icon.infoWindowAnchor = new GPoint( 26, 19 );
	icon.infoShadowAnchor = new GPoint( 0, 0);
	clusterer_ilumina.SetIcon(icon);
	clusterer_ilumina.SetMaxVisibleMarkers(1);
	
	clusterer_clima = new Clusterer(map,'clusterer_clima');
	var icon= new GIcon();
	icon.image="./img/icono_emp_instaladoras.png";
	icon.iconSize=new GSize(52,39);
	icon.iconAnchor=new GPoint(26,19);
	icon.shadow='';
	icon.shadowSize = new GSize( 0, 0 );
	icon.infoWindowAnchor = new GPoint( 26, 19 );
	icon.infoShadowAnchor = new GPoint( 0, 0);
	clusterer_clima.SetIcon(icon);
	clusterer_clima.SetMaxVisibleMarkers(1);
	
}
