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

    if(gridread_ajax_object.secondary_menu_active){
        $(".gridread-nav-secondary .gridread-secondary-nav-menu").addClass("gridread-secondary-responsive-menu");

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

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

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

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

    if(gridread_ajax_object.primary_menu_active){
            // grab the initial top offset of the navigation 
            var gridreadstickyNavTop = $('.gridread-primary-menu-container').offset().top;
            
            // our function that decides weather the navigation bar should have "fixed" css position or not.
            var gridreadstickyNav = function(){
                var gridreadscrollTop = $(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 (gridreadscrollTop > gridreadstickyNavTop) {
                        $('.gridread-primary-menu-container').addClass('gridread-fixed');
                    } else {
                        $('.gridread-primary-menu-container').removeClass('gridread-fixed'); 
                    }
                }
            };

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

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

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

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

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

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

    if($(".gridread-footer-social-icon-search").length){
        $(".gridread-footer-social-icon-search").on('click', function (e) {
            e.preventDefault();
            document.getElementById("gridread-search-overlay-wrapper").style.display = "block";
            const gridread_focusableelements = 'button, [href], input';
            const gridread_search_modal = document.querySelector('#gridread-search-overlay-wrapper');
            const gridread_firstfocusableelement = gridread_search_modal.querySelectorAll(gridread_focusableelements)[0];
            const gridread_focusablecontent = gridread_search_modal.querySelectorAll(gridread_focusableelements);
            const gridread_lastfocusableelement = gridread_focusablecontent[gridread_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 === gridread_firstfocusableelement) {
                  gridread_lastfocusableelement.focus();
                  e.preventDefault();
                }
              } else {
                if (document.activeElement === gridread_lastfocusableelement) {
                  gridread_firstfocusableelement.focus();
                  e.preventDefault();
                }
              }
            });
            gridread_firstfocusableelement.focus();
        });
    }

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

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

    if(gridread_ajax_object.backtotop_active){
        if($(".gridread-scroll-top").length){
            var gridread_scroll_button = $( '.gridread-scroll-top' );
            gridread_scroll_button.hide();

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

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

    var gridread_show_dots_pagination;
    var gridread_show_next_prev_navigation;
    var gridread_autoheight_active;
    var gridread_slider_rtl;
    if(gridread_ajax_object.slider_active){
        if(gridread_ajax_object.show_dots_pagination){
            gridread_show_dots_pagination = true;
        } else {
            gridread_show_dots_pagination = false;
        }
        if(gridread_ajax_object.show_next_prev_navigation){
            gridread_show_next_prev_navigation = true;
        } else {
            gridread_show_next_prev_navigation = false;
        }
        if(gridread_ajax_object.autoheight_active){
            gridread_autoheight_active = true;
        } else {
            gridread_autoheight_active = false;
        }
        if(gridread_ajax_object.slider_rtl){
            gridread_slider_rtl = true;
        } else {
            gridread_slider_rtl = false;
        }

        if ( $().owlCarousel ) {
            var gridreadcarouselwrapper = $('.gridread-posts-carousel');
            var imgLoad = imagesLoaded(gridreadcarouselwrapper);
            imgLoad.on( 'always', function() {
                gridreadcarouselwrapper.each(function(){
                        var $this = $(this);
                        $this.find('.owl-carousel').owlCarousel({
                            autoplay: gridread_ajax_object.slider_autoplay,
                            loop: gridread_ajax_object.slider_loop,
                            margin: 5,
                            smartSpeed: 250,
                            dots: gridread_show_dots_pagination,
                            dotsSpeed: 100,
                            nav: gridread_show_next_prev_navigation,
                            navSpeed: 100,
                            mouseDrag: true,
                            touchDrag: true,
                            rtl: gridread_slider_rtl,
                            autoplayTimeout: gridread_ajax_object.slider_autoplaytimeout,
                            autoplaySpeed: gridread_ajax_object.slider_autoplayspeed,
                            autoplayHoverPause: gridread_ajax_object.slider_autoplayhoverpause,
                            autoHeight: gridread_autoheight_active,
                            navText: [ '<i class="fas fa-angle-left"></i>', '<i class="fas fa-angle-right"></i>' ],
                            responsive:{
                            0:{
                                items: 1
                            },
                            320:{
                                items: 1
                            },
                            414:{
                                items: 2
                            },
                            601:{
                                items: 3
                            },
                            791:{
                                items: 4
                            },
                            1001:{
                                items: 5
                            },
                            1113:{
                                items: 6
                            },
                            1277:{
                                items: 7
                            }
                        }
                    });
                });
            });
        } // end if
    }

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

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

});