var mapa;
var mapas = new Array();
var str_mapas = new Array();
var centrox = 42.880516;
var centroy = -8.544810;	
var zoom = 14;

var sidebar_table="";
var geocoder = null;

function aleatorio(inferior,superior){ 
	numPosibilidades = superior - inferior 
	aleat = Math.random() * numPosibilidades 
	aleat = Math.round(aleat) 
	return parseInt(inferior) + aleat 
} 

function mapaPosiciones(pos)
{
	if(!mapas[pos])
	{
		var str_mapa = str_mapas[pos] + '?' + aleatorio(1,10000).toString()
//		alert('cargando posiciones: ' + str_mapa)
		if(str_mapa)
			mapas[pos] = new GGeoXml(str_mapa);
	}
	return mapas[pos];
}

function marcoGoogle(nick, lat, longit, icono, infoHtml)
{
	var point = new GLatLng(lat, longit);
	var marker = new GMarker(point, icono);

	
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(infoHtml);
	});
	
	return marker;

}


function TextualZoomControl() {
    }
    
    TextualZoomControl.prototype = new GControl();

    TextualZoomControl.prototype.initialize = function(mapa) {
		
		var container = document.createElement("div");
		var zoomInDiv = document.createElement("div");
		var texto = CoordenadasGPS+' '+mapa.getCenter();
		this.setButtonStyle_(zoomInDiv);
		container.appendChild(zoomInDiv);
		
		zoomInDiv.appendChild(document.createTextNode(texto));
		mapa.getContainer().appendChild(container);
		return container;
     }
     
     
     TextualZoomControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(60,6));
	 }
	 
	 TextualZoomControl.prototype.setButtonStyle_ = function(button) {
		button.style.textDecoration = "underline";
		button.style.color = "#000000";
		button.style.fontWeight = "bold";
		button.style.backgroundColor = "white";
		button.style.font = "small Arial";
		button.style.fontSize = "11px";
		button.style.border = "1px solid black";
		button.style.padding = "2px";
		button.style.marginBottom = "3px";
		button.style.textAlign = "center";
		button.style.width = "20em";
		button.style.cursor = "pointer";
    }
    
    
    
function inicia_mapa(nombre, x, y, icono, infoHtml)
{
//	if (GBrowserIsCompatible()) 
//	{
		mapa = new GMap2(document.getElementById("map"));
		mapa.addControl(new GSmallMapControl());
		mapa.addControl(new GMapTypeControl());
		mapa.setCenter(new GLatLng(x, y), 14);

	    var icon = new GIcon();
	    icon.image = icono;
    //	icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	    icon.iconSize = new GSize(24, 24);
	    icon.shadowSize = new GSize(22, 20);
	    icon.iconAnchor = new GPoint(6, 20);
	    icon.infoWindowAnchor = new GPoint(5, 1);

		mapa.addOverlay(marcoGoogle(nombre, x, y, icon, infoHtml));
		
		
//	}
//	else {
//		alert("Lo sentimos, la API Google Maps no es compatible con su navegador");
//	}
}   

function inicia_mapa2(nombre, x, y, z, icono, infoHtml,CoordenadasGPS)
{
//	if (GBrowserIsCompatible()) 
//	{
		if ((x!=0)&&(y!=0))
		{
				centrox = x
				centroy = y
		}
		
		zoom = z
	

		if(!mapa)
		{
//			alert('iniciando mapa');
			mapa = new GMap2(document.getElementById("map"));
			mapa.setCenter(new GLatLng(centrox, centroy), zoom); 
			mapa.addControl(new GMapTypeControl());
//			mapa.addControl(new GLargeMapControl());
			mapa.addControl(new GSmallMapControl());
//			mapa.addControl(new GZoomControl());
			mapa.addControl(new GMapTypeControl());
			if((x!=0) && (y!=0)){
				mapa.addControl(new TextualZoomControl());
			}
		}

		mapa.setCenter(new GLatLng(centrox, centroy), zoom); 
		
		if ((x!=0)&&(y!=0))
		{
			if(!geocoder)
				geocoder = new GClientGeocoder();
				
			var icon = new GIcon();
			icon.image = icono;
		//	icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
			icon.iconSize = new GSize(24, 24);
			icon.shadowSize = new GSize(22, 20);
			icon.iconAnchor = new GPoint(6, 20);
			icon.infoWindowAnchor = new GPoint(5, 1);

			mapa.addOverlay(marcoGoogle(nombre, centrox, centroy, icon, infoHtml));
				
		}
		//var d=new GMarker(new GLatLng(42.880886, -8.548179)).getPoint().distanceFrom(new GMarker(new GLatLng(42.880461, -8.548179)).getPoint())/1000;
		//alert(d.toFixed(5));

		// if (document.getElementById("address").value != "")
		// {
			// showAddress(document.getElementById("address").value+ ' santiago de compostela'); 
		// }
//	}
//	// display a warning if the browser was not compatible
//	else {
//		alert("Lo sentimos, la API Google Maps no es compatible con su navegador");
//	}
}   


function inicia_mapa3(nombre, x, y, z, icono, infoHtml)
{

//	if (GBrowserIsCompatible()) 
//	{
		mapa = new GMap2(document.getElementById("map"));
		mapa.addControl(new GSmallMapControl());
		mapa.setCenter(new GLatLng(x, y), z);

	    var icon = new GIcon();
	    icon.image = icono;
    //	icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	    icon.iconSize = new GSize(24, 24);
	    icon.shadowSize = new GSize(22, 20);
	    icon.iconAnchor = new GPoint(6, 20);
	    icon.infoWindowAnchor = new GPoint(5, 1);

		mapa.addOverlay(marcoGoogle(nombre, x, y, icon, infoHtml));
		
		
//	}
//	else {
//		alert("Lo sentimos, la API Google Maps no es compatible con su navegador");
//	}
}

