/*****

Multiple Image Cross Fade Redux
Version 1.0
Last revision: 05.07.2007
by macuarium.com

Rewrited from:

Image Cross Fade Redux
Version 1.0
Last revision: 02.15.2006
steve@slayeroffice.com

Rewrite of old code found here: http://slayeroffice.com/code/imageCrossFade/index.html


*****/


window.addEventListener?window.addEventListener("load",so_init,false):window.attachEvent("onload",so_init);

var d=document, capas = new Array(), zInterval = null, current=0, pause=false;

var numerocapas = 1;

function so_init() {
	if(!d.getElementById || !d.createElement)return;
	
	css = d.createElement("link");
	css.setAttribute("href","http://slayeroffice.com/code/imageCrossFade/xfade2.css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	d.getElementsByTagName("head")[0].appendChild(css);
	
	for (a = 0;a < numerocapas;a++){
		capas[a] = new Array ();
		capas[a]["imgs"] = new Array ();
		capas[a]["imgs"] = d.getElementById("imageContainer" + (a + 1)).getElementsByTagName("img");
		capas[a]["currentimg"] = 0;
		for(i=1;i<capas[a]["imgs"].length;i++) capas[a]["imgs"][i].xOpacity = 0;
		capas[a]["imgs"][0].style.display = "block";
		capas[a]["imgs"][0].xOpacity = .99;
		
		capas[a]["thistimeout"] = setTimeout("so_xfade (" + a + ")",500);
	}
}

function so_xfade(numerodecapa) {
	thiscurrent = capas[numerodecapa]["currentimg"];
	cOpacity = capas[numerodecapa]["imgs"][thiscurrent].xOpacity;
	nIndex = capas[numerodecapa]["imgs"][thiscurrent+1]?thiscurrent+1:0;
	nOpacity = capas[numerodecapa]["imgs"][nIndex].xOpacity;
	
	cOpacity-=.05; 
	nOpacity+=.05;
	
	capas[numerodecapa]["imgs"][nIndex].style.display = "block";
	capas[numerodecapa]["imgs"][thiscurrent].xOpacity = cOpacity;
	capas[numerodecapa]["imgs"][nIndex].xOpacity = nOpacity;
	
	setOpacity(capas[numerodecapa]["imgs"][thiscurrent]); 
	setOpacity(capas[numerodecapa]["imgs"][nIndex]);
	
	if(cOpacity<=0) {
		capas[numerodecapa]["imgs"][thiscurrent].style.display = "none";
		capas[numerodecapa]["currentimg"] = nIndex;
		capas[numerodecapa]["thistimeout"] = setTimeout("so_xfade (" + numerodecapa + ")",500);
	} else {
		capas[numerodecapa]["thistimeout"] = setTimeout("so_xfade (" + numerodecapa + ")",100);
	}
	
	function setOpacity(obj) {
		if(obj.xOpacity>.99) {
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}
	
}
