/*
	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
*/

var d=document, imgs = new Array(), zInterval = null, current=0, pause=false, patharr = new Array(), offset = 1, trans=0, queue = new Array(), sslink = null, pIndex=null;

if (d.getElementById("rotator") != "undefined") {
	window.addEventListener?window.addEventListener('load',so_init,false):window.attachEvent('onload',so_init);
}


function so_init()
{
	if(!d.getElementById || !d.createElement)return;

	imgs = d.getElementById('rotator').getElementsByTagName('img');
	for(i=1;i<imgs.length;i++) {
		imgs[i].xOpacity = 0;
		patharr[i] = imgs[i].src;
	}
	imgs[0].style.display = 'block';
	imgs[0].xOpacity = .99;

	myWait = setTimeout(so_xfade,3000);
	
	sslink = d.getElementById( 'playss' );
	if (sslink)	sslink.innerHTML = "Stop Slideshow";
}

function so_xfade()
{
	cOpacity = imgs[current].xOpacity;
	//if (!trans) alert( pIndex );
	//nIndex = imgs[current+1]?current+1:0;
	if ( arguments[0] == 'jump' ) {
		pIndex = arguments[1] - 0;
		if (current == pIndex) {
			pIndex = null;
			return;
		}
		nIndex = pIndex;
	}
	else if ( pIndex !== null ) {
		nIndex = pIndex;
	}
	else {
		if ( offset >= 0 )
			nIndex = imgs[current+offset]?current+offset:0;
		else
			nIndex = (current-Math.abs(offset) >= 0)?current-Math.abs(offset):imgs.length-1;
	}

	nOpacity = imgs[nIndex].xOpacity;

	cOpacity-=.02;
	nOpacity+=.02;

	imgs[nIndex].style.display = 'block';
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;

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

	if(cOpacity<=0)
	{
		trans = 0;
		imgs[current].style.display = 'none';
		current = nIndex;
		pIndex = null;
		if ( !check_queue() )
			if ( !pause )
				myWait = setTimeout(so_xfade,4000);
	}
	else
		trans = setTimeout(so_xfade,10);

	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) + ')';
	}
}


function set_image( ind ) {
	switch_slideshow( 'off' );
	if (trans)
		add_to_queue('jump;'+ind);
	else
		so_xfade( 'jump',ind );
}

function new_image( new_offset ) {
	if ( !new_offset ) new_offset = 1;
//	alert(trans);
	if (trans)
		add_to_queue(new_offset);
	else {
		switch_slideshow('off')
		offset = new_offset;
		//alert(offset);
		so_xfade();
	}
}

function switch_slideshow() {
	dir = arguments[0];
	if ( pause && dir!='off') {
		offset = 1;
		pause = false;
		if (sslink)	sslink.innerHTML = "Stop Slideshow";
		so_xfade();
	}
	else {
		clearTimeout( myWait );
		pause = true;
		if (sslink)	sslink.innerHTML = "Play Slideshow";	
	}
}

function add_to_queue( new_offset ) {
	queue.push( new_offset );
}

function check_queue() {
	if (queue.length > 0) {
		if (String(queue[0]).indexOf(';') != -1) {
			set_image( queue.shift().split(';')[1] );
		}
		else
			new_image( queue.shift() );
	
		return true;
	}
	else return false;
}