new AjaxTabUtils();   // This is needed to declare the static variables
function AjaxTabUtils()
{
	//-----------------------------------------------------------------------------------------------------------
	// Static functions
	//-----------------------------------------------------------------------------------------------------------
	
	AjaxTabUtils.init = init;

	//-----------------------------------------------------------------------------------------------------------
	// Functions
	//-----------------------------------------------------------------------------------------------------------
	
	function init()
	{
		// Handle clicks on ajax tabs
		$('.ajax_tab_bar a').click(function(event){
			event.preventDefault();
			load_tab(this);
		});
		
		// Load first tab in any tab control
		$('.ajax_tab_bar .active').each(function(i){
			load_tab(this);
		});
	}
	
	function load_tab(clicked_tab)
	{
		var parentdiv = $(clicked_tab).parents().get(1);
		parentdiv = '#' + parentdiv.id;
		$(parentdiv + ' .ajax_tab_bar').children().removeClass('active');
		var content_div = $(parentdiv + ' .ajax_tab_control_content');
		
		$.ajax
		({
			url: clicked_tab.href,
			beforeSend:function()
			{
				$(content_div).css('background-image','url(' + BASE + '/img/template/ajax.gif)').html('');
			},
			complete:function()
			{
				$(content_div).css('background-image','');
			},
			success:function(msg)
			{
				$(content_div).html(msg);
				post_load_init(content_div);
				ScrollUtils.init(parentdiv.id);
			}
		});
		
			
		$(clicked_tab).addClass('active');
	}

	function post_load_init(content_div)
	{
		if (content_div == null)
		{
			process_carousels();
		}
		else
		{
			process_carousels(content_div);
		}
	}

	function process_carousels(contentDiv)
	{
		var selector = '.carousel';
		
		if (contentDiv != null)
		{
			selector = '#' + contentDiv.attr('id') + ' ' + selector;
		}
			
		$(selector).each(function(i)
		{
			carousel_init(this.id);
		});
	}
}
