/*
	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;

/* global slideshow objects array */
var objectsArr = [];
/* global images array */
var imgs = [];
/* objects counter */
var counter = 0;

function so_init()
{
	if(!d.getElementById || !d.createElement) return;
    slideShowContainers.each(function(s) {
        if ($(s) != null) {
        	objectsArr[counter] = new slideShow(); 
            objectsArr[counter].init(s, counter);
            ++counter;  
        }
	});
}

/* new slide show class */
var slideShow = Class.create({
    initialize: function() {
        this.imgs = [];
        this.current = 0;
        this.pause = false;
    },
    init: function(containerName, counter) {
        imgs[counter] = d.getElementById(containerName).getElementsByTagName('img');
        for(i=1; i<imgs[counter].length; i++) imgs[counter][i].xOpacity = 0;
        imgs[counter][0].style.display = 'block';
        imgs[counter][0].xOpacity = .99;
        setTimeout("objectsArr["+counter+"].so_xfade("+this.current+", "+counter+");", 3000); 
    },
    
    so_xfade: function(current, counter) {
        cOpacity = imgs[counter][current].xOpacity;
        nIndex = imgs[counter][current+1]?current+1:0;
        nOpacity = imgs[counter][nIndex].xOpacity;
    
        cOpacity-=.05;
        nOpacity+=.05;
    
        imgs[counter][nIndex].style.display = 'block';
        imgs[counter][current].xOpacity = cOpacity;
        imgs[counter][nIndex].xOpacity = nOpacity;
    
        this.setOpacity(imgs[counter][current]);
        this.setOpacity(imgs[counter][nIndex]);
    
        if(cOpacity<=0)
        {
            imgs[counter][current].style.display = 'none';
            objectsArr[counter].current = nIndex;
            setTimeout("objectsArr["+counter+"].so_xfade("+this.current+", "+counter+");", 3000);
        }
        else
        {
            setTimeout("objectsArr["+counter+"].so_xfade("+this.current+", "+counter+");", 50);
        }     
    },
    
    setOpacity: function(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) + ')';
    }
});