// JavaScript Document

//funcion contra el spam
function antispamper(cuenta,dominio,clase)
{
	document.write("<a class='"+ clase +"' href=\"mailto:" + cuenta + "@" + dominio + "\">" + cuenta + "@" + dominio + "</a>");
}

function antispam(clase)
{
	var usuario = "info"
	var dominio = "friomago.com"
	document.write("<a class='" + clase + "' href=\"mailto:" + usuario + "@" + dominio + "\">" + usuario + "@" + dominio + "</a>")
}

function opcionmenu(idioma){
	if ((self.location.href == 'http://servidor.friomago/') || (self.location.href == 'http://www.friomago.com/nueva/') || (self.location.href == 'http://www.friomago.com/'))
	{
		var marcado='inicio';
	}
	else
	{
		var documento = self.location.href.match( /\/([^/]+)$/ )[1];
		marcado = documento.replace(".aspx","");
	}
	
	if (document.getElementById(marcado)!=null)
	{
		document.getElementById(marcado).className='on';
		document.getElementById(marcado).onmouseout=function(){}
	}
}

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;ln=en&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;ln=en&amp;key="+ kayarriba +"' type='text/javascript'></script>");
		}
	}
}

/*     GOOGLE MAP     */

function limpiar(idcapa){
	capalimpiar = document.getElementById(idcapa);
	capalimpiar.innerHTML="";
}

function imprimir(que) {
	var ventana = window.open("", "", "");
	var contenido = "<html><body onload='window.print();window.close();'>" + document.getElementById(que).innerHTML + "</body></html>";
	ventana.document.open();
	ventana.document.write(contenido);
	ventana.document.close();
}	

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-150)+"px";
	}
}
	
	var long = 36.778853;
	var lat = -2.803536;
	var zoom = 13;
    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 GLargeMapControl());
		map.setCenter(new GLatLng(36.778853,-2.803536), zoom);
		map.addControl(new GMapTypeControl());
		map.setMapType(G_HYBRID_MAP);
		
		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.778853,-2.803536);
		var marca = createMarker(punto,"<strong>Frio Mago S.L.<\/strong><br />C/ B&eacute;tica, n&ordm; 8, Pol&iacute;gono Ciavieja<br />04700 El Ejido (Almer&iacute;a)<br />Tel.:<strong>+34 902 19 85 90<\/strong><br />Fax:<strong>+34 950 48 68 32<\/strong>");
		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 se encuentra la direccion, revisela de nuevo incluyendo el municipio, la provincia y si es necesario el pais.");
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("No se encuentra la direccion, revisela de nuevo incluyendo el municipio, la provincia y si es necesario el pais.");
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("No se encuentra la direccion, revisela de nuevo incluyendo el municipio, la provincia y si es necesario el pais.");

		//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("No se encuentra la direccion, revisela de nuevo incluyendo el municipio, la provincia y si es necesario el pais.");

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("No se encuentra la direccion, revisela de nuevo incluyendo el municipio, la provincia y si es necesario el pais.");
	    
	   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...
	}