// JavaScript Document

function traduce(idioma) {
//	location.href = location.href.substring(0, location.href.lastIndexOf('_')) + '_' + idioma + '.html';
//	alert('Disculpe las molestias; esta web a\372n no est\341 disponible en castellano.');
	if(location.href.indexOf('portal') == -1) {
		if(idioma == 'es_ES')
			location.href = location.href.replace(/ca_ES/g, idioma);
		else
			location.href = location.href.replace(/es_ES/g, idioma);
	}
	else {
		if(idioma == 'es_ES')
			location.href = location.href + '&lang=es';
		else
			location.href = location.href + '&lang=ca';
	}
}

function init() {
	if(document.getElementById('wrapperDiapositivas'))
		initDiapos();
}

var preDeslizamiento = 1000;
var anchoFoto = 775;
var diapoActual = 1;

var matrizImagenes = new Array();

function precargaDiapos() {
	if (typeof numDiapos == "undefined")
		return;
	for(var i = 1; i < numDiapos + 1; i++) {
		matrizImagenes[i] = new Image();
		matrizImagenes[i].src = 'http://www.antfacilities.com/site/img/fotos' + apartado + i + '.jpg';
		matrizImagenes[i].onerror = function(evt) {
			matrizImagenes[i] = new Image();
			matrizImagenes[i].src = 'http://www.antfacilities.com/site/img/fotos' + apartado + i + '.png';
		//	alert(matrizImagenes[i].src);
		}
	}
}

function initDiapos() {
	
	precargaDiapos();
	
	if(location.href.indexOf('portal') != -1) {
		
		if(apartado == 'Noticias') {
			document.getElementById('diapos_a').src = 'http://www.antfacilities.com/site/img/fotos' + apartado + '1.jpg';
			if(idioma == 'es')
				document.getElementById('marcaDeAgua').src = 'http://www.antfacilities.com/site/img/marca-de-agua-noticias-es_ES.png';
			else
				document.getElementById('marcaDeAgua').src = 'http://www.antfacilities.com/site/img/marca-de-agua-noticias-ca_ES.png';
			setTimeout('$("#wrapperDiapositivas").fadeOut("slow")', 1500);
			if(location.href.indexOf('limitstart') != -1) {
				document.getElementById('wrapperDiapositivas').style.display = 'none';
			}
			return;
		}
		
		if(apartado == 'Noticia') {
			document.getElementById('wrapperDiapositivas').style.display = 'none';
			return;
		}
		
		if(typeof( window[ 'apartado' ] ) == "undefined") {
			document.getElementById('wrapperDiapositivas').style.display = 'none';
			return;
		}
		
		if(apartado == 'Clientes') {
			document.getElementById('diapos_a').src = 'http://www.antfacilities.com/site/http://www.antfacilities.com/site/img/fotos' + apartado + '1.jpg';
			if(idioma == 'es')
				document.getElementById('marcaDeAgua').src = 'http://www.antfacilities.com/site/img/marca-de-agua-clientes-es_ES.png';
			else
				document.getElementById('marcaDeAgua').src = 'http://www.antfacilities.com/site/img/marca-de-agua-clientes-ca_ES.png';
			
			if(location.href.indexOf('limitstart') != -1) {
				document.getElementById('wrapperDiapositivas').style.marginLeft = '431px';
				document.getElementById('wrapperDiapositivas').style.background = '#dbdbdb';
				setTimeout('pasaDiapos()', 1000);
				redimensiona();
			}
		}
		
	}
	
	document.getElementById('diapos_a').src = 'http://www.antfacilities.com/site/img/fotos' + apartado + '1.jpg';
	if(apartado != 'Trabajo' && apartado != 'Presupuesto') {
		setTimeout('deslizaDiapos()', preDeslizamiento);
		setTimeout('ocultaMarcaDeAgua();', preDeslizamiento);
	}
	if(apartado == 'Trabajo' || apartado == 'Presupuesto' || apartado == 'Suscripcion' || apartado == 'Sugerencias') {
	/*	var f = document.getElementById('wrapperDiapositivas');
		var anchoContenido = document.getElementById('wrapperContenido').scrollWidth;
		anchoContenido = parseInt(anchoContenido);
		f.style.marginLeft = '430px';
		if((findPosX(f) + document.getElementById('diapos_a').width) > anchoContenido) {
			f.style.width = anchoContenido - findPosX(f) + 'px';
		}*/
		document.getElementById('wrapperDiapositivas').style.marginLeft = '430px';
		document.getElementById('wrapperDiapositivas').style.background = '#dbdbdb';
		setTimeout('pasaDiapos()', 1000);
		redimensiona();
	}
	
	if(apartado == 'AvisoLegal') {
		document.getElementById('wrapperDiapositivas').style.marginLeft = '500px';
		document.getElementById('wrapperDiapositivas').style.background = '#dbdbdb';
	//	setTimeout('pasaDiapos()', 1000);
		redimensiona();
	}
	if(document.getElementById('wrapperLemaSobreDiapos'))
		setTimeout('muestraLemaSobreDiapos();', preDeslizamiento);
}

