<!--
/*
*	Autor: Pablo Salaberri
*	Descripción:
*		Módulo gestor de menus
*
*
*
*/
//Button class
function XButton(parentmenu,nombre,cssclass,title,action,image){
	this.nombre = nombre;
	this.parentmenu = parentmenu;
	this.cssclass = cssclass;
	this.title = title;
	this.childs = Array();
	this.ischild=false;
	this.action = action;
	this.menu=null;
	this.expanded = false;
	if (image=='') image = null;
	this.image = image;
	if (typeof(_xbutton_prototype_called) == 'undefined')
  {
     _xbutton_prototype_called = true;
     XButton.prototype.setup_button = setup_button;
     XButton.prototype.insertChild = insertChild;
     XButton.prototype.clear = clear;
     XButton.prototype.expand = expand;
     XButton.prototype.retract = retract;
     XButton.prototype.set_on= set_on;
     XButton.prototype.set_off= set_off;
     XButton.prototype.click= click;
  }
  function insertChild(parentmenu,nombre,id,cssclass,title,action,image){
  	nbutt = new XButton(parentmenu,nombre,id,cssclass,title,action,image);
  	nbutt.ischild = true;
  	this.childs.push(nbutt);
  }
  function set_on(){
  	if (this.image != null)
	setClass(this.button,this.button.cssclass+'on');
  }
  function set_off(){
  	if (this.image != null){
		if (this.on) return;
		setClass(this.button,this.button.cssclass);
  	}
	
  }
  function clear(retract){
  	
	
  	if (!this.expanded){
	  	this.on=false;
	  	this.set_off();
  	}
  	
  	if ((this.childs.length!==0)&& (this.expanded==true)){
  		
  		if (retract){
  			 this.retract();
  			 this.on=false;
  			 this.set_off();
  			 this.expanded=false;
  		}
  		else{
	  		for (j=0;j<this.childs.length;j++){
	  			
	  			this.childs[j].clear();
	  		}
  		}
  		
  	}
  	return;
  }
  function click(){
 		
		this.parentmenu.clear_selection(this.ischild);
		this.on = true;
		this.set_on();
		
		//this.clear_selection();
		if (this.childs.length!=0){
			if (this.expanded==false){
				
				this.expand();
				setClass(this.button,this.button.cssclass+'on');
				this.on = true;
				this.expanded = true;
				
			}
			else{
				this.retract()
				setClass(this.button,this.button.cssclass);
				this.on = false;
				this.expanded = false;
				
			
			}
		}
		else{
			var action = htmlspecialchars_decode(this.action, 'ENT_QUOTES');
			
			eval(action);	
		}
		
  }
  function setup_button(where){
		var main = document.createElement('div');
		var button = document.createElement('div');
			setClass(button,this.cssclass);
			button.cssclass = this.cssclass;
			
		if (this.image!=null){
			var imagein = document.createElement('img');
			imagein.src=this.image;
			button.appendChild(imagein);
		}
		else{
			
			button.innerHTML = this.title;
			
		}
		button.on = false;
		button.id = 'mainbutton';
		button._parent = this;
		button.action = this.action;
		
		button.onmouseover = function(){
				this._parent.set_on();
		}
		button.onmouseout = function(){
				this._parent.set_off();
		}
		button.onclick = function (){
				this._parent.click();
		}
			
			
		this.button=button;
		main.appendChild(button);
		var maincontainer = (where);
		this.menu = main;
		
		if (maincontainer!= null )maincontainer.appendChild(main);
		
	}
	
	function expand(){
		if (this.childs.length!=0){
			for (i=0; i < this.childs.length; i++){
				this.childs[i].setup_button(this.menu);
			}
		}	
		
	}
	function retract(){
		while(this.menu.lastChild){
			if (this.menu.lastChild.id != 'mainbutton'){
				this.menu.removeChild(this.menu.lastChild);
			}
			else{
				break;
			}
		}
		
	}
			
}

function Xmenu(padre,container){
	this.childs = Array();
	this.padre = padre; 
	this.container=findObject(container);
	if (this.container==null){
		 alert('No container');
		 return;
	}
	
	if (typeof(_xmenu_prototype_called) == 'undefined')
  {
     _xmenu_prototype_called = true;
     Xmenu.prototype.insertChild = insertChild;
     Xmenu.prototype.setup_menu = setup_menu;
     Xmenu.prototype.clear_selection = clear_selection;
  }
	
	
	
	function setup_menu(){
		var i=0;
		while ( i<this.padre.length){
			//XButton(padre,nombre,cssclass,title,action)
			var nbutton = new XButton(this,this.padre[i+1],this.padre[i+2],this.padre[i+3],this.padre[i+4],this.padre[i+5],this.padre[i+6]);
			i=i+6;
			if (i > this.padre.length) break;
			//alert('A creado boton '+this.padre[i+1]+' que es:'+this.padre[i]);
			if (this.padre[i]=='sub'){
				
				while(i<this.padre.length){
					if (this.padre[i]=='') break;
					//alert('B creado boton '+this.padre[i+1]);
					nbutton.insertChild(this,this.padre[i+1],this.padre[i+2],this.padre[i+3],this.padre[i+4],this.padre[i+5],this.padre[i+6]);
					i=i+6;
					//alert('siguiente boton '+this.padre[i+1]);
					if (i > this.padre.length) break;
				}	
			}
			this.childs.push(nbutton);
			
			
		}
		for (i=0; i<this.childs.length;i++){
			this.childs[i].setup_button(this.container);
			
		}
				
		
		
	}
	
	
	function clear_selection(retract){
		
		for (i=0; i<this.childs.length;i++){
			this.childs[i].clear(!retract);
		}
	}
	
	function insertChild(nombre,cssclass,title,action){
		var child = Array();
			child.push(nombre);
			child.push(cssclass);
			child.push(title);
			child.push(action);
			this.childs.push(child);
		
	}	
	this.setup_menu();
}
//-->