/*****

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",own,false):window.attachEvent("onload",own);

var d=document, imgs = new Array();

function own () {
	css = d.createElement("link");
	css.setAttribute("href","includes/xfade2.css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	d.getElementsByTagName("head")[0].appendChild(css);

	so_init('slideshow-1','img');
}

function so_init(imageframe,type) {
	if(!d.getElementById || !d.createElement)return;
	frames[imageframe] = d.getElementById(imageframe).getElementsByTagName(type);
	frames[imageframe]['currentnum'] = 0;
	imgs=frames[imageframe];
	imgs['random'] = Math.floor(Math.random()*imgs.length);
	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
	imgs[0].style.display = "block";
	imgs[0].xOpacity = .99;
	
	setTimeout(function () {so_xfade(imageframe)},5000);
}

function so_xfade(imageframe) {
	imgs = frames[imageframe];
	current = imgs['currentnum'];
	nIndex = imgs[current+1]?current+1:0;
	
	imgs[nIndex].style.display = "block";
	imgs[current].xOpacity-=.05;
	imgs[nIndex].xOpacity+=.05;
	
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	
	if(imgs[current].xOpacity<0) {
		imgs[current].style.display = "none";
		imgs['currentnum'] = nIndex;
		while (imgs['currentnum'] == imgs['random']) {
			imgs['random'] = Math.floor(Math.random()*imgs.length);
		}
		setTimeout(function() {so_xfade(imageframe)},5000);
	} else {
		setTimeout(function() {so_xfade(imageframe)},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) + ")";
	}
	
}