/*
 * 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 options			= options || {}; 
		var total_items		= tabs.length;


		var data = $(this).data('featureList')
		
		if ( ! data ) {
			 $(this).data('featureList',{
				timer : '', 
				interval: 5000, 
				currentIndex : 0,
				output : $(options.output)
				
			})
			tabs.data.currentIndex = options.start_item || 0
			data = $(this).data('featureList')
		}
		data = (!data) ? {} : data
		
		var output	= data.output;
		if(!output)
			return


		
		
		function slide(nr, btest) {
			var currentIndex = data.currentIndex || 0

			var cur = output.filter(":eq(" + currentIndex + ")")
			if(cur.has('.media').get(0)) {	
				cur.find('.media > .player').hide()
			}
			
			
			if (typeof nr == "undefined" || nr == null) {
				nr = currentIndex + 1;
				nr = nr >= total_items ? 0 : nr;
			}

			tabs.removeClass('current').filter(":eq(" + nr + ")").addClass('current');

			output.stop(true, true).filter(":visible").fadeOut(2000);
			
			cur = output.filter(":eq(" + nr + ")")
			var hasPlayer = false			
			
				data.currentIndex = nr;	
			if(cur.has('.media').get(0)) {
				cur.find('.media > .player').hide()
				hasPlayer = true
				tabs.featureList('pause')
				data.interval = 5000
			} else {
				data.interval = 5000
				options.transition_interval = 5000
			}
			cur.fadeIn(3000,function() {
				
				if(hasPlayer)
					cur.find('.media > .player').show()
			});


		}
		

		var methods = {
			pause : function( ) { 
				//alert("Hi")
				clearInterval( data.timer );
			},
			restart : function( ) { 
				clearInterval( data.timer );
				data.timer = setInterval(function () {
					slide(null, 1);
				}, data.interval);
			
			}
		};

		// Method calling logic
		if ( methods[options] ) {
		  return methods[ options ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		}
		
		
		options.pause_on_hover		= options.pause_on_hover		|| true;
		options.transition_interval	= options.transition_interval	|| 5000;

		output.hide().eq( data.currentIndex ).show();
		tabs.eq( data.currentIndex ).addClass('current');

		tabs.click(function() {
			this.blur()
			if ($(this).hasClass('current')) {
				return false;	
			}

			slide( tabs.index( this) );
			return false;
		});
		
		if (options.transition_interval > 0) {
			data.timer = setInterval(function () {
				slide();
			}, options.transition_interval);

			if (options.pause_on_hover) {
				tabs.mouseenter(function() {
					clearInterval(data.timer );

				}).mouseleave(function() {
					clearInterval( data.timer );
					data.timer = setInterval(function () {
						slide();
					}, options.transition_interval);
				});
			}
		}
		
		//new jQuery.featureList(tabs, output, options);

		return this;	
	};

	$.featureList = function(tabs, output, options) {
	
		
	};
})(jQuery);

