/*
	Multiple Image Cross Fade
	Version 1.1
	Last revision: 06-14-2010
	deen@sabagency.com
	
	Rewrite of Image Cross Fade Redux Version 1.0 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 = 1, current=0, pause=false, preload=0;

function so_init()
{
	if(!d.getElementById || !d.createElement)return;

	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 = d.getElementById('slideshow').getElementsByTagName('img');
	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
	imgs[0].style.display = 'block';
	imgs[0].xOpacity = 1;

	setTimeout(so_xfade,1000);
}

function so_xfade()
{
	nIndex = imgs[current+1]?current+1:0;
	nOpacity = imgs[nIndex].xOpacity;

	fIndex = nIndex;
	for (i=1; i<=14; i++) {
//		fIndex = imgs[nIndex+1]?nIndex+1:0;
		fIndex = imgs[fIndex+1]?fIndex+1:0;
	}
	fOpacity = imgs[fIndex].xOpacity;
	if (navigator.appName=='Microsoft Internet Explorer') imgs[fIndex].style.display = 'none';

	nOpacity+=.02;
	fOpacity-=.02;

	imgs[nIndex].style.display = 'block';
	imgs[nIndex].xOpacity = nOpacity;
	imgs[fIndex].xOpacity = fOpacity;

	setOpacity(imgs[nIndex]);
	setOpacity(imgs[fIndex]);

	if(nOpacity>=1.0)
	{
		if (preload == 0) {
			if (imgs[nIndex+1]) {
				imgs[nIndex+1].setAttribute('src',imgs[nIndex+1].getAttribute('delayed'));
			} else {
				preload = 1;
			}	
		}

		imgs[fIndex].style.display = 'none';
		imgs[fIndex].xOpacity = 0;
		setOpacity(imgs[fIndex]);
		current = nIndex;
		nIndex = imgs[current+1]?current+1:0;
		imgs[nIndex].xOpacity = 0;
		setOpacity(imgs[nIndex]);
		imgs[nIndex].style.zIndex = zInterval++;

		setTimeout(so_xfade,1000);
	}
	else
	{
		setTimeout(so_xfade,50);
	}

	function setOpacity(obj)
	{
		if(obj.xOpacity>1) obj.xOpacity = 1;
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
//		obj.style.filter = 'alpha(opacity=' + (obj.xOpacity*100) + ')';
	}
}
