jQuery(document).ready(function($){

	// Fix IE select dropdowns where select has fixed width and content that exceeds that width
	jQuery('#top_banner select').ieSelectWidth({applyStyle:false});


	// Drop down menus
	////////////////////////////////////////////////////////////////////////////

	var search_form = jQuery('#popout_search_form');

	jQuery('#header').removeClass('no_js');

	// keep rollover state on top level links when in a child  link list
	jQuery('#main_nav li ul').hover(
		function(){
			jQuery(this).closest('ul').prev('a').addClass('active');
		},
		function(){
			jQuery(this).closest('ul').prev('a').removeClass('active');
		}
	);

	// main nav drop downs
	jQuery('.dropdown').each(function () {
		jQuery(this).parent().eq(0).hoverIntent({
			over: function () {
				search_form.removeData('active');

				if ( ! jQuery('.dropdown:eq(0)', this).is(':visible') )
				{
					// hide any menus open already
					jQuery('.dropdown').slideUp('fast');
					jQuery('.dropdown').css('z-index', '99');

					// show current menu
					jQuery('.dropdown:eq(0)', this).css('z-index', '999');
					jQuery('.dropdown:eq(0)', this).slideDown('fast');
				}
			},
			out: function () {
				// don't hide the search form if the user is using it.
				if( !search_form.data('active') )
				{
					jQuery('.dropdown:eq(0)', this).slideUp('fast');
					jQuery('*').datepicker('hide');
				}
			},
			interval: 50,
			timeout: 500
		});
	});

	// jQuery('.dropdown a').not('#popout_search_form a')
	// 	.each(function(){
	// 		$(this).data('previous_background', $(this).css('background'));
	// 	})
	// 	.hover(
	// 		function(){
	// 			jQuery(this).stop();
	// 			jQuery(this).css('background', '#07394b');
	// 		},
	// 		function(){
	// 			jQuery(this).css('background', '#07394b');
	// 			jQuery(this).stop().animate({
	// 				backgroundColor: '#4887a1'
	// 			}, 'medium');
	// 		}
	// 	);

	search_form.find('select')
		.click(function(){
			if( search_form.data('active') && search_form.data('active')==jQuery(this) )
			{
				search_form.removeData('active');
			}
			else
			{
				search_form.data('active', jQuery(this))
			}
		})
		.blur(function(){
			search_form.removeData('active');
		}/*)
		.change(function(){
			jQuery.removeData(search_form, 'active');
		}*/
	);

	// External links from editable pages
	jQuery('span.ext a').click(function(){
		window.open(this.href);
		return false;
	});

	// Deal with ajaxy page loading

	// back/forwards button
	jQuery(window).hashchange(function(){
		if(location.hash.substring(2,1)=='!')
		{
			loadPage(location.hash.slice(2));
		}
	});

	// link clicked
	jQuery('.ajax_link').live('click', function(e){
		location.hash = '!'+jQuery(this).attr('href');

		e.preventDefault();
	});

	// page refresh or deep link
	jQuery(window).hashchange();

	function loadPage(url)
	{
		jQuery.blockUI({
			message: '<img src="/images/ajax/ajax-loader.gif" alt="Please Wait" />',
			css: { padding: '10px', width: '', borderRadius: '10px', left: '50%', borderColor: '#000' },
			overlayCSS: {opacity: .5},
			fadeIn: 0,
			onBlock: function(){
				jQuery('#ajax_content')
					.load(url + '?ajax=true', function(){
						jQuery('#back_button').show();
						jQuery.unblockUI();
					}
				);
			}
		});
	}

	jQuery('#back_button').show();

	jQuery(".more_info_tip, a.special, div.special")
		.click(function(e){
			e.preventDefault();
		})
		.each(function(){
			$(this).qtip({
				content: jQuery(this).attr('title'),
				position: {
					my: 'top left',
					target: 'mouse',
					viewport: $(window), // Keep it on-screen at all times if possible
					adjust: {
						x: 10,  y: 20
					}
				},
				hide: {
					fixed: true // Helps to prevent the tooltip from hiding ocassionally when tracking!
				},
				style: {
					classes: 'ui-tooltip-shadow ui-tooltip-blue',
					tip: { corner: false }
				}
			});
		}
	);

	$('form#marketplace_select_form').submit(function(){
		if ($('select#marketplace_dd').length>0 && !$('select#marketplace_dd').val())
		{
			alert( $('#please_select_a_marketplace').attr('title') );
			return false;
		};
	});

	// Passenger Details form
	/////////////////////////////////////////////////////////////////////////////////////

	$('label.latitudes_radio input[type=radio]:checked').each(function(){
		if ( !$(this).val() )
		{
			$(this).closest('li').next('li').hide().find('input').attr('disabled', 'disabled');
		};
	});
	$('label.latitudes_radio input[type=radio]').click(function(){
		if ($(this).val())
		{
			$(this).closest('li').next('li').slideDown('fast').find('input').removeAttr('disabled');
		}
		else
		{
			$(this).closest('li').next('li').slideUp('fast').find('input').attr('disabled', 'disabled');
		};
	});
});


