$(document).ready(function()
{
	function update_selected_fixtures(overwrite_dates)
	{
		// Highlight selected fixtures, un-highlight un-selected ones
		$('td.fixture_item').each(
			function()
			{
				$(this).toggleClass( 'selected', $(this).find('input.fixture_select').is(':checked') );
			}
		);
		
		var fixture_names='';
		var resort_name='';
		var search_start_date=false;
		var search_end_date=false;
		
		// Loop over selected fixtures, in case they're multi-selectable check-boxes
		$('input.fixture_select:checked').each( function()
		{
			var $data = $(this).siblings('ul.search_data');
			
			// Combine fixture names, up to a limit
			if ( fixture_names.length > 0 )
			{
				fixture_names += ', ';
			}
			fixture_names += $.trim( $data.children('li.fixture_name').text() );
			if ( fixture_names.length > 30 )
			{
				fixture_names = 'Multiple Tickets';
			}
			
			// Over-write resort name (should all be the same anyway)
			resort_name = $.trim( $data.children('li.resort_name').text() );
			
			// Turn the string representation into a JS Date object
			var date_parts = $.trim( $data.children('li.search_date').text() ).split('-');
			var this_start_date = new Date(
				date_parts[0],
				date_parts[1] - 1, // 0-based months
				date_parts[2]
			);
			// Use this start date if it's earlier than the previous earliest
			if ( ! search_start_date || this_start_date < search_start_date )
			{
				search_start_date = this_start_date;
			}
			
			// Calculate end date, which we will then use for duration
			var search_duration = parseInt( $.trim( $data.children('li.search_duration').text() ), 10 );
			var this_end_date = new Date(this_start_date.getTime());
			this_end_date.setDate( this_start_date.getDate() + search_duration );
			// Use this end date if it's later than the previous latest
			if ( ! search_end_date || this_end_date > search_end_date )
			{
				search_end_date = this_end_date;
			}
		});
		
		if ( $('#standalone_fixture_resort').length == 1 )
		{
			resort_name = $('#standalone_fixture_resort').text();
		}
		
		// Set the display labels on the search form
		$('h4#fixture_search_title').text(fixture_names);
		if ( resort_name )
		{
			$('#search_resort_name').text(resort_name).parent().show();
		}
		
		// Set the search date and duration, except when loading the page with a fixture pre-selected
		//	or when there is nothing selected
		if ( overwrite_dates && $('input.fixture_select:checked').length > 0 )
		{
			
			$('#srhfrm_departure_date').val(
				search_start_date.getFullYear()
				+ '-'
				+ (search_start_date.getMonth() < 9 ? '0' : '') // leading 0 on 1-digit months (months 0 to 8)
				+ (search_start_date.getMonth() + 1) // back to 1-based months!
				+ '-'
				+ (search_start_date.getDate() < 10 ? '0' : '') // leading 0 on 1-digit days
				+ search_start_date.getDate()
			);
			$('#srhfrm_duration').val(
				Math.ceil(
					( search_end_date.getTime() - search_start_date.getTime() )
					/
					( 1000*60*60*24 ) // Convert difference into days
				)
			);
		}
		
		// Make sure the return text shows the right thing
		srhfrm_update_return( $('#srhfrm').get() );
	}
	
	$('input.fixture_select').bind('click', function(){ update_selected_fixtures(true); } );
	
	// If a fixture is pre-selected from the query string, act as though it was clicked
	//	but don't overwrite the dep date and duration
	update_selected_fixtures(false);
	
	// Hide the departure drop-down if user doesn't want a flight
	$('input[name=packagetype]').bind(
		'click', function()
		{
			$('tr#search_departure_row').toggle(
				$(this).val() == 'DP'
			);
			// Remove the departure airport from the list of required fields
			required_fields = $.grep(
				required_fields,
				function(field) { return field != 'airportcombinedfromid' }
			);
			// For DP, it's required after all
			if ( $(this).val() == 'DP' )
			{
				required_fields.push('airportcombinedfromid');
			}
		}
	);
	// And do it on load
	$('input[name=packagetype]:checked').click();
	
	
	$('a.book_tickets').bind('click',function() {
		$('#tab_title_0').click();
		return false;
	});
});

function srhfrm_validate_callback(errors)
{
	if (
		$("input.fixture_select:checked").length == 0
		&&
		$("input.fixture_select[type=hidden]").length == 0
	)
	{
		errors.push('The fixture you require tickets for.');
	}
}

