/*
 * FeatureList - simple and easy creation of an interactive "Featured Items" widget
 * Examples and documentation at: http://jqueryglobe.com/article/feature_list/
 * Version: 1.0.0 (01/09/2009)
 * Copyright (c) 2009 jQueryGlobe
 * Licensed under the MIT License: http://en.wikipedia.org/wiki/MIT_License
 * Requires: jQuery v1.3+
*/
;(function($) {
  $.fn.featureList = function(options) {
    var tabs  = $(this);
    var output  = $(options.output);

    new jQuery.featureList(tabs, output, options);

    return this;  
  };

  $.featureList = function(tabs, output, options) {
    function slide(nr) {
      if (typeof nr == "undefined") {
        nr = visible_item + 1;
        nr = nr >= total_items ? 0 : nr;
      }

      tabs.removeClass('current').filter(":eq(" + nr + ")").addClass('current');

      if (options.animations) {
        output.stop(true, true).filter(":visible").fadeOut();
        output.filter(":eq(" + nr + ")").fadeIn(function() {
          visible_item = nr;  
        });
      } else {
        output.stop(true, true).filter(":visible").hide();
        output.filter(":eq(" + nr + ")").show(0, function() {
          visible_item = nr;  
        });
      }
    }

    var options      = options || {}; 
    var total_items    = tabs.length;
    var visible_item  = options.start_item || 0;

    options.pause_on_hover    = options.pause_on_hover    || true;
    if (typeof options.leave_via_tab == 'undefined') {
      options.leave_via_tab = true;
    }
    if (options.transition_interval == 'undefined') 
      options.transition_interval = 6000;

    if (options.animations == 'undefined')
      options.animations = true;

    output.hide().eq( visible_item ).show();
    tabs.eq( visible_item ).addClass('current');

    tabs.click(function() {
      if ($(this).hasClass('current') && options.leave_via_tab) {
        return true;
      }

      slide(tabs.index(this));
      return false;
    });

    if (options.transition_interval > 0) {
      var timer = setInterval(function () {
        slide();
      }, options.transition_interval);

      if (options.pause_on_hover) {
        tabs.mouseenter(function() {
          clearInterval( timer );

        }).mouseleave(function() {
          clearInterval( timer );
          timer = setInterval(function () {
            slide();
          }, options.transition_interval);
        });
      }
    }
  };
})(jQuery);

