// -- Clase que gestiona el menu principal --

var swtpMenu = Class.create( );

swtpMenu.prototype = {
	
	initialize: function( ) {
		//Buscamos los A que son hijos de li con la clase submenu aplicada
		var nodos = $$( '#menu li ul' );
		$A( nodos ).each(
			function( nodo ) {
				if( nodo ) {
					this.buttonsInsert( nodo.parentNode );
					var boton1 = this.searchNodeByTag( nodo.parentNode.firstChild, 'IMG' );
					var boton2 = this.searchNodeByTag( boton1.nextSibling, 'IMG' );
					this.processUL( boton1 );
					this.attachEvent( boton1 );
					this.attachEvent( boton2 );
				}
			}.bind( this )
		);
	},
	
	buttonsInsert: function( nodo, visible ) {
		var code = '<img src="/swtp/imagenes/mas.gif" alt="boton mas" class="boton" /><img src="/swtp/imagenes/menos.gif" alt="boton menos" class="boton" style="display: none;" />';
		if( visible ) {
			code = '<img src="/swtp/imagenes/mas.gif" alt="boton mas" class="boton" style="display: none;" /><img src="/swtp/imagenes/menos.gif" alt="boton menos" class="boton" />';
		}
		new Insertion.Top( nodo, code );
	},
	
	changeButton: function( nodo ) {
		var buttons = nodo.parentNode.getElementsByTagName( 'IMG' );
		
		if( buttons.length ) {
			if( buttons[ 0 ].style.display == 'none' ) {
				buttons[ 1 ].style.display = 'none';		
				buttons[ 0 ].style.display = '';
			}
			else {
				buttons[ 0 ].style.display = 'none';			
				buttons[ 1 ].style.display = '';		
			}
		}
	},
	
	processUL: function( nodo ) {
		//Creamos un div contenedor (para el correcto funcionamiento del efecto) y lo ocultamos (si no tiene la clase actual aplicada)
		nodo = this.searchNodeByTag( nodo, 'UL' );
		var div = document.createElement( 'DIV' );
		var parent = nodo.parentNode;
		div.appendChild( nodo.cloneNode( true ) );
		parent.replaceChild( div, nodo );
		div.style.display = 'none';
		if( this.isCurrent( nodo ) ) {
			this.processEvent( div );
		}else if( this.isCurrent2( nodo ) ) {
			this.processEvent( div );
		}		
	},
	
	isCurrent: function( nodo ) {
		var list = $A( nodo.getElementsByTagName( 'LI' ) );
		var current = list.find( function( nodo ) {
			return ( Element.hasClassName( $( nodo ), 'actual' ) );
		});
		if( current ) {
			return( true );
		}
		else {
			return( false );
		}
	},

	isCurrent2: function( nodo ) {
		var list = $A( nodo.getElementsByTagName( 'LI' ) );
		var current = list.find( function( nodo ) {
			return ( Element.hasClassName( $( nodo ), 'madre_actual' ) );
		});
		if( current ) {
			return( true );
		}
		else {
			return( false );
		}
	},

	changeClassToLI: function( nodo ) {
		var li = nodo.parentNode;
		if( Element.hasClassName( $( li ), 'madre_expandido' ) ) {
			Element.toggleClassName( $( li ), 'madre' );
                        Element.removeClassName( $( li ), 'madre_expandido' );
		}
		else {
			//Element.addClassName( $( li ), 'expandido' );
                        Element.removeClassName( $( li ), 'madre' );
                        Element.toggleClassName( $( li ), 'madre_expandido' );
		}
	},
	
	searchNodeByTag: function( nodo, tagName, previous ) {
		if( ! nodo ) {
			return( false );
		}
		while( nodo.tagName != tagName ) {
			if( previous ) {
				nodo = nodo.previousSibling;
			}
			else {
				nodo = nodo.nextSibling;
			}
			if( ! nodo ) {
				return( false );
			}
		}
		return( nodo );
	},
	
	attachEvent: function( nodo ) {
		nodo.onclick = this.toogle.bindAsEventListener( this );
	},
	
	processEvent: function( nodo ) {
		this.changeButton( nodo );
		this.changeClassToLI( nodo );
                Effect.toggle($( this.searchNodeByTag( nodo, 'DIV' )),'slide', { duration: 0.8 });
               // Effect.toggle(nodo.next('div'),'slide', { duration: 0.8 });
	},

	toogle: function( evt ) {
		this.processEvent( Event.element( evt ) );
	}

}

