jQuery(document).ready(function($) {

    if(freshwp_ajax_object.sticky_menu){
    // grab the initial top offset of the navigation 
    var freshwpstickyNavTop = $('.freshwp-primary-menu-container').offset().top;
    
    // our function that decides weather the navigation bar should have "fixed" css position or not.
    var freshwpstickyNav = function(){
        var freshwpscrollTop = $(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 (freshwpscrollTop > freshwpstickyNavTop) {
            $('.freshwp-primary-menu-container').addClass('freshwp-fixed');
        } else {
            $('.freshwp-primary-menu-container').removeClass('freshwp-fixed'); 
        }
    }
    };

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

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

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

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

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

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

    $(".post").fitVids();

    var scrollButtonEl = $( '.freshwp-scroll-top' );
    scrollButtonEl.hide();

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

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

    if(freshwp_ajax_object.sticky_sidebar){
        $('.freshwp-main-wrapper, .freshwp-sidebar-one-wrapper, .freshwp-sidebar-two-wrapper').theiaStickySidebar({
            containerSelector: ".freshwp-content-inner-wrapper",
            additionalMarginTop: 0,
            additionalMarginBottom: 0,
            minWidth: 1107,
        });

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

});