var _menuids = ["sidebarmenu1"];

var _delayedCommand = [];
var _delayedLi = [];
var _delayedTimeout = null;

function timerProc() {
    for (var i = 0; i < _delayedCommand.length; i++) {
        if (_delayedCommand[i] === "out") {
            hoverOutT(_delayedLi[i]);
        }
        else if (_delayedCommand[i] === "over") {
            hoverOverT(_delayedLi[i]);
        }
    }
    
    _delayedCommand = [];
    _delayedLi = [];
    _delayedTimeout = null;
}

function delayCommand(cmd, li) {
    // This function introduces a short delay so that extraneous elements that
    // the user hovers over do not affect the user experience.
    
    // Remove any opposites of this command.
    for (var i = _delayedCommand.length - 1; i >= 0; i--) {
        if (_delayedLi[i].index(li) !== -1 && _delayedCommand[i] !== cmd) {
            _delayedCommand.splice(i, 1);
            _delayedLi.splice(i, 1);
        }
    }

    // Push the command.
    _delayedCommand.push(cmd);
    _delayedLi.push(li);
    
    // Clear old timeout (if any), set new timeout.
    if (_delayedTimeout !== null) {
        window.clearTimeout(_delayedTimeout);
    }   
    
    _delayedTimeout = window.setTimeout(timerProc, 300);
}

function hoverOver() {
    delayCommand("over", $(this));
}

function hoverOut() {
    var li = $(this);
    delayCommand("out", li);
}

function hoverOverT(obj)
{
    var ul = $("ul:first", obj);

    $("> a", obj).addClass("trailfolder");

    if (ul.length === 1) {
        var ulOuterHeight;
        var ulTop;
        var windowScrollTop = $(window).scrollTop();
        var windowHeight = $(window).height();

        // Set display:block but temporarily hide. This lets us get measurements
        // of the hidden elements and move them before they become visible.

        ul.css("visibility", "hidden");
        ul.css("display", "block");

        ulOuterHeight = ul.outerHeight(true);
        ulTop = obj.offset().top;
    
        var parent = obj.parent();
        if (parent.length === 1) {
            var li = $("> li", ul);
            if (li.length !== 0) {
                var i;          
                var h = 0;

                // Center the submenu (ul).

                var liOuterHeightSum = 0;
                for (i = 0; i < li.length; i++) {
                    var lii = li.eq(i);
                    var liOuterHeight = lii.outerHeight(true);
                    liOuterHeightSum += liOuterHeight;
                    if (liOuterHeightSum < ulOuterHeight / 2) {
                        h -= liOuterHeight;
                    }
                    
                    // Added at runtime to cut page size.
                    if ($("> ul", lii).length !== 0) {
                        $("> a", lii).addClass("subfolderstyle");
                    }
                }

                // Prevent overflow at bottom of screen.
                // The extra 20px is to avoid going under Chrome's status bar overlay.
                for (i = 0; i < li.length && ulTop + ulOuterHeight + h > windowScrollTop + windowHeight - 20; i++) {
                    h -= li.eq(i).outerHeight(true);
                }
    
                // Prevent overflow at top of screen (has priority over overflow at bottom).

                for (i = 0; i < li.length && ulTop + h < windowScrollTop; i++) {
                    h += li.eq(i).outerHeight(true);
                } 

                ul.css("top", h + "px");
                
                li.hover(hoverOver, hoverOut);
            }

            ul.css("left", parent.outerWidth(true) + "px");
        }
 
        ul.css("visibility", "");
    }
}

function hoverOutT(li) {
    $("> a", li).removeClass("trailfolder");

    var ul = $("ul:first", li);
    if (ul.length === 1) {
        ul.css("display", "none");
    }
}

// Document init / ready

function initsidebarmenu() {
    for (var i = 0; i < _menuids.length; i++) {
        var menu = $("#" + _menuids[i]);
        $("> li", menu).hover(hoverOver, hoverOut);
    }
}

$(document).ready(initsidebarmenu);
