/*
Better(?) Image cross fader (C)2004 Patrick H. Lauke aka redux
Inspired by Steve at Slayeroffice http://slayeroffice.com/code/imageCrossFade/ 
preInit "Scheduler" idea by Cameron Adams aka The Man in Blue
http://www.themaninblue.com/writing/perspective/2004/09/29/ 
Tweaked to deal with empty nodes 19 Feb 2006
Clean-up and tweaks by Skye Poier <skye@F4.ca> - 2 Apr 2006
*/

var galleryId = 'slideshow';    // Change this to the ID of the gallery list
var gallery;                // This will be the object reference to the list later on
var galleryImages;          // Array that will hold all child elements of the list
var currentImage;           // Keeps track of which image should currently be showing
var previousImage;
var preInitTimer;

var displaySeconds = 5;
var fadeDelay = 50;
var fadeStep = 10;

preInit();

// An inspired kludge that - in most cases - manages to initially hide the
// image gallery list before even onload is triggered (at which point it's
// normally too late, and the whole list already appeared to the user before
// being remolded) 

function preInit() {
    if ((document.getElementById) && (gallery = document.getElementById(galleryId))) {
        gallery.style.visibility = "hidden";
        clearTimeout(preInitTimer);
    } else {
        preInitTimer = setTimeout("preInit()", 10);
    }
}

// Initialize the crossfade effect.

function fadeInit() {
    if (document.getElementById) {
        preInit(); 

        // Shouldn't be necessary, but IE can sometimes get ahead of itself
        // and trigger fadeInit first.
        galleryImages = new Array;
        var node = gallery.firstChild;

        // Instead of using childNodes (which also gets empty nodes and messes
        // up the script later) we do it the old-fashioned way and loop through
        // the first child and its siblings
        while (node) {
            if (node.nodeType == 1) {
                galleryImages.push(node);
            }
            node = node.nextSibling;
        }
        for (i = 0; i < galleryImages.length; i++) {
            // Loop through all these child nodes and set up their styles.
/*
            galleryImages[i].style.position = 'absolute';
            galleryImages[i].style.top = 0;
            galleryImages[i].style.left = 0;
            galleryImages[i].style.margin = 0;
*/
            galleryImages[i].style.zIndex = 0;
            // Set their opacity to transparent.
            fader(i, 0);
        }
        // Make the list visible again.
        gallery.style.visibility = 'visible';
        // Initialise a few parameters to get the cycle going.
        currentImage=0;
        previousImage=galleryImages.length-1;
        opacity=100;
        fader(currentImage,100);
        // Start the whole crossfade process.
        window.setTimeout("crossfade(100)", 100);
    }
}

// Helper function to deal specifically with images and the cross-browser
// differences in opacity handling

function fader(imageNumber, opacity) {
    var obj = galleryImages[imageNumber];
    if (obj.style) {
        if (obj.style.MozOpacity != null) {  
            // Mozilla's pre-CSS3 proprietary rule
            obj.style.MozOpacity = (opacity / 100) - .001;
        } else if (obj.style.opacity != null) {
            // CSS3 compatible 
            obj.style.opacity = (opacity / 100) - .001;
        } else if (obj.style.filter != null) {
            // IE's proprietary filter
            obj.style.filter = "alpha(opacity=" + opacity + ")";
        }
    }
}

function crossfade(opacity) {
    if (opacity < 100) {
        fader(currentImage, opacity);
        opacity += fadeStep;
        window.setTimeout("crossfade(" + opacity + ")", fadeDelay);
    } else {
        // Make the previous image transparent
        // (now fully covered by the current image)
        fader(previousImage, 0);
        fader(currentImage, 100);
        window.setTimeout("nextImage()", displaySeconds * 1000);
    }
}

function nextImage() {
    previousImage = currentImage;
    currentImage += 1;
    if (currentImage >= galleryImages.length) {
            currentImage = 0;
    }
    // Make sure the current image is on top of the previous one.
    galleryImages[previousImage].style.zIndex = 0;
    galleryImages[currentImage].style.zIndex = 100;
    // Restart crossfade.
    opacity = 0;
    window.setTimeout("crossfade(" + opacity + ")", fadeDelay);
}

// addEvent handler for IE and other browsers
// Cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew

function addEvent(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {
        elm.addEventListener(evType, fn, useCapture);
        return true;
    } else if (elm.attachEvent) {
        var r = elm.attachEvent("on"+evType, fn);
        return r;
    }
} 

// initialise fader by hiding image object first
addEvent(window, 'load', fadeInit);

