(function($) {

    $.cookie.json = true;

    window.hasMouse = 'onmousemove' in document.documentElement;
    window.hasTouch = 'ontouchstart' in document.documentElement;
    window.isSmall = false;
    window.baseUrl = location.protocol + '//' + location.host + '/';

    window.navigation = {
        initMenu: function(resp){
            var $mainMenu = $('#main-nav, .main-menu-container');

            if(resp.shop && resp.shop.children.length) {
                $mainMenu
                    .find('li.nav-shopping')
                    .find('> ul').remove()
                    .end()
                    .append('<ul class="sub-menu">' + this.getNavHtml(resp.shop, 0) + '</ul>');
            }

            if(resp.recipe && resp.recipe.children.length) {
                $mainMenu
                    .find('li.nav-recipes')
                    .find('> ul').remove()
                    .end()
                    .append('<ul class="sub-menu">' + this.getNavHtml(resp.recipe, 0) + '</ul>');
            }

            if(resp.account && resp.account.children.length) {
                $mainMenu
                    .find('li.nav-account')
                    .find('> ul').remove()
                    .end()
                    .append('<ul class="sub-menu">' + this.getNavHtml(resp.account, 0) + '</ul>');
            }

            // if (window.isSmall && window.matchMedia) {
            //     navigation.matchMediaHandler(window.matchMedia("(max-width: 991px)"));
            // }

            var offcanvasNav = $('#main-nav').data('hcOffcanvasNav');
            if(offcanvasNav) offcanvasNav.update(true);
        },
        getNavHtml: function(children, level, parent, disableChildNav) {
            var config = wpConfig.menuConfig || {
                subMenuClass: 'sub-menu',
                menuItemClass: 'menu-item',
                menuItemHasChildrenClass: 'menu-item-has-children',
                menuItemSelectedClass: 'menu-item-selected',
                maxLevel: -1
            };

            if(config.maxLevel > 0 && level > config.maxLevel) return '';

            var self = this,
                html = '',
                childItems,
                hasChildren,
                classNames;

            if(children && !_.isArray(children) && _.isArray(children.children)) {
                children = children.children;
            }

            if(_.isArray(children)) { 
                if(level > 1) html += '<ul class="' + config.subMenuClass + ' ' + config.subMenuClass + '-' + level + ' clearfix">';
                
                _.each(children, function(item) {
                    classNames = '';
                    childItems = item.children; 

                    if(childItems && !_.isArray(childItems) && _.isArray(childItems.children)) { 
                        childItems = childItems.children; 
                    } 
                    hasChildren = childItems && childItems.length && (config.maxLevel == -1 || level < config.maxLevel);

                    if(hasChildren) classNames += config.menuItemHasChildrenClass;
                    if(item.selected) classNames += config.menuItemHasChildrenClass;
                    if(config.menuItemClass && classNames) {
                        classNames = config.menuItemClass + ' ' + classNames;
                    }

                    html += '<li class="' + classNames + '">';
                    html += '<a href="' + item.url + '" data-id="' + freshop.ui.encodeForHtml(item.id) +'">' + freshop.ui.encodeForHtml(item.title) + '</a>';

                    if(hasChildren) {
                        html += self.getNavHtml(childItems, level + 1, item, disableChildNav);
                    }

                    html += '</li>';
                });

                if(level > 1) html += '</ul>';
            }

            return html;
        }
        ,matchMediaHandler : function(mql) {
            mql = mql || {};

            if(mql.matches) {
                window.isSmall = true;
                fpClientSearch.initMobileSearch();
            }
            else {
                console.log('**** matchMediaHandler ****');
                window.isSmall = false;
                fpClientSearch.initSearch();
            }

            fpClient.initMinilistIndicator();
        }
    };

    window.fpClientSearch = {
        initMobileSearch : function(){
            var $mobileSearchWrapper = $('.mobile-header-wrapper').find('.mobile-search')
                ,$lgMdSearchWrapper = $('#header .top-menu-search').find('#search');

            $lgMdSearchWrapper.appendTo($mobileSearchWrapper);
        }
        ,initSearch : function(){
            var $mobileSearchWrapper = $('.mobile-header-wrapper').find('#search')
                ,$lgMdSearchWrapper = $('#header').find('.top-menu-search');

            $mobileSearchWrapper.appendTo($lgMdSearchWrapper);
        }
    };

    window.backToTop = {
        init : function () {
            var back_to_top_offset = 200,
                back_to_top_scroll_top_duration = 600,
                $back_to_top = $('#footer > .cd-top'),
                $window = $(window),
                fadeTimeout;

            $window.on('scroll', function(){
                var scrollTop = $window.scrollTop();

                if(scrollTop > back_to_top_offset ) {
                    if(!$back_to_top.hasClass('cd-is-visible')){
                        $back_to_top.addClass('cd-is-visible');

                        clearTimeout(fadeTimeout);

                        // remove button after 3 seconds with no scrolling
                        fadeTimeout = setTimeout(function() {
                            $back_to_top.removeClass('cd-is-visible');
                        }, 3000);
                    }
                }
                else{
                    $back_to_top.removeClass('cd-is-visible');
                }
            });

            //smooth scroll to top
            $back_to_top.on('click', function(event){
                event.preventDefault();
                $('body,html').animate({ scrollTop: 0 }, back_to_top_scroll_top_duration );
            });
        }
    };

    $(document).ready(function() {
        backToTop.init();
        // navigation.init();
        fpClient.init();

        fpClient.loader = new fpClient.custom.loading();

        var mql;
        if (window.matchMedia) {
            mql = window.matchMedia("(max-width: 991px)");
            mql.addListener(navigation.matchMediaHandler);
        }
        navigation.matchMediaHandler(mql);

        $('form.wpcf7-form .dropdown').dropdown();

        window.freshopInitialized = function() {
            $(document).trigger('freshopInitialized');
        };
    });
})(jQuery); 