// JavaScript Document

function s1() {
$('#s1')
		.after('<div id="nav-slides">')
		.cycle({
			fx: 'scrollLeft', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			speed:    1000, // defines the number of milliseconds it will take to transition from one slide to the next.
			timeout:  4600, // specifies how many milliseconds will elapse between the start of each transition
			pause: 1, // so that pauses when user hovers over a slide
			delay:  2000 // set a delay before 1st slide starts transitioning
			//pager: '#nav-slides' //instructs the plugin to create navigation elements, one for each slide, and add them to the container identified by the value of the pager option.
		});
}

function packages() {
$('#packages-slides-wrap')
		//.after('<div id="package-slides">')
		.cycle({
			fx: 'scrollLeft', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			speed:    500, // defines the number of milliseconds it will take to transition from one slide to the next.
			timeout:  0, // specifies how many milliseconds will elapse between the start of each transition
			pause: 1, // so that pauses when user hovers over a slide
			delay:  2000, // set a delay before 1st slide starts transitioning
			prev:   '#pkg-prev', 
    		next:   '#pkg-next', 
			//after:   onAfter,
			//pager: '#package-slides' //instructs the plugin to create navigation elements, one for each slide, and add them to the container identified by the value of the pager option.
		});
}

function onAfter(curr, next, opts) {
    var index = opts.currSlide;
    $('#pkg-prev')[index == 0 ? 'hide' : 'show']();
    $('#pkg-next')[index == opts.slideCount - 1 ? 'hide' : 'show']();
}

// BEGIN: Tool Tip Engine Copyrite: crystalsholidays.com
var arrowSide;
var toolTipMother;
var proceedFadeOut;
var lastObjectHovered;
var triggerDelay = 300;

// Fading smoothness depends on browser, tooltip size and gfx card
var fadeInSpeed = 200;
var fadeOutSpeed = 300;


function fadeOutVoyageOverlay() {
    $("#theToolTip").fadeOut();
    $("#toolTipArrow").fadeOut(fadeOutSpeed);
}

function initVoyageOverlay() {

    // Add overlay tooltip containers if page has voyage modules tooltips defined.
    if ($(".hiddenOverlayContainer").length > 0) {
        $("body").append("<div id='toolTipArrow'>&nbsp;</div>")
			.append("<div id='theToolTip'></div>");
    }
    else { return }

    $(window).bind('resize', function () {
        position(toolTipMother, "#theToolTip");
    }).bind('scroll', function () {
        position(toolTipMother, "#theToolTip");
    });

    // Hover/Mouse over function (using hover to cover also child elements)
    $(".theHoverLay").hover(function (e) {
        //setTimeout(fadeOut,100);
        setTimeout(displayTooltip, 100 + triggerDelay)
        var mother = $(this);
        toolTipMother = mother;

        function displayTooltip() {
            if (mother != toolTipMother) { return }
            var toolTipContent = $(mother).find(".hiddenOverlayContainer").html();
            if (toolTipContent == null) { return }
            $("#theToolTip").html(toolTipContent);
            if (position(toolTipMother, "#theToolTip") == "position failed") { return }
            $("#theToolTip").fadeIn(fadeInSpeed);
            $("#toolTipArrow").fadeIn(fadeInSpeed);
        }
        function fadeOut() {
            if (proceedFadeOut) {
                $("#theToolTip").fadeOut(fadeOutSpeed);
                $("#toolTipArrow").fadeOut(fadeOutSpeed);
            }
            proceedFadeOut = true;
        }

        // Hover out function
    }, function () {
        toolTipMother = null;
        proceedFadeOut = true;
        setTimeout(fadeOut, 350);
        function fadeOut() {
            if (proceedFadeOut) {
                $("#theToolTip").fadeOut(fadeOutSpeed);
                $("#toolTipArrow").fadeOut(fadeOutSpeed);
            }
        }
    })

    // Hover over function
    $("#theToolTip").hover(function () {
        proceedFadeOut = false;
        $(this).fadeIn(200);
        $("#toolTipArrow").fadeIn(fadeInSpeed);

        // Hover out function
    }, function () {
        $(this).fadeOut();
        $("#toolTipArrow").fadeOut(fadeOutSpeed);
    })
}

$(document).ready(function () {
    initVoyageOverlay();
});

