/***********************************************************************************/
/*                                                                                 */
/* Better(?) Image cross fader (C)2004 Patrick H. Lauke aka redux                  */
/*                                                                                 */
/* Inspired by Steve at Slayeroffice http://slayeroffice.com/code/imageCrossFade/  */
/*                                                                                 */
/* glPreInit "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 glGalleryID = "shGallery";   /* the ID of the gallery list                     */
var glGallery;                   /* the object reference to the list               */
var glGalleryImages;             /* array that will hold all child elements        */
var glCurrentImage;              /* image currently being showed                   */
var glPreviousImage;
var glPreInitTimer;

glPreInit();
glAddEvent(window, 'load', glFadeInit)


/* functions                                                                       */


/* glAddEvent handler for IE and other browsers by Scott Andrew                    */
function glAddEvent(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   */
/* gallery 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 glPreInit()
{
  if ((document.getElementById) && (glGallery = document.getElementById(glGalleryID)))
  {
    glGallery.style.visibility = "hidden";
    if (typeof glPreInitTimer != 'undefined') clearTimeout(glPreInitTimer);   // thanks to Steve Clay http://mrclay.org/ for this small Opera fix
  }
  else
  {
    glPreInitTimer = setTimeout("glPreInit()", 2);
  }
}


/* helper function to deal specifically with images and the cross-browser          */
/* differences in opacity handling                                                 */
function glFader(imageNumber, opacity)
{
var obj = glGalleryImages[imageNumber];

  if (obj.style)
  {
    if (obj.style.MozOpacity != null)
    {
      obj.style.MozOpacity = (opacity/100) - .001;   // Mozilla's pre-CSS3 proprietary rule
    }
    else if (obj.style.opacity != null)
    {
      obj.style.opacity = (opacity/100) - .001;   // CSS3 compatible
    }
    else if (obj.style.filter != null)
    {
      obj.style.filter = "alpha(opacity=" + opacity + ")";   // IE's proprietary filter
    }
  }
}


function glFadeInit()
{
  if (document.getElementById)
  {
    glPreInit();   // glPreInit() shouldn't be necessary, but IE can sometimes get ahead of itself and trigger glFadeInit first
    glGalleryImages = new Array;

  // 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
  var node = glGallery.firstChild;

    while (node)
    {
      if (node.nodeType == 1)
      {
        glGalleryImages.push(node);
      }
      node = node.nextSibling;
    }

    for(i = 0; i < glGalleryImages.length; i++)
    {
      // loop through all these child nodes and set up their styles
      glGalleryImages[i].style.position = "absolute";
      glGalleryImages[i].style.top = 0;
      glGalleryImages[i].style.zIndex = 0;

      glFader(i, 0);   // set their opacity to transparent
    }

    glGallery.style.visibility = 'visible';   // make the list visible again

    // initialise a few parameters to get the cycle going
    glCurrentImage = 0;
    glPreviousImage = glGalleryImages.length - 1;
    opacity = 100;
    glFader(glCurrentImage, 100);

    window.setTimeout("glCrossfade(100)", 1000);   // start the whole crossfade process after a second's pause
  }
}


function glCrossfade(opacity)
{
  if (opacity < 100)
  {
    glFader(glCurrentImage, opacity);   // current image not faded up fully yet... so increase its opacity

    /* glFader(glPreviousImage, 100-opacity);                              */
    opacity += 2;
    window.setTimeout("glCrossfade(" + opacity + ")", 70);
  }
  else
  {
    glFader(glPreviousImage, 0);   // make the previous image - which is now covered by the current one - fully transparent

    // current image is now previous image, as we advance in the list
    glPreviousImage = glCurrentImage;
    glCurrentImage += 1;
    if (glCurrentImage >= glGalleryImages.length) glCurrentImage = 0;   // start over from first image if we cycled through all images

    // make sure the current image is on top of the previous one
    glGalleryImages[glPreviousImage].style.zIndex = 0;
    glGalleryImages[glCurrentImage].style.zIndex = 100;

    // and start the crossfade after a five second pause
    opacity = 0;
    window.setTimeout("glCrossfade(" + opacity + ")", 5000);
  }
}

