function fechaMenu() {
    var uls = document.getElementsByTagName("ul");
    for (var i = 0; i < uls.length; i++) {
        if (uls[i].className == "itensmenu" && uls[i].style.display != "none") {
            window.setTimeout("fecha('" + uls[i].id + "', " + uls[i].clientHeight + ", " + uls[i].clientHeight + ")", 30);
        }
    }
}

function menu(idMenu, salvaMenuAtual) {
    if (typeof salvaMenuAtual != 'boolean') {
       salvaMenuAtual = true;
    }
    fechaMenu();
    var liMenu = document.getElementById(idMenu);
    if (liMenu) {
        if (liMenu.style.display == "none") {
            liMenu.style.overflow = "hidden";
            liMenu.style.height = 0;
            liMenu.style.display = "block";
            var alturaTotal = liMenu.scrollHeight - 0;
            window.setTimeout("abre('" + idMenu + "', 0, " + alturaTotal + ")", 30);
            if (salvaMenuAtual) {
                MenuAjax.setMenuAtual(idMenu, {callback:setMenuAtualCallback});
            }
        }
    }
}

function setMenuAtualCallback() {
}

function abre(idMenu, altura, alturaTotal) {
    var liMenu = document.getElementById(idMenu);
    var incremento = 20;
    incremento = 20 - Math.floor(altura * 10 / alturaTotal);
    altura = altura + incremento;
    if (altura > alturaTotal) {
        liMenu.style.height = alturaTotal + "px";
    } else {
        liMenu.style.height = altura + "px";
        window.setTimeout("abre('" + idMenu + "', " + altura + ", " + alturaTotal + ")", 30);
    }
}

function fecha(idMenu, altura, alturaTotal) {
    var liMenu = document.getElementById(idMenu);
    incremento = 10 + Math.floor(altura * 10 / alturaTotal);
    altura = altura - incremento;
    if (altura < 0) {
        liMenu.style.height = "0px";
        liMenu.style.display = "none";
    } else {
        liMenu.style.height = altura + "px";
        window.setTimeout("fecha('" + idMenu + "', " + altura + ", " + alturaTotal + ")", 30);
    }
}

function abreMenuAtual() {
    MenuAjax.getMenuAtual({callback:getMenuAtualCallback});
}

function getMenuAtualCallback(idMenu) {
    menu(idMenu, false);
}
    
function loadComum() {
    focarPrimeiroCampo();
    if (typeof abreMenuAtual == 'function') {
        abreMenuAtual();
    }
    if (typeof load == 'function') {
        load();
    }
}

window.onload = loadComum;

