// JavaScript Document

var nAct, nOldAct;
var aDivs;
var nDur, nDelay;
var nID1;
var nID2;
var nInt, nTime;

function Crossfader(divs, fadetime, delay){
	aDivs=divs; nAct = -1;
	for(var i=0;i<divs.length;i++)
	{
		document.getElementById(divs[i]).style.opacity = 0;
		document.getElementById(divs[i]).style.position = "absolute";
		document.getElementById(divs[i]).style.filter = "alpha(opacity=0)";
		document.getElementById(divs[i]).style.visibility = "hidden";
	}
	nDur=fadetime; nDelay=delay;
	newfade();
}

function newfade(){
	if(nID2){clearInterval(nID2);}
	nOldAct = nAct;
	nAct++;
	if(!aDivs[nAct]){nAct = 0;}
	if(nAct == nOldAct){return false;}
	
	document.getElementById(aDivs[nAct]).style.visibility = "visible";	
	nInt = 50; nTime = 0;
	nID2 = setInterval(function(){fade()}, nInt);
}

function fade(){
	nTime += nInt;
	var ieop = Math.round(easeInOut(nTime, 0, 1, nDur) * 100);
	var op = ieop / 100;
	document.getElementById(aDivs[nAct]).style.opacity = op;
	document.getElementById(aDivs[nAct]).style.filter = "alpha(opacity="+ieop+")";
	if(nOldAct > -1){
		document.getElementById(aDivs[nOldAct]).style.opacity = 1 - op;
		document.getElementById(aDivs[nOldAct]).style.filter = "alpha(opacity="+(100 - ieop)+")";
	}
	if(nTime == nDur){
		clearInterval(nID2);
		if(nOldAct > -1){document.getElementById(aDivs[nOldAct]).style.visibility = "hidden";}
	}
}

function next(){newfade();}

function previous(){
	if(nID1){clearInterval(nID1);}
	nOldAct = nAct;
	nAct--;
	if(!aDivs[nAct]){nAct = aDivs.length-1;}
	if(nAct == nOldAct){return false;}
	document.getElementById(aDivs[nAct]).style.visibility = "visible";	
	nInt = 50; nTime = 0;
	nID2 = setInterval(function(){fade()}, nInt);
}

function posicionum(pos){
	if(nID2){clearInterval(nID2);}
	nOldAct = nAct; nAct=pos;
	if(!aDivs[nAct]){nAct = 0;}
	if(nAct == nOldAct){return false;}
	document.getElementById(aDivs[nAct]).style.visibility = "visible";	
	nInt = 50; nTime = 0;
	nID2 = setInterval(function(){fade()}, nInt);
}

function easeInOut(t,b,c,d){return c/2 * (1 - Math.cos(Math.PI*t/d)) + b;}