/*
	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
*/

/*
    Modified Version by: Gianluca Montorsi
    Support for more rotators on each page
    Support for "last picture" restart usign sessvars: 
        now the slideshow remembers last picture displayed when page changes and when the user
        come back on the same page the slideshow restarts from that picture instead of the first one
    gianluca.montorsi@gmail.com
*/

//window.addEventListener?window.addEventListener('load',so_init,false):window.attachEvent('onload',so_init);

var d=document, imgs = new Array(), zInterval = null, current=0, pause=true, idelay=3000, delay=3000;

//get page name
var sPath = window.location.pathname;
//var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
//use sessvars to store current picture for each page

var _rotatorId='rotator', _rotatorName = sPage;

var foo = new Object(); // { }

if (!sessvars.xfade) {
    sessvars.xfade = foo
}


function xfade_init(rotatorId, rotatorName) {

    if (rotatorId) {
        _rotatorId = rotatorId
    }
    
    if (rotatorName) {
        _rotatorName = rotatorName
    }
 
    current = sessvars.xfade[_rotatorName] || 0

    so_init();

}

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);*/
    if (d.getElementById(_rotatorId)) {
	    imgs = d.getElementById(_rotatorId).getElementsByTagName('img');
	    for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;

        imgs[current].style.display = 'block';
        imgs[current].xOpacity = .99;
        pause = false;
	    setTimeout(so_xfade, idelay);
	
	}

}

function so_xfade() {

    if (pause) {
        setTimeout(so_xfade, 1000);
        return;
    }

	cOpacity = imgs[current].xOpacity;
	nIndex = imgs[current+1]?current+1:0;
	nOpacity = imgs[nIndex].xOpacity;

	cOpacity-=.05;
	nOpacity+=.05;

	imgs[nIndex].style.display = 'block';
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;

	setOpacity(imgs[current]);
	setOpacity(imgs[nIndex]);
	if(cOpacity<=0)
	{
		imgs[current].style.display = 'none';
		current = nIndex;
		sessvars.xfade[_rotatorName] = current;
		try {
		    setTimeout(so_xfade, array_delay[current]);
		} 
		catch (err) {
		    setTimeout(so_xfade, delay);
		}
	}
	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) + ')';
	}
	
}
