var timer = null;
var headerImages = [];
var disableImageLinks;

function showHeader (elm, elmIdx) {

	if (disableImageLinks == true) {

	} else {

		if (elm.hasClass('active') || elm.attr('rel') == '') {

			// DO NOTHING

		} else {

			disableImageLinks = true;

			var preloadElm = headerImages[elmIdx][0];
			var targetElm = $('#HeaderImage img').not('#LogoImage img').not('#LegalNotice img');

			$(preloadElm).load(function(){
				headerImages[elmIdx][2] = true;
				elm.addClass('active').siblings().removeClass('active').blur();
				targetElm.replaceWith($(this)).animate({'opacity' : 1},{ duration: 500, queue: false, easing: 'easeInSine', complete: function(){ disableImageLinks = false; }}).parent().parent().removeClass('load');
			});

			targetElm.animate(
				{'opacity' : 0},
				{ duration: 250, queue: false, easing: 'easeOutSine', complete: function(){
					if (headerImages[elmIdx][2] != true) {
						$(preloadElm).attr({'src' : headerImages[elmIdx][1]});
					} else {
						elm.addClass('active').siblings().removeClass('active').blur();
						$(this).replaceWith($(preloadElm)).animate({'opacity' : 1},{ duration: 500, queue: false, easing: 'easeInSine', complete: function(){ disableImageLinks = false; }}).parent().parent().removeClass('load');
					};
				}}
			);
		}
	}
};

function nextHeaderImage()
{
	var total = $('#ImageNav a').size();
	var current = -1;
	$('#ImageNav a').each(function(i) {
		if ($(this).hasClass("active")) current = i;
	});
	next_image = current + 1;
	if (next_image > total - 1) {
		next_image = 0;
	}
	showHeader($("#ImageNav a:eq(" + next_image + ")"), next_image);
	//clearInterval(timer);
}

function prepHeader () {

	$('#ImageNav a').each(function(i) {
		var imgObj = $('<img>');
		var imgSrc = $(this).attr('rel');
		headerImages[i] = [imgObj, imgSrc, false];
		$(this).click(function(){
			showHeader($(this), i);
			return false;
		});
	});

	$('#LogoImage').css('opacity',0.75);
	$('#HeaderShadow').css('opacity',0.25);

	$("li:last", "#MainNav > ul").addClass("nomargin");
	$('#ImageNav a:first').addClass('active');
	timer = setInterval("nextHeaderImage()", 10000);
}

function quickGallery () {

	$('.quickGallery').each( function(){

		var	viewWrapper = $('div.wrapper', this);
		var viewTarget = $('img', viewWrapper);
		var galleryThumbs = $('.thumbs', this);
		var activeThumb = $('a:first', galleryThumbs).addClass('active');
		var currentWidth = viewWrapper.width();
		var currentHeight = viewWrapper.height();

		viewTarget.load( function(){

			var newWidth = $(this).width();
			var newHeight = $(this).height();

			viewWrapper.removeClass('load').animate({'width' : newWidth + 4, 'height' : newHeight + 4},{queue: true, complete: function(){

				viewTarget.animate({'opacity' : 1},{ queue: true, complete: function(){
					currentWidth = viewWrapper.width();
					currentHeight = viewWrapper.height();
					$('a.active', galleryThumbs).removeClass('active');
					activeThumb.addClass('active');
					}});

			}});

		});

		$('a', galleryThumbs).click( function(){
			if (! $(this).hasClass('active')) {
				var viewSrc = $(this).attr('href');
				activeThumb = $(this);
				viewWrapper.width(currentWidth).height(currentHeight);
				viewTarget.animate(
					{'opacity' : 0},
					{queue: false, complete: function(){ $(this).attr({'src' : viewSrc}).parent().addClass('load'); }}
				);
			}
			return false;
		});

	});

};

function prepMainNav(activeElm) {
	if(activeElm != '') {
		var targetElm = $('li.' + activeElm);
		targetElm.addClass('active').find('a:first').addClass('active');
	}
}

function prepNav(activeElm) {
	if(activeElm != '') {
		var targetElm = $('li.' + activeElm);
		targetElm.each( function(i) {
			var currentItem = $(targetElm[i]);
			currentItem.addClass('active').find('a:first').addClass('active').end().parents('li').addClass('active').find('a:first').addClass('active');
		})
	}

	$('#SideNav li > ul').parent().find('a:first').wrapInner('<span style="float: left"></span>').append('<span style="float: right">&raquo;</span>');

}

function toggleQuicknav () {
	if ( $("#FooterNav td.trigger a").html () == "close" ) { $("#FooterNav td.trigger a").html ( "open" ); }
	else { $("#FooterNav td.trigger a").html ( "close" ); }
	$('#FooterNav div.linkWrapper').slideToggle(250);
	return false;
}

