//Menu Vertical Plegat
function MenuVerticalPlegat(idDiv) {
    this.Div = getObject(idDiv);

    this.InicialirzarContenidors(this.Div);
};

MenuVerticalPlegat.prototype.InicialirzarContenidors = function(oObjecte) {
    for (var i = 0; i < oObjecte.childNodes.length; i++) {
        if (oObjecte.childNodes[i].className == "emcontenidor" 
            || oObjecte.childNodes[i].className == "emcontenidortext"
            || oObjecte.childNodes[i].className == "emcontenidorimatge"
            || oObjecte.childNodes[i].className == "emcontenidorimatgetext") {

                //Objecte actual: contenidors
                if (typeof(oObjecte.contenidors) == "undefined") {
                    oObjecte.contenidors = new Array();
                }

                oObjecte.contenidors.push(oObjecte.childNodes[i]);

                oTitol = getChildObjectNoText(oObjecte.childNodes[i], 0);

                if (oTitol) {
                    oTitol.Menu = this;
                }

                if (oObjecte.childNodes[i] != this.Div) {
                    if (oTitol) {
                        oTitol.onclick = this.TitolClick;
                    }
                    
                    oContingut = getChildObjectNoText(oObjecte.childNodes[i], 1);

                    if (oContingut) {
                        GW_AmagarContingut(oContingut);
                    }
                }
        }

        //Recursivitat
        if (oObjecte.childNodes[i].childNodes && oObjecte.childNodes[i].childNodes.length > 0) {
            this.InicialirzarContenidors(oObjecte.childNodes[i]);
        }
    }
};

MenuVerticalPlegat.prototype.TitolClick = function(e) {
    //Plegar tots amb nivell > 1
    for (var i = 0; i < this.Menu.Div.contenidors.length; i++) { 
        oContingut = getChildObjectNoText(this.Menu.Div.contenidors[i], 1); 

        if (oContingut) { 
            GW_AmagarContingut(oContingut);
            if (oContingut.contenidors){
	            for (var j = 0; j < oContingut.contenidors.length; j++) { 
	                oFill = getChildObjectNoText(oContingut.contenidors[j], 1); 
	                
	                if (oFill) { 
	                    GW_AmagarContingut(oFill); 
	                }
	            }
	        }
        }
    }

    //Mostrar l'actual
    this.Menu.MostrarElementMenu(this.parentNode);
    
    oContingut = getChildObjectNoText(this.parentNode, 1);

    if (oContingut) {
         oContingut.style.display = "block";
    }
};

MenuVerticalPlegat.prototype.MostrarElementMenu = function(oElement) {
    var oPare = oElement;

    while (oPare && oPare.parentNode) {
        oPare = oPare.parentNode.parentNode;

        if (oPare && (oPare.className == "emcontenidor" 
            || oPare.className == "emcontenidortext"
            || oPare.className == "emcontenidorimatge"
            || oPare.className == "emcontenidorimatgetext")) {

                oContingut = getChildObjectNoText(oPare, 1);
                
                if (oContingut) {
                    oContingut.style.display = "block";
                }
        }
    }
};

MenuVerticalPlegat.prototype.MarcarElementsMenu = function(arrIds) {
    for (i = 0; i < arrIds.length; i++) {
        this.MarcarElementMenu(arrIds[i]);
    }    
};

MenuVerticalPlegat.prototype.MarcarElementMenu = function(sId) {
    oElement = getObject(sId);
    
    if (oElement) {
        this.MostrarElementMenu(oElement);

        oTitol = getChildObjectNoText(oElement, 0);
        
        if (oTitol.tagName == "A") {
            oTitol = getChildObjectNoText(oTitol, 0);
        }

        if (oTitol) {
            oTitol.className = oTitol.className + "marcat";
        }
    }

};


//Funcions utils
function GW_ExecutarAlLoadPagina(sFuncio) {
    attachObjEvent(this, "load", function() { eval(sFuncio); });
}

function GW_AmagarContingut(oContingut) {
    oContingut.style.display = "none";
    
    if (oContingut.contenidors && oContingut.contenidors.length > 0) {
        for (var i = 0; i < oContingut.contenidors.length; i++) {
            oContingutFill = getChildObjectNoText(oContingut.contenidors[i], 1);

            if (oContingutFill) {
                GW_AmagarContingut(oContingutFill);
            }
        }
    }
}

//Privat
if (!document.all) { emulateEventHandlers(["click", "mousemove", "keypress", "keyup", "keydown", "mouseover", "mouseout", "mouseup", "mousedown", "focus", "resize"]); }
function emulateEventHandlers(eventNames) { for (var i = 0; i < eventNames.length; i++) { document.addEventListener(eventNames[i], function (e) {window.event = e;}, true); } }
function attachObjEvent(obj, sEvent, sFunction) { if (!document.all) { obj.addEventListener(sEvent, sFunction, false); } else { obj.attachEvent("on" + sEvent, sFunction); } }
function getObject(sId) { return document.getElementById(sId); }
function getChildObjectNoText(oObject, iIndex) { var iCont = 0; for (var i = 0; i < oObject.childNodes.length; i++) { if (oObject.childNodes[i].tagName != null) { if (iCont == iIndex) { return oObject.childNodes[i]; } else { iCont++; } } } return false; }
function checkObject(oObject) { if (oObject && (typeof(oObject) == "object")) { return true; } return false; }
