/*****
 
Image Cross Fade Redux
Version 1.0
Last revision: 02.15.2006
steve@slayeroffice.com
 
Please leave this notice intact. 
 
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 imagesArray = [];
var current = 0;
var pause = 8;		// Time to pause each image in seconds.

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

    // Get a list of all the img object inside imageContainer.
    imagesArray = document.getElementById("imageContainer").getElementsByTagName("img");

	// Set the opacity of each image object to 0.
    for(i = 1; i < imagesArray.length; i++)
        imagesArray[i].xOpacity = 0;
    
    // Set the opacity of the first image to .99.
    imagesArray[0].style.display = "block";
    imagesArray[0].xOpacity = 0.99;

   
    setTimeout(so_xfade, 1000 * pause);
}

function so_xfade()
{
    // Opacity of current image.
    cOpacity = imagesArray[current].xOpacity;
    
    // Set the index of the next image to current + 1 (or 0 if current + 1 doesn't exist).
    nIndex = imagesArray[current + 1] ? current + 1 : 0;
    
    // Opacity of next image.
    nOpacity = imagesArray[nIndex].xOpacity;

    // Decrease current image's opacity value.
    cOpacity-=0.05;
    
    // Increase current image's opacity value.
    nOpacity+=0.05;

    // Make the next image visible.
    imagesArray[nIndex].style.display = "block";
    
    // Change the opacity property of the current and next images
    imagesArray[current].xOpacity = cOpacity;
    imagesArray[nIndex].xOpacity = nOpacity;

    // Set the opacity of the current and next images.
    setOpacity(imagesArray[current]);
    setOpacity(imagesArray[nIndex]);

    // If the opacity of the curren image is <= 0, hide it,
    // Set the current index to the next image's index,
    // And recursively call fade.
    
    if(cOpacity <= 0)
    {
	    // If cOpacity is <= 0, then the current image has totally faded.
		// So hide the current image, set current to the next image's index.
		// And call fade recursively.
        imagesArray[current].style.display = "none";
        current = nIndex;
        setTimeout(so_xfade, 1000 * pause);
    }
    else
    {
		// If the current image hasn't been totally faded, fade it some more.
		setTimeout(so_xfade,50);
    }

    function setOpacity(obj)
    {
        if(obj.xOpacity > 0.99)
        {
            obj.xOpacity = 0.99;
            return;
        }
        obj.style.opacity = obj.xOpacity;
        obj.style.MozOpacity = obj.xOpacity;
        obj.style.filter = "alpha(opacity=" + (obj.xOpacity * 100) + ")";
    }
}
