/*
 * Tab handler, generates tab menu and content based on ul/li list.
 */
(function($) {

	$.sb_ui_tab_handler = function(element,fade_duration) {
		
		// Support multiple elements (classes)
		$(element).each(function() {
		
			var tab_content = $(this);
			var tab_content_li = tab_content.children("li");

			// Milliseconds or string value (500 or "fast" etc.)
			if ( !fade_duration )
			{
				fade_duration = 'fast';
			}
			
			// Add correct class name to content list
			tab_content.addClass("sb_ui_tab_content");
		
			// Creating tab menu string
			var tab_menu_str = "<ul class=\"sb_ui_tab_menu\">\n";
			tab_content_li.each(function()
			{
			
				var tab_title_org = $(this).attr("title");
				var tab_id = $(this).attr("id");
				var tab_class = $(this).attr("class");
				var tab_title = tab_title_org;
				var sb_ui_title_override = $(this).children("span.sb_ui_title_override");
				var use_link = true;
				
				// Clear id and class since we will transfer those to the new one
				$(this).attr("class", "");
				$(this).attr("id", "");
			
				// This system has support to override the title as tab name with anything you put in the span with class sb_ui_title_override
				if ( sb_ui_title_override.size() > 0 )
				{
					// Set new tab name
					tab_title = sb_ui_title_override.html();
					
					// If the override contains a href link, disable content switching for this one
					if ( sb_ui_title_override.children("a") )
					{
						$(this).addClass("sb_ui_disable_switch");
						
						// Dont add standard a href to this one
						use_link = false;
					}
					
					// Remove the override span from content
					sb_ui_title_override.remove();
				}
			
				// Add menu item
				tab_menu_str += "\t<li title=\""+ tab_title_org +"\" class=\""+ tab_class +"\" id=\""+ tab_id +"\">";
				
				if ( use_link == true )
				{
					tab_menu_str += "<a href=\"javascript:void(0);\"><span>" + tab_title + "</span>";
				}
				else
				{
					tab_menu_str += tab_title;
				}
				
				tab_menu_str += "</a></li>\n";
				
				// Remove title from content
				$(this).removeAttr("title");
			});
			tab_menu_str += "</ul>\n\n";
			
			// Create tab menu element
			var tab_menu = $(tab_menu_str);
			
			// Append menu before content
			tab_content.before( tab_menu );
			
			// Hide all but first tab content and set first tab to active, IF no active is set
			tab_content_li.hide();
			if ( !tab_menu.children("li").hasClass("active") )
			{
				tab_content.children("li:first").show();
				tab_menu.find("li:first").addClass("active");
			}
			else
			{
				var tab_active = tab_menu.children("li.active").index()+1;
				tab_content.children("li:nth-child(" + tab_active + ")").show();
			}
		
			// Handle tab menu click
			tab_menu.find("li a").bind("click", function() {
			
				// Set content
				var tab_active = tab_content.children("li:nth-child(" + ($(this).parent().index()+1) + ")");
			
				if ( !tab_active.hasClass("sb_ui_disable_switch") )
				{
			
					// Switch active class
					tab_menu.find("li").removeClass("active");
					$(this).parent().addClass("active");
				
					// Display correct tab
					tab_content_li.hide();
					tab_active.fadeIn(fade_duration);
					
				}
			
			});
		
		});
	
	}
	
})(jQuery);
