$(function(){
  
  $('#home-promo').contentRotator({
    itemSelector : '.promo-content',
    onChange     : updateControls,
    fadeSpeed    : 1000,
    delay            : 10300
  });

  $('#promo-controls a').click(function(){
    var href = $(this).attr('href');
    if (href == '#previous') {$('#home-promo').contentRotator('previous');} else if (href == '#next') {$('#home-promo').contentRotator('next');} else if (/show/.test(href)) {
      var itemIndex = href.split('/')[1];
      $('#home-promo').contentRotator('show', itemIndex);
    }
    return false
  });

});

function updateControls(){
  var currentItem = $('#home-promo').contentRotator('currentItem');
  $('#promo-controls a').removeClass('current');
  $('#promo-controls a[href$="'+currentItem+'"]').addClass('current');
}

$.fn.contentRotator = function(options_or_method_name){

  var rotator = this[0];

  var returnValue;  

  var settings = {
    delay          : 6000,
    itemSelector   : '.item',
    autoPlay       : true,
    fadeSpeed      : 'normal',
    baseZIndex     : 1,
    onChange       : function(){}}

  rotator.next = function(calledInternally){
    rotator.show(rotator.nextIndex(), calledInternally);
  }

  rotator.nextIndex = function(){
    if (rotator.currentItemIndex == $(rotator).find(settings.itemSelector).length-1) {
      return 0;
    } else {
      return rotator.currentItemIndex+1;
    }
  }

  rotator.previous = function(calledInternally){
    rotator.show(rotator.previousIndex(), calledInternally);
  }

  rotator.previousIndex = function(){
    if (rotator.currentItemIndex == 0) {
      return $(rotator).find(settings.itemSelector).length-1;
    } else {
      return rotator.currentItemIndex-1;
    }
  }

  rotator.show = function(itemIndex, calledInternally){
    
    if (rotator.transitioning) {return}
    
    var itemIndex    = parseInt(itemIndex);
    var visibleItems = $(rotator).find(settings.itemSelector+':visible');

    if (!rotator.initialized) {

      $(rotator).find(settings.itemSelector).eq(itemIndex).show();
      rotator.hideOthers(itemIndex);

    } else if (rotator.currentItemIndex != itemIndex) {
      
      var currentItem = visibleItems.eq(0);
      var newItem     = $(rotator).find(settings.itemSelector).eq(itemIndex);
      
      rotator.transitioning = true;
      
      currentItem.css('z-index', settings.baseZIndex)
      newItem.css('z-index', settings.baseZIndex+1);

      newItem.fadeIn(settings.fadeSpeed, function(){ 
        rotator.transitioning = false; 
        rotator.hideOthers(itemIndex);
      });
      currentItem.fadeOut(settings.fadeSpeed);
    }
  
    if (typeof(calledInternally) != undefined && !calledInternally) {rotator.resetTimer();}
    
    rotator.currentItemIndex = itemIndex;
    rotator.onChange();
  }

  rotator.hideOthers = function(itemIndex){
    $(rotator).find(settings.itemSelector).each(function(i){
      if (i != itemIndex) {$(this).hide();}
    });
  }

  rotator.currentItem = function(){
    returnValue = rotator.currentItemIndex;
  }

  rotator.start = function(){rotator.startTimer();}

  rotator.stop = function(){rotator.stopTimer();}

  rotator.startTimer = function() {
    rotator.timer = setInterval(function(){rotator.next(true)}, settings.delay);
  }

  rotator.stopTimer = function() {clearInterval(rotator.timer);}

  rotator.resetTimer = function() {
    rotator.stopTimer();
    rotator.startTimer();
  }

  rotator.onChange = function(){settings.onChange();}

  if (typeof(options_or_method_name) == 'object'){

    rotator.intialized    = false;
    rotator.transitioning = false;

    $.extend(settings, options_or_method_name);
    rotator.settings = settings;
    rotator.currentItemIndex = 0;

    rotator.show(0, true);
    
    if (settings.autoPlay) {rotator.start();}

    rotator.initialized = true;

  } else {

    settings = rotator.settings;

    var args = Array.prototype.slice.call(arguments);
    var method_name = args.shift()
    rotator[method_name].apply(null, args);
  }
  
  if (returnValue != undefined) {
    return returnValue;
  } else {
    return self;
  }

}
