/* Rotating Panel */
var rotatingPanel = function () {
  var panels = [];

  $('.rotating-panel').each(function (i) {
      panels.push($(this));
  })
  $('#slidepager li.selected').mouseover(function (e) {
    rotatingPanel.stop();
    e.preventDefault();
  });
  $('#slidepager li.selected').mouseout(function (e) {
    rotatingPanel.start();
    e.preventDefault();
  });
  return {
    currentIndex: 0,
    delay: 10,
    fade: 200,
    start: function () {
      $(document).everyTime(this.delay + 's', 'rotating-panel', function () { rotatingPanel.rotate(this.currentIndex); }, 0)
    },
    stop: function () {
      $(document).stopTime('rotating-panel');
    },
    rotate: function (direction) {
        this.stop();
        direction = direction - 1;
        var oldIndex = this.currentIndex;

        if (direction < (panels.length) & direction >= 0) {
            this.currentIndex = direction;

            if (this.currentIndex < 0) {
                this.currentIndex = (panels.length - 1);
            }
        }
        else {
            this.currentIndex++;

            if (this.currentIndex > (panels.length - 1)) {
                this.currentIndex = 0;
            }
        }

        if (oldIndex != this.currentIndex) {
            $('#slidepager li').removeClass("selected");
            $('#slidepager li').eq(this.currentIndex).addClass("selected");    

            panels[oldIndex].fadeOut(this.fade);
            panels[this.currentIndex].fadeIn(this.fade, function () { rotatingPanel.start(); });
        }
    }
  }
} ();

