// *****************************************************
// derived from a script by brothercake -- http://www.brothercake.com/
// This script is published by DigitHaus, Inc. under the GPL
//******************************************************

/*
  Example:

  var xf = new CrossFader();
  xf.transitionInterval = 20; // optional

  xf.elements = new Array( object...); // optional
  xf.add(element);
  xf.add(element);

  xf.go();

*/

// the constructor.
/*
  Must pass the object's name, because we have to set a timer interval
  on itself. Is there some way to know the object's name introspectively?
*/
function CrossFader( objectName, elementArray )
{
  if (objectName != null && objectName != '') {
    this.objectName = objectName;
  }
  else
  {
    alert("Please pass the object name!!");
  }
  this.elements = elementArray;

  this.transitionClock = null;
  this.transitionCounter = 1;
  this.transitionTime = 1; // seconds
  this.transitionInterval = 8; // seconds

  this.currentElementIndex = null;

  // keeps track of the elements to fade in and out
  this.fadeIn = null;
  this.fadeOut = null;
}


// adds an element
CrossFader.prototype.add = function(element)
{
  if (this.elements == null)
  {
    this.elements = new Array();
  }

  this.elements.push(element);
}


// initializes the object
CrossFader.prototype.go = function()
{
  if (this.elements != null) {
    this.transitionCounter = 1;
    this.currentElementIndex = 0;
    var currentElement = this.elements[this.currentElementIndex];

    if (currentElement != null) {
      //determine the supported form of opacity
      if(typeof currentElement.style.opacity != 'undefined')
      {
        this.type = 'w3c';
      }
      else if(typeof currentElement.style.MozOpacity != 'undefined')
      {
        this.type = 'moz';
      }
      else if(typeof currentElement.style.KhtmlOpacity != 'undefined')
      {
        this.type = 'khtml';
      }
      else if(typeof currentElement.filters == 'object')
      {
        //weed out win/ie5.0 by testing the length of the filters collection (where filters is an object with no data)
        //then weed out mac/ie5 by testing first the existence of the alpha object (to prevent errors in win/ie5.0)
        //then the returned value type, which should be a number, but in mac/ie5 is an empty string
        this.type = (this.getAlphaFilter(currentElement) != null) ? 'ie' : 'none';
      }
      else
      {
        this.type = 'none';
      }

      // show the current element (the first element)
      this.showElement(currentElement);
      this.fadeIn = null;
      this.fadeOut =currentElement;

      // set the timer for the next transition
      this.elementClock = setInterval(this.objectName+'.crossfade()', this.transitionInterval*1000);
    }
  }
}



// CKIM: customized this function to work on entire block elements, rather
//       than just images
CrossFader.prototype.crossfade = function()
{
  //if the timer is not already going
  if(this.transitionClock == null && this.elements != null)
  {
    this.fadeOut = this.elements[this.currentElementIndex];

    this.currentElementIndex++;
    if (this.currentElementIndex >= this.elements.length) {
      this.currentElementIndex = 0;
    }
    this.fadeIn = this.elements[this.currentElementIndex];

    //skip the transition if both fadeIn and fadeOut are the same
    if (this.fadeIn != this.fadeOut) {
      //if any kind of opacity is supported
      if(this.type != 'none')
      {
        //create fade resolution argument as 20 steps per transition
        this.resolution = this.transitionTime * 20;

        //start the timer
        this.transitionClock = setInterval(this.objectName+'.continueCrossfade()', this.transitionTime*1000/this.resolution);
      }

      //otherwise if opacity is not supported
      else
      {
        //just do a visibility swap
        this.hideELement(this.fadeOut);
        this.showElement(this.fadeIn);
      }
    }
  }
};


CrossFader.prototype.stop = function()
{
  clearInterval(this.elementClock);
}

//crossfade timer function
CrossFader.prototype.continueCrossfade = function()
{
  //decrease the counter on a linear scale
  this.transitionCounter -= (1 / this.resolution);

  //set new opacity value on both elements
  //using whatever method is supported
  switch(this.type)
  {
  case 'ie' :
      if (this.fadeOut != null) {
        this.getAlphaFilter(this.fadeOut).opacity = this.transitionCounter * 100;
      }
      this.getAlphaFilter(this.fadeIn).opacity = (1 - this.transitionCounter) * 100;
      break;

    case 'khtml' :
      if (this.fadeOut != null) {
        this.fadeOut.style.KhtmlOpacity = this.transitionCounter;
      }
      this.fadeIn.style.KhtmlOpacity = (1 - this.transitionCounter);
      break;

    case 'moz' :
      //restrict max opacity to prevent a visual popping effect in firefox
      if (this.fadeOut != null) {
        this.fadeOut.style.MozOpacity = (this.transitionCounter == 1 ? 0.9999999 : this.transitionCounter);
      }
      this.fadeIn.style.MozOpacity = (1 - this.transitionCounter);
      break;

    default :
      //restrict max opacity to prevent a visual popping effect in firefox
      if (this.fadeOut != null) {
        this.fadeOut.style.opacity = (this.transitionCounter == 1 ? 0.9999999 : this.transitionCounter);
      }
      this.fadeIn.style.opacity = (1 - this.transitionCounter);
  }

  //now that we've gone through one fade iteration
  //we can show the image that's fading in
  this.showElement(this.fadeIn);

  //if the counter has reached the bottom
  if(this.transitionCounter < (1 / this.resolution))
  {
    //clear the timer
    clearInterval(this.transitionClock);
    this.transitionClock = null;

    //reset the counter
    this.transitionCounter = 1;

    //remove the duplicate image
    if (this.fadeOut != null) {
      this.hideElement(this.fadeOut);
    }
  }
};


// for IE support, gets the Alpha filter
CrossFader.prototype.getAlphaFilter = function(obj)
{
  var result = null;
  if (obj != null && obj.filters != null)
  {
    try
    {
      result = obj.filters.item("DXImageTransform.Microsoft.Alpha");
    }
    catch(e)
    {
      result = obj.filters.alpha;
    }
  }
  return result;
}


// hides an element
CrossFader.prototype.hideElement = function(obj)
{
  //obj.style.display = 'none';
  obj.style.visibility = 'hidden';
}

// shows an element
CrossFader.prototype.showElement = function(obj)
{
  //obj.style.display = 'block';
  obj.style.visibility = 'visible';
}




