/*
AutoTab - Associate button to toggle display of any elemtn. Click each given element automatically, stop when user click
Works for IE7, FF3, Chrome, Safari, Opera
By: Chonla
Create Date: 10 August 2009
URL: http://blog.chonla.com
*/

(function($) {
	$.fn.autotab = function(options)
	{
		var defaults = {
			interval: 2,
			smooth: false,
			fadespeed: "fast",
			ontabchanged: null
		};
		options = $.extend(defaults, options);
		var tabs = this;
		var tabbing = true;
		var current = tabs.filter(":first").attr("show","1");
		var c = 1;
		var next = $(tabs.get(c));
		tabs.filter(":not(:first)").attr("show","0").each(function()
		{
			$("#" + $(this).attr("id") + "_holder").hide();
		});
		if (typeof(options.ontabchanged) == "function")
		{
			options.ontabchanged(current.get(0));
		}
		clickaction = function(o)
		{
			current = tabs.filter("[show=1]");
			if (options.smooth)
				$("#" + o.attr("show","1").attr("id") + "_holder").fadeIn(options.fadespeed);
			else
				$("#" + o.attr("show","1").attr("id") + "_holder").show();
			$("#" + current.attr("show","0").attr("id") + "_holder").hide();
			if (typeof(options.ontabchanged) == "function")
			{
				options.ontabchanged(o.get(0));
			}
		};
		tabs.click(function(){
			clickaction($(this));
			tabbing = false;
		});
		tabwalk = function()
		{
			if (tabbing)
			{
				clickaction(next);
				if (c >= (tabs.size() - 1)) c = -1;
				next = $(tabs.get(++c));
				setTimeout(tabwalk, options.interval * 1000);
			}
		};
		if (tabbing)
		{
			setTimeout(tabwalk, options.interval * 1000);
		}
	};
})(jQuery);

