$j( document ).ready( function() {
	// Setup defaults
	$j( 'ul#search-check li input' ).removeAttr( 'checked' );
	if( $j( 'body' ).attr( 'id' ) == 'destination-flights' )
	{
		$j( 'ul#search-check li input#check-flights' ).attr( 'checked', 'checked' );
	}
	else if( $j( 'body' ).attr( 'id' ) == 'destination-hotels' )
	{
		$j( 'ul#search-check li input#check-hotels' ).attr( 'checked', 'checked' );
	}
	else if( $j( 'body' ).attr( 'id' ) == 'destination-villas' )
	{
		$j( 'ul#search-check li input#check-villas' ).attr( 'checked', 'checked' );
	}
	else
	{
		$j( 'ul#search-check li input#check-flights' ).attr( 'checked', 'checked' );
		$j( 'ul#search-check li input#check-hotels' ).attr( 'checked', 'checked' );
	}
	
	displaySearch();
    $j( 'ul#search-check li input' ).click( displaySearch );
} );

/**
 * This function controls what search options to display depending on what combinations of items are selected
 * 
 */
function displaySearch()
{
	$j( 'ul#search-check li input' ).removeAttr( 'disabled' );
	$j( 'input.search-extras' ).val( '' );

	// Only allow hotels or villas
	if( $j( 'ul#search-check li input#check-hotels' ).is( ':checked' ) )
	{
		$j( 'ul#search-check li input#check-villas' ).attr( 'disabled', 'disabled' );
	}
	else if( $j( 'ul#search-check li input#check-villas' ).is( ':checked' ) )
	{
		$j( 'ul#search-check li input#check-hotels' ).attr( 'disabled', 'disabled' );
	}
	
    // If one item is selected
    if( $j( 'ul#search-check li input:checked' ).length == 1 )
    {
        // If item is whitelabel, redirect
        if( $j( 'ul#search-check li input:checked' ).hasClass( 'whitelabel' ) || $j( 'ul#search-check li input:checked' ).hasClass( 'whitelabelOnly' ) ) 
        {
        	$j( 'ul#search-check li input:not(:checked)' ).attr( 'disabled', 'disabled' );
        	window.location = $j( 'ul#search-check li input:checked' ).val();
        }
        // If item is standalone
        if( $j( 'ul#search-check li input:checked' ).hasClass( 'standalone' ) ) 
        {
        	$j( 'ul#search-check li input:not(:checked)' ).attr( 'disabled', 'disabled' );
        	searchBox( $j( 'ul#search-check li input:checked' ).val() );
        }
        // Else display search box
        else
        {
        	$j( 'ul#search-check li input.standalone' ).attr( 'disabled', 'disabled' );
            $j( 'ul#search-check li input.whitelabelOnly' ).attr( 'disabled', 'disabled' );
            searchBox( $j( 'ul#search-check li input:checked' ).val() );
        }
    }
    // If multiple items are selected
    else if( $j( 'ul#search-check li input:checked' ).length > 1 )
    {
    	$j( 'ul#search-check li input.whitelabelOnly' ).attr( 'disabled', 'disabled' );
    	$j( 'ul#search-check li input.standalone' ).attr( 'disabled', 'disabled' );
    	
    	// Setup search box
    	if( $j( 'ul#search-check li input#check-hotels' ).is( ':checked' ) && $j( 'ul#search-check li input#check-flights' ).is( ':checked' ) )
    	{
    		searchBox( 'flightshotels' );
    	}
    	else if( $j( 'ul#search-check li input#check-villas' ).is( ':checked' ) && $j( 'ul#search-check li input#check-flights' ).is( ':checked' ) )
    	{
    		searchBox( 'flightsvillas' );
    	}
    	else if( $j( 'ul#search-check li input#check-hotels' ).is( ':checked' ) )
    	{
    		searchBox( 'hotels' );
    	}
    	else if( $j( 'ul#search-check li input#check-villas' ).is( ':checked' ) )
    	{
    		searchBox( 'villas' );
    	}
    	else if( $j( 'ul#search-check li input#check-flights' ).is( ':checked' ) )
    	{
    		searchBox( 'flights' );
    	}
    	else
    	{
    		$j( 'ul#search-check li input' ).removeAttr( 'disabled' );
    		$j( 'ul#search-check li input' ).removeAttr( 'checked' );
    	}
    	
    	// Add extras into search
    	var currentValue = '';
    	if( $j( 'ul#search-check li input#check-attractions' ).is( ':checked' ) )
    	{
			currentValue += 'attractions,';
    	}
    	if( $j( 'ul#search-check li input#check-insurance' ).is( ':checked' ) )
    	{
    		currentValue += 'insurance,';
    	}
    	if( $j( 'ul#search-check li input#check-carhire' ).is( ':checked' ) )
    	{
    		currentValue += 'carhire,';
    	}
		$j( 'input.search-extras' ).each( function() {
			$j( this ).val( currentValue );
		} );
    }
}

/**
 * Hides and shows search boxes
 * 
 * @param id
 */
function searchBox( id )
{		
	if( id == 'flightshotels' )
	{
	    $j( 'div#packageDestinations' ).html( $j( 'div#hotelDestinations' ).html() );
		$j( 'input#searchAccommodationType' ).val( 'hotel' );
	}
	else if( id == 'flightsvillas' )
	{
	    $j( 'div#packageDestinations' ).html( $j( 'div#villaDestinations' ).html() );
		$j( 'input#searchAccommodationType' ).val( 'villa' );
		id = 'flightshotels';
	}
	
	$j( 'div.search-block' ).hide();
	$j( 'div#' + id ).show();
}