var astonishNavTimer = null;

function init_item_nav() {
  //alert( 'item nav init' );
  var $items = jQuery('#block-views-success-stories-block .views-row');
  $items.css('position', 'absolute');
  $items.not(':first').hide();
  astonishNavTimer = setInterval(item_nav_next,8777);
  jQuery('#block-views-success-stories-block .block-title').before('<ul class="success-nav"></ul>');
  $items.each(function(i,e){
    var id = 'success-story-'+i;
    var classes = '';
    if(i==0)
      classes += 'first active';
    if(i==$items.length-1)
      classes += 'last';
    jQuery('ul.success-nav').append('<li class="'+classes+'"><a href="#" id="'+id+'">'+i+'</a></li>');
  });
  jQuery('ul.success-nav li a').bind('click',item_jump);
}

function item_update_active() {
  var $items = jQuery('#block-views-success-stories-block .views-row');
  var $nav_items = jQuery('ul.success-nav li');
  var $curr_idx = -1;
  $items.each(function(i,e){
    if(jQuery(e).is(':visible')) {
      $curr_idx = i;
      return;
    }
  });
  if($curr_idx >= 0){
    $nav_items.removeClass('active').eq($curr_idx).addClass('active');
  }
}

function item_jump() {
  astonishNavTimer = clearTimeout(astonishNavTimer);
  var $a = jQuery(this);
  var $items = jQuery('#block-views-success-stories-block .views-row');
  $items.filter(':visible').fadeOut('slow');
  $items.eq(this.id.replace('success-story-','')).fadeIn('slow',item_update_active);
  return false;
}

function item_nav_next() {
  var $items = jQuery('#block-views-success-stories-block .views-row');
  var $curritem = $items.filter(':visible');
  var $next = $curritem.next();
  $curritem.fadeOut('slow');
  if($next.length > 0)
    $next.fadeIn('slow',item_update_active);
  else
    $items.first().fadeIn('slow',item_update_active);
}

function item_nav_prev() {
  var $items = jQuery('#block-views-success-stories-block .views-row');
  var $curritem = $items.filter(':visible');
  var $next = $curritem.prev();
  $curritem.fadeOut('slow');
  if($next.length > 0)
    $next.fadeIn('slow',item_update_active);
  else
    $items.last().fadeIn('slow',item_update_active);
}

jQuery( function() {
  init_item_nav();
} );



/* Auto Rotate the Banner Tabs */
/* Code found at http://drupal.org/node/264299 - comment #32 */

(function($) {
Drupal.behaviors.quicktabs_rotater = {
  attach: function(context, settings) {
    var featureRotate = function() {
      var activeTab = $('#quicktabs-banner_tabs ul.quicktabs_tabs').find('li.active');
      var nextTab = activeTab.next();
      var innerContent = $("#quicktabs-banner_tabs .quicktabs_tabpage > *")

      if(nextTab.length){
        nextTab.find('a').trigger('click');
        innerContent.hide();
        innerContent.fadeIn(700);
      }
      else {
        $('#quicktabs-banner_tabs ul.quicktabs_tabs li a').first().trigger('click');
        innerContent.hide();
        innerContent.fadeIn(700);
      }

      featureTimeout = setTimeout(featureRotate, rotateSpeed);

      return false;
    };

    // Define delay time here
    var rotateSpeed = 8000;
    // Comment the next line to disable auto-rotate
    var featureTimeout = setTimeout(featureRotate, rotateSpeed);

    // Disable auto-rotate when the mouse is over the quicktabs div
    $("#quicktabs-banner_tabs").bind("mouseenter", function() {
      clearTimeout(featureTimeout);
      featureTimeout = false;
      return false;
    } ) . bind("mouseleave", function() {
      featureTimeout = setTimeout(featureRotate, rotateSpeed);
      return false;
    });
    }
  };
}(jQuery));;

