var ControlMenu = Class.create({
	
	
	
	idRaiz : "mi_0",
	
	
	claseN1Abierto : "elemN1Abierto",
	claseN1Cerrado : "elemN1Cerrado",
	claseN1AbiertoN2 : "elemN1AbiertoN2",
	claseN1CerradoN2 : "elemN1CerradoN2",
	claseTituloElemN1 : "tituloElemN1",
	
	claseN2Abierto : "elemN2Abierto",
	claseN2Cerrado : "elemN2Cerrado",
	claseN2AbiertoN3 : "elemN2AbiertoN3",
	claseN2CerradoN3 : "elemN2CerradoN3",
	claseTituloElemN2 : "tituloElemN2",

	claseN3Abierto : "elemN3Abierto",
	claseN3Cerrado : "elemN3Cerrado",
	claseTituloElemN3 : "tituloElemN3",


	
	listaN2 : "ul.contenedorMenuN2",
	listaN3 : "ul.contenedorMenuN3",

	cerradoAutomatico : function () 
	{
		var raiz =  $(this.idRaiz);
				
		if(raiz != null){
			var listas = raiz.select(this.listaN2);
			
			for(var i=0; i < listas.length; i++)
			{		
				listas[i].style.display = "none";			
			}
			
			var listas = raiz.select(this.listaN3);
			
			for(var i=0; i < listas.length; i++)
			{		
				listas[i].style.display = "none";			
			}
		}
	},
	
	trataMenu : function (idActual) 
	{

		var clasesElemento = $(idActual).classNames();

		/*if (clasesElemento.include(this.claseN1Abierto)) {	
			this.cerradoManual();
			this.cambiaClase(idActual, this.claseN1Abierto, this.claseN1Cerrado);
		}
		else if (clasesElemento.include(this.claseN1Cerrado)) {
			this.cerradoManual();
			this.cambiaClase(idActual, this.claseN1Cerrado, this.claseN1Abierto);
		}*/
	
		if (clasesElemento.include(this.claseN1CerradoN2)) {	
			this.cerradoManual();
			this.cambiaClase(idActual, this.claseN1CerradoN2, this.claseN1AbiertoN2);
			var descendientes = $(idActual+"_0"); 
			if(descendientes != null){
				descendientes.setStyle({
					"display" : "block"
				});
			}
		}
		else if (clasesElemento.include(this.claseN1AbiertoN2)) {
			this.cerradoManual();
			this.cambiaClase(idActual, this.claseN1AbiertoN2, this.claseN1CerradoN2);
			var descendientes = $(idActual+"_0"); 
			if(descendientes != null){
				descendientes.setStyle({
					"display" : "none"
				});
			}
		}
		
		/* nivel 2*/
		
/*		if (clasesElemento.include(this.claseN2Abierto)) {	
			this.cambiaClase(idActual, this.claseN2Abierto, this.claseN2Cerrado);
		}
		
		else if (clasesElemento.include(this.claseN2Cerrado)) {
			this.cambiaClase(idActual, this.claseN2Cerrado, this.claseN2Abierto);
		}*/
	
		if (clasesElemento.include(this.claseN2CerradoN3)) {	
		
		
			/*Cerramos hermanos*/		
			var raiz =  $(this.idRaiz);
			var listas = raiz.select("."+this.claseN2AbiertoN3);		
			for(var i=0; i < listas.length; i++)
			{		
				this.cambiaClase(listas[i].id, this.claseN2AbiertoN3, this.claseN2CerradoN3);		
				var descendientes = $(listas[i].id+"_0"); 
				if(descendientes != null){
					descendientes.setStyle({
						"display" : "none"
					});
				}						
			}
			
			this.cambiaClase(idActual, this.claseN2CerradoN3, this.claseN2AbiertoN3);
			var descendientes = $(idActual+"_0"); 
			if(descendientes != null){
				descendientes.setStyle({
					"display" : "block"
				});
			}
			
			
			
		}
		else if (clasesElemento.include(this.claseN2AbiertoN3)) {
			this.cambiaClase(idActual, this.claseN2AbiertoN3, this.claseN2CerradoN3);
			var descendientes = $(idActual+"_0"); 
			if(descendientes != null){
				descendientes.setStyle({
					"display" : "none"
				});
			}
		}
	
		/* nivel 3*/
		
/*		if (clasesElemento.include(this.claseN3Abierto)) {	
			this.cambiaClase(idActual, this.claseN3Abierto, this.claseN3Cerrado);
		}*/
		else if (clasesElemento.include(this.claseN3Cerrado)) {
			this.cambiaClase(idActual, this.claseN3Cerrado, this.claseN3Abierto);
		}
	

	}, 
	
	cerradoManual : function () 
	{

		/*Cerrado de primer nivel*/
		var raiz =  $(this.idRaiz);
		
		/*Cierra listas desplegadas N2 */
		var listas = raiz.select(this.listaN2);		
		for(var i=0; i < listas.length; i++)
		{		
			listas[i].style.display = "none";					
		}
		/*Cierra listas desplegadas N3 */
		var listas = raiz.select(this.listaN3);		
		for(var i=0; i < listas.length; i++)
		{		
			listas[i].style.display = "none";					
		}
		/*Cambia titulo a cerrado  N1*/		
/*		var listas = raiz.select("."+this.claseN1Abierto);		
		for(var i=0; i < listas.length; i++)
		{		
			this.cambiaClase(listas[i].id, this.claseN1Abierto, this.claseN1Cerrado);				
		}*/

		var listas = raiz.select("."+this.claseN1AbiertoN2);		
		for(var i=0; i < listas.length; i++)
		{		
			this.cambiaClase(listas[i].id, this.claseN1AbiertoN2, this.claseN1CerradoN2);					
		}

		/*Cambia titulo a cerrado  N2*/		
		/*var listas = raiz.select("."+this.claseN2Abierto);		
		for(var i=0; i < listas.length; i++)
		{		
			this.cambiaClase(listas[i].id, this.claseN2Abierto, this.claseN2Cerrado);					
		}*/
		
		/*Cambia titulo a cerrado  N2*/		
		var listas = raiz.select("."+this.claseN2AbiertoN3);		
		for(var i=0; i < listas.length; i++)
		{		
			this.cambiaClase(listas[i].id, this.claseN2AbiertoN3, this.claseN2CerradoN3);					
		}
		


	},
	
	cambiaClase : function (id, elimina, anyade)
	{
		$(id).removeClassName(elimina);
		$(id).addClassName(anyade);
	},
	
	colorearMenuInicio: function (id) {

		/*obtengo las clases del elemento llamante*/
		var clasesElemento = $(id).classNames();
		if(clasesElemento.include(this.claseN1Cerrado)){
			this.cambiaClase(id, this.claseN1Cerrado, this.claseN1Abierto);
		}
		var array = id.split("_");
		if(array.size() == 2){
			if(clasesElemento.include(this.claseN1Cerrado)){
				this.cambiaClase(id, this.claseN1Cerrado, this.claseN1Abierto);
			}			
		}
		if(array.size() == 3){
			var descendientes =  $(array[0] + "_" + array[1] + "_0"); 
			if(descendientes != null){
				descendientes.setStyle({
					"display" : "block"
				});
			}
			
			var descendientes =  $(array[0] + "_" + array[1]); 
			if(descendientes != null){
					var clasesElemento2 = $(descendientes).classNames();
					if(clasesElemento2.include(this.claseN1CerradoN2)){
						this.cambiaClase(descendientes, this.claseN1CerradoN2, this.claseN1AbiertoN2);
					}
			}
			
			if(clasesElemento.include(this.claseN1CerradoN2)){
				this.cambiaClase(id, this.claseN1CerradoN2, this.claseN1AbiertoN2);
			}			
			if(clasesElemento.include(this.claseN2Cerrado)){
				this.cambiaClase(id, this.claseN2Cerrado, this.claseN2Abierto);
			}			
		}
		if(array.size() == 4){
			var descendientes =  $(array[0] + "_" + array[1] + "_0"); 
			if(descendientes != null){
				descendientes.setStyle({
					"display" : "block"
				});
			}
			var descendientes =  $(array[0] + "_" + array[1] +  "_" + array[2] + "_0"); 
			if(descendientes != null){
				descendientes.setStyle({
					"display" : "block"
				});
			}
			
			var descendientes =  $(array[0] + "_" + array[1]); 
			if(descendientes != null){
					var clasesElemento2 = $(descendientes).classNames();
					if(clasesElemento2.include(this.claseN1CerradoN2)){
						this.cambiaClase(descendientes, this.claseN1CerradoN2, this.claseN1AbiertoN2);
					}
			}
				
			var descendientes =  $(array[0] + "_" + array[1] +  "_" + array[2]); 
			if(descendientes != null){
					var clasesElemento2 = $(descendientes).classNames();
					if(clasesElemento2.include(this.claseN2CerradoN3)){
						this.cambiaClase(descendientes, this.claseN2CerradoN3, this.claseN2AbiertoN3);
					}
			}


			if(clasesElemento.include(this.claseN1CerradoN2)){
				this.cambiaClase(id, this.claseN1CerradoN2, this.claseN1AbiertoN2);
			}			
			if(clasesElemento.include(this.claseN2CerradoN3)){
				this.cambiaClase(id, this.claseN2CerradoN3, this.claseN2AbiertoN3);
			}			
			if(clasesElemento.include(this.claseN3Cerrado)){
				this.cambiaClase(id, this.claseN3Cerrado, this.claseN3Abierto);
			}			
		}
	}
	
	
});

