/*
 * 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+
 
 * Extension - Info
 * Version: 1.0.1
 * Author: Bassem Dghaidy
 * Email: bassem (at) musicnation (dot) me
*/

;(function($) {
	$.fn.featureList = function(options) {
		var tabs	= $(this);
		var output	= $(options.output);

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

		return this;	
	};

	$.featureList = function(tabs, output, uplink, downlink, options) {
		function slide(nr) {
			if (typeof nr == "undefined") {
				nr = visible_item + 1;
			}
			
			// Other options
			if (nr >= total_items)
				reset();
			else
				slide_down();
		}
		
		// Move the current class to the argument
		function set_current(cur_var)
		{
			tabs.removeClass('current').filter(":eq(" + cur_var + ")").addClass('current');

			output.stop(true, true).filter(":visible").fadeOut(function()
			{
				// IE Patch
				if (jQuery.browser.msie)
					this.style.removeAttribute("filter");
			});
			output.filter(":eq(" + cur_var + ")").fadeIn(function() {
				visible_item = cur_var;
				
				// IE Patch
				if (jQuery.browser.msie)
					this.style.removeAttribute("filter");
			});
		}

		var options			= options || {}; 
		var total_items		= tabs.length;
		var visible_item	= options.start_item || 0;
		var cursor_end		= options.end_item || total_items;
		var visible_lm		= options.visible_elements || total_items;
		
		options.pause_on_hover		= options.pause_on_hover		|| true;
		options.transition_interval	= options.transition_interval	|| 5000;

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

		tabs.click(function() {
			if ($(this).hasClass('current')) {
				return false;	
			}

			set_current( 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() {
					clearInterval( timer );
					timer = setInterval(function () {
						slide();
					}, options.transition_interval);
				});
			}
		}
		
		// Extended functions
		function slide_up()
		{
			if ((visible_item - 1) >= 0)
			{
				visible_item--;
				if ($(".li" + visible_item ).is(':not(:visible)'))
				{
					$(".li" + visible_item ).fadeIn(100, function()
					{
						$(this).show();
						
						// IE Patch
						if (jQuery.browser.msie)
							this.style.removeAttribute("filter");
					});
					
					$(".li" + cursor_end ).fadeOut();
					cursor_end--;
				}
				set_current(tabs.index( tabs.eq( visible_item ) ));
			}
		}
		
		function slide_down()
		{
			if ((visible_item + 1) <= (total_items - 1))
			{
				visible_item++; 
				if ($(".li" + visible_item ).is(':not(:visible)'))
				{
					$(".li" + ( visible_item - visible_lm ) ).css('display','none').fadeOut(100);
					$(".li" + visible_item ).fadeIn(100, function()
					{
						$(this).show();
						
						// IE Patch
						if (jQuery.browser.msie)
							this.style.removeAttribute("filter");
					});
					cursor_end = visible_item;
				}
				set_current(tabs.index( tabs.eq( visible_item ) ));
			}
			else
			{
				reset();
			}
		}
		
		// Go back to the first element while clearing all in-visible elements
		function reset()
		{
			for (var i = (total_items - 1); i >= 0; i--)
			{
				if (i  >= visible_lm)
				{
					$(".li" + i ).css('display','none').fadeOut(100, function()
					{						
						// IE Patch
						if (jQuery.browser.msie)
							this.style.removeAttribute("filter");
					});
				}
				else
				{
					$(".li" + i ).fadeIn("slow", function()
					{
						$(this).show();
						
						// IE Patch
						if (jQuery.browser.msie)
							this.style.removeAttribute("filter");
					});
				}
			}
			
			set_current(tabs.index( tabs.eq( 0 ) ));
		}
		
		// Reset the interval timer (If existing)
		function reset_timer()
		{
			if (options.transition_interval > 0) 
			{
				clearInterval( timer );
				timer = setInterval(function () 
				{
					slide();
				}, options.transition_interval);
			}
		}
		
		// Click up function
		uplink.click(function()
		{
			slide_up();
			reset_timer()
		});
		
		// Click down function
		downlink.click(function()
		{
			slide_down();
			reset_timer();
		});
	};
})(jQuery);
