// Niveles abiertos del menu.
abierto = Array();
abierto[1] = 0;
abierto[2] = 0;
abierto[3] = 0;



function abre(id, nivel){
	if (abierto[nivel] == 0){
		abierto[nivel] = id;
		estilo(1,nivel,id);
		abrir(id, nivel);
	}
	else{
		if (abierto[nivel] == id){			
			for (i=nivel;i<=3;i++){
				estilo(0,i,abierto[i]);
				cerrar(abierto[i],nivel);
				abierto[i] = 0;
			}		
		}
		else{
			if (abierto[nivel] > 0){				
				for (i=nivel;i<=3;i++){
					estilo(0,i,abierto[i]);
					cerrar(abierto[i],nivel);
					abierto[i] = 0;
				}				
				abierto[nivel] = id;
				estilo(1,nivel,id);
				abrir (id, nivel);
				
				
			}			
		}
	}
}


function estilo(hover,nivel,id){
	tit = id + "_tit";
	
	if ($(tit)){
		if (hover==1)
			$(tit).className = "toggler_" + nivel + "_selected";
		else
			$(tit).className = "toggler_" + nivel;
	}
		
}

function abrir(id, nivel){	
	desc = id + "_desc";
	
	/*if ($(desc)){
		$(desc).setStyle('height','auto');
		$(desc).setStyle('visibility','visible');
	}*/
	
	/*var myVerticalSlide = new Fx.Slide(desc);
	myVerticalSlide.slideOut();*/
	
	
	
	if ($(desc) && $('altura_menu_'+id)){
		
		//Ampliar lo que se expande a continuacion.
		if (nivel==2){
			var fx3 = new Fx.Tween($(abierto[1] + "_desc"), {duration:200, wait:false});
			altura = parseInt($('altura_menu_'+abierto[1]).value) + parseInt($('altura_menu_'+id).value);
			//alert(altura);
			fx3.start('height', altura);
		}
		
		
		var fx = new Fx.Tween($(desc), {
				duration:300, 
				wait:false,	
				
				onComplete: function() {
					var fx2 = new Fx.Tween($(desc), {duration:500, wait:false});	
					fx2.set('opacity', '0'); 
					fx2.start('opacity', '1');  
				}
		});	
		altura = $('altura_menu_'+id).value;
		//alert(altura);
		fx.start('height', altura);
		
		
	}
			
}

function cerrar(id, nivel){
	desc = id + "_desc";
	
	if (nivel==2){
			var fx3 = new Fx.Tween($(abierto[1] + "_desc"), {duration:50, wait:false});
			altura = parseInt($('altura_menu_'+abierto[1]).value);
			//alert(altura);
			fx3.start('height', altura);
		}
	
	if ($(desc)){
		$(desc).setStyle('height','0px');
		$(desc).setStyle('visibility','hidden');
		//$(desc).innerHTML="";
	}
	
	/*var myVerticalSlide = new Fx.Slide(desc);
	myVerticalSlide.slideIn();*/
			
}


function pad_in(element){
	var fx = new Fx.Tween(element, {duration:150, wait:false});
	fx.start('padding-left', '5');
}

function pad_out(element){
	var fx = new Fx.Tween(element, {duration:150, wait:false});
	fx.start('padding-left', '0');
}


window.addEvent('domready', function() {
	var n1 = $('n1').value;
	var n2 = $('n2').value;	
	var n3 = $('n3').value;
	
	abierto[1] = n1;
	abierto[2] = n2;
	abierto[3] = n3;
		
	/*if (n1!=-1) abre(n1, 1);
	if (n2!=-1) abre(n2, 2);
	if (n3!=-1) abre(n3, 3);*/
	estilo(1,1,n1);
	estilo(1,2,n2);
	estilo(1,3,n3);
});





function abreMenu(id, nivel){
	var myRequest = new Request({
		url:    'js/abreMenu.php',
		method: 'get',
		data:   'id=' + id + '&nivel=' + (nivel+1),

		onRequest: function(){
		},

		onComplete: function(html){
		},

		onSuccess: function(responseText, responseXML){        	
			// Si la solicitud se terminó exitosamente.
			/*
				responseText - (string) The returned text from the request.
				responseXML - (mixed) The response XML from the request.
				
				$('componente').set('html', responseText);
			*/  		         
			escribir = id + "_desc";
			$(escribir).innerHTML = responseText;
			
			abre(id,nivel);
			//$('altura_menu')
		},

		onFailure: function(xhr){
			// Si la solicitud se completó con problemas.
			/*
				xhr - (XMLHttpRequest) The transport instance.
			*/
			alert('fail');
		},

		onCancel: function(){
		},

		onException: function(headerName, value){
			// La transmisión de la cabecerá falló.
			/*
				headerName - (string) The name of the failing header.
				value - (string) The value of the failing header.
			*/
		}
	}).send();
}
