$(function() {
    $("div[class^=MEN01_nivel]").bind('mouseover', MenuEstadoOver).bind('mouseout', MenuEstadoOut);
    ExpandTreeMagic("JER");

})

function cambiarDisplay(div_seleccionado, div_desplegar) {

    if (!document.getElementById) return false;
    var id = div_seleccionado.getAttribute('id');
    var nivel = div_seleccionado.className.substring(13, 12)
    var fila = document.getElementById(div_desplegar);
    var filaActiva = document.getElementById(id);

    if (fila != null) {

        if ($(fila).is(":hidden")) {
            $(fila).slideDown(700, "easeOutCirc");

            $(filaActiva).children("div").next("div").filter(function() {
                this.className = "MEN01_desplegar_" + nivel + "_ON";
            })

            filaActiva.className = "MEN01_nivel_" + nivel + "_ACTIVO";
            filaActiva.firstChild.className = "MEN01_vineta_" + nivel + "_ACTIVO";
            $(filaActiva).bind('mouseover', MenuEstadoActivo1).bind('mouseout', MenuEstadoActivo2);
        } else {
            $(fila).slideUp(700, "easeOutBounce");

            $(filaActiva).children("div").next("div").filter(function() {
                this.className = "MEN01_desplegar_" + nivel + "_OFF";
            })

            filaActiva.firstChild.className = "MEN01_vineta_" + nivel + "_OVER";
            $(filaActiva).unbind('mouseover');
            $(filaActiva).unbind('mouseout');
            $(filaActiva).bind('mouseover', MenuEstadoOver).bind('mouseout', MenuEstadoOut);
        }
    } else {

        filaActiva.className = "MEN01_nivel_" + nivel + "_OVER2";

        $(filaActiva).filter(function() {
            this.firstChild.className = "MEN01_vineta_" + nivel + "_OVER";
        })

        $(filaActiva).hover(MenuEstadoQuery1, MenuEstadoQuery2);
    }
}



function MenuEstadoQuery1() {
    var nivel = this.className.substring(13, 12);
    this.className = "MEN01_nivel_" + nivel + "_OVER2";
    this.firstChild.className = "MEN01_vineta_" + nivel + "_OVER";
}

function MenuEstadoQuery2() {
    var nivel = this.className.substring(13, 12);
    this.className = "MEN01_nivel_" + nivel + "_OVER2";
    this.firstChild.className = "MEN01_vineta_" + nivel + "_OVER";
}

function MenuEstadoActivo1() {
    var nivel = this.className.substring(13, 12);
    this.className = "MEN01_nivel_" + nivel + "_ACTIVO";
    this.firstChild.className = "MEN01_vineta_" + nivel + "_ACTIVO";
}

function MenuEstadoActivo2() {
    var nivel = this.className.substring(13, 12);
    this.className = "MEN01_nivel_" + nivel + "_ACTIVO";
    this.firstChild.className = "MEN01_vineta_" + nivel + "_ACTIVO";
}

function MenuEstadoOver() {
    var estado = this.className.slice(-1);
    var nivel = this.className.substring(13, 12);
    if (estado == "T") { estado = "" };
    if (estado == "O") { estado = "" };
    if (estado == "R") { estado = "" };
    this.firstChild.className = "MEN01_vineta_" + nivel + "_OVER";
    this.className = "MEN01_nivel_" + nivel + "_OVER" + estado;
}

function MenuEstadoOut() {
    var estado = this.className.slice(-1);
    var nivel = this.className.substring(13, 12);
    if (estado == "T") { estado = "" };
    if (estado == "R") { estado = "" };
    if (estado == "O") { estado = "" };
    this.firstChild.className = "MEN01_vineta_" + nivel + "_OUT";
    this.className = "MEN01_nivel_" + nivel + "_OUT" + estado;

}


function EnlaceMenuPricipal(url, target) {
    target = target.toLowerCase();
    if (target == '_blank') window.open(url);
    if (target == '_self') self.location.href = url;
    if (target == '_top') top.location.href = url;
    if (target == '_parent') parent.location.href = url;
}