function emptyBlankParagraphs() {
	$('p').each(function(){
		if ($(this).html() == '&nbsp;') {
		$(this).remove();
		}
	})
}

$(document).ready(function() {
    $("#PropertyBookingBlock input#id_arrive").focus(function() {
        if ($(this).val() == "Date of Arrival") $(this).val("");
    }).blur(function() {
        if ($(this).val() == "") $(this).val("Date of Arrival");
    });
    $("#PropertyBookingBlock input#id_depart").focus(function() {
        if ($(this).val() == "Date of Departure") $(this).val("");
    }).blur(function() {
        if ($(this).val() == "") $(this).val("Date of Departure");
    });
    // $("#PropertyBookingBlock input#id_rooms").focus(function() {
        // if ($(this).val() == "No. Rooms") $(this).val("");
    // }).blur(function() {
        // if ($(this).val() == "") $(this).val("No. Rooms");
    // });
    $("#PropertyBookingBlock input#id_adults").focus(function() {
        if ($(this).val() == "No. Adults") $(this).val("");
    }).blur(function() {
        if ($(this).val() == "") $(this).val("No. Adults");
    });
    $("#PropertyBookingBlock input#id_children").focus(function() {
        if ($(this).val() == "No. Children") $(this).val("");
    }).blur(function() {
        if ($(this).val() == "") $(this).val("No. Children");
    });
    $("#PropertyBookingBlock input#id_promo").focus(function() {
        if ($(this).val() == "Promotion Code") $(this).val("");
    }).blur(function() {
        if ($(this).val() == "") $(this).val("Promotion Code");
    });
    $("#PropertyBookingBlock form").submit(function() {
        if ($("#id_arrive").val() == "Date of Arrival") $("#id_arrive").val("");
        if ($("#id_depart").val() == "Date of Departure") $("#id_depart").val("");
        // if ($("#id_rooms").val() == "No. Rooms") $("#id_rooms").val("");
        if ($("#id_adults").val() == "No. Adults") $("#id_adults").val("");
        if ($("#id_children").val() == "No. Children") $("#id_children").val("");
        if ($("#id_promo").val() == "Promotion Code") $("#id_promo").val("");

        function blur_all() {
            $("#id_arrive").blur();
            $("#id_depart").blur();
            // $("#id_rooms").blur();
            $("#id_adults").blur();
            $("#id_children").blur();
            $("#id_promo").blur();
        }

        if ($("#PropertyBookingBlock form #id_arrive").val() == "") {
            alert("Please pick an arrival date");
            blur_all();
            $("#PropertyBookingBlock form #id_arrive").focus();
            return false;
        }
        if ($("#PropertyBookingBlock form #id_depart").val() == "") {
            alert("Please pick an departure date");
            blur_all();
            $("#PropertyBookingBlock form #id_depart").focus();
            return false;
        }
        // if ($("#PropertyBookingBlock form #id_rooms").val() == "") {
           // alert("Please enter the number of rooms required");
           // blur_all();
           // $("#PropertyBookingBlock form #id_rooms").focus();
           // return false;
        // }
        if ($("#PropertyBookingBlock form #id_adults").val() == "") {
            alert("Please enter the number of adults required");
            blur_all();
            $("#PropertyBookingBlock form #id_adults").focus();
            return false;
        }
    });

    $("#PropertyBookingBlock #id_arrive, #BookingTable #id_arrive").datepick({
        minDate: 0,
        beforeShow: customRange,
        changeMonth: false,
        changeYear: false,
        closeAtTop: false,
        showOn: 'both',
        buttonImageOnly: true,
        buttonImage: '/media/img/calendar.png',
        dateFormat: 'yy-mm-dd'
    });

    $("#PropertyBookingBlock #id_depart, #BookingTable #id_depart").datepick({
        beforeShow: customRange,
        changeMonth: false,
        changeYear: false,
        closeAtTop: false,
        showOn: 'both',
        buttonImageOnly: true,
        buttonImage: '/media/img/calendar.png',
        dateFormat: 'yy-mm-dd'
    });

    function customRange(a) {
        var b = null;
        if (a.id == 'id_arrive') {
            if ($("#id_depart").datepick('getDate') != null) {
                b = $("#id_depart").datepick('getDate');
                b.setDate(b.getDate() - 1);
            }
            return {
                maxDate: b
            }
        } else {
            if ($("#id_arrive").datepick('getDate') != null) {
                b = $("#id_arrive").datepick('getDate');
                b.setDate(b.getDate() + 1);
            } else {
                b = 1;
            }
            return {
                minDate: b
            }
        }
    }

});