/******************************************

Fichero:		/lib/js/Menu.js
Fecha:			05-08-2004
Autor:			Korvus

Copyright (c) Vivace Multimedia
http://www.vivacemultimedia.com

******************************************/

/**
 * @class Menu
 * @package lib.js
 * @author Korvus
 */
 
/**
 * @function Menu
 * @access public
 *
 * @param sObjectID ( string )
 * @param [ nWidth ] ( int )
 * @param [ nHeight ] ( int )
 */

function Menu( sObjectID, nWidth, nHeight )
{
	
	// ==================== Atributos ==========================
	
	this.id					=			sObjectID;
	this.display			=			"none";
	
	this._type				=			null;
	this._items				=			new Array;
	
	/**
	 * Nodo HTML relacionado con el menú
	 * @var _node ( DOMElement )
	 * @access private
	 */
	
	this._node				=			null;
	
	/**
	 * Define si al expandir un elemento se contraerá el resto o no
	 * @var hideall ( Boolean )
	 * @access public
	 */
	
	this.hideall			=			false;
	
	/**
	 * Espaciado entre los elementos del menú
	 * @var spacing ( Integer )
	 * @access public
	 */
	
	this.spacing			=			0;
	
	/**
	 * Anchura del menú
	 * @var width ( Integer )
	 * @access public
	 */
	
	this.width				=			parseInt( nWidth );
	
	/**
	 * Altura del menú
	 * @var height ( Integer )
	 * @access public
	 */
	
	this.height				=			parseInt( nHeight );
	
	// ================ Metodos SET / ADD ======================

	/**
	 * Añade un elemento al menú
	 *
	 * @function addItem
	 * @access public
	 * 
	 * @param sValue ( String )
	 * @param [ sClassName ] ( String ) Estilo de la capa que contiene el elemento
	 * @param [ sHref ] ( String ) Página que se llamará al realizar el evento indicado por el último parámetro sobre la capa contenedora del elemento
	 * @param [ sEvent ] ( String ) Evento que lanza la llamada a 'sHref'
	 */

	this.addItem = function( sValue, sClassName, sHref, sEvent )
	{ 
	 this._items.push( { "value": sValue, "class": sClassName, "href": sHref, "event": typeof( sEvent ) == "undefined" ? "click" : sEvent } );
	 this._items[ this._items.length - 1 ]._menu = this;
	};
	
	/**
	 * Añade un submenú
	 *
	 * @function addSubMenu
	 * @param oSubMenu ( Menu )
	 * @param [ nItem ] ( Integer ) Elemento al que se le añadirá el submenu ( predeterminado: el último creado mediante 'addItem' )
	 */
	
	this.addSubMenu = function( oSubMenu, nItem )
	{
	 nItem = ( typeof( nItem ) == "undefined" && this._items.length > 0 ) ? this._items.length - 1 : 0;
	 this._items[ nItem ].submenu = oSubMenu;
	};
	
	// Setea el tipo de menu activo
	
	this.setType = function( sNewType )
	{
	 if( typeof( sNewType ) == "undefined" || [ "vertical", "horizontal" ].indexOf( sNewType ) == -1 )
	  return( false );
	  
	 this._type = sNewType;
	};
	
	// ==================== Metodos GET ========================
	
	this.getType = function()
	{ return( this._type ); };

	// ================== Otros metodos ========================
	
	this.show = function()
	{ 
	 this.refresh();
	 this._node.style.display = this.display = "block"; 
	 
	 /**
	  * Cambia el estado del menú en la cookie correspondiente
	  */
	 
	 document.setCookie( "menu-" + this.id, this.display );	 
	};
	
	/**
	 * Expande / Contrae el menú
	 * @function toggle
	 * @param nItemID ( int )
	 */
	
	this.toggle = function( nItemID )
	{
	 for( var i = 0; i < this._items.length; i++ )
	 {
	  if( typeof( this._items[ i ][ "submenu" ] ) == "object" && this._items[ i ][ "submenu" ]._items.length != 0 )
	  {
	   if( i == nItemID ) this._items[ i ][ "submenu" ][ this._items[ i ][ "submenu" ].display == "block" ? "hide" : "show" ]();
	   else if( this.hideall == true ) this._items[ i ][ "submenu" ].hide();
	  }
	 }
	}

	/** 
	 * Oculta el menú
	 * @function hide	 
	 */

	this.hide = function()
	{ 
	 this.refresh();
	 this._node.style.display = this.display = "none"; 
	 
	 /**
	  * Cambia el estado del menú en la cookie correspondiente
	  */
	 
	 document.setCookie( "menu-" + this.id, this.display );
	};
	
	/**
	 * Actualiza el nodo HTML relacionado con el menú
	 * @function refresh
	 */
	
	this.refresh = function()
	{ 
	 if( this._node == null )
	  this._node = document.getElementById( this.id );
	}

	this.toString = function()
	{
	 switch( this.getType() )
	 {
	  case "vertical":
	   
	   /**
	    * Recogemos el valor de la cookie para determinar si se debe mostrar el menú desplegado o no
		*/
	   
	   if( document.getCookie( "menu-" + this.id ) == "block" )
	    this.display = "block";

	   /**
	    * Atributos de la capa contenedora de los elementos del menú
	    * @var oAttributes ( Array )
		*/

	   var oAttributes = new Array();
	   
	   /**
	    * Valores imprescindibles de la capa contenedora
		*/
	   
	   oAttributes[ "id" ] = this.id;
	   oAttributes[ "style" ]  = "display: " + this.display + "; position: inherit;";
	   oAttributes[ "style" ] += "width: " + ( this.width == 0 ? "100%" : this.width + "px" ) + ";";
	   //oAttributes[ "style" ] += "height: " + ( this.height == 0 ? "100%" : this.height + "px" ) + ";";
	   
	   /**
	    * Código HTML del menú y sus subelementos / submenús
	    * @var sCode ( String )
		*/
	   
	   var sCode = new String( "<div " + oAttributes.ajoin( "=\"", "\" " ) + ">" );

	   for( var i = 0; i < this._items.length; i++ )
	   {
	    /**
		 * Atributos relacionados con el elemento actual
		 * @var oAttributes ( Array )
		 */
		   
	    oAttributes = new Array();
		
	    /** 
		 * Si se ha definido una referencia externa para el elemento actual, seteamos el código correspondiente
		 * para el evento onclick de éste
		 */
		   
	    if( typeof( this._items[ i ][ "href" ] ) == "string" )
		 oAttributes[ "onclick" ] = ( this._items[ i ][ "href" ].indexOf( "javascript:" ) == -1 ) ? ( "document.setCookie( 'menu-" + this.id + "', 'block' ); document.location = unescape( '" + escape( this._items[ i ][ "href" ] ) + "' ); if( window.event ){ window.event.cancelBubble = true; }" ) : this._items[ i ][ "href" ].replace( "javascript:", "" );

	    /**
		 * Si el elemento tiene un submenú relacionado, y éste posee al menos 1 elemento, le asignamos en el evento
		 * click el código necesario para mostrar / ocultar dicho submenu
		 */

		if( this._items[ i ][ "submenu" ] instanceof Menu && this._items[ i ][ "submenu" ]._items.length > 0 )
		 oAttributes[ "on" + this._items[ i ][ "event" ] ] = "with( getObject( '" + this.id + "' ) ){ toggle( " + i + " ); } if( window.event ){ window.event.cancelBubble = true; }";
		
		/**
		 * Estilo del elemento del menú
		 */
		
		oAttributes[ "class" ] = this._items[ i ][ "class" ];
	    
		/**
	     * Si el espaciado entre elementos del menú es diferente de '0' añadimos una pequeña capa con la altura estipulada
	 	 */
	   
	    if( this.spacing > 0 )
		 sCode += "<div style=\"height: " + this.spacing + "px; overflow: hidden;\"></div>";
		
		sCode += "<div " + oAttributes.ajoin( "=\"", "\" " ) + ">";
		sCode += this._items[ i ][ "value" ];
		 
		/**
		 * El elemento actual tiene submenu
		 */
		  
		if( typeof( this._items[ i ][ "submenu" ] ) == "object" )
		{
		 this._items[ i ][ "submenu" ].display = "none";
		 this._items[ i ][ "submenu" ].width = this.width;
		 
		 sCode += "</div>" + this._items[ i ][ "submenu" ].toString() + "<div>";
		}
		
		sCode += "</div>";
	   }
	   
	   return( sCode + "</div>" );
	  
	  break;
	 }	 
	};

}