var midaX=null,midaY=null,margeX=16,margeY=16,utilX=null,utilY=null,imgX=null,imgY=null,clicX=null,clicY=null;
var cnt=0,step=15,delay=30,timer=0,timeout=0,fade=true,present=false,fadegris=true;
var incW=null,incH=null,incL=null,incT=null,novaL=null,novaT=null,actL=null,actT=null,actW=null,actH=null;
var capa=null,foto=null,nav=null,pict=null,info=null,gris=null,urlImg=null,tagTitle=null,actual=null,codi=-1;

function CreateXmlHttp() {
    try { // Provar IE
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Funcionarà amb JavaScript 5.0
    }
    catch(e) {
        try {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(oc) {  
            xmlHttp=null;
        }
    }
    // Si no se trataba de un IE, probamos con esto
    if(!xmlHttp && typeof XMLHttpRequest != "undefined")
    {
        xmlHttp=new XMLHttpRequest();
    }
    return xmlHttp;
}

function carregar() {
	CreateXmlHttp();
	var peticio=urlImg;
	xmlHttp.onreadystatechange=creaFoto;
	xmlHttp.open("GET",peticio,true);
	xmlHttp.send("");
}

function mides(evt) {
	capa=document.getElementById('Layer1');
	foto=document.getElementById('foto');
	info=document.getElementById('info');
	nav=document.getElementById('navegacio');
	gris=document.getElementById("Layer2");
	midaX=document.body.parentNode.clientWidth;
	midaY=document.body.parentNode.clientHeight;
	utilX=midaX-2*margeX;
	utilY=midaY-30-2*margeY;
	window.clearInterval(timer);
	if(capa.style.visibility=='visible') veure(evt,actual);
}

function veure(evt,ordre) {
	if(evt==null) {
		res();
	} else if(evt.type=="resize") {
		window.clearTimeout(timeout);
		midaGris();
	} else if(evt.type=="click" && !document.getElementById('pict')) {
		fadegris=true;
		capaGris(); // crear la capa per amagar la pàgina
		fade=true;
		actL=evt.clientX;
		actT=evt.clientY;
		actW=1;
		actH=1;
	} else {
		return;
	}
	iniciar(ordre);
	setCapa();
	if(utilX<imgX || utilY<imgY) escalar(imgX,imgY);
	posicio();
	if(document.getElementById('pict')) foraFoto(false);
	cnt=0;
	timer=setInterval("transicio()",delay);
}

function iniciar(ordre) {
	actual=ordre;
	codi=imgs[ordre][0];
	urlImg=imgs[ordre][1];
	imgX=imgs[ordre][2];
	imgY=imgs[ordre][3];
	tagTitle=imgs[ordre][4];
	vistes=imgs[ordre][5];
}

function setCapa() {
	info.style.display="none";
	capa.style.backgroundImage="url(../visuals/cargando.gif)";
	capa.style.backgroundRepeat="no-repeat";
	capa.style.backgroundPosition="center";
	capa.style.paddingBottom='6px';
	capa.style.opacity=1; // FF
	capa.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity=100)"; // IE
}

function escalar(X, Y) {
	escala=Math.max(X/(utilX),Y/(utilY));
	imgX=Math.round(X/escala);
	imgY=Math.round(Y/escala);
}

function posicio() {
	novaL=10+(utilX-imgX)/2;
	novaT=10+(utilY-imgY)/2;
	incW=(imgX-actW)/step;
	incH=(parseInt(imgY)-actH)/step;
	incL=(novaL-actL)/step;
	incT=(novaT-actT)/step;
}

function transicio() {
	actW=parseFloat(actW)+parseFloat(incW);
	actH=parseFloat(actH)+parseFloat(incH);
	actL=parseFloat(actL)+parseFloat(incL);
	actT=parseFloat(actT)+parseFloat(incT);
	capa.style.width=Math.round(actW)+"px";
	capa.style.height=Math.round(actH)+"px";
	capa.style.left=Math.round(actL)+"px";
	capa.style.top=Math.round(actT)+"px";
	capa.style.visibility='visible';
	cnt++;
	if (fadegris) gris.style.opacity=cnt/(2*step); // FF
	if (fadegris) gris.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="+100*cnt/(2*step)+")"; // IE
	if (cnt==step) {
		fadegris=false;
		clearInterval(timer);
		carregar();
	}
}

