$(document).ready(function()
{	
	set_up_tabs();
	set_up_tab_items();
	
	// Choose the initial tabs
	$('a[href=#whats-on]:first').trigger('click');
	//$('a[href=#test-content]:first').trigger('click');
		
	//var jump_to = document.location.hash;
		
	// User has specified what they want to see
	//$('a[href='+jump_to+']').trigger('click');
});

function set_up_tabs()
{
	// Let the CSS know we have JavsScript enabled
	$('.tab').addClass('js');

	// When a link to a tab is clicked, show its related tab
	$('a.tab').click(show_tab);
}

function set_up_tab_items()
{
	// Go through each of the tab content areas
	$('.tab:not(a)').each(function()
	{
		var tab_block = $(this);
		
		// Find the list of items within this tab
		var items = tab_block.find('.items');
		var need_nav = (items.length >= 1);
		
		// Skip added prev & next links
		if (!need_nav) return;
		
		// Add some tab navigation
		items.after('<div class="nav"></div>');
		var nav = tab_block.find('div.nav');
		
		// The first tab item is the always the default
		items = items.children('li');
		items.hide().filter(':first').addClass('on').show();
		
		// Go through each of the items and add prev & next links
		items.each(function()
		{
			var item = $(this)
			var prev = item.prev('li');
			var next = item.next('li');

			if (prev.length)
			{
				// We have a previous list item, so add a link to it
				var a = item.append(' <a href="#'+prev.attr('id')+'" class="eventNav prevEvent">prev</a> ');
				item.find('a[href=#'+prev.attr('id')+']').click(show_tab_item);
			}
			if (next.length)
			{
				// We have a next list item, so add a link to it
				var a = item.append(' <a href="#'+next.attr('id')+'" class="eventNav nextEvent">next</a> ');
				item.find('a[href=#'+next.attr('id')+']').click(show_tab_item);
			}
		});
		// Add events for prev and next links
		tab_block.find('a.eventNav').click(show_tab_item);
	});
}

function show_tab()
{
	var a = $(this);

	// Get the name of the group of tabs
	// e.g. <div class="tab [name]"></div>
	var tab_block = a.attr('class').match(/tab (\w+)/)[1];
	var link = a.attr('href');
	var parentID = $(this).parent().attr('id').replace(/tab/,'');
	
	// Turn OFF all tabs and associated content first
	$('.'+tab_block).removeClass('on');
	$('.'+tab_block+':not(a)').hide();
	
	// Turn ON this tab and its related content
	$('a[href='+link+'], '+link).addClass('on').fadeIn();
	$('#eventBlock').css('background-image','url("/wp-content/themes/coliseum/style/images/eventBox'+parentID+'.png")');
}

function show_tab_item(a)
{
	var a = $(this);
	var href = a.attr('href').match(/(#\w+)$/)[0];
	var item = $(href);
	// Turn OFF all tab items first
	a.parent().parent().children('li').removeClass('on').hide();
	// Turn ON this tab item
	item.addClass('on').fadeIn();
}
