$(document).ready(function() {

    if ($('.stay-visible').length) {
        $('.stay-visible').each(function(idx, elem) {
            $(elem).stayVisible();
        });
    }


    $(window).scroll(function() {

        if ($('.stay-visible').length) {
            $('.stay-visible').each(function(idx, elem) {
                $(elem).stayVisible();
            });
        }
    });

    $(window).resize(function() {
        if ($('.stay-visible').length) {
            $('.stay-visible').each(function(idx, elem) {
                $(elem).stayVisible();
            });
        }
    });



});


$.fn.stayVisible = function() {
    var additTop = 0;
    var additBtm = 0;
    var stayElem = $(this);
    var excludeHeight = 0;


    if ($.inArray(currScrSize, $(this).data('sv-size')) >= 0) {


        $($(this).data('stacktop')).each(function(idx, elem) {
            additTop = $(elem)[0].getBoundingClientRect().bottom;
        });

        //console.log('additTop'+additTop);
        //if ($('.nav-block.sticky-top.active').length){
        additBtm = $('footer').offset().top - additTop;
        //}

        var visBlkHeight = $(this).outerHeight();

        if ($(this).parent().is('.vsb-si')) {
            var orgTop = $(this).parent().offset().top - additTop;
        } else {
            var orgTop = $(this).offset().top - additTop;
        }


        if ($(document).scrollTop() > orgTop) {
            //console.log(orgTop);
            if (!$(this).parent().is('.vsb-si')) {

                $(this).wrap('<div class="vsb-si ' + $(this).data('sv-class') + '" style="display:' + $(this).css('display') + '; position:' + $(this).css('position') + '; height:' + visBlkHeight + 'px;"></div>');
                orgTop = $(this).parent().offset().top;
                $(this).addClass($(this).data('sv-class'));
            }

            //console.log(additBtm);

            if ($('.captain-col.col-12').length) {
                excludeHeight = $('.captain-col.col-12').height();
            }

            if ($(document).scrollTop() < additBtm - visBlkHeight - 20) {
                //$(this).css({'position':'fixed','top':($(document).scrollTop()-orgTop)+'px','width':$(this).parent().width()+'px','z-index':'100'});
                $(this).css({
                    'position': 'fixed',
                    'top': (additTop - excludeHeight) + 'px',
                    'z-index': '100'
                });
                //,'width':$(this).parent().width()+'px'
                // $(this).parent().css({
                //'height': $(this).outerHeight() + 'px'
                // });
            }

            //$(this).width($(this).parent('.vsb-si').eq(0).width());

        } else {
            //$(stayElem).unwrap();
            $(stayElem).css({
                'position': '',
                'top': ''
            });
        }

        //$(this).width($(this).parent('.vsb-si').eq(0).width());
    } else {
        if ($(this).parent().is('.vsb-si')) {
            $(this).unwrap('.vsb-si');
        }
        $(this).removeClass($(this).data('sv-class')).removeAttr('style');
    }
}

var nav_timer;

$(document).on({
    mouseenter: function() {
        if ((currScrSize == 'xxl') || (currScrSize == 'xl') || (currScrSize == 'lg')) { //
            var _currElem = $(this);
            var _currObj = $(this).children('.item-wrapper').children('.menu-caret'),
                _currSubMenu = $($(_currObj).data('target'));
            nav_timer = setTimeout(function() {


                if ($(_currElem).not('.trigger')) {
                    $('[data-bs-toggle="navmenu"][data-bs-target="' + $(_currObj).data('bs-target') + '"]').addClass('trigger');
                    $(_currElem).addClass('trigger');
                }

                $($(_currObj).data('bs-target')).slideDown(300, function() {
                    $($(_currObj).data('bs-target')).addClass('show');
                    $($(_currObj).data('bs-target')).css('display', '');
                });
            }, 100);
        }

    },
    mouseleave: function() {
        if ((currScrSize == 'xxl') || (currScrSize == 'xl') || (currScrSize == 'lg')) { //
            clearTimeout(nav_timer);
            var _currObj = $(this).children('.item-wrapper').children('.menu-caret'),
                _currSubMenu = $($(_currObj).data('bs-target'));

            if ($(this).is('.trigger')) {
                $('[data-bs-toggle="navmenu"][data-bs-target="' + $(_currObj).data('target') + '"]').removeClass('trigger');
                $(this).removeClass('trigger');

                $($(_currObj).data('target')).find('.nav-menu.show').each(function(index, element) {
                    $(this).removeClass('show');
                    $('[data-bs-toggle="navmenu"][data-bs-target="#' + $(this).attr('id') + '"]').removeClass('trigger');
                });
            }
            $($(_currObj).data('bs-target')).slideUp(300, function() {
                $($(_currObj).data('bs-target')).css('display', '');
                $($(_currObj).data('bs-target')).removeClass('show');
            });

        }
    }


}, ".nav-block .nav-item, .nav-block .menu-item");