// used when popping open a login screen to redirect to the requested page in window instead of dialog
var redirectToPage = false;
var returnPage = false;
var preload = new Array('/img/layout/ajax-loader2.gif', '/img/thickbox/logo.png');

for (var x = 0, c = preload.length; x < c; x++) {
	var preloaded = new Image();
	preloaded.src = preload[x];
}

$(document).ready(function() {
	$('.noJS').remove();
	$('.noJShide').hide();
	$('.withJS').show();

	if ($.browser.msie && /8.0/.test(navigator.userAgent)) $('.btnCancel, .withJS').not('.chooseLocationType').remove();

	$('#same_as_lead_guest').click(function() {
		if ($(this).attr('checked')) {
			$('#BillingAddressLine1').val($('#UserAddressLine1').val());
			$('#BillingAddressLine2').val($('#UserAddressLine2').val());
			$('#BillingCity').val($('#UserCity').val());
			$('#BillingCountry').val($('#UserCountry').val());
			$('#BillingPostCode').val($('#UserPostCode').val());
		}
		else {
			$('#BillingAddressLine1').val('');
			$('#BillingAddressLine2').val('');
			$('#BillingCity').val('');
			$('#BillingCountry').val('');
			$('#BillingPostCode').val('');
		}
	});

	/* removing favourites */
	$('.removeFavouriteHotel').click(function() {
		var currentElement = this;

		$.get($(this).attr('href'), function() {
			$(currentElement).parent().parent().remove();

			if ($('table.myBookingsTable tr').length <= 1) {
				$('table.myBookingsTable').hide();
				$('div.removeFavourites').remove();
				$('table.myBookingsTable').after('<p>You have no hotels stored in your favourites.</p>');
				$('table.myBookingsTable').remove();
			}
		});

		return false;
	});

	/* newsletter module */
	$('#SubscriptionEmail').focus(function() {
		if ($('#SubscriptionEmail').val() == 'email') {
			$('#SubscriptionEmail').val('');
		}
	});
	$('#SubscriptionEmail').blur(function() {
		if ($('#SubscriptionEmail').val() == '') {
			$('#SubscriptionEmail').val('email');
		}
	});
	$('form#newsletterForm').submit(function() {
		$.post($(this).attr('action'), {'data[Subscription][email]': $('#SubscriptionEmail').val()}, function(json) {
			if (json == true) {
				$('form#newsletterForm').remove();
				$('#newsletterBlock p').text('Thank you! You have been subscribed');
			}
			else {
				$('#newsletterBlock p').after('<p class="errorNewsletter">* ' + json + '</p>');
			}
		}, 'json');

		return false;
	});

	/* mainmenu dividers */
	$('#mainmenu li').addClass("insertdivide"); // add dividers
	$('#mainmenu li.current').prev('li').removeClass("insertdivide"); // remove divider to the left of current menu item
	$('#mainmenu li a').hover(function(){
		/* remove dividers on adjacent menu items on hover */
		$(this).parent('li').prev().removeClass("insertdivide");
		$(this).parent('li').removeClass("insertdivide");
	}, function(){
		/* on mouseout add back dividers */
		$(this).parent('li').prev().addClass("insertdivide");
		$(this).parent('li').addClass("insertdivide");
		$('#mainmenu li.current').prev('li').removeClass("insertdivide"); // add back any dividers that should be there
    });

	/* Date Picker */
	$('#hotel_checkin_date').change(function(){
		//from hidden to selects
		var d = $('#hotel_checkin_date').val().split("/");

		$('#HotelCheckInYearMonth').val(parseInt(d[2],10)+'-'+d[1]);
		$('#HotelCheckInDay').val(parseInt(d[0],10));
	});

	$('#HotelCheckInDay, #HotelCheckInYearMonth').change(function(){
		var v = $('#HotelCheckInYearMonth').val() + '-' +$('#HotelCheckInDay').val();
		var d = v.split("-");

		$('#hotel_checkin_date').val(smallThan10Add0(d[2]) + '/' + smallThan10Add0(d[1]) + '/' + smallThan10Add0(d[0]));
		var m = parseInt(d[1],10)-1;
		var y = parseInt(d[0],10);
		if(m < 0){
			y -= 1;
			m = 11;
		}

		$('#hotel_checkin_date').dpSetDisplayedMonth(m, y);

	});

	function smallThan10Add0(val)
	{
		if (parseInt(val, 10) < 10) val = '0' + parseInt(val, 10);

		return val;
	}

	if ($('#hotel_checkin_date').length > 0) {
		$('#hotel_checkin_date').datePicker();

		$.get('/bookings/max_checkin_date/', function(data) {
			$('#hotel_checkin_date').dpSetEndDate(data);
		});
	}

	$('.tooltips').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		opacity: 1,
		showBody: " - ",
		fade: 250,
		top: 15,
		left: 15
	});

	$('.tooltips').click(function(){
		return false;
	});

	/* Grant's spiffy bevelled table headers */
	if ($('.myBookingsTable th').height() >= 40) $('.myBookingsTable th').toggleClass('big');


	/*FINAL BOOKING FORM VALIDATION*/
	$('#finalBookingForm').submit(function(){
		var errMsg = '';
		var errMsgBase = 'Finalise your booking:';


		if (!$('#IncorrectInformation0').attr('checked') || !$('#UnderStandTerms0').attr('checked')) {
			errMsg += "<p>&middot; You need to agree to the terms and conditions before we can complete your booking.</p>";
		}


		if (errMsg != '') {
			unload();
			showLightBox(errMsgBase, '<br />' + errMsg, 300, 120);
			return false;
		}

		loader('PROCESSING PAYMENT');

		return true;
	});

	if(1 === $('#seoTop').length) {
		$('#box_tips_and_advice').after($('#seoTop'));
		$('#seoTop').removeClass('hidden');
	}
});

function showFieldTitle(fieldId)
{
	var title = $(fieldId).attr('title');

	$(fieldId).val(title);

	$(fieldId).focus(function() {
		if ($(fieldId).val() == title) {
			$(fieldId).val('');
		}
	});

	$(fieldId).blur(function() {
		if ($(fieldId).val().length == 0) {
			$(fieldId).val(title);
		}
	});
}

function unload()
{
	$('div.loaderOverlay').remove();
	$('div.loaderContent').remove();
}

function loader(message)
{
	showLoadingLightBox(message);
}
