/*****

Image Cross Fade Redux
Version 1.0
Last revision: 02.15.2006
steve@slayeroffice.com

Please leave this notice intact. 

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, imgs = new Array(), imgs2 = new Array(), zInterval = null, current=0, pause=false,interval=5000;

function so_init() {
	if(!d.getElementById || !d.createElement)return;
	/*css = d.createElement("link");
	css.setAttribute("href","js/xfade2.css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	d.getElementsByTagName("head")[0].appendChild(css);*/
	
	imgs = d.getElementById("imageContainer1").getElementsByTagName("img");
	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
	imgs[0].style.display = "block";
	imgs[0].xOpacity = .99;

	imgs2 = d.getElementById("imageContainer2").getElementsByTagName("img");
	for(i=1;i<imgs2.length;i++) imgs2[i].xOpacity = 0;
	imgs2[0].style.display = "block";
	imgs2[0].xOpacity = .99;
	
	setTimeout(so_xfade,interval);
}

function so_xfade() {
	so_xfadeLocal(imgs, true, 1);
	so_xfadeLocal(imgs2, false, 2);
}

function so_xfadeFader() {
	so_xfadeLocal(imgs, true, 1);
}

function so_xfadeLocal(lImgs, setTheTimer, which) {
	cOpacity = lImgs[current].xOpacity;
	nIndex = lImgs[current+1]?current+1:0;
	nOpacity = lImgs[nIndex].xOpacity;
	
	if ( which == 1 ) {
		cOpacity-=.05; 
		nOpacity+=.05;
	} else {
		cOpacity = 0.0;
		nOpacity = 0.99;
	}
	
	lImgs[nIndex].style.display = "block";
	lImgs[current].xOpacity = cOpacity;
	lImgs[nIndex].xOpacity = nOpacity;
	
	setOpacity(lImgs[current]); 
	setOpacity(lImgs[nIndex]);
	
	if ( setTheTimer ) {
		if(cOpacity<=0) {
			lImgs[current].style.display = "none";
			setTimeout(so_xfade,interval);
			current = imgs[current+1]?current+1:0;
		} else {
			setTimeout(so_xfadeFader,50);
		}
	}
}

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) + ")";
}