/*
 * 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
 * Heavily modified by The Department of Web Communications at Colorado State University
 * Requires: jQuery v1.3+
*/
(function($) {
	$.fn.featureList = function(options) {
		var tabs	= $(this);
		var output	= $(options.output);

		new jQuery.featureList(container, tabs, output, options);

		return this;	
	};

	$.featureList = function(container, tabs, output, options) {
		function slide(nr) {
			//console.log("what?:"+cycle);
			
			tabs.removeClass('current').filter(":eq(" + nr + ")").addClass('current');
			
			/*/fades from one to another
			output.stop(true, true).filter(":visible").fadeOut();
			output.filter(":eq(" + nr + ")").fadeIn(function() {
				visible_item = nr;	
			});
			//*/
			
			//*/replacement fade
			output.stop(true, true).css({'z-index' : '10'}).removeClass('current');	
			//indicator.css({'top': nr * tabs.height()});
			
			indicator.animate({
				left: nr * tabs.width() + 10 + nr*4 + 40
			}, 700, function(){
				// Animation complete.
			});
			
			output.filter(":eq(" + nr + ")").css({'z-index' : '100'}).addClass('current').fadeIn(transition_speed, function() {
				visible_item = nr;
				output.filter(":not(.current)").css({'display' : 'none'});
			});
			
			
			//*/
		}
		
		function autoSlide(nr) {
			if(cycle){
				if (typeof nr == "undefined") {
					nr = visible_item + 1;
					//nr = nr >= total_items ? 0 : nr;
					if(nr >= total_items ) {
						nr = 0;
						loopCount++;
						//console.log(loopCount);
						if(loopCount == number_loop){
							cycle=false;
						}
					}
					else {
						nr = nr;
					}
				}
				slide(nr);
			} else {
				return false;	
			}
		}

		var options			 = options || {}; 
		var total_items		 = tabs.length;
		var visible_item	 = options.start_item || 0;
		var number_loop      = options.number_loop;
		var transition_speed = options.transition_speed || 1000;
		var indicator_class  = options.indicator_class ;
		
		container.prepend('<div class="' + indicator_class + '"></div>');
		var indicator = $('div.'+indicator_class+'');

		var setLoop = false;
		var loopCount = 0;
		
		if(number_loop){
			//console.log("loop:"+number_loop);
			setLoop = true;	
		} else {
			//console.log("no loop")	;
		}		
		
		//adding cycle manangement...
		var cycle = true;

		options.pause_on_hover		= options.pause_on_hover || true;
		options.transition_interval	= options.transition_interval || 4000;

		output.hide().eq( visible_item ).show();
		tabs.eq( visible_item ).addClass('current');

		tabs.click(function() {
			if ($(this).hasClass('current')) {
				return false;	
			}

			slide( tabs.index( this) );
			cycle=false;
			//console.log(timer);
			return false;
		});

		if (options.transition_interval > 0) {
			var timer = setInterval(function () {
				autoSlide();
			}, options.transition_interval);

			if (options.pause_on_hover) {
				tabs.mouseenter(function() {
					clearInterval( timer );
				}).mouseleave(function() {
					clearInterval( timer );
					timer = setInterval(function () {
						autoSlide();
					}, options.transition_interval);
				});
			}
		}
	};
})(jQuery);