function ExpandTreeMagic(variable) {
    try {
        var regexS = "[\\?&]" + variable + "=([^&#]*)";
        var valor = "";
        var regex = new RegExp(regexS,"i");
        var tmpURL = window.location.href;
        var results = regex.exec(tmpURL);
        if (results == null)
            valor = "";
        else
            valor = "Bot_" + results[1];

        var classnamejer;
        if (valor != "") {
            e = document.getElementById(valor);
            if (e != null) {
                classnamejer = e.className;

                if (classnamejer == 'MEN01_nivel_0_OUT' || classnamejer == 'MEN01_nivel_0_OUT2') {
                    cambiarDisplay(e, 'MEN01_N1_' + e.getAttribute('id').substring(4));
                }
                if (classnamejer == 'MEN01_nivel_1_OUT' || classnamejer == 'MEN01_nivel_1_OUT2') {
                    var id_nivel0 = e.parentNode.getAttribute('id').substring(9);
                    var div = document.getElementById('Bot_' + id_nivel0);
                    if (div != null) {
                        cambiarDisplay(div, 'MEN01_N1_' + id_nivel0);
                        cambiarDisplay(e, 'MEN01_N2_' + e.getAttribute('id').substring(4));
                    }
                }
                if (classnamejer == 'MEN01_nivel_2_OUT' || classnamejer == 'MEN01_nivel_2_OUT2') {
                    var id_nivel0 = e.parentNode.parentNode.getAttribute('id').substring(9);
                    var div_nivel0 = document.getElementById('Bot_' + id_nivel0);
                    if (div_nivel0 != null) {
                        cambiarDisplay(div_nivel0, 'MEN01_N1_' + id_nivel0);
                        var id_nivel1 = e.parentNode.getAttribute('id').substring(9);
                        var div_nivel1 = document.getElementById('Bot_' + id_nivel1);
                        if (div_nivel1 != null) {
                            cambiarDisplay(div_nivel1, 'MEN01_N2_' + id_nivel1);
                            cambiarDisplay(e, 'MEN01_N3_' + e.getAttribute('id').substring(4));
                        }
                    }
                }
                if (classnamejer == 'MEN01_nivel_3_OUT' || classnamejer == 'MEN01_nivel_3_OUT2') {
                    var id_nivel0 = e.parentNode.parentNode.parentNode.getAttribute('id').substring(9);
                    var div_nivel0 = document.getElementById('Bot_' + id_nivel0);
                    if (div_nivel0 != null) {
                        cambiarDisplay(div_nivel0, 'MEN01_N1_' + id_nivel0);
                        var id_nivel1 = e.parentNode.parentNode.getAttribute('id').substring(9);
                        var div_nivel1 = document.getElementById('Bot_' + id_nivel1);
                        if (div_nivel1 != null) {
                            cambiarDisplay(div_nivel1, 'MEN01_N2_' + id_nivel1);
                            var id_nivel2 = e.parentNode.getAttribute('id').substring(9);
                            var div_nivel2 = document.getElementById('Bot_' + id_nivel2);
                            if (div_nivel2 != null) {
                                cambiarDisplay(div_nivel2, 'MEN01_N3_' + id_nivel2);
                                cambiarDisplay(e, 'MEN01_N4_' + e.getAttribute('id').substring(4));
                            }
                        }
                    }
                }
                if (classnamejer == 'MEN01_nivel_4_OUT' || classnamejer == 'MEN01_nivel_4_OUT2') {
                    var id_nivel0 = e.parentNode.parentNode.parentNode.parentNode.getAttribute('id').substring(9);
                    var div_nivel0 = document.getElementById('Bot_' + id_nivel0);
                    if (div_nivel0 != null) {
                        cambiarDisplay(div_nivel0, 'MEN01_N1_' + id_nivel0);
                        var id_nivel1 = e.parentNode.parentNode.parentNode.getAttribute('id').substring(9);
                        var div_nivel1 = document.getElementById('Bot_' + id_nivel1);
                        if (div_nivel1 != null) {
                            cambiarDisplay(div_nivel1, 'MEN01_N2_' + id_nivel1);
                            var id_nivel2 = e.parentNode.parentNode.getAttribute('id').substring(9);
                            var div_nivel2 = document.getElementById('Bot_' + id_nivel2);
                            if (div_nivel2 != null) {
                                cambiarDisplay(div_nivel2, 'MEN01_N3_' + id_nivel2);
                                var id_nivel3 = e.parentNode.getAttribute('id').substring(9);
                                var div_nivel3 = document.getElementById('Bot_' + id_nivel3);
                                if (div_nivel3 != null) {
                                    cambiarDisplay(div_nivel3, 'MEN01_N4_' + id_nivel3);
                                    cambiarDisplay(e, 'MEN01_N5_' + e.getAttribute('id').substring(4));
                                }
                            }
                        }
                    }
                }
                if (classnamejer == 'MEN01_nivel_5_OUT' || classnamejer == 'MEN01_nivel_5_OUT2') {
                    var id_nivel0 = e.parentNode.parentNode.parentNode.parentNode.parentNode.getAttribute('id').substring(9);
                    var div_nivel0 = document.getElementById('Bot_' + id_nivel0);
                    if (div_nivel0 != null) {
                        cambiarDisplay(div_nivel0, 'MEN01_N1_' + id_nivel0);
                        var id_nivel1 = e.parentNode.parentNode.parentNode.parentNode.getAttribute('id').substring(9);
                        var div_nivel1 = document.getElementById('Bot_' + id_nivel1);
                        if (div_nivel1 != null) {
                            cambiarDisplay(div_nivel1, 'MEN01_N2_' + id_nivel1);
                            var id_nivel2 = e.parentNode.parentNode.parentNode.getAttribute('id').substring(9);
                            var div_nivel2 = document.getElementById('Bot_' + id_nivel2);
                            if (div_nivel2 != null) {
                                cambiarDisplay(div_nivel2, 'MEN01_N3_' + id_nivel2);
                                var id_nivel3 = e.parentNode.parentNode.getAttribute('id').substring(9);
                                var div_nivel3 = document.getElementById('Bot_' + id_nivel3);
                                if (div_nivel3 != null) {
                                    cambiarDisplay(div_nivel3, 'MEN01_N4_' + id_nivel3);
                                    var id_nivel4 = e.parentNode.getAttribute('id').substring(9);
                                    var div_nivel4 = document.getElementById('Bot_' + id_nivel4);
                                    if (div_nivel4 != null) {
                                        cambiarDisplay(div_nivel4, 'MEN01_N5_' + id_nivel4);
                                        cambiarDisplay(e, 'MEN01_N6_' + e.getAttribute('id').substring(4));
                                    }

                                }
                            }
                        }
                    }
                }
                if (classnamejer == 'MEN01_nivel_6_OUT' || classnamejer == 'MEN01_nivel_6_OUT2') {
                    var id_nivel0 = e.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.getAttribute('id').substring(9);
                    var div_nivel0 = document.getElementById('Bot_' + id_nivel0);
                    if (div_nivel0 != null) {
                        cambiarDisplay(div_nivel0, 'MEN01_N1_' + id_nivel0);
                        var id_nivel1 = e.parentNode.parentNode.parentNode.parentNode.parentNode.getAttribute('id').substring(9);
                        var div_nivel1 = document.getElementById('Bot_' + id_nivel1);
                        if (div_nivel1 != null) {
                            cambiarDisplay(div_nivel1, 'MEN01_N2_' + id_nivel1);
                            var id_nivel2 = e.parentNode.parentNode.parentNode.parentNode.getAttribute('id').substring(9);
                            var div_nivel2 = document.getElementById('Bot_' + id_nivel2);
                            if (div_nivel2 != null) {
                                cambiarDisplay(div_nivel2, 'MEN01_N3_' + id_nivel2);
                                var id_nivel3 = e.parentNode.parentNode.parentNode.getAttribute('id').substring(9);
                                var div_nivel3 = document.getElementById('Bot_' + id_nivel3);
                                if (div_nivel3 != null) {
                                    cambiarDisplay(div_nivel3, 'MEN01_N4_' + id_nivel3);
                                    var id_nivel4 = e.parentNode.parentNode.getAttribute('id').substring(9);
                                    var div_nivel4 = document.getElementById('Bot_' + id_nivel4);
                                    if (div_nivel4 != null) {
                                        cambiarDisplay(div_nivel4, 'MEN01_N5_' + id_nivel4);
                                        var id_nivel5 = e.parentNode.getAttribute('id').substring(9);
                                        var div_nivel5 = document.getElementById('Bot_' + id_nivel5);
                                        if (div_nivel5 != null) {
                                            cambiarDisplay(div_nivel5, 'MEN01_N6_' + id_nivel5);
                                            cambiarDisplay(e, 'MEN01_N7_' + e.getAttribute('id').substring(4));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                if (classnamejer == 'MEN01_nivel_7_OUT' || classnamejer == 'MEN01_nivel_7_OUT2') {
                    var id_nivel0 = e.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.getAttribute('id').substring(9);
                    var div_nivel0 = document.getElementById('Bot_' + id_nivel0);
                    if (div_nivel0 != null) {
                        cambiarDisplay(div_nivel0, 'MEN01_N1_' + id_nivel0);
                        var id_nivel1 = e.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.getAttribute('id').substring(9);
                        var div_nivel1 = document.getElementById('Bot_' + id_nivel1);
                        if (div_nivel1 != null) {
                            cambiarDisplay(div_nivel1, 'MEN01_N2_' + id_nivel1);
                            var id_nivel2 = e.parentNode.parentNode.parentNode.parentNode.parentNode.getAttribute('id').substring(9);
                            var div_nivel2 = document.getElementById('Bot_' + id_nivel2);
                            if (div_nivel2 != null) {
                                cambiarDisplay(div_nivel2, 'MEN01_N3_' + id_nivel2);
                                var id_nivel3 = e.parentNode.parentNode.parentNode.parentNode.getAttribute('id').substring(9);
                                var div_nivel3 = document.getElementById('Bot_' + id_nivel3);
                                if (div_nivel3 != null) {
                                    cambiarDisplay(div_nivel3, 'MEN01_N4_' + id_nivel3);
                                    var id_nivel4 = e.parentNode.parentNode.parentNode.getAttribute('id').substring(9);
                                    var div_nivel4 = document.getElementById('Bot_' + id_nivel4);
                                    if (div_nivel4 != null) {
                                        cambiarDisplay(div_nivel4, 'MEN01_N5_' + id_nivel4);
                                        var id_nivel5 = e.parentNode.parentNode.getAttribute('id').substring(9);
                                        var div_nivel5 = document.getElementById('Bot_' + id_nivel5);
                                        if (div_nivel5 != null) {
                                            cambiarDisplay(div_nivel5, 'MEN01_N6_' + id_nivel5);
                                            var id_nivel6 = e.parentNode.getAttribute('id').substring(9);
                                            var div_nivel6 = document.getElementById('Bot_' + id_nivel6);
                                            if (div_nivel6 != null) {
                                                cambiarDisplay(div_nivel6, 'MEN01_N7_' + id_nivel6);
                                                cambiarDisplay(e, 'MEN01_N8_' + e.getAttribute('id').substring(4));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //.....
            }
        }
    }
    catch (er) {
        alert(er.message);
    }
}

function desabilitaFuncion(obj) {
    obj.onclick = new Function('return false;' + obj.onclick.toString().getFuncBody())
}

function habilitaFuncion(obj) {
    //alert(obj.onclick.toString().getFuncBody());
    if (!obj.onclick.toString().indexOf("return false;") != -1) {
        strClick = obj.onclick.toString().getFuncBody().replace("return false;", "")
        obj.onclick = new Function(strClick);
    }
}

String.prototype.getFuncBody = function() {
    var str = this.toString();
    str = str.replace(/[^{]+{/, "");
    str = str.substring(0, str.length - 1);
    str = str.replace(/\n/gi, "");
    if (!str.match(/\(.*\)/gi)) str += ")";
    return str;
} 