// JavaScript Document
function antispam(cuenta,dominio,clase){
	document.write("<a class='"+ clase +"' href=\"mailto:" + cuenta + "@" + dominio + "\">" + cuenta + "@" + dominio + "</a>");
}

function googlekey(keyabajo,kayarriba,domabajo,domarriba){
	var documento = self.location.href.match( /\/([^/]+)$/ )[1];
	if (document.location.href == 'http://servidor.'+ domabajo +'/'+ documento)
	{
		document.write("<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key="+ keyabajo +"' type='text/javascript'></script>");
	}
	else
	{
		if (document.location.href == 'http://'+ domarriba +'/'+ documento);
		{
			document.write("<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key="+ kayarriba +"' type='text/javascript'></script>");
		}
	}
}

function aparecer(){
	if (document.getElementById('capanota').className=='capaon')
	{
		document.getElementById('capanota').className='capaoff';
		document.getElementById('capanota').style.display='none';
	}
	else
	{
		document.getElementById('capanota').className='capaon';
		document.getElementById('capanota').style.display='';
	}
}

function opcionmenu(){
	if ((self.location.href == 'http://servidor.novaimagen/') || (self.location.href == 'http://www.novaimagen.com/nueva/') || (self.location.href == 'http://www.novaimagen.com/'))
	{	
		var marcado=null;
	}
	else
	{
		var documento = self.location.href.match( /\/([^/]+)$/ )[1];
		marcado = documento.replace(".aspx","");
	}
	
	if (document.getElementById(marcado)!=null)
	{
		document.getElementById(marcado).src='./imagenes/logo' + marcado + 'on.gif';
	}
}

function intercambiar(producto,productos){
	for (i=1;i<=productos;i++){
		if (i==producto){
			document.getElementById('capa_' + i).style.display='';
		}
		else {
			document.getElementById('capa_' + i).style.display='none';
		}
	}
}

/*     GOOGLE MAP     */

function limpiar(idcapa){
	capalimpiar = document.getElementById(idcapa);
	capalimpiar.innerHTML="";
}

function colocarventana(){
	var puntoprincipal = document.getElementById("capa_pagina");
	//extraemos la posicion de la capa capa_pagina
	ejex = puntoprincipal.offsetLeft;
	ejex=ejex+630;
	//colocamos la capa ventana en la posicion x de la capa pagina
	var posicionarcapa = document.getElementById("ventana");
	posicionarcapa.style.left=ejex+"px";
	posicionarcapa.style.top="25px";
}

function desplegar(nombre,cerrar){
	var capacerrar;
	capacerrar = document.getElementById(nombre);
	if (cerrar == 0){
		capacerrar.style.display = "none";
	}
	else
	{
		capacerrar.style.display = '';
	}
}


// funciones arrastrar capa
var capa = null;
var _IE_ = navigator.userAgent.indexOf("MSIE") != -1;

function liberaCapa() {
	capa = null;
}

function clickCapa(e, obj) {
	capa = obj.parentNode;
	if (_IE_) {
		difX = e.offsetX;
		difY = e.offsetY;
	} else {
		difX = e.layerX;
		difY = e.layerY;
	}
}

function mueveCapa(e) {
	if (capa != null) {
		capa.style.top = (e.clientY-difY)+"px";
		capa.style.left = (e.clientX-difX)+"px";
	}
}


	
	var long = 36.773671;
	var lat = -2.811996;
	var zoom = 16;
    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;
	var to_htmls = [];
	var from_htmls = [];
	var gmarkers = [];
	var htmls = [];
	
	function tohere(i) {
	   gmarkers[i].openInfoWindowHtml(to_htmls[i]);
	}
	function fromhere(i) {
	   gmarkers[i].openInfoWindowHtml(from_htmls[i]);
	}
	
	
    function onLoad() {
		
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map"));
		
        gdir = new GDirections(map, document.getElementById("indicaciones"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
		
		map.addControl(new GSmallMapControl());
		map.setCenter(new GLatLng(36.775571,-2.811996), zoom);
		map.addControl(new GMapTypeControl());
		
		function createMarker(point,html,icono,bsnr) {
			var marker = new GMarker(point,icono);
			to_htmls[bsnr] = html + '<br><br>Obtener indicaciones: <b>hasta aqu&iacute;</b> - <a href="javascript:fromhere(' + bsnr + ')" class="enlace_mapa">desde aqu&iacute;</a>' +
			'<br>Lugar de partida:<form action="#" onsubmit="setDirections(this.from.value, this.to.value); return false">' +
			'<input class="caja_mapa" type="text" SIZE=40 MAXLENGTH=40 name="from" id="fromAddress" value="" /><br>' +
			'<INPUT class="boton_mapa" value="OK" TYPE="SUBMIT">' +
			'<input type="hidden" id="toAddress" name="to" value="' + long + ',' + lat +
				  // "(" + name + ")" +
			 '"/>';
			
			from_htmls[bsnr] = html + '<br><br>Obtener indicaciones: <a href="javascript:tohere(' + bsnr + ')" class="enlace_mapa">hasta aqu&iacute;</a> - <b>desde aqu&iacute;</b>' +
			 '<br>Destino:<form action="#" onsubmit="setDirections(this.from.value, this.to.value); return false">' +
			 '<input class="caja_mapa" type="text" SIZE=40 MAXLENGTH=40 name="to" id="toAddress" value="" /><br>' +
			 '<INPUT class="boton_mapa" value="OK" TYPE="SUBMIT">' +
			 '<input type="hidden" id="fromAddress" name="from" value="' + long + ',' + lat +
				  // "(" + name + ")" +
			 '"/>';
			
				html = "<div>"+ html +"<\/div>";			
				
				GEvent.addListener(marker, "click", function() {
					if (map.getZoom()<17){
					map.setCenter(point, 17);
					}
				});
				
				GEvent.addListener(marker, "mouseover", function() {
					marker.openInfoWindow(html);
				});
				
				GEvent.addListener(marker, "dblclick", function() {
					if (map.getZoom()<17){
						map.setCenter(point, 17);
					}
				});
				gmarkers[bsnr] = marker;
				htmls[bsnr] = html;
				html = html + '<br><br>Obtener indicaciones: <a href="javascript:tohere('+ bsnr +')" class="enlace_mapa">hasta aqu&iacute;</a> - <a href="javascript:fromhere('+ bsnr +')" class="enlace_mapa">desde aqu&iacute;</a>';
				return marker;
		}
		var punto = new GLatLng(36.773749,-2.812071);
		var marca = createMarker(punto,"<img style='padding:0 0 0 40px;' src='imagenes/localizacion.jpg' alt=''>");
		GBrowserIsCompatible(marca);
		map.addOverlay(marca);
      }
    }
	
	function setDirections(fromAddress, toAddress) {
	  colocarventana();
	  desplegar("ventana",1);
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": "es" });
	  
    }

    function handleErrors(){
		//para cerrar la ventana si da un error al buscar el lugar
	   desplegar("ventana",0);
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found fort that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

		//else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
		//alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("No se encuentra la ruta o no se puede realizar en coche.");
	   
	}

	function onGDirectionsLoad(){
      // Use this function to access information about the latest load()
      // results.
      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	}
	
