var ndd;
;(function ($) {

    var debug = true;

    /**
     * navDropDown
     */

    var navDropDown = {
        nav: null,
        navItems: [],
        currentItem: null,
        initialize: function () {
            this.nav = $('header nav');
            $('> ul > li', this.nav).each(function () {
                navDropDown.addItem($(this));
            });
        },
        addItem: function (item) {
            this.navItems.push(new navDropDownItem(item, this));
        },
        hideOthers: function (item) {
            $.each(this.navItems, function (index, _item) {
                if (_item == item) return; 
                _item.hideMenu();
            });
        }

    };
    ndd = navDropDown;

    var navDropDownItem = function (item, parent) {
        var _this = this;
        this.li = item;
        this.parent = parent;
        this.ul = item.children('ul');
        if (this.ul.length) {
            this.hasMenu = true;
            this.liWidth = this.li.outerWidth();
            this.ulHeight = this.ul.height();
            this.ulWidth = this.ul.width();
            this.subitemCount = this.ul.children('li').length;
            if (this.ul.is(':hidden')) {
                this.ul.width(this.liWidth);
                this.ul.height(0);
            }
        } else {
            this.hasMenu = false;
        }
        this.timeout = null;
        item.hover(
            function (e) { return _this.onMouseOver(e); },
            function (e) { return _this.onMouseOut(e); }
        );
    };
    navDropDownItem.prototype = {
        hideMenuDelay: 360,
        showRate: 92,      // rate of animation per submenu item when showing
        showDuration: 0,    // duration of entire animation when showing
        hideRate: 68,      // rate of animation per submenu item when hiding
        hideDuration: 0,    // duration of entire animation when hiding
        ratioVisible: 0,
        zIndexHidden: 20,
        zIndexShown: 30,
        _originalZIndex: null,
        onMouseOver: function (event) {
            if (!this._originalZIndex)
                this._originalZIndex = this.ul.css('zIndex');
            this.parent.hideOthers();
            if (this.hasMenu) {
                this.showMenu();
            }
            // important that this follows the call to showMenu()
            this.li.addClass('hover');
        },
        onMouseOut: function (event) {
            if (this.hasMenu) {
                var t = this;
                this.timeout = setTimeout(function () { t.hideMenu(); }, this.hideMenuDelay);
            } else {
                this.li.removeClass('hover');
            }
        },
        showMenu: function () {
            clearTimeout(this.timeout);
            this.ul.stop();
            this.setRatioVisible();
            this.showDuration = (1 - this.ratioVisible) * this.showRate * Math.sqrt(1.6 * this.subitemCount);
            this.ul.add(this.li).css({zIndex: this._originalZIndex + 10});
            this.ul.animate({height: this.ulHeight, width: this.ulWidth}, this.showDuration, 'swing');
        },
        hideMenu: function () {
            if (this.hasMenu) {
                var t = this;
                this.ul.stop();
                clearTimeout(this.timeout);
                this.setRatioVisible();
                if (this.ratioVisible == 0)
                    return this.afterHideMenu();
                this.hideDuration = (this.ratioVisible) * this.hideRate * this.subitemCount;
                this.ul.add(this.li).css({zIndex: this._originalZIndex});
                this.ul.animate({height: 0, width: this.liWidth}, this.hideDuration, 'swing', function () { t.afterHideMenu(); });
            }
        },
        afterHideMenu: function () {
            // ensure that animation completed, not just stopped
            //if (this.ul.height() == 0) {
                this.li.removeClass('hover');
                this.ul.hide();
            //}
        },
        setRatioVisible: function () {
            this.ratioVisible = this.ul.height() / this.ulHeight;
        }
    };

    $(function () {
        navDropDown.initialize();
    });

})(jQuery);

