/*
	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, imgs = new Array(), zInterval = null, current=new Array(), pause=false, divID=1;
function startSlideshow()
{
	divID = ActiveSlideshows[0];
	setTimeout(so_xfade,3000);
}

function so_init(divID)
{
	//console.log("so_init(" + divID + ")");
	if(!d.getElementById || !d.createElement)return;
	imgs[divID] = new Array();
	current[divID] = 0;
	css = d.createElement('link');
	css.setAttribute('href','slideshow2.css');
	css.setAttribute('rel','stylesheet');
	css.setAttribute('type','text/css');
	d.getElementsByTagName('head')[0].appendChild(css);

	imgs[divID] = d.getElementById('rotator_'+divID).getElementsByTagName('img');
	//console.log("Found " + imgs[divID].length + " images");
	currentCount = imgs[divID].length ;
	//console.log("Should Show: " + currentCount);
	i = 0;
	while(i < currentCount)
	{
		//console.log("Image Count: " + i);
	   //	console.log("Image " + i + " " + d.getElementById('rotator_'+divID).getElementsByTagName('img')[i].src);
		imgs[divID][i].xOpacity = 0;
		i++;
	}
	imgs[divID][0].style.display = 'block';
	imgs[divID][0].xOpacity = .99;


}

function so_xfade()
{
//	console.log("Fading "+divID);
	cOpacity = imgs[divID][current[divID]].xOpacity;
	nIndex = imgs[divID][current[divID]+1]?current[divID]+1:0;
	nOpacity = imgs[divID][nIndex].xOpacity;

	cOpacity-=.05;
	nOpacity+=.05;

	imgs[divID][nIndex].style.display = 'block';
	imgs[divID][current[divID]].xOpacity = cOpacity;
	imgs[divID][nIndex].xOpacity = nOpacity;

	setOpacity(imgs[divID][current[divID]]);
	setOpacity(imgs[divID][nIndex]);

	if(cOpacity<=0)
	{
		imgs[divID][current[divID]].style.display = 'none';

		current[divID] = nIndex;
//		console.log("Current Slide: " + CurrentSlideshow);

		divID = CurrentSlideshow < NumSlideshows-1 ? ActiveSlideshows[CurrentSlideshow+1] : ActiveSlideshows[0];
		CurrentSlideshow = CurrentSlideshow  < NumSlideshows-1 ? CurrentSlideshow+1 : 0;
		setTimeout(so_xfade,3000);

	}
	else
	{
		setTimeout(so_xfade,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) + ')';
	}
}
