﻿function IsActive(link) {
    try {
        if (activeMenu) {
            for (var i = 0; i < activeMenu.length; i++) {
                if (activeMenu[i] == link) {
                    return true;
                }
            }
        }
        return false;
    }
    catch (e) {
        return false;
    }
}
function GetMatch(href) {
    var re = new RegExp("\\?menu=(.*)$");
    var m = re.exec(href);
    if (m == null) {
        return null;
    }
    else {
        return m[1];
    }
}

function CollapseMenu() {
    $("#leftnav > ul > li").each(
        function(i) {
            var currentUL = this.getElementsByTagName("ul")[0];
            currentUL.style.marginLeft = "18px";
            
            var currentA = this.getElementsByTagName("a")[0];
            currentA.style.display = "inline";
            currentA.style.marginLeft = "10px";

            var a = document.createElement("a");
            a.style.display = "inline";
            var img = document.createElement("img");

            img.style.width = "11px";
            img.style.height = "11px";
            a.appendChild(img);
            a.href = "javascript:void(0)";

            if (IsActive(GetMatch(currentA.href))) {
                currentUL.style.display = "block"
                img.src = imgGeneralPath + "minus.gif";
                img.alt = "Plegar menú (" + currentA.innerHTML + ")";
            }
            else {
                currentUL.style.display = "none";
                img.src = imgGeneralPath + "plus.gif";
                img.alt = "Desplegar menú (" + currentA.innerHTML + ")";
            }

            a.onclick = function() {
                if (currentUL.style.display == "none") {
                    currentUL.style.display = "block"
                    img.src = imgGeneralPath + "minus.gif";
                    img.alt = "Plegar menú (" + currentA.innerHTML + ")";
                }
                else {
                    currentUL.style.display = "none";
                    img.src = imgGeneralPath + "plus.gif";
                    img.alt = "Desplegar menú (" + currentA.innerHTML + ")";
                }
            };
            this.removeChild(currentA);
            var div = document.createElement("div");
            this.insertBefore(div, this.childNodes[0]);
            div.appendChild(a);
            div.appendChild(currentA);
        }
    );
}