// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
//

(function($) {

  $.fn.rotate = function() {

    return this.each(function() {
      if($(this).children().size() < 2) {
        return;
      }
      var rotator = new Rotator($(this));
      $(this).children().hide();
      rotator.current().show();
      setTimeout(function() { nextRotation(rotator); }, 6000);
    });

  }

  function nextRotation(rotator) {
    rotator.current().fadeOut('medium');
    rotator.next().fadeIn('medium');
    setTimeout(function() { nextRotation(rotator); }, 6000);
  }


  function Rotator(element) {
    this.cursor = 0;
    this.element = element;
  }


  Rotator.prototype = {

    current: function() {
      return this.element.children().eq(this.cursor);
    },

    next: function() {
      if(this.cursor >= this.element.children().size() - 1) {
        this.cursor = 0;
      } else {
        this.cursor++;
      }
      return this.current();
    }
  };

 })(jQuery);


$(document).ready(function() { $('ul.Rotator').rotate(); });
