/**
  * Thomas Nicolosi
  * Red Bus Corp
  *thomas@redbuscorp.com
  *
  * This plug-in allows you to use jQuery selectors to identify elements that fade-out and fade-in on
  * a continuously rotating basis.
  *
  * Delay time between the fade-in and fade-out is configurable by setting the delayTime in milliseconds.
  * for example to set a delay time of 5 seconds call the function like this:
  * $([selector]).rotatingFade({delayTime: 5000});
  *
  * You can also adjust the fade time in a similar way.  You can use 'slow' (this is default), 'fast'., or a time in milliseconds.
  * So in the above example I will adjust the fade time to 2 seconds like so:
  * $([selector]).rotatingFade({delayTime: 5000, fadeTime: 2000});
  *
  * Note: To smooth out text fading in IE use css to place a background color behind the text containing element.
  *
  * Revisions:
  * 2010 August 17
  * Rev. 1.0.0
  * Initial Release.
  * 
  * 2010 August 18
  * Rev. 1.1.0
  * Added method to clear scheduled events, 
  * allow faster load of linked pages from within animated block, 
  * and prevent javascript error in IE.
  *
  * 2010 August 25
  * Rev. 1.2.0
  * Added params "pageIdentifier" and "activePage" to more reliably clear scheduled events
  * when navigating away from the page where the animation was set (if needed and applicable).
  * pageIdentifier is a variable that must be set outside of the script that 
  * uniquely identifies each page in the website.
  * activePage is a string that identifies the page where the animation is active.
  * default values for each are 'none'. This allows the animation to work across
  * all pages.
  *
  */
(function($) {
  //jQuery plugin definition.
  $.fn.rotatingFade = function(params) {
      //Merge default and user parameters.
    params = $.extend( {delayTime: 3000, fadeTime: 'slow', pageIdentifier: 'none', activePage: 'none'}, params);
    //Use keepFade to allow us to escape the fadeIn/Out looping.
    //Attach to window in order to stop all looping at the same time.   
    window.keepFade = true;
      //Hide everything except the first node.
      this.each(function(){
          $(this).not(':first-child').hide();
      $(this).children('a').click(function(){
                window.keepFade = false;
                });
          });
      //Load everything into an array:
      var theArray = $(this).map(function(i){
       return $(this);
      });
      //Load the elements from the array, the index, and the delay timer for the fade.
      var fadeNode = function (nodeArray, nodeIndex, delayTimer, fadeTimer){
        //Fade-in the next element after the curent element has faded out.
        var nextFade = function(anArray, anIndex, theDelayTime, theFadeSpeed){
            //Set up the next fade by incrementing the node index for the object array.
            anIndex++;
            //Set the node index to zero when the end of the array is reached.
            if (anIndex >= anArray.length) {
              anIndex = 0;
            }
            //Fade-in the current element.
            anArray[anIndex].fadeIn(theFadeSpeed);
            //Submit this element for fade-out after the designated delay.
      if (params.activePage === params.pageIdentifier) {
        setTimeout(function(){fadeNode (anArray, anIndex, theDelayTime, theFadeSpeed);}, theDelayTime);
        };
        }
        //Fade-out the current node.
    if (params.activePage === params.pageIdentifier) {
      nodeArray[nodeIndex].fadeOut(fadeTimer, function(){
          //Submit the next element to fade-in.
          nextFade(nodeArray, nodeIndex, delayTimer, fadeTimer);
        }); 
      };
      };
      //Submit the first element to fade out after the designated delay time.
      setTimeout(function(){ fadeNode(theArray, 0, params.delayTime, params.fadeTime); }, params.delayTime);
      //Allow jQuery chaining.
      return $(this);
    };
})(jQuery);