function deslizaDiapos() {
	var paso = 10;
	var tiempo = 25;
	
	var anchoContenido = document.getElementById('wrapperContenido').scrollWidth;
	anchoContenido = parseInt(anchoContenido);
	
	if(document.getElementById('wrapperDiapositivas')) {
		var f = document.getElementById('wrapperDiapositivas');
		
		if(f.style.marginLeft == '')
			f.style.marginLeft = '0px';
		
		var margen = f.style.marginLeft.substring(0, f.style.marginLeft.length - 2);
		margen = parseInt(margen);
		
		if((findPosX(f) + anchoFoto + paso) > anchoContenido) {
			f.style.width = anchoContenido - findPosX(f) - paso + 'px';
		}
		
		if(apartado != 'Clientes' && apartado != 'Limpieza') {
			if(margen == 290) {
				margen = 296;
				f.style.marginLeft = margen + 'px';
				f.style.width = anchoContenido - findPosX(f) + 'px';
				if(numDiapos > 1)
				setTimeout('pasaDiapos()', 1000);
			}
		
			if(margen < 290) {
				margen = margen + paso;
				f.style.marginLeft = margen + 'px';
				f.style.background = 'none';
				setTimeout('deslizaDiapos()', tiempo);
			}
		}
		
		if(apartado == 'Clientes') {
			if(margen == 420) {
				margen = 431;
				f.style.marginLeft = margen + 'px';
				f.style.width = anchoContenido - findPosX(f) + 'px';
				if(numDiapos > 1)
				setTimeout('pasaDiapos()', 1000);
			}
		
			if(margen < 420) {
				margen = margen + paso;
				f.style.marginLeft = margen + 'px';
				f.style.background = 'none';
				setTimeout('deslizaDiapos()', tiempo);
			}
		}
		
		if(apartado == 'Limpieza') {
			if(margen == 310) {
				margen = 320;
				f.style.marginLeft = margen + 'px';
				f.style.width = anchoContenido - findPosX(f) + 'px';
				if(numDiapos > 1)
				setTimeout('pasaDiapos()', 1000);
			}
		
			if(margen < 310) {
				margen = margen + paso;
				f.style.marginLeft = margen + 'px';
				f.style.background = 'none';
				setTimeout('deslizaDiapos()', tiempo);
			}
		}
	}
}

var siguienteDiapo = 1;
var rutaNuevaImagen = '';

function pasaDiapos() {
	var siguienteDiapo = 1;
	if(diapoActual < numDiapos)
		siguienteDiapo = diapoActual + 1;
	
	var diapo_a_ocultar = document.getElementById('diapos_b');
	var diapo_a_mostrar = document.getElementById('diapos_a');
	
	if(diapoActual % 2 != 0) {
		diapo_a_ocultar = document.getElementById('diapos_a');
		diapo_a_mostrar = document.getElementById('diapos_b');
	}
	
	rutaNuevaImagen = 'http://www.antfacilities.com/site/img/fotos' + apartado + siguienteDiapo + '.jpg';
	
	diapo_a_mostrar.src = rutaNuevaImagen;
	diapo_a_mostrar.onerror = function(evt) {
		diapo_a_mostrar.src = 'http://www.antfacilities.com/site/img/fotos' + apartado + siguienteDiapo + '.png';
	}
	diapoActual = siguienteDiapo;
	
	ocultaDiapo(diapo_a_ocultar);
	muestraDiapo(diapo_a_mostrar);
	
//	alert(diapo_a_mostrar.src);

	if((siguienteDiapo + 1) <= numDiapos) {
		var pruebaImagen = new Image(10,10);
		pruebaImagen.src = 'http://www.antfacilities.com/site/img/fotos' + apartado + siguienteDiapo + 1 + '.png';
		pruebaImagen.src = 'http://www.antfacilities.com/site/img/fotos' + apartado + siguienteDiapo + 1 + '.jpg';
		// Comentado, pues causaba un error de 'stack overflow' en IE
	/*	pruebaImagen.onerror = function(evt) {
			pruebaImagen.src = 'http://www.antfacilities.com/site/img/fotos' + apartado + siguienteDiapo + 1 + '.png';
		}*/
	}
	
	setTimeout('pasaDiapos()', 3000);
}

function ocultaDiapo(laDiapo) {
	if(laDiapo.id.indexOf('diapos_a') != -1)
		$("#diapos_a").fadeOut("slow");
	else
		$("#diapos_b").fadeOut("slow");
}

function muestraDiapo(laDiapo) {
	if(laDiapo.id.indexOf('diapos_a') != -1)
		$("#diapos_a").fadeIn("slow");
	else
		$("#diapos_b").fadeIn("slow");
}

function ocultaMarcaDeAgua() {
	$("#marcaDeAgua").fadeOut("slow");
}

function muestraLemaSobreDiapos() {
//	$("#lemaSobreDiapos").fadeIn("slow");
	$("#wrapperLemaSobreDiapos").fadeIn("slow");
}

function findPosX(obj) {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}


function redimensiona() {
	var f = document.getElementById('wrapperDiapositivas');
	if(!f.style.width)
		f.style.width = document.getElementById('wrapperContenido').scrollWidth - findPosX(f) + 'px';
		
	var anchoActual = f.style.width.substring(0, f.style.width.length - 2);
	anchoActual = parseInt(anchoActual);
	
	var anchoContenido = document.getElementById('wrapperContenido').scrollWidth;
	anchoContenido = parseInt(anchoContenido);
	
	if(anchoActual < document.getElementById('diapos_a').width) {
		f.style.width = anchoContenido - findPosX(f) + 'px';
		if(anchoContenido - findPosX(f) > document.getElementById('diapos_a').width) {
			f.style.width = document.getElementById('diapos_a').width + 'px';
		}
	}
		
/*	if(anchoActual < anchoFoto) {
		f.style.width = anchoContenido - findPosX(f) + 'px';
		if(anchoContenido - findPosX(f) > anchoFoto)
			f.style.width = anchoFoto + 'px';
	}*/
}




























