﻿//-----------------------------------------------------------------------------
// <description>
//    This file contains the javascript functions that support the PSE.com top
//    navigation control.
// </description>
// <author>Vertex Seattle</author>
// <created>07/28/2010</created>
// <updatelog>
// Date         Developer       Description
//-----------------------------------------------------------------------------
// 12/08/2021   John Doe        Sample
// </updatelog>
//-----------------------------------------------------------------------------
$(document).ready(function () {

    function megaHoverOver() {
        $(this).find(".dropMenu").stop().fadeTo('fast', 1).show();

        //Calculate width of all ul's
        (function ($) {
            jQuery.fn.calcSubWidth = function () {
                rowWidth = 0;
                //Calculate row
                $(this).find("ul").each(function () {
                    rowWidth += $(this).width();
                });
            };
        })(jQuery);

        if ($(this).find(".dropMenuRow").length > 0) {
            var biggestRow = 0;
            $(this).find(".dropMenuRow").each(function () {
                $(this).calcSubWidth();
                if (rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });
            $(this).find(".dropMenu").css('width', biggestRow);
        }
        else {
            $(this).calcSubWidth();
            //Set Width
            $(this).find(".dropMenu").css('width', rowWidth);
        }

        var windowWidth = $(window).width();
        var dropMenuWidth = $(this).find(".dropMenu").width();
        var dropMenuArrow = $(this).find(".dropMenuArrow");
        var dropMenuOffset = 1;
        var dropMenuArrowOffset = 1;

        //get top menu left offset
        var topMenuOffset = $(this).offset();
        var topMenuCenter = parseInt(topMenuOffset.left + ($(this).width() / 2));

        //if the drop menu width will fit within the page try to center it on the top menu option
        if (dropMenuWidth < windowWidth) {
            dropMenuOffset = parseInt(topMenuCenter - dropMenuWidth / 2);

            if (dropMenuOffset < 1) {
                dropMenuOffset = 1;
            }

            if ((dropMenuOffset + dropMenuWidth) > windowWidth) {
                dropMenuOffset = dropMenuOffset + dropMenuWidth - windowWidth;
            }
        }

        //drop menu arrow is 49 pixels wide - adjust offset by half that i.e. 25
        dropMenuArrowOffset = topMenuCenter - dropMenuOffset - 25;
        dropMenuArrow.css('left', dropMenuArrowOffset + 15);

        //drop menu offset is relative to top menu item so adjust
        dropMenuOffset = dropMenuOffset - topMenuOffset.left;

        $(this).find(".dropMenu").css("left", dropMenuOffset);

        var ribbonOffset = +30;
        //if ($("#s4-ribbonrow").length > 0) {
        //    ribbonOffset = ribbonOffset + $("#s4-ribbonrow").offsetHeight;
        //}

       $(this).find(".dropMenu").css("top", ribbonOffset);
    }

    function megaHoverOut() {
        $(this).find(".dropMenu").stop().fadeTo('fast', 0, function () {
            $(this).hide();
        });
    }


    var config = {
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
        interval: 300, // number = milliseconds for onMouseOver polling interval    
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
        timeout: 300, // number = milliseconds delay before onMouseOut    
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
    };
    $("ul#topMenuList li .dropMenu").css({ 'opacity': '0' });
    $("ul#topMenuList li").hoverIntent(config);
});


