/*
 * 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.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);


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

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

		return this;	
	};

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

			if(tabs!=null) {
				tabs.removeClass('sel').filter(":eq(" + nr + ")").addClass('sel');
			}

			output.stop(true, true).filter(":visible").customFadeOut(1000);
			output.filter(":eq(" + nr + ")").customFadeIn(1000,function() {
				visible_item = nr;	
			});
			//Fade in Textbox
			if(options.text_appears_after) {
				clearInterval(innertimer);
				startInnerSlide(nr);
				}
		}
		
		function innerslide(nr) {
			output.find(".csc-textpic-text").hide();
			//alert(output.children(".csc-textpic-text").length);
			output.filter(":eq(" + nr + ")").find(".csc-textpic-text").fadeIn(1000);
			clearInterval(innertimer);
		}
		
		function startInnerSlide(nr) {
			innertimer = setInterval(function () {
				innerslide(nr);
			}, options.text_appears_after);
		}


		var options			= options || {}; 
		var total_items		= output.length;
		var visible_item	= options.start_item || 0;
		var counter = 0;
		var innertimer;

		options.pause_on_hover		= options.pause_on_hover		|| false;
		options.transition_interval	= options.transition_interval	|| 5000;
		options.text_appears_after	= options.text_appears_after	|| false;
		options.nav_event = options.nav_event || 'click';
		options.auto_play = options.auto_play || 'yes'; 
		
		
		
		

		output.hide().eq( visible_item ).show();
		if(options.text_appears_after) {
			output.find(".csc-textpic-text").hide();
			startInnerSlide(visible_item);
			}
		if(tabs!=null) {
			tabs.eq( visible_item ).addClass('sel');
		}

		if(options.nav_event=='mouseover'&&tabs!=null) {
			tabs.mouseenter(function() {
				if ($(this).hasClass('sel')) {
					return false;	
				}
	
				slide( tabs.index( this) );
			});
		}
		
		if(options.nav_event=='click'&&tabs!=null) {
			tabs.click(function() {
				if ($(this).hasClass('sel')) {
					return false;	
				}
	
				slide( tabs.index( this) );
				return false;
			});
		}
		
		if(prev_nav!=null) {
			prev_nav.click(
				function () {
					counter--;
					if(counter<0) {
						counter = total_items-1;	
					}
					slide(counter);
					return false;	
				}						
			);
		}
		
		
		
		if(next_nav!=null) {
			next_nav.click(
				function () {
					
					counter++;
					if(counter>total_items-1) {
						counter = 0;	
					}
					slide(counter);
					return false;	
				}						
			);
		}
		

		if ( (options.transition_interval > 0) && (options.auto_play == 'yes')) {
			//alert(options.auto_play);
			
			var timer = setInterval(function () {
				slide();
			}, options.transition_interval);

			if (options.pause_on_hover&&tabs!=null) {
				output.mouseenter(function() {
					clearInterval( timer );

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