$(document).ready(function() {

    // BEGIN MAIN DROP DOWN NAVIGATION
    // documentation - flowplayer.org/tools/tooltip.html


    // 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();
	        });
	    }
	);

    $("#nav_1").tooltip({
        relative: true,
        position: 'bottom right',
        offset: [0, -149],
        effect: 'slidedownup',
        onBeforeShow: function() {
            $('#nav_1').addClass('navActive');
        },
        onHide: function() {
            $('#nav_1').removeClass('navActive');
        }
    });

    $("#nav_2").tooltip({
        relative: true,
        position: 'bottom right',
        offset: [0, -104],
        effect: 'slidedownup',
        onBeforeShow: function() {
            $('#nav_2').addClass('navActive');
        },
        onHide: function() {
            $('#nav_2').removeClass('navActive');
        }
    });

    $("#nav_3").tooltip({
        relative: true,
        position: 'bottom right',
        offset: [0, -105],
        effect: 'slidedownup',
        onBeforeShow: function() {
            $('#nav_3').addClass('navActive');
        },
        onHide: function() {
            $('#nav_3').removeClass('navActive');
        }
    });

    $("#nav_4").tooltip({
        relative: true,
        position: 'bottom right',
        offset: [0, -105],
        effect: 'slidedownup',
        onBeforeShow: function() {
            $('#nav_4').addClass('navActive');
        },
        onHide: function() {
            $('#nav_4').removeClass('navActive');
        }
    });

    $("#nav_5").tooltip({
        relative: true,
        position: 'bottom right',
        offset: [0, -110],
        effect: 'slidedownup',
        onBeforeShow: function() {
            $('#nav_5').addClass('navActive');
        },
        onHide: function() {
            $('#nav_5').removeClass('navActive');
        }
    });


    // END MAIN DROP DOWN NAVIGATION



    // showhide home page booking form
    $('#book_home').showhide({
        target_obj: $('#boxBookForm'),
        default_open: bookingWidgetShowOpen,
        plus_class: 'plus',
        minus_class: 'minus',
        plus_text: '',
        minus_text: ''
    });
    $('#boxBookSub').showhide({
        target_obj: $('#boxBookFormSub'),
        default_open: bookingWidgetShowOpen,
        plus_class: 'plus',
        minus_class: 'minus',
        plus_text: '',
        minus_text: ''
    })


    // show search - hide email and cookie as search
    $('.showSearch').click(function() {
        $('#formHeadEmail').css("display", "none");
        $('#formHeadItinerary').css("display", "none");
        $('#formHeadAccount').css("display", "none");
        $('#formHeadSearch').css("display", "block");
        $.cookie('headerForm', 'search');
    });
    // show email - hide search and cookie as email
    $('.showEmail').click(function() {
        $('#formHeadEmail').css("display", "block");
        $('#formHeadItinerary').css("display", "none");
        $('#formHeadAccount').css("display", "none");
        $('#formHeadSearch').css("display", "none");
        $.cookie('headerForm', 'email');
    });
    // show itinerary - hide search and cookie as email
    $('.showItinerary').click(function() {
        $('#formHeadEmail').css("display", "none");
        $('#formHeadItinerary').css("display", "block");
        $('#formHeadAccount').css("display", "none");
        $('#formHeadSearch').css("display", "none");
        $.cookie('headerForm', 'itinerary');
    });
    // show email - hide search and cookie as email
    $('.showAccount').click(function() {
        $('#formHeadEmail').css("display", "none");
        $('#formHeadItinerary').css("display", "none");
        $('#formHeadAccount').css("display", "block");
        $('#formHeadSearch').css("display", "none");
        $.cookie('headerForm', 'account');
    });

    //COOKIE for header email - search tabs
    //header form state
    var headerForm = $.cookie('headerForm');
    //if headerForm cookie is set to email then show email and hide search
    if (headerForm == 'email') {
        $('.showEmail').click();
    };
    //if headerForm cookie is set to search then show search and hide email
    if (headerForm == 'search') {
        $('.showSearch').click();
    };
    if (headerForm == 'itinerary') {
        $('.showItinerary').click();
    };
    if (headerForm == 'account') {
        $('.showAccount').click();
    };

    /* toggle between two maps in map overlay */
    $('.toggleMaps').click(function() {
        $("[id$='boxMap']").each(function() { $(this).hide(); });

        var itemToShow = $(this).attr("item");
        $("[id$='" + itemToShow + "']").show();

        //$('#boxMap1').toggle();
        //$('#boxMap2').toggle();
    });

    /* show-hide more info for search form */
    $('#toggleHeaderFormMoreInfo1').click(function(ev) {
        ev.preventDefault();
        $('#targetHeaderFormMoreInfo1').toggle();
        $('#toggleHeaderFormMoreInfo1').toggleClass('on');
    });
    $('#toggleHeaderFormMoreInfo2').click(function(ev) {
        ev.preventDefault();
        $('#targetHeaderFormMoreInfo2').toggle();
        $('#toggleHeaderFormMoreInfo2').toggleClass('on');
    });
    $('#toggleHeaderFormMoreInfo3').click(function(ev) {
        ev.preventDefault();
        $('#targetHeaderFormMoreInfo3').toggle();
        $('#toggleHeaderFormMoreInfo3').toggleClass('on');
    });

    /* use the class .draggable to make items draggable */
    $(function() {
        $("div.containmentArea").each(function() {
            var $image = $(this).find("img");
            var windowWidth = $(window).width();
            var windowHeight = $(window).height();
            var viewareaWidth = (windowWidth < 1100) ? windowWidth - 100 : 1000;
            var viewareaHeight = (windowHeight < 700) ? windowHeight - 100 : 600;
            var imgWidth = parseFloat($image.css("width"));
            var imgHeight = parseFloat($image.css("height"));
            var containerWidth = imgWidth * 2 - viewareaWidth;
            var containerHeight = imgHeight * 2 - viewareaHeight;
            var containerLeft = -1 * (containerWidth - viewareaWidth) / 2;
            var containerTop = -1 * (containerHeight - viewareaHeight) / 2;

            var imgLeftOld = ($image.css('left') == "auto") ? 0 : parseFloat($image.css('left'));
            var imageTopOld = ($image.css('top') == "auto") ? 0 : parseFloat($image.css('top'));
            var imgLeft = -1 * (containerLeft - imgLeftOld);
            var imgTop = -1 * (containerTop - imageTopOld);

            if (imgWidth > viewareaWidth || imgHeight > viewareaHeight) {
                $(this).css({
                    'width': containerWidth,
                    'height': containerHeight + 35,
                    'position': 'relative',
                    'left': containerLeft,
                    'top': containerTop - 35
                });

                $image.css({
                    'left': imgLeft,
                    'top': imgTop + 35
                });

                $("#maps").css({ width: viewareaWidth, height: viewareaHeight });
                $("div.boxMapImage").css({ width: viewareaWidth, height: viewareaHeight - 30 });

                if (imgWidth > viewareaWidth && imgHeight > viewareaHeight)
                    $(this).find(".draggable").draggable({ containment: 'parent' });
                else if (imgWidth > viewareaWidth)
                    $(this).find(".draggable").draggable({ containment: 'parent', axis: 'x' });
                else
                    $(this).find(".draggable").draggable({ containment: 'parent', axis: 'y' });
            }
            else {
                $image.css('cursor', 'default');
            }
        });
    });


    // BEGIN MODULES

    //WRAPPER RIGHT COLUMN ACCORDION
    $("#accordion").tabs("#accordion div.pane", { tabs: 'h2', effect: 'slide', initialIndex: null });

    //MEDIA GALLERY: TABS
    // setup ul.tabsModMediaGallery to work as tabs for each div directly under div.panesModMediaGallery
    $("ul.tabsModMediaGallery").tabs("[id$='pnlPanes'] > div");

    //MEDIA GALLERY - PHOTOS: SCROLLABLE
    // initialize scrollable together with the circular plugin
    //$("#modGalleryPhotos").scrollable({ 
    $('.scrollableGalleryPhotos').scrollable({
        size: 1,
        clickable: true,
        circular: true,
        next: '.nextMTE',
        prev: '.prevMTE',
        onSeek: function(event) {
            event.preventDefault();
            //$('div.pageindex').text(this.getPageIndex()+1);
            //$('div.pageamount').text(this.getPageAmount());
            event.target.getRoot().parent().find('div.pageindex').text(this.getIndex() + 1);
            //event.target.getRoot().parent().find('div.pageamount').text(this.getPageAmount());
        }
    });
    // generate values for initial load: 1 of X
    //$('div.pageindex').text(1);
    //$('div.pageamount').text($(".modItemGalleryPhotos").length - $("#modGalleryPhotos .cloned").length);

    $('.scrollableGalleryPhotos').each(function() {
        $(this).parent().find('div.pageindex').text(1);
        $(this).parent().find('div.pageamount').text($(this).find('.modItemGalleryPhotos').length - $(this).find('.cloned').length);
    });

    //SPECIALS: SCROLLABLE
    // initialize scrollable together with the circular, autoscroll & navigator plugins
    //$("#modSpecials").scrollable({ 
    $('.scrollableSpecials').scrollable({
        size: 1,
        clickable: false,
        circular: true,
        loop: true
        //}).circular().navigator({navi:'.naviSpec' }).autoscroll({interval: 7000});
    }).navigator({ navi: '.naviSpec' }).autoscroll({ interval: 7000 });

    $('.scrollableGallery1').scrollable({
        size: 5,
        next: '.nextPage',
        prev: '.prevPage'
        //}).circular().navigator({navi:'.naviSpec' }).autoscroll({interval: 7000});
        //}).navigator({ navi: '.boxGallery1Nav' });
    });

    //ITINERARY: SCROLLABLE
    // initialize scrollable together with the circular, autoscroll & navigator plugins
    $("#modItinerary").scrollable({
        size: 1,
        clickable: true,
        circular: true
    }).navigator({ navi: '.naviItin' }).autoscroll({ interval: 7000 });

    //END ACCORDION MODS


    // show the multimedia tabs only if there are > 1 tabs to show
    $('.tabsModMediaGallery').each(function() {
        if ($(this).find('li').length > 1) {
            $(this).show();
        }
    });




    // qtvr
    $("a.qtvr").each(function() {

        var el = $(this);
        var target = $('#qtvr');

        $(target).appendTo("body");
        el.overlay({
            target: target
        });
        el.addClass("pointer");

        $(this).click(function() {
            $('#qtvrContainer').html("");

            var embedCode = '<object classid="clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b" width="450" height="316" codebase="http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0">';
            embedCode += '<param name="src" value="' + $(this).attr("rel") + '" />';
            embedCode += '<param name="qtsrc" value="' + $(this).attr("rel") + '" />';
            embedCode += '<param name="autoplay" value="true" />';
            embedCode += '<param name="loop" value="false" />';
            embedCode += '<param name="controller" value="true" />';
            embedCode += '<embed type="video/quicktime" width="450" height="316" src="' + $(this).attr("rel") + '" controller="true" loop="false" autoplay="true" qtsrc="' + $(this).attr("rel") + '"></embed>';
            embedCode += '</object>';

            $('#qtvrContainer').html(embedCode);
            $('#qtvrCaption').html($(this).attr('title'));
        });
    });



    // overlay
    //$("a[rel]").not("a.qtvr").overlay().addClass("pointer");


    // overlay
    $("img[rel]").overlay().addClass("pointer");
    //$("a[rel]").overlay().addClass("pointer");

    $("a[rel]").each(function() {
        var el = $(this);
        var target = el.attr("rel");
        $(target).appendTo("body");
        el.overlay({ target: target });
        el.addClass("pointer");
    });

    // row striping for content tables
    $('.tblContent tr:odd').addClass('tblAltRow');


});
