(function ($) {
    var common = {
        init: function () {
            this.cacheDOM();
            this.eventListeners();
            this.defaultActions();
        },
        cacheDOM: function () {
            this.$html = $('html');
            this.$body = $('body');
            this.$mainHeader = this.$body.find('#masthead');
            this.mainHeaderHeight = this.$mainHeader.outerHeight();
        },
        eventListeners: function () {
            $(window).on('resize', this.updateVarsOnResize.bind(this));
            $(window).on('load', this.updateVarsOnResize.bind(this));
        },
        updateVarsOnResize: function () {
            this.mainHeaderHeight = this.$mainHeader.outerHeight();
            this.defaultActions();
        },
        defaultActions: function () {
            //fix header height before scroll so we avoid the jagged movement
            if (this.$body.hasClass('menu-sticky')) {
                this.$body.css('padding-top', this.mainHeaderHeight);
            }
        },
        showSubMenu: function (e) {
            e.preventDefault();
            $(this).toggleClass('active');
            $(this).parent().find('.sub-menu').first().slideToggle();
        }
    };
    var stickyHeader = {
        init: function () {
            this.eventListeners();
        },
        eventListeners: function () {
            $(window).on('scroll', this.shrinkHeader.bind(this));
        },
        shrinkHeader: function () {
            if (common.$html.scrollTop() > $('.site-branding').outerHeight() ) {
                common.$body.addClass('nav-shrink');
            } else {
                common.$body.removeClass('nav-shrink');
            }
        }
    };

    //main navigation
    var mainNavigation = {
        init: function () {
            this.cacheDOM();
            this.eventListeners();
        },
        cacheDOM: function () {
            this.$mainNavigationContainer = common.$mainHeader.find('.main-navigation');
            this.$mainNavigation = this.$mainNavigationContainer.find('.nav-menu');
            this.$menuToggle = common.$body.find('.menu-toggle');
            this.$menuOverlay = common.$body.find('.menu-overlay');
            this.subMenuDropdownTrigger = this.$mainNavigationContainer.find('.dropdown-toggle')
        },
        eventListeners: function () {
            this.$menuToggle.on('click', this.showMobileMenu.bind(this));
            this.subMenuDropdownTrigger.on('click', common.showSubMenu);
        },
        showMobileMenu: function (e) {
            e.preventDefault();
            var $el = $(e.currentTarget);
            this.$mainNavigation.fadeToggle();
            $el.toggleClass('active');
            this.$mainNavigationContainer.toggleClass('menu-open');
            this.$menuOverlay.toggleClass('active');
        }

    };

    var secondaryNavigation = {
        init: function () {
            this.secondaryMenuTrigger = common.$body.find('.secondary-menu-toggle');
            this.subMenuDropdownTrigger = this.secondaryMenuTrigger.find('.dropdown-toggle');

            this.secondaryMenuTrigger.on('click', this.toggleMenu.bind(this));
            this.subMenuDropdownTrigger.on('click', common.showSubMenu);
        },
        toggleMenu: function (e) {
            var $el = $(e.currentTarget);
            $('.custom-page-menu').fadeToggle();
            $el.toggleClass('active');
            $('.page-menu-wrapper').toggleClass('secondary-menu-open');
        }
    };

    //search bar
    var searchBar = {
        init: function () {
            this.cacheDOM();
            this.eventListeners();
        },
        cacheDOM: function () {
            this.$toggleSearchElement = common.$body.find('#togglesearchbar');
            this.$searchBar = common.$body.find('#top-search');
            this.searchBarOpen = false;
        },
        eventListeners: function () {
            this.$toggleSearchElement.on('click', this.toggleSearchBar.bind(this));
            $(document).on('keyup', this.toggleSearchBar.bind(this));
            $(document).on('click', this.hideSearchBar.bind(this));
        },
        toggleSearchBar: function (e) {
            //bail early
            if (e.type === 'keyup' && e.keyCode !== 27) {
                return;
            }

            e.preventDefault();
            this.$searchBar.slideToggle(400);
            //reverse condition on showing
            this.searchBarOpen = !(this.searchBarOpen);
            this.$toggleSearchElement.find('i.fa-search').toggleClass("fa-close");
        },
        hideSearchBar: function (e) {
            var $el = $(e.target);


            if ($el.is(this.$toggleSearchElement) || $el.is(this.$toggleSearchElement.find('i.fa-search')) || $el.is(this.$searchBar.find('.search-field'))) {
                return;
            }

            if (this.searchBarOpen === false) {
                return;
            }

            this.searchBarOpen = !(this.searchBarOpen);
            this.$toggleSearchElement.find('i.fa-search').removeClass("fa-close");
            this.$searchBar.slideToggle(400);
        }
    };

    //back top
    var backToTop = {
        init: function () {
            this.cacheDOM();
            this.eventListeners();
        },
        cacheDOM: function () {
            this.$backToTopButton = common.$body.find('#scroll-top');
        },
        eventListeners: function () {
            //event listeners
            $(window).on('scroll', this.toggleShowBackToTopButton.bind(this));
            this.$backToTopButton.on('click', this.backToTop.bind(this));
        },
        toggleShowBackToTopButton: function () {
            var windowScrolled = common.$html.scrollTop();
            if (windowScrolled > common.mainHeaderHeight) {
                this.$backToTopButton.addClass('show');
            } else {
                this.$backToTopButton.removeClass('show');
            }
        },
        backToTop: function (e) {
            e.preventDefault();
            common.$html.stop().animate({scrollTop: 0}, 'slow');
            //return false;
        }
    };

    // Woo notice bar dismissal
    var wooNoticeDismiss = {
        init: function() {
            this.cacheDOM();
            this.removeMargin();
            this.eventListeners();
        },
        cacheDOM: function() {
            this.$dismissLink = common.$body.find('.woocommerce-store-notice__dismiss-link');
            this.$wooStoreNotice = common.$body.find('.woocommerce-store-notice');
            this.$footer = common.$body.find('footer#colophon');
            this.$noticeID   = common.$body.find( '.woocommerce-store-notice' ).data( 'notice-id' ) || '';
            this.$cookieName = 'store_notice' + this.$noticeID;
            this.distance =  this.$wooStoreNotice.outerHeight();
        },
        eventListeners: function() {
            this.$dismissLink.on('click', this.removeMargin.bind(this) );
        },
        removeMargin: function() {
            if ( 'hidden' === Cookies.get( this.$cookieName ) ) {
                $( this.$footer ).css('margin-bottom', 0 );
            } else {
                $( this.$footer ).css('margin-bottom', this.distance );
            }
        }
    }

    $(function () {
        common.init();

        if (common.$body.hasClass('menu-sticky')) {
            stickyHeader.init();
        }

        mainNavigation.init();
        secondaryNavigation.init();
        searchBar.init();
        backToTop.init();
        wooNoticeDismiss.init();
    });


})(jQuery);