(function(M, $) {
	/**
	 * Print the document if the print parameter was used
	 */
	if(document.location.href.match(/\?.*print/i)) {
		window.print();
	}

    /**
     * Submenu object
     */
    var Submenu = (function() {
        function Submenu(el, parent, menu) {
            this.el         = el;
            this.parent     = parent;
            this.link       = $('a', this.parent)[0];
            this.el.submenu = this;
            this.timer      = null;
            this.menu       = menu;

            this.__hide = this.__hide.bind(this);

            // Set up event handlers
            M.addEvent(this.link, 'mouseover', this.show.bind(this));
            M.addEvent(this.link, 'mouseout',  this.hide.bind(this));
            M.addEvent(this.el,   'mouseover', this.prolong.bind(this));
            M.addEvent(this.el,   'mouseout',  this.hide.bind(this));
        }

        /**
         * Class name that indicates opened state. Add
         * appropriate styling yourself with CSS.
         */
        Submenu.prototype.className = 'opened';

        /**
         * Delay in ms before the menu is closed after leaving
         * either the submenu itself or its parent menu item.
         */
        Submenu.prototype.closingDelay = 500;

        /**
         * Kill the timer that would close the menu.
         *
         * Use this function as an event handler to stop the
         * closing timer from closing the menu as the mouse
         * leaves the menu item and moves to the submenu itself.
         */
        Submenu.prototype.prolong = function(e) {
            clearTimeout(this.timer);
            this.timer = null;
        };

        /**
         * Add the submenu class name for the opened state if
         * it is not already applied.
         *
         * @see Submenu.prototype.className
         */
        Submenu.prototype.show = function(e) {
            // Close all other submenu's that might be open
            this.menu.closeAllExcept(this);

            // Make sure the self-closing behaviour is stopped,
            // as we clearly want it to open and stay open
            this.prolong();

            M.addClass(this.parent, this.className);
        };

        /**
         * Remove the submenu class name for the opened state
         * if it is present.
         *
         * @see Submenu.prototype.className
         */
        Submenu.prototype.__hide = function() {
            M.removeClass(this.parent, this.className);
        };

        /**
         * Event handler that triggers __hide() after an amount of time,
         * allowing some error for the user.
         */
        Submenu.prototype.hide = function(e) {
            this.timer = setTimeout(this.__hide, this.closingDelay);
        };

        return Submenu;
    })();

    /**
     * Entire menu that knows about the root nav element, and
     * keeps track of all available submenu's.
     */
    var Menu = (function() {
        function Menu(el) {
            this.el       = el;
            this.el.menu  = this;
            this.submenus = [];
            this.collectSubmenus();
        }

        /**
         * Immediately close all submenu's, should they be open.
         * This should be called when opening a new submenu, since we
         * don't want to have multiple submenu items open.
         *
         * @param <Submenu> except do not close this submenu item
         */
        Menu.prototype.closeAllExcept = function(except) {
            this.submenus.filter(function(e) { return e === except; }).invoke('__hide');
        };

        /**
         * Loop through the DOM and find submenu's, creating Submenu items for
         * them and storing them in `this.submenus`.
         */
        Menu.prototype.collectSubmenus = function() {
            M.toArray(this.el.childNodes).filter(M.isElement).forEach(function(li) {
                var ul;
                if(ul = $('ul', li)[0]) {
                    this.submenus.push(new Submenu(ul, li, this));
                }
            }, this);
        };

        return Menu;
    })();

    new Menu($('nav ul')[0]);
})(Mutil.nativize(), Mutil.$);


