$(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, -133],
        effect: 'slidedownup',
        onBeforeShow: function() {
            $('#nav_1').addClass('navActive');
        },
        onHide: function() {
            $('#nav_1').removeClass('navActive');
        }
    });

    $("#nav_2").tooltip({
        relative: true,
        position: 'bottom right',
        offset: [0, -149],
        effect: 'slidedownup',
        onBeforeShow: function() {
            $('#nav_2').addClass('navActive');
        },
        onHide: function() {
            $('#nav_2').removeClass('navActive');
        }
    });

    $("#nav_3").tooltip({
        relative: true,
        position: 'bottom right',
        offset: [0, -156],
        effect: 'slidedownup',
        onBeforeShow: function() {
            $('#nav_3').addClass('navActive');
        },
        onHide: function() {
            $('#nav_3').removeClass('navActive');
        }
    });

    $("#nav_4").tooltip({
        relative: true,
        position: 'bottom right',
        offset: [0, -208],
        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: false,
        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");
        $('#formHeadSearch').css("display", "block");
        $.cookie('headerForm', 'search');
    });
    // show email - hide search and cookie as email
    $('.showEmail').click(function() {
        $('#formHeadEmail').css("display", "block");
        $('#formHeadSearch').css("display", "none");
        $.cookie('headerForm', 'email');
    });

    //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') {
        $('#formHeadEmail').css("display", "block");
        $('#formHeadSearch').css("display", "none");
    };
    //if headerForm cookie is set to search then show search and hide email
    if (headerForm == 'search') {
        $('#formHeadEmail').css("display", "none");
        $('#formHeadSearch').css("display", "block");
    };

    /* 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() {
        $('#targetHeaderFormMoreInfo1').toggle();
        $('#toggleHeaderFormMoreInfo1').toggleClass('on');
    });

    /* use the class .draggable to make items draggable */
    $(function() {
        $("div.containmentArea").each(function() {
            var $image = $(this).find("img");
            var viewareaWidth = 1000;
            var viewareaHeight = 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 imgLeft = -1 * (containerLeft - parseFloat($image.css('left')));
            var imgTop = -1 * (containerTop - parseFloat($image.css('top')));

            if (imgWidth > viewareaWidth || imgHeight > viewareaHeight) {
                $(this).css({
                    'width': containerWidth,
                    'height': containerHeight,
                    'position': 'relative',
                    'left': containerLeft,
                    'top': containerTop
                });

                $image.css({
                    'left': imgLeft,
                    'top': imgTop
                });

                $(this).find(".draggable").draggable({ containment: 'parent' });
            }
            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("div.panesModMediaGallery > 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: true,
        circular: true,
        loop: true
        //}).circular().navigator({navi:'.naviSpec' }).autoscroll({interval: 7000});
    }).navigator({ navi: '.naviSpec' }).autoscroll({ interval: 7000 });

    //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();
        }
    });




    // 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');


    // uber map
    $('#boxContentMap, #homeMap').click(function() {
        popWindow('/map.aspx', 'map', 1024, 725, false);
    });
});

function popWindow(url, popname, popwidth, popheight, scrolling) {

	window.name = "daddy";
	var scrollBoolean = "yes";
	if (scrolling) {
		scrollBoolean = scrolling;
	}
	var newWidth = popwidth;
	var newHeight = popheight;
	var newTop = 0, newLeft = 0;
	if (window.outerHeight) {
		newTop = window.screenY + ((window.outerHeight - newHeight) / 2);
	}
	else {
		newTop = window.screenTop + ((document.body.offsetHeight - newHeight) / 2);
	}
	if (window.outerWidth) {
		newLeft = window.screenX + ((window.outerWidth - newWidth) / 2);
	}
	else {
		newLeft = window.screenLeft + ((document.body.offsetWidth - newWidth) / 2);
	}
	var popupWin = window.open(url, popname,
		'screenX=' + newLeft +
		',screenY=' + newTop +
		',left=' + newLeft +
		',top=' + newTop +
		',width=' + newWidth +
		',height=' + newHeight +
		',menubar=no' +
		',toolbar=no' +
		',location=no' +
		',status=no' +
		',resizable=yes' +
		',scrollbars=' + scrollBoolean);
}

