/***********************************************************************************/
/*                                                                                 */
/* Better(?) Image cross fader (C)2004 Patrick H. Lauke aka redux                  */
/*                                                                                 */
/* Inspired by Steve at Slayeroffice http://slayeroffice.com/code/imageCrossFade/  */
/*                                                                                 */
/* whPreInit "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                                    */
/*                                                                                 */
/***********************************************************************************/

/* general variables                                                               */

var hmHomeID = "hmHome";         /* the ID of the home list                        */
var hmHome;                      /* the object reference to the list               */
var hmHomeImages;                /* array that will hold all child elements        */
var hmCurrentImage;              /* image currently being showed                   */
var hmPreviousImage;
var hmPreInitTimer;

hmPreInit();
hmAddEvent(window, 'load', hmFadeInit)


/* functions                                                                       */


/* hmAddEvent handler for IE and other browsers by Scott Andrew                    */
function hmAddEvent(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;
 }
} 


/* an inspired kludge that - in most cases - manages to initially hide the image   */
/* home list before event onload is triggered (at which point it's normally too    */
/* late, and the whole list already appeared to the user before being remolded)    */
function hmPreInit()
{
    if ((document.getElementById)&&(hmHome=document.getElementById(hmHomeID)))
    {
        hmHome.style.visibility = "hidden";
        /* thanks to Steve Clay http://mrclay.org/ for this small Opera fix        */
        if (typeof hmPreInitTimer != 'undefined') clearTimeout(hmPreInitTimer);
    } else {
        hmPreInitTimer = setTimeout("hmPreInit()", 2);
    }
}


/* helper function to deal specifically with images and the cross-browser          */
/* differences in opacity handling                                                 */
function hmFader(imageNumber, opacity)
{
    var obj = hmHomeImages[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 hmFadeInit()
{
    if (document.getElementById)
    {
        /* hmPreInit() shouldn't be necessary, but IE can sometimes get ahead of   */
        /* itself and trigger hmFadeInit first                                     */
        hmPreInit();
        hmHomeImages = new Array;
        var node = hmHome.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)
            {
                hmHomeImages.push(node);
            }
            node = node.nextSibling;
        }

        for(i = 0; i < hmHomeImages.length; i++)
        {
            /* loop through all these child nodes and set up their styles          */
            hmHomeImages[i].style.position = 'absolute';
            hmHomeImages[i].style.top = 0;
            hmHomeImages[i].style.zIndex = 0;

            /* set their opacity to transparent                                    */
            hmFader(i, 0);
        }
        /* make the list visible again                                             */
        hmHome.style.visibility = 'visible';

        /* initialise a few parameters to get the cycle going                      */
        hmCurrentImage = 0;
        hmPreviousImage = hmHomeImages.length - 1;
        opacity = 100;
        hmFader(hmCurrentImage, 100);

        /* start the whole crossfade process after a second's pause                */
        window.setTimeout("hmCrossfade(100)", 1000);
    }
}


function hmCrossfade(opacity) {
        if (opacity < 100)
        {
            /* current image not faded up fully yet...so increase its opacity      */
            hmFader(hmCurrentImage, opacity);

            /* hmFader(hmPreviousImage, 100-opacity);                              */
            opacity += 2;
            window.setTimeout("hmCrossfade(" + opacity + ")", 70);
        } else {
            /* make the previous image - which is now covered by the current one   */
            /* fully - transparent                                                 */
            hmFader(hmPreviousImage, 0);

            /* current image is now previous image, as we advance in the list      */
            hmPreviousImage = hmCurrentImage;
            hmCurrentImage += 1;
            if (hmCurrentImage >= hmHomeImages.length)
            {
                /* start over from first image if we cycled through all images     */
                hmCurrentImage = 0;
            }
            /* make sure the current image is on top of the previous one           */
            hmHomeImages[hmPreviousImage].style.zIndex = 0;
            hmHomeImages[hmCurrentImage].style.zIndex = 100;

            /* and start the crossfade after a five second pause                   */
            opacity = 0;
            window.setTimeout("hmCrossfade(" + opacity + ")", 5000);
        }
        
}

