jQuery(document).ready( function($) {
  var banner_timeout = null;
  var rotate_banners = function ( $element, delay ) {
    var transition = 1000;
    
    $('#banner_controls .play').addClass('playing');
    
    var $next = $element.next();
    if ( $next.length < 1 ) {
      $next = $element.siblings(':first-child');
    }
    if ( $next.length > 0 ) {
      $element.fadeOut(transition, function() {
        $element.removeClass('active');
        $next.addClass('active');
        $('#banner_controls ul li').removeClass('active')
              .eq($next.index()).addClass('active');
        $next.fadeIn(transition, function() {
          banner_timeout = setTimeout( function(){
            rotate_banners($next, delay);
          }, delay );
        });
      });
    }
  };
  
  $('#banners').append('<div id="banner_controls"><a href="#" class="play">Play</a><ul></ul></div>');
  $('#banners .banner').each( function() {
    $('#banner_controls ul').append('<li><a href="#"></a></li>');
  });
  $('#banner_controls ul li:first-child').addClass('active');
  
  $('#banners .banner:not(:first-child)').hide();
  
  if ( $('#banners .banner').length > 1 ) {
    $('#banner_controls .play').addClass('playing');
    banner_timeout = setTimeout( function() { 
      banner_timeout = rotate_banners($('#banners .banner:first-child'),8000);
    }, 8000);
  }
  
  $('#banner_controls .play').click( function() {
    if ( $(this).hasClass('playing') ) {
      $(this).removeClass('playing');
      $('#banners .banner').stop(true, true);
      if ( banner_timeout != null ) {
        clearTimeout(banner_timeout);
      }
    } else {
      if ( $('#banners .banner').length > 1 ) {
        $('#banner_controls .play').addClass('playing');
        banner_timeout = rotate_banners($('#banners .banner:visible'),8000);
      }
    }
    return false;
  });
  
  $('#banner_controls ul a').click( function() {
    $play = $('#banner_controls .play');
    if ( $play.hasClass('playing') ) {
      $play.removeClass('playing');
      $('#banners .banner').stop(true, true);
      if ( banner_timeout != null ) {
        clearTimeout(banner_timeout);
      }
    }
    $('#banner_controls ul li').removeClass('active');
    $('#banners .banner').hide()
          .eq($(this).parent().index()).show().addClass('active');
    $(this).parent().addClass('active');
    return false;
  });
  
  
  var feature_timeouts = [];
  var rotate_features = function ( $element, delay ) {
    var index = $element.parents('.feature-multiple').index('.feature-multiple');
    var transition = 1000;
    var $next = $element.next();
    if ( $next.length < 1 ) {
      $next = $element.siblings(':first-child');
    }
    if ( $next.length > 0 ) {
      $element.fadeOut(transition, function() {
        $element.removeClass('active');
        $next.addClass('active');
        $element.parents('.feature-multiple').find('.feature_controls ul li').removeClass('active')
              .eq($next.index()).addClass('active');
        $next.fadeIn(transition, function() {
          feature_timeouts[index] = setTimeout( function(){
            rotate_features($next, delay);
          }, delay );
        });
      });
    }
  };
  $('.feature-multiple').each( function() {
    var index = $(this).index('.feature-multiple');
    $(this).append('<div class="feature_controls"><ul></ul></div>');
    var $controller_list = $(this).find('.feature_controls ul');
    var first = true;
    var $element = null;
    $(this).find('.hentry').each( function() {
      $(this).css({position: 'absolute', display: 'none'});
      if ( first ) {
        first = false;
        $element = $(this).css({position: 'absolute', display: 'block'});
      }
      $controller_list.append('<li><a href="#"></a></li>');
      $controller_list.children(':first-child').addClass('active');
    });
    feature_timeouts[index] = setTimeout( function() {
      rotate_features($element, 8000);
    }, 8000);
  });
  $('.feature_controls ul a').click( function() {
    var index = $(this).parent().index();
    $(this).parents('.feature-multiple')
            .find('.hentry').stop(true, false).fadeTo(0, 1).hide()
            .eq(index).show();
    clearTimeout(feature_timeouts[index]);
    $(this).parent().siblings().removeClass('active').end().addClass('active');
    return false;
  });
});