//
// (C) 2010 BioMedia Amsterdam
//
(function($)
{
	$.fn.featuredTabs = function(options)
	{
		var settings = 	$.extend(
			{
				autoSwitch : true,
				autoSwitchTime : 4000
			}
			, options);
		
		var ft = $(this);
		if (ft.length == 0)
			return;
		ft.hover(function() { ft.data("ft_disable_anim", true); }, 
				 function() { ft.data("ft_disable_anim", false); });
		ft.addClass("featured_tabs_container");

		ft.find("> div").addClass("featured_tab");
		var activate = function(litag, animate)
			{
				var atag = litag.find("a");
				
				if (atag.length == 0) // ie sux
					return;
				var tabID = atag.attr("href").split("#")[1];
				//console.log(tabID); 
				
				if (animate)
					$("#" + tabID).fadeIn().siblings("div").fadeOut();
				else
					$("#" + tabID).show().siblings("div").hide();
				litag.addClass("featured_tabs_active").siblings().removeClass("featured_tabs_active");
			};
		ft.find("ul a").each(function(i,e)
		{	
			$(e).click(function(ev) 
			{ 
				activate($(e).parent());
				ev.preventDefault();
			}
			);
			
		});
		
		activate(ft.find("ul li:first"), false);
		
		if (settings.autoSwitch)
		{
			var f = function()
			{
				if(!ft.data('ft_disable_anim'))
				{
					var active = ft.find(".featured_tabs_active");
					var next = active.next();
					if (next.length == 0)
						next = active.siblings("li:first");
					//console.log(next);
					if (next.length > 0)
						activate(next, true);
				}
				setTimeout(f, settings.autoSwitchTime );
			};
			setTimeout(f,  settings.autoSwitchTime );
		}
			
		return this;
	};
})(jQuery);