function creaFoto() {
    if(xmlHttp.readyState==4 && xmlHttp.status==200) {
		capa.style.backgroundImage="none";
		try {
			foto.removeChild(pict);
		} catch(e) {
			res();
		}
		pict = document.createElement("img");
		pict.setAttribute("id","pict");
		pict.setAttribute("src",urlImg);
		pict.setAttribute("width",imgX);
		pict.setAttribute("height",imgY);
		if(present) timeout=window.setTimeout("seguent()",5000);
		foto.style.opacity=0; // FF
		foto.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity=1)"; // IE
		foto.appendChild(pict);
		cnt=1;
		if(fade) timer=setInterval("fadein()",15);
    }
}

function fadein() {
	cnt++;
	foto.style.opacity=cnt/step; // FF
	foto.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="+100*cnt/step + ")"; // IE
	if(cnt==step) {
		clearInterval(timer);
		nav.style.display='block';
		txtInfo=document.createTextNode(parseInt(actual)+1+"/"+imgs.length+' - '+imgs[actual][4]);
		info.appendChild(txtInfo);
		info.style.display="block";
		capa.style.paddingBottom='0px';
		capa.style.height='auto';
		tanca = document.getElementById('aclose');
		tanca.focus();
		if (imgs[actual][1].indexOf('material') != -1) {
			var peticio = "../gestio/comptadors.php?codi=" + codi;
			xmlHttp.onreadystatechange = res;
			xmlHttp.open("GET", peticio, true);
			xmlHttp.send("");
		}
	}
}

function fadeout() {
	cnt--;
	capa.style.opacity=cnt/step; // FF
	capa.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="+parseInt(100*cnt/step) + ")"; // IE
	gris.style.opacity=cnt/(2*step); // FF
	gris.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="+parseInt(100*cnt/(2*step)) + ")"; // IE
	if(cnt==0) {
		clearInterval(timer);
		foraFoto(true);
		gris.style.display="none";
	}
}

function foraFoto(amaga) {
	pict.setAttribute('src','../visuals/spacer.gif');
	try {
		info.removeChild(txtInfo);
	} catch(e) {
		res();
	}
	nav.style.display="none";
	if(amaga) {
		foto.removeChild(pict);
		capa.style.width="1px";
		capa.style.height="1px";
		capa.style.visibility='hidden';
	}
}

function sortir() {
	cnt=step;
	timer=setInterval("fadeout()",delay);
	atura();
}

function res() {
}

function capaGris() {
	midaGris();
	gris.style.opacity=0; // FF
	gris.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity=1)"; // IE
	gris.style.display="block";
}

function midaGris() {
	gris.style.width=midaX+"px";
	gris.style.height=midaY+"px";
	gris.style.left="0px";
	gris.style.top="0px";
}

function primera() {
	window.clearTimeout(timeout);
	if(actual>0) veure(null,0);
}

function anterior() {
	window.clearTimeout(timeout);
	if(actual>0) {
		actual--;
		veure(null,actual);
	}
}

function seguent() {
	window.clearTimeout(timeout);
	if (actual<imgs.length-1) {
		actual++;
		veure(null,actual);
	} else {
		atura();
	}
}

function darrera() {
	window.clearTimeout(timeout);
	if (actual<imgs.length-1) veure(null,imgs.length-1);
}

function presenta() {
	present=true;
	imgCtrl=document.getElementById('ctrl');
	imgCtrl.setAttribute('src','../visuals/nav_pause.gif');
	imgCtrl.setAttribute('title','Aturar la presentació');
	lnkCtrl=document.getElementById('actrl');
	lnkCtrl.setAttribute('onclick','atura()');
	timeout=window.setTimeout("seguent()",1000);}

function atura() {
	present=false;
	imgCtrl=document.getElementById('ctrl');
	imgCtrl.setAttribute('src','../visuals/nav_play.gif');
	imgCtrl.setAttribute('title','Iniciar la presentació');
	lnkCtrl=document.getElementById('actrl');
	lnkCtrl.setAttribute('onclick','presenta()');
	window.clearTimeout(timeout);
}
