// JavaScript Document
function antispam(cuenta,dominio,clase)
{
	document.write("<a class='"+ clase +"' href=\"mailto:" + cuenta + "@" + dominio + "\">" + cuenta + "@" + dominio + "</a>");
}

function opcionmenu(idioma){
	if ((self.location.href == 'http://servidor.pinturasartes/') || (self.location.href == 'http://www.pinturasartes.com/nueva/') || (self.location.href == 'http://www.pinturasartes.com/'))
	{
		var marcado='empresa';
	}
	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(){}
	}
}

/* GOOGLE MAP*/


function googlekey(keyabajo,kayarriba,domabajo,domarriba,idioma){
	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;hl="+ idioma +"&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;hl="+ idioma +"&amp;key="+ kayarriba +"' type='text/javascript'></script>");
		}
	}
}

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;
	//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";
	}
}


function opcionmenu(idioma){
	if ((self.location.href == 'http://servidor.agroiris/'+ idioma + '/') || (self.location.href == 'http://www.agroiris.com/nueva/'+ idioma + '/') || (self.location.href == 'http://www.agroiris.com/'+ idioma + '/'))
	{
		var marcado='empresa';
	}
	else
	{
		var documento = self.location.href.match( /\/([^/]+)$/ )[1];
		marcado = documento.replace(".aspx","");
	}
	
	if (document.getElementById(marcado)!=null)
	{
		document.getElementById(marcado).className='capa_seccion_menu_on';
		document.getElementById(marcado).onmouseout=function(){}
	}
}
	var lengua;
	var long = 36.743515;
	var lat = -2.803295;
	var zoom = 15;
    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.743515,-2.803295), zoom);
		map.addControl(new GMapTypeControl());
		
		function createMarker(point,html,icono,bsnr) {
			var marker = new GMarker(point,icono);
			
			if (lengua == "es")
			{
				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 + ")" +
				 '"/>';
			}
			if (lengua == "en")
			{
				to_htmls[bsnr] = html + '<br><br>Get directions: <b>to here</b> - <a href="javascript:fromhere(' + bsnr + ')" class="enlace_mapa">from here</a>' +
				'<br>From:<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>Get directions: <a href="javascript:tohere(' + bsnr + ')" class="enlace_mapa">to here</a> - <b>from here</b>' +
				 '<br>To :<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 + ")" +
				 '"/>';
			}
			
			if (lengua == "de")
			{
				to_htmls[bsnr] = html + '<br><br>Wegbeschreibung: <b>bis hier</b> - <a href="javascript:fromhere(' + bsnr + ')" class="enlace_mapa">von hier aus</a>' +
				'<br>Von:<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>Wegbeschreibung: <a href="javascript:tohere(' + bsnr + ')" class="enlace_mapa">bis hier</a> - <b>von hier aus;</b>' +
				 '<br>Bis:<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;
				if (lengua == "es")
				{
					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>';
				}
				if (lengua == "en")
				{
					html = html + '<br><br>Destination: <a href="javascript:tohere('+ bsnr +')" class="enlace_mapa">to here</a> - <a href="javascript:fromhere('+ bsnr +')" class="enlace_mapa">from here</a>';
				}
				if (lengua == "de")
				{
					html = html + '<br><br>Wegbeschreibung: <a href="javascript:tohere('+ bsnr +')" class="enlace_mapa">bis hier</a> - <a href="javascript:fromhere('+ bsnr +')" class="enlace_mapa">von hier aus</a>';
				}
				
				return marker;
		}
		var punto = new GLatLng(36.743515,-2.803295);
		var marca = createMarker(punto,"<strong>S.A.T. Agroiris<\/strong><br />Carretera de Almerimar, Km 1<br />04700 El Ejido(Almer&iacute;a)<br />Tel.:<strong> +34 950 48 90 75<\/strong><br />Fax.:<strong> +34 950 48 12 61<\/strong>");
		GBrowserIsCompatible(marca);
		map.addOverlay(marca);
      }
    }
	
	function setDirections(fromAddress, toAddress) {
	  colocarventana();
	  desplegar("ventana",1);
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": lengua });
	  
    }

    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...
	}

/* FIN GOOGLE MAP */


function presentacion(seccion){
	var capa1=document.getElementById('capa_1');
	var capa2=document.getElementById('capa_2');
	var presentacion1=document.getElementById('presentacion_1');
	var presentacion2=document.getElementById('presentacion_2');
	if (seccion==1)
	{
		capa1.className='capa_on';
		capa2.className='capa_off';
		presentacion1.style.display='';
		presentacion2.style.display='none';
	}
	if (seccion==2)
	{
		capa1.className='capa_off';
		capa2.className='capa_on';
		presentacion1.style.display='none';
		presentacion2.style.display='';
	}
}