var quickPhotosArray = new Array();
var thumbIdx = 0;

$(document).ready(function() {
    //remove the target="_blank" from google mini search results
    $(".txtContent .boxSearchResults table td a[target='_blank']").attr('target', '');

    // create custom tooltip effect for jQuery Tooltip 
    $.tools.tooltip.addEffect("slidedownup",
    // opening animation 
		    function(done) {
		        this.getTip().slideDown().show();
		    },
    // closing animation 
		    function(done) {
		        this.getTip().slideUp(function() {
		            $(this).hide();
		            done.call();
		        });
		    }
		);


    var navOffSet1 = -149;
    var navOffSet2 = -371;
    var navOffSet3 = -505;
    
    if(brand == "Classic"){
        navOffSet1 = -125;
        navOffSet2 = -236;
        navOffSet3 = -375;
    }

    // BEGIN drop down navigation tool tips
    $(".nav1").tooltip({
        relative: true,
        position: 'bottom right',
        offset: [0, navOffSet1],
        //predelay: 300,
        effect: 'slidedownup',
        onBeforeShow: function() {
            $(this).addClass('active');
        },
        onHide: function() {
            $(this).removeClass('active');
        }
    });
    $(".nav2").tooltip({
        relative: true,
        position: 'bottom right',
        offset: [0, navOffSet2],
        //predelay: 300,
        effect: 'slidedownup',
        onBeforeShow: function() {
            $(this).addClass('active');
        },
        onHide: function() {
            $(this).removeClass('active');
        }
    });
    $(".nav3").tooltip({
        relative: true,
        position: 'bottom right',
        offset: [0, navOffSet3],
        //predelay: 300,
        effect: 'slidedownup',
        onBeforeShow: function() {
            $(this).addClass('active');
        },
        onHide: function() {
            $(this).removeClass('active');
        }
    });
    // END drop down navigation tool tips

    //more link if in use
    $("#thirdLevelMoreLink").tooltip({
        relative: true,
        position: 'bottom right',
        offset: [0, -52],
        effect: 'slidedownup'
    });

    //jQuery UI accordion - easy to toggle states
    /*$("#accordion").accordion({
    collapsible: true,
    autoHeight: false,
    active: false
    });*/

    $(".scrollableSpecials").scrollable({
        mousewheel: true,
        circular: false,
        speed: 300,
        onSeek: function(evt, idx) {
            var parentContainer = this.getItemWrap().parent().parent();
            parentContainer.find('.pageIndexSpecials').html(idx + 1);
        }
    });

    //BEGIN HOME GALLERY
    //GALLERY: TABS
    // initialize GALLERY-PHOTOS scrollable
    $(".scrollableContainer").scrollable({
        mousewheel: true,
        circular: false,
        speed: 300,
        onSeek: function(evt, idx) {
            var parentContainer = this.getItemWrap().parent().parent();
            parentContainer.find('.pageIndex').html(idx + 1);
        }
    });
    $('.hlOneWidePhoto').fancybox();

    // video gallery
    $("a.hlOneWideVideo").fancybox({
        'titleShow': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'href': this.href,
        'type': 'swf',
        'swf': { 'wmode': 'transparent', 'allowfullscreen': 'true' }
    });

    //END HOME GALLERY





    //BEGIN HOME SEO BOX
    $('#boxSEO').delay(2500).slideUp('slow', function() {
        // Animation complete - now fade in open button
        $('#boxSEOBtnOpen').show();
    });

    $('#boxSEOBtnClose').click(function() {
        $('#boxSEO').slideUp('slow', function() {
            // Animation complete - now fade in open button
            $('#boxSEOBtnOpen').show();
        });

    });

    $('#boxSEOBtnOpen').click(function() {
        $('#boxSEO').slideDown('slow');
        $('#boxSEOBtnOpen').hide();
    });
    //END HOME SEO BOX

    // quick photo gallery
    $('.quickPhotoGalleryThumbnail').click(function() {
        var newSrc = $(this).attr('large');
        var thumb = $(this).attr('src');
        thumbIdx = $('.quickPhotoGalleryThumbs:visible').find('.quickPhotoGalleryThumbnail').index($(this));

        var large = $(this).parent().parent().parent().find('.quickPhotoGalleryLargeImage');
        var fullSize = $(this).parent().parent().parent().find('.viewFullsize')
        $(fullSize).fadeOut('fast', function() {
            $(large).fadeOut('fast', function() {
                $(this).load(function() {
                    $(this).fadeIn('fast');
                    $(fullSize).fadeIn('fast');
                });
                $(this).attr('src', newSrc);
                $(this).attr('thumb', thumb);
            });
        });
    });

    // lightbox for quick photo gallery
    buildQuickPhotoGalleryArray();

    $(".viewFullsize").click(function() {
        $(this).parent().find('.quickPhotoGalleryLargeImage').trigger('click');
    });
    $(".quickPhotoGalleryLargeImage").click(function() {
        thumbIdx = $('.quickPhotoGalleryThumbs:visible .quickPhotoGalleryThumbnail').index($('.quickPhotoGalleryThumbnail[src="' + $(this).attr('thumb') + '"]'))
        if (quickPhotosArray.length > 0) {
            $.fancybox(
					quickPhotosArray, {
					    'type': 'image',
					    'index': thumbIdx,
					    onClosed: function() {
					        buildQuickPhotoGalleryArray();
					    }

					}
				);
        }
    });

    $("a#btnViewMaps").fancybox({
        //'width': '90%',
        //'height': '90%',
        'width': 1000,
        'height': 650,
        'autoScale': false,
        'type': 'iframe'
    });
    $("a#footerInteractiveMap").fancybox();

    function startFancybox() {

    }

    $('#footerEmailSignupSubmit').click(function() {
        var urlToUse = "/contact-us.aspx";
        var email = $('#footerEmailSignup').val();
        if (email == document.getElementById('footerEmailSignup').defaultValue) {
            email = '';
        }
        var queryString = "?email=" + email;
        window.location = urlToUse + queryString;
    });

    $('#footerEmailSignup').keypress(function(e) {
        if (window.event) {
            key = window.event.keyCode;
        }
        else {
            key = e.which;
        }
        if (key == 13) {
            e.preventDefault();
            $('#footerEmailSignupSubmit').click();
        }
    });

    $('#footerSiteSearchSubmit').click(function() {
        var postForm = document.createElement("form");
        postForm.method = "get";
        postForm.action = $('.hdnSearchResultsPage').val();
        addFormField(postForm, "q", $('#search-field').val());
        document.body.appendChild(postForm);
        postForm.submit();
    });
    $('#search-field').keypress(function(e) {
        if (window.event) {
            key = window.event.keyCode;
        }
        else {
            key = e.which;
        }
        if (key == 13) {
            e.preventDefault();
            $('#footerSiteSearchSubmit').click();
        }
    });



    /* booking widget */
    $("#arrivalDate").datepicker({
        minDate: '0',
        showOn: 'focus',
        onSelect: function(dateText, inst) { setMinDepartureDate(dateText, inst); }
    });
    $("#arrivalDate").change(function() { setMinDepartureDate($("#arrivalDate").val(), $("#arrivalDate")); });

    $("#departureDate").datepicker({
        minDate: '0',
        showOn: 'focus',
        onSelect: function(dateText, inst) { setMaxArrivalDate(dateText, inst); }
    });
    $("#departureDate").change(function() { setMaxArrivalDate($("#departureDate").val(), $("#departureDate")); });

    function setMinDepartureDate(dateText, datepicker) {
        var dt = new Date(dateText);
        dt.setDate(dt.getDate() + 1);
        $('#departureDate').datepicker('option', 'minDate', dt);
        if ($('#departureDate').val() == '') {
            $('#departureDate').datepicker('setDate', dt);
        }
    }
    function setMaxArrivalDate(dateText, datepicker) {
        var dt = new Date(dateText);
        dt.setDate(dt.getDate() - 1);
        $('#arrivalDate').datepicker('option', 'maxDate', dt);
    }
    $('#bookCalendar').click(function() {
        $('#arrivalDate').trigger('focus');
    });
    $('#btnSubmitBooking').click(function() {
        submitBookingWidget();
    });
    function submitBookingWidget() {
        //var urlToUse = "https://www.myfidelio.net/webui/AvailabilitySearch.aspx";
        var queryString = "?arrival=" + $('#arrivalDate').val();

        var diff_date = Math.round((Date.parse($('#departureDate').val()) - Date.parse($('#arrivalDate').val())) / 86400000);
        queryString += "&nights=" + diff_date;

        queryString += "&rooms=1";

        var a = $('#Adults').val();
        var c = $('#Children').val();
        queryString += "&adults=" + a + "&children=" + c;

        window.open("/FormPost.htm" + queryString, "myfidelio");
    }
    /* end booking widget */

    $('.propertyViewLocation').fancybox();
    // view floorplan on accommodation detail
    $('.viewFloorplan').fancybox();
    // any link tagged to open in a lightbox
    $('.quickLightbox').fancybox();
    $('.quickLightboxIframe').fancybox({
        'width': '90%',
        'height': '90%',
        //'autoScale': false,
        'type': 'iframe'
    });
    $('.digitalBrochure').fancybox({
        'type': 'swf',
        'swf': {
            'wmode': 'transparent',
            'allowfullscreen': 'true'
        },
        'width': 960,
        'height': 400
    });
    /* landing page promo gallery */
    $(function() {

        $(".scrollablePromoGallery").scrollable({
            onBeforeSeek: function(evt, idx) {
                $('.scrollablePromoGallery').css('width', '648px');
            }
        });

        function repositionImage(container, image) {
            var containerHeight = $(container).height();
            var imgHeight = $(image).height();

            var diff = parseInt((imgHeight - containerHeight) / 2);
            $(image).css('margin-top', '-' + diff + 'px');
        }

        repositionImage($('.promoGalleryPhoto'), $('.promoGalleryPhoto img.promoGalleryLarge'));

        // fix each thumb - comment out if we want to vertically center each thumbnail
        /*
        $('.scrollablePromoGallery .thumbnail img').each(function() {
        repositionImage($(this).parent(), $(this));
        });
        */

        $(".scrollablePromoGallery .thumbnail img").click(function() {
            var dataContainer = $(this).parent().parent();
            var title = $(dataContainer).find('.promoTitle').text();
            var description = $(dataContainer).find('.promoDescription').html();
            var linkText = $(dataContainer).find('.linkText').text();
            var linkUrl = $(dataContainer).find('.linkUrl').text();
            var linkTarget = $(dataContainer).find('.linkTarget').text();
            var fullImage = $(this).attr('full');

            $('.promoGalleryMidWrapper').fadeOut('fast', function() {
                $('.promoGalleryTitle').html(title);
                $('.promoGalleryDescription').html(description);

                if (linkUrl != '') {
                    $('.promoGallerySelectedButton').text(linkText);
                    $('.promoGallerySelectedButton').attr('href', linkUrl);
                    $('.promoGallerySelectedButton').attr('target', linkTarget);
                    $('.promoGallerySelectedButton').show();
                }
                else {
                    $('.promoGallerySelectedButton').hide();
                }

                $('.promoGalleryPhoto img.promoGalleryLarge').load(function() {
                    repositionImage($('.promoGalleryPhoto'), $(this));
                    $('.promoGalleryMidWrapper').fadeIn('fast');
                });

                $('.promoGalleryPhoto img.promoGalleryLarge').attr('src', fullImage);

            });
        });
    });
    /* end landing page promo gallery */

    $(".quickPhotoGalleryThumbs img[title]").tooltip({
        tipClass: 'quickPgTooltip',
        effect: 'slide',
        offset: [10, 0]
    });
});
function buildQuickPhotoGalleryArray() {
    quickPhotosArray = new Array();
    $('.quickPhotoGalleryThumbs:visible').find('.quickPhotoGalleryThumbnail').each(function() {
        quickPhotosArray.push($(this).attr('full'));
    });
}
function addFormField(form, fieldName, fieldValue) {
    var inputField = document.createElement("input");
    inputField.setAttribute("name", fieldName);
    inputField.setAttribute("id", fieldName);
    inputField.setAttribute("type", "hidden");
    inputField.setAttribute("value", fieldValue);
    form.appendChild(inputField);
}


