/*
 * Feature-List - 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+
 *
 * Example Usage w/1 option:
 * $('#tabs li a').featureList({
 *   output		: '#output li',
 *   start_item	: 1 
 * });
 * Required : 
 * 		output - output element
 * Options :
 * 		start_item - Index of active tab by default (counting starts from 0) (default 0)
 *		pause_on_hover - If true, animations will pause when the mouse hovers over the tabs (default true)
 *		transition_interval - Length of time between transitions (0 = no automatic transitions) (default 5000)
 *		stop_on_click - If true, animations will stop once an item is clicked (i.e. user takes control) (default false)
 *    tabChanged_func - If true, calls external function tabChanged on every tab change and passes the current tab # (default false)
 *    fade_duration - sets the milliseconds for the fade in/out animation - defaults to 400
 * 
*/
;(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');

			output.stop(true, true).filter(":visible").fadeOut(options.fade_duration);
			output.filter(":eq(" + nr + ")").fadeIn(options.fade_duration,function() {
				visible_item = nr;	
			});

      if (options.tabChanged_func) {  //calls external function tabChanged on tab change if set
        tabChanged(nr);
      }
    }

		var options			= options || {}; 
		var total_items		= tabs.length;
		var visible_item	= options.start_item || 0;
    var stop_rotating = false;
    
		options.pause_on_hover = options.pause_on_hover || true;
		options.transition_interval = options.transition_interval || 5000;
    options.stop_on_click = options.stop_on_click || false;
    options.tabChanged_func = options.tabChanged_func || false;
    options.fade_duration = options.fade_duration || 400;
    
		output.hide().eq( visible_item ).show();
		tabs.eq( visible_item ).addClass('current');

		tabs.click(function(e) {
			e.preventDefault();			
      if (options.stop_on_click) { // stops theater rotation if a control element is clicked if true
        clearInterval( timer );
        stop_rotating = true;
      }
			if ($(this).hasClass('current')) {
				return false;	
			}
			slide( tabs.index( this) );
		});

		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() {
					if(!stop_rotating) {
            clearInterval( timer );
            timer = setInterval(function () {
              slide();
           	}, options.transition_interval);
          }
				});
			}
		}
	};
})(jQuery);
