(function ($) {
    "use strict";
    var CORE = {
        init: function () {
        },
    };

    CORE.scrollSpy = {
        init: function () {
            this.scrollSpyLanding();
        },
        scrollSpyLanding: function () {
            var is_onePage = $("body.is_one_page");
            if (!is_onePage.length) {
                return;
            }

            var $menu = $("#mainnav");
            var $header = $("#header").height();
            var topMenuHeight = $menu.outerHeight() + 50;
            CORE.scrollSpy.menuScroll($menu, topMenuHeight);
        },
        menuScroll: function (element, offset) {
            // Declare all global variables
            var topMenu = element;
            var topOffset = offset ? offset : 0;
            var menuItems = $(topMenu).find("a");
            var lastId;

            // Save all menu items into scrollItems array
            var scrollItems = $(menuItems).map(function () {
                var item = $($(this).attr("href"));
                if (item.length) {
                    return item;
                }
            });

            // When the menu item is clicked, get the #id from the href value, then scroll to the #id element
            $('a[href*=#]:not([href=#])').click(function () {
                if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
                    var target = $(this.hash);

                    target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
                    if (target.length) {
                        $('html, body').animate({
                            scrollTop: target.offset().top -  $("#header").height()
                        }, 0);
                        return false;
                    }
                }
            });

            // When page is scrolled
            $(window).scroll(function () {
                var nm = $("html").scrollTop();
                var nw = $("body").scrollTop();
                var fromTop = (nm > nw ? nm : nw) + topOffset;


                // When the page pass one #id section, return all passed sections to scrollItems and save them into new array current
                var current = $(scrollItems).map(function () {
                    if ($(this).offset().top <= fromTop)
                        return this;
                });

                // Get the most recent passed section from current array
                current = current[current.length - 1];
                var id = current && current.length ? current[0].id : "";
                if (lastId !== id) {
                    lastId = id;
                    // Set/remove active class
                    $(menuItems)
                        .parent().removeClass("active")
                        .end().filter("[href='#" + id + "']").parent().addClass("active");
                }

            });
        }

    };


    $(document).ready(function () {
        CORE.init();
        CORE.scrollSpy.init();
    });
})(jQuery);