function inicia_mapa4(nombre, x, y, z, icono, infoHtml,nombrediv)
{

//	if (GBrowserIsCompatible()) 
//	{

		mapa = new GMap2(document.getElementById(nombrediv));
		mapa.addControl(new GSmallMapControl());
		mapa.setCenter(new GLatLng(x, y), z);

	    var icon = new GIcon();
	    icon.image = icono;
    //	icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	    icon.iconSize = new GSize(24, 24);
	    icon.shadowSize = new GSize(22, 20);
	    icon.iconAnchor = new GPoint(6, 20);
	    icon.infoWindowAnchor = new GPoint(5, 1);

		mapa.addOverlay(marcoGoogle(nombre, x, y, icon, infoHtml));
		
		
//	}
//	else {
//		alert("Lo sentimos, la API Google Maps no es compatible con su navegador");
//	}
}


function inicia_mapa5(nombre, x, y, z, icono, infoHtml,nombrediv)
{

//	if (GBrowserIsCompatible()) 
//	{

		mapa = new GMap2(document.getElementById(nombrediv));
		mapa.addControl(new GSmallMapControl());
		mapa.setCenter(new GLatLng(x, y), z);

	    var icon = new GIcon();
	    icon.image = icono;
    //	icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	    icon.iconSize = new GSize(12, 12);
	    icon.shadowSize = new GSize(22, 20);
	    icon.iconAnchor = new GPoint(6, 20);
	    icon.infoWindowAnchor = new GPoint(5, 1);

		mapa.addOverlay(marcoGoogle(nombre, x, y, icon, infoHtml));
		
		
//	}
//	else {
//		alert("Lo sentimos, la API Google Maps no es compatible con su navegador");
//	}
}

function muestraruta(coords)
{

        var path = coords.split(" ");
	   
          
	    // Is this the polyline?
	    if (path.length > 2) {
		    var points = [];
		    for (var p=0; p<path.length-1; p++) {
		        var bits = path[p].split(",");
		        var point = new GPoint(parseFloat(bits[0]),parseFloat(bits[1]));
		        points[p] = point;
		        
		    }
		    mapa.addOverlay(new GPolyline(points, "#ff0000", 5, 0.7));
		   // mapa.setCenter(new GLatLng(bits[1], bits[0]), zoom); 
		    
	    }
}

function cambiaKML(chk,puntos)
{	
//	if (chk.checked)
//		mapa.addOverlay(puntos);
//	else
//		mapa.removeOverlay(puntos);	

	mapa.addOverlay(puntos);
	if (chk.checked)
		puntos.show();//mapa.addOverlay(puntos);
	else
		puntos.hide();//mapa.removeOverlay(puntos);
}

function buscaDireccion(infoHTML)
{
	var ciudad = "santiago de compostela";
	var calle = document.getElementById("address").value;
	var numero = document.getElementById("numero").value;
	
	if(calle || numero)
		showAddress(ciudad, calle, numero, infoHTML)
}

function showAddress(ciudad, calle, numero, infoHTML) 
{
//	alert('buscando dirección: ' + address);
	var address = ciudad + " " + calle + " " + numero;
	if (geocoder) {
		geocoder.getLatLng(
			address ,
			function(point) {
				if (!point) {
					alert(address + " no encontrada");
				} else {
					mapa.setCenter(point, 16);
					var icon = new GIcon();
					icon.image = "http://mapa.santiagoturismo.com/imagenes/planoweb/icons/estrella.gif";
					icon.iconSize = new GSize(32, 32);
					icon.iconAnchor = new GPoint(15,30);
					icon.infoWindowAnchor = new GPoint(5, 1);
					var marker = new GMarker(point, icon);
					mapa.addOverlay(marker);
					centro = point.toString().replace("(","").replace(")","").split(",");
					centrox = centro[0];
					centroy = centro[1];
					
					if(calle)
					{
						GEvent.addListener(marker, "click", function() {
							marker.openInfoWindowHtml(infoHTML + address);
						});
					}
					
					//marker.openInfoWindowHtml(address);
				}
			}
		);
	}
}

function urlAbrirListado(categoria,centrox,centroy)
{
	window.location.href = "abrirlistado.asp?categoria=" + categoria +"&x=" + centrox + "&y=" + centroy +"&address=" + document.getElementById('address').value + "&numero=" + document.getElementById('numero').value + "&idioma=" + document.getElementById('idioma').value;
}

function prepararimpresion()
{
	/////////////////////////////////////////////////////////////////
	//// PARA IMPRIMIR LOS ICONOS
	//// Cada vez que añado o quito una capa.
	////
	//// recupero los elementos gmarker que tienen clase gmnoprint (esta clase tiene display:none)
	////// y sobreescribo los estilos display y border
		var marks = mapa.getPane(G_MAP_MARKER_PANE).childNodes; 
		for (var i=0; i<marks.length; i++) { 
		if (marks[i].className == "gmnoprint") {
			marks[i].style.display = "block"
			marks[i].style.border = "none"
		} 
		}
	//// recupero las sombras de los elementos gmarker 
	//// y sobreescribo el estilo display para que no las imprima
		var shadows = mapa.getPane(G_MAP_MARKER_SHADOW_PANE).childNodes;   
		for (var i=0; i<shadows.length; i++) { 
			shadows[i].style.display= "none"
		} 
	/////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////
	return true;
}	

