/* Modified menu.js from confluence master scripts */
/* Reproduced here so we can use confluence like menus on non confluence pages */

$(document).ready(function() {
  $(".ajs-menu-bar").each(function () {
    var shownDropDown = null;
    var hideDropDown = function (e) {
        if (typeof dropDownTimer != "undefined" && dropDownHider) {
            clearTimeout(dropDownTimer);
            delete dropDownTimer;
            dropDownHider();
            dropDownHider = null;
        }
    };
    var el = this;
    $(".ajs-button", el).each(function () {
        $(this).mouseover(hideDropDown);
    });
    $(".ajs-menu-item", el).each(function () {
        var it = this, $it = $(this),
            dd = $(".ajs-drop-down", it);
        if (!dd.length) return;

        dd = dd[0];
        dd.hidden = true;
        dd.focused = -1;
        dd.hide = function () {
            if (!this.hidden) {
                $it.toggleClass("opened");
                var as = $("a", this);
                $(this).toggleClass("hidden");
                this.hidden = true;
                $(document).unbind("click", this.fhide).unbind("keydown", this.fmovefocus).unbind("keypress", this.blocker);
                if (this.focused + 1) {
                    $(as[this.focused]).removeClass("active");
                }
                this.focused = -1;
            }
        };
        dd.show = function () {
            if (typeof this.hidden == "undefined" || this.hidden) {
                $(this).toggleClass("hidden");
                $it.toggleClass("opened");
                this.hidden = false;
                var dd = this, $dd = $(this);
                this.timer = setTimeout(function () {$(document).click(dd.fhide);}, 1);
                $(document).keydown(dd.fmovefocus).keypress(dd.blocker);
                var as = $("a", dd);
                as.each(function (i) {
                    var grandpa = this.parentNode.parentNode;
                    $(this).hover(function (e) {
                        if (grandpa.focused + 1) {
                            $(as[grandpa.focused].parentNode).removeClass("active");
                        }
                        $(this.parentNode).addClass("active");
                        grandpa.focused = i;
                    }, function (e) {
                        if (grandpa.focused + 1) {
                            $(as[grandpa.focused].parentNode).removeClass("active");
                        }
                        grandpa.focused = -1;
                    });
                });
            }
        };
        dd.fmovefocus = function (e) {dd.movefocus(e);};
        dd.fhide = function (e) {dd.hide(e);};
        dd.blocker = function (e) {
            var c = e.which;
            if (c == 40 || c == 38) {
                return false;
            }
        };
        dd.movefocus = function (e) {
            var c = e.which,
                a = this.getElementsByTagName("a");
            if (this.focused + 1) {
                $(a[this.focused].parentNode).removeClass("active");
            }
            switch (c) {
                case 40:
                case 9: {
                    this.focused++;
                    break;
                }
                case 38: {
                    this.focused--;
                    break;
                }
                case 27: {
                    this.hide();
                    return false;
                }
                default: {
                    return true;
                }
            }
            if (this.focused < 0) {
                this.focused = a.length - 1;
            }
            if (this.focused > a.length - 1) {
                this.focused = 0;
            }
            a[this.focused].focus();
            $(a[this.focused].parentNode).addClass("active");
            e.stopPropagation();
            e.preventDefault();
            return false;
        };
        dd.show();
        clearTimeout(dd.timer);
        var $dd = $(dd),
            offset = $dd.offset();
        dd.hide();
        if (offset.left + $dd.width() > $(window).width()) {
            $dd.css("margin-left", "-" + (($dd.width()) - ($it.width())) + "px");
        }
        var a = $(".trigger", it);
        if (a.length) {
            var killHideTimerAndShow = function() {
                clearTimeout(dropDownTimer);
                delete dropDownTimer;
                dropDownHider();
                dropDownHider = null;
                dd.show();
            };
            
            var showMenu = function (millis) { 
              var changingMenu = typeof dropDownTimer != "undefined";
              shownDropDown = dd;
              if (changingMenu) {
                  killHideTimerAndShow();
              }
              else {
                  dropDownShower = function () {dd.show(); delete dropDownShowerTimer;};
                  dropDownShowerTimer = setTimeout(dropDownShower, millis);
              }
            };
            var hideMenu = function (millis) {
                var passingThrough = typeof dropDownShowerTimer != "undefined";
                if (passingThrough) {
                    clearTimeout(dropDownShowerTimer);
                    delete dropDownShowerTimer;
                }
                if (typeof dropDownTimer != "undefined") {
                    clearTimeout(dropDownTimer);
                    delete dropDownHider;
                }
                dropDownHider = function () {dd.hide(); delete dropDownTimer;};
                dropDownTimer = setTimeout(dropDownHider, millis);
            };

            var overHandler = function (e) {
              showMenu(500);
            };

            var outHandler = function (e) {
              hideMenu(300);
            };
            a.click(function (e) { return false; });
            $it.mouseover(overHandler);
            $it.mouseout(outHandler);
            
            var keypressHandler = function (e) {
              if (shownDropDown)
                shownDropDown.hide();
              if (e.which == 27) {
                dd.hide();
                } else {
                  showMenu(0);
                }
            };
            a.keypress(keypressHandler); 
        }
    });
  });
});

