jQuery(document).ready(function($) {
    'use strict';

    if(gridflex_ajax_object.secondary_menu_active){

        $(".gridflex-nav-secondary .gridflex-secondary-nav-menu").addClass("gridflex-secondary-responsive-menu");

        $( ".gridflex-secondary-responsive-menu-icon" ).on( "click", function() {
            $(this).next(".gridflex-nav-secondary .gridflex-secondary-nav-menu").slideToggle();
        });

        $(window).on( "resize", function() {
            if(window.innerWidth > 1112) {
                $(".gridflex-nav-secondary .gridflex-secondary-nav-menu, nav .sub-menu, nav .children").removeAttr("style");
                $(".gridflex-secondary-responsive-menu > li").removeClass("gridflex-secondary-menu-open");
            }
        });

        $( ".gridflex-secondary-responsive-menu > li" ).on( "click", function(event) {
            if (event.target !== this)
            return;
            $(this).find(".sub-menu:first").toggleClass('gridflex-submenu-toggle').parent().toggleClass("gridflex-secondary-menu-open");
            $(this).find(".children:first").toggleClass('gridflex-submenu-toggle').parent().toggleClass("gridflex-secondary-menu-open");
        });

        $( "div.gridflex-secondary-responsive-menu > ul > li" ).on( "click", function(event) {
            if (event.target !== this)
                return;
            $(this).find("ul:first").toggleClass('gridflex-submenu-toggle').parent().toggleClass("gridflex-secondary-menu-open");
        });

    }

    if(gridflex_ajax_object.primary_menu_active){

        // grab the initial top offset of the navigation 
        var gridflexstickyNavTop = $('.gridflex-primary-menu-container').offset().top;
        
        // our function that decides weather the navigation bar should have "fixed" css position or not.
        var gridflexstickyNav = function(){
            var gridflexscrollTop = $(window).scrollTop(); // our current vertical position from the top
                 
            // if we've scrolled more than the navigation, change its position to fixed to stick to top,
            // otherwise change it back to relative

            if(window.innerWidth > 1112) {
                if (gridflexscrollTop > gridflexstickyNavTop) {
                    $('.gridflex-primary-menu-container').addClass('gridflex-fixed');
                } else {
                    $('.gridflex-primary-menu-container').removeClass('gridflex-fixed'); 
                }
            }
        };

        gridflexstickyNav();
        // and run it again every time you scroll
        $(window).on( "scroll", function() {
            gridflexstickyNav();
        });

        $(".gridflex-nav-primary .gridflex-primary-nav-menu").addClass("gridflex-primary-responsive-menu");

        $( ".gridflex-primary-responsive-menu-icon" ).on( "click", function() {
            $(this).next(".gridflex-nav-primary .gridflex-primary-nav-menu").slideToggle();
        });

        $(window).on( "resize", function() {
            if(window.innerWidth > 1112) {
                $(".gridflex-nav-primary .gridflex-primary-nav-menu, nav .sub-menu, nav .children").removeAttr("style");
                $(".gridflex-primary-responsive-menu > li").removeClass("gridflex-primary-menu-open");
            }
        });

        $( ".gridflex-primary-responsive-menu > li" ).on( "click", function(event) {
            if (event.target !== this)
            return;
            $(this).find(".sub-menu:first").toggleClass('gridflex-submenu-toggle').parent().toggleClass("gridflex-primary-menu-open");
            $(this).find(".children:first").toggleClass('gridflex-submenu-toggle').parent().toggleClass("gridflex-primary-menu-open");
        });

        $( "div.gridflex-primary-responsive-menu > ul > li" ).on( "click", function(event) {
            if (event.target !== this)
                return;
            $(this).find("ul:first").toggleClass('gridflex-submenu-toggle').parent().toggleClass("gridflex-primary-menu-open");
        });

    }

    if($(".gridflex-social-icon-search").length){
        $(".gridflex-social-icon-search").on('click', function (e) {
            e.preventDefault();
            document.getElementById("gridflex-search-overlay-wrap").style.display = "block";
            const gridflex_focusableelements = 'button, [href], input';
            const gridflex_search_modal = document.querySelector('#gridflex-search-overlay-wrap');
            const gridflex_firstfocusableelement = gridflex_search_modal.querySelectorAll(gridflex_focusableelements)[0];
            const gridflex_focusablecontent = gridflex_search_modal.querySelectorAll(gridflex_focusableelements);
            const gridflex_lastfocusableelement = gridflex_focusablecontent[gridflex_focusablecontent.length - 1];
            document.addEventListener('keydown', function(e) {
              let isTabPressed = e.key === 'Tab' || e.keyCode === 9;
              if (!isTabPressed) {
                return;
              }
              if (e.shiftKey) {
                if (document.activeElement === gridflex_firstfocusableelement) {
                  gridflex_lastfocusableelement.focus();
                  e.preventDefault();
                }
              } else {
                if (document.activeElement === gridflex_lastfocusableelement) {
                  gridflex_firstfocusableelement.focus();
                  e.preventDefault();
                }
              }
            });
            gridflex_firstfocusableelement.focus();
        });
    }

    if($(".gridflex-search-closebtn").length){
        $(".gridflex-search-closebtn").on('click', function (e) {
            e.preventDefault();
            document.getElementById("gridflex-search-overlay-wrap").style.display = "none";
        });
    }

    if(gridflex_ajax_object.fitvids_active){
        $(".entry-content, .widget").fitVids();
    }

    if(gridflex_ajax_object.backtotop_active){
        if($(".gridflex-scroll-top").length){
            var gridflex_scroll_button = $( '.gridflex-scroll-top' );
            gridflex_scroll_button.hide();

            $(window).on( "scroll", function() {
                if ( $( window ).scrollTop() < 20 ) {
                    $( '.gridflex-scroll-top' ).fadeOut();
                } else {
                    $( '.gridflex-scroll-top' ).fadeIn();
                }
            } );

            gridflex_scroll_button.on( "click", function() {
                $( "html, body" ).animate( { scrollTop: 0 }, 300 );
                return false;
            } );
        }
    }

    if(gridflex_ajax_object.sticky_sidebar_active){
        $('.gridflex-main-wrapper, .gridflex-sidebar-one-wrapper, .gridflex-sidebar-two-wrapper').theiaStickySidebar({
            containerSelector: ".gridflex-content-wrapper",
            additionalMarginTop: 0,
            additionalMarginBottom: 0,
            minWidth: 960,
        });

        $(window).on( "resize", function() {
            $('.gridflex-main-wrapper, .gridflex-sidebar-one-wrapper, .gridflex-sidebar-two-wrapper').theiaStickySidebar({
                containerSelector: ".gridflex-content-wrapper",
                additionalMarginTop: 0,
                additionalMarginBottom: 0,
                minWidth: 960,
            });
        });
    }

});