var itineraryId = 'date';

$(document).ready(function()
{
	if (location.hash.length > 0) {
		itineraryId = location.hash.substr(1);
		$('option[value="' + itineraryId +'"]').attr('selected', 'selected');
	}
	closeAllTables();	
	processSelectBox();
	processSwitchButtons();
	openSelectedItinerary();
});

var processSelectBox = function()
{
	$('#sortby').change(function() 
	{
		freeItinerary();
		itineraryId = $(this).val();
		openSelectedItinerary();
	});
}

var processSwitchButtons = function()
{
	$('#openall').click(function() 
	{
		openAllTables();
		return false;
	});
	$('#closeall').click(function() 
	{
		closeAllTables();
		return false;
	});	
}

var closeAllTables = function()
{
	$('#'+itineraryId+'Block table.headersTable').removeClass('activeHeader');
	$('#'+itineraryId+'Block div.dataBlock').removeClass('activeData');
}

var openAllTables = function()
{
	$('#'+itineraryId+'Block table.headersTable').addClass('activeHeader');
	$('#'+itineraryId+'Block div.dataBlock').addClass('activeData');
}

var freeItinerary = function()
{
	closeAllTables();
	$('#'+itineraryId+'Block').hide('slow');
}

var openSelectedItinerary = function()
{
	$('#'+itineraryId+'Block').show('slow',function()
	{
		processAllTables();	
	});		
}

var processAllTables = function()
{
	$('#'+itineraryId+'Block table.headersTable tr').unbind('click');
	$('#'+itineraryId+'Block table.headersTable tr').click(function() 
	{
		var parentHeaderTable = $(this).parents('table.headersTable').eq(0);
		parentHeaderTable.toggleClass('activeHeader');
		parentHeaderTable.next().toggleClass('activeData');
	});		
}
