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

    if(gridfeel_ajax_object.secondary_menu_active){

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

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

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

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

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

    }

    if(gridfeel_ajax_object.primary_menu_active){

        if(gridfeel_ajax_object.sticky_menu_active){
        // grab the initial top offset of the navigation 
        var gridfeelstickyNavTop = $('.gridfeel-primary-menu-container').offset().top;
        
        // our function that decides weather the navigation bar should have "fixed" css position or not.
        var gridfeelstickyNav = function(){
            var gridfeelscrollTop = $(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(gridfeel_ajax_object.sticky_mobile_menu_active){
                if (gridfeelscrollTop > gridfeelstickyNavTop) {
                    $('.gridfeel-primary-menu-container').addClass('gridfeel-fixed');
                } else {
                    $('.gridfeel-primary-menu-container').removeClass('gridfeel-fixed');
                }
            } else {
                if(window.innerWidth > 1112) {
                    if (gridfeelscrollTop > gridfeelstickyNavTop) {
                        $('.gridfeel-primary-menu-container').addClass('gridfeel-fixed');
                    } else {
                        $('.gridfeel-primary-menu-container').removeClass('gridfeel-fixed'); 
                    }
                }
            }
        };

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

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

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

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

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

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

    }

    if($(".gridfeel-sidebar-one-wrapper").length){
    $(".gridfeel-main-wrapper").before($(".gridfeel-sidebar-one-wrapper"));
    $(window).on( "resize", function() {
        if(window.innerWidth > 960) {
            $(".gridfeel-main-wrapper").before($(".gridfeel-sidebar-one-wrapper"));
        } else {
            $(".gridfeel-main-wrapper").after($(".gridfeel-sidebar-one-wrapper"));
        }
    });
    }

    if($(".gridfeel-social-icon-search").length){
        $(".gridfeel-social-icon-search").on('click', function (e) {
            e.preventDefault();
            document.getElementById("gridfeel-search-overlay-wrap").style.display = "block";
            const gridfeel_focusableelements = 'button, [href], input';
            const gridfeel_search_modal = document.querySelector('#gridfeel-search-overlay-wrap');
            const gridfeel_firstfocusableelement = gridfeel_search_modal.querySelectorAll(gridfeel_focusableelements)[0];
            const gridfeel_focusablecontent = gridfeel_search_modal.querySelectorAll(gridfeel_focusableelements);
            const gridfeel_lastfocusableelement = gridfeel_focusablecontent[gridfeel_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 === gridfeel_firstfocusableelement) {
                  gridfeel_lastfocusableelement.focus();
                  e.preventDefault();
                }
              } else {
                if (document.activeElement === gridfeel_lastfocusableelement) {
                  gridfeel_firstfocusableelement.focus();
                  e.preventDefault();
                }
              }
            });
            gridfeel_firstfocusableelement.focus();
        });
    }

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

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

    if(gridfeel_ajax_object.backtotop_active){
        if($(".gridfeel-scroll-top").length){
            var gridfeel_scroll_button = $( '.gridfeel-scroll-top' );
            gridfeel_scroll_button.hide();

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

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

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

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

});