// Calculate Voyage Module Tooltip position
function position(mother, toolTip) {
    if (mother == null) { return }
    var toolTipX, toolTipY;
    var offsetX = 0;
    var offsetY = 5;
    var sTop = $(document).scrollTop();
    var sLeft = $(document).scrollLeft();
    var winWidth = $(window).width();
    var winHeight = $(window).height();
    var motherTop = $(mother).offset().top;
    var motherLeft = $(mother).offset().left;
    var arrowWidth = 23;
    var arrowHeight = $("#toolTipArrow").height();
    var motherWidth = $(mother).width();
    var motherHeight = $(mother).height();
    var toolTipWidth = $(toolTip).width();
    var toolTipHeight = $(toolTip).height();
    var spaceAvailableAbove = motherTop + (motherHeight / 2) - sTop;
    var spaceAvailableToTheLeft = motherLeft - sLeft - offsetX;
    var spaceAvailableToTheRight = winWidth - motherLeft - motherWidth - offsetX + sLeft;
    var spaceAvailableUnderneath = winHeight - (motherHeight / 2) - motherTop + sTop;

    // Determine horizontal position
    if (spaceAvailableToTheRight < toolTipWidth) {

        if (spaceAvailableToTheLeft < spaceAvailableToTheRight) {
            // Display to the right
            toolTipX = motherWidth + motherLeft + offsetX + arrowWidth;
        }
        else {
            //Display to the left
            toolTipX = motherLeft - offsetX - toolTipWidth - arrowWidth;
            arrowSide = "left";
        }
    } else {
        // Display to the right
        toolTipX = motherWidth + motherLeft + offsetX + arrowWidth;
        arrowSide = "right";

    }
    // Determine vertical position
    if ((winHeight) < toolTipHeight) {
        toolTipY = sTop - offsetY;
    }
    else if (spaceAvailableUnderneath < -20) {
        return "position failed";
    }
    else if (spaceAvailableUnderneath < (toolTipHeight / 2)) {
        toolTipY = winHeight + sTop - toolTipHeight - offsetY;
    }
    else if (spaceAvailableAbove < 0) {
        return "position failed";
    }
    else if (spaceAvailableAbove < (toolTipHeight / 2)) {
        toolTipY = sTop + offsetY;
    }
    else {
        toolTipY = motherTop + (motherHeight / 2) - (toolTipHeight / 2);
    }

    $(toolTip).css({
        top: toolTipY,
        left: toolTipX
    });
    displayArrow(mother);

    function displayArrow(mother) {
        var toolTipWidth = $(toolTip).width();
        var toolTipHeight = $(toolTip).height();
        var arrowX = toolTipX - arrowWidth + 1;
        var arrowY = toolTipY + (toolTipHeight / 2) - arrowHeight / 2;
        var toolTipArrow = $("#toolTipArrow");
        var activityClass = (mother.parents('.shoreExcursion').length > 0) ? true : false;

        if (arrowSide == "left") {
            toolTipArrow.removeClass("activityTipLeft").removeClass("activityTipRight");
            (activityClass) ? toolTipArrow.addClass("activityTipRight").removeClass("activityTipLeft") : null;
            toolTipArrow.addClass("vmOverlayArrowRight").removeClass("vmOverlayArrowLeft");
            arrowX = toolTipX + toolTipWidth - 2;
        } else {
            toolTipArrow.removeClass("activityTipLeft").removeClass("activityTipRight");
            (activityClass) ? toolTipArrow.removeClass("activityTipRight").addClass("activityTipLeft") : null;
            toolTipArrow.removeClass("vmOverlayArrowRight").addClass("vmOverlayArrowLeft");
            arrowX = toolTipX - arrowWidth + 1;
        }
        if (arrowY > motherTop + motherHeight / 2 - arrowHeight / 2) {
            arrowY = motherTop + motherHeight / 2 - arrowHeight / 2;
        }
        if (arrowY < motherTop + motherHeight / 2 - arrowHeight / 2) {
            arrowY = motherTop + motherHeight / 2 - arrowHeight / 2;
        }
        if ((motherTop - sTop + motherHeight / 2 - arrowHeight / 2) < 0) {
            arrowY = toolTipY;
        }
        // mother object is beneath the fold
        if ((motherTop - winHeight - sTop) > 0) {
            arrowY = winHeight + sTop - arrowHeight - 3;
        }
        // mother object is close to beneath the fold
        if ((motherTop - winHeight - sTop + arrowHeight / 2 + motherHeight / 2) > 0) {
            arrowY = winHeight + sTop - arrowHeight - 5;
        }
        toolTipArrow.css({ top: arrowY, left: arrowX });
    }
}
// END: Tool Tip Engine
