/*****
Image Cross Fade
Version 3.0 (beta 1)
Last revision: 12/12/2007

Original version by Steven G. Chipman <steve@slayeroffice.com>
original code here: www.slayeroffice.com/code/imageCrossFade/xfade2.html

Revised by Adam Messinger <www.zenscope.com>

Please leave this notice intact.
*****/

/* TODO: pause on hover feature */

var xfade = function()
{


  /****** DON'T EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU'RE DOING ******/

  var pub = {}; // pub object will contain public properties, methods

  var arrItems = [];  // array of items to fade
  var current = 0;     // current item

  function so_xfade( nDelay )
  {

    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 ) + " )";
    }

    var cOpacity = arrItems[current].xOpacity;
    var nIndex = arrItems[current+1]?current+1:0;

    var nOpacity = arrItems[nIndex].xOpacity;

    cOpacity -= 0.05;
    nOpacity += 0.05;

    arrItems[nIndex].style.display = "block";
    arrItems[current].xOpacity = cOpacity;
    arrItems[nIndex].xOpacity = nOpacity;

    setOpacity( arrItems[current] );
    setOpacity( arrItems[nIndex] );

    /* alias for so_xfade prevents "too much recursion" error when function
       calls itself */
    var fRe_fade = function() { so_xfade( nDelay ); };

    if( cOpacity <= 0 ) {
      arrItems[current].style.display = "none";
      current = nIndex;
      setTimeout( fRe_fade, nDelay );
    } else {
      setTimeout( fRe_fade, 50 );
    }
  }

  /* make init a public method */
  pub.init = function( strXfade_id, oSettings )
  {
    if( !document.getElementById( strXfade_id ) || !document.createElement ) {
      return false;
    }

    /* establish default settings */
    oSettings = oSettings || {};
      // slideshow height
    oSettings.slide_height = oSettings.slide_height || '200px';
      // elements within container to fade/cycle
    oSettings.toFade = oSettings.toFade || 'img';
      // delay before first item fades (milliseconds)
    oSettings.beginDelay = oSettings.beginDelay || 500;
      // delay before each additional item fades (milliseconds)
    oSettings.delay = oSettings.delay || 8500;

    var elmContainer = document.getElementById( strXfade_id );
    arrItems = elmContainer.getElementsByTagName( oSettings.toFade );

    elmContainer.style.position = 'relative';
    elmContainer.style.height = oSettings.slide_height;

    for( var i = 1; i < arrItems.length; i++ ) {
      arrItems[i].xOpacity = 0;
      arrItems[i].style.display = 'none';
      arrItems[i].style.position = 'absolute';
    }

    arrItems[0].style.display = "block";
    arrItems[0].style.position = 'absolute';
    arrItems[0].xOpacity = 0.99;

    var fBegin_fade = function() { so_xfade( oSettings.delay ); };

    setTimeout( fBegin_fade, oSettings.beginDelay );
  };

  return pub;  // expose init as public method

}();

function doXfade() {
  xfade.init( 'photos', {
    slide_height: '630px',
    toFade: 'li',
    beginDelay: 1500,
    delay: 2500
  });
}

if ( window.addEventListener ) {
  window.addEventListener("load",doXfade,false);
} else if ( window.attachEvent ) {
  window.attachEvent("onload",doXfade);
} else {
  window.onload = doXfade;
}