<!--
/*
*	Autor: Pablo Salaberri
*	Descripción:
*		Módulo gestor de menus
*
*	
*
*/

var click_object;
var button_counter = 0;

var MENU_ITEMS=[];
//Button class
function XButton(parentmenu,nombre,cssboxclass,cssclass,title,action,dblclickaction,count){
	this.nombre = nombre;
	this.parentmenu = parentmenu;
	this.cssclass = cssclass;
	this.cssboxclass = cssboxclass;
	this.title = title;
	this.childs = Array();
	this.ischild=false;
	this.action = action;
	this.dblclickaction=dblclickaction;
	this.menu=null;
	this.expanded = false;
	this.ondbl=false;
	this.on=false;
	
	this.btnnumber = count; 
	
	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;
     XButton.prototype.clickdbl= clickdbl;
     XButton.prototype.mouseover= mouseover;
     XButton.prototype.mouseout= mouseout;
     XButton.prototype.set_recursive=set_recursive;
     XButton.prototype.set_click=set_click;
     XButton.prototype.set_dblclick=set_dblclick;
     XButton.prototype.select_name=select_name;
     XButton.prototype.expand_name=expand_name;
     
     
  }
  
   function select_name(nombre){
  //	alert('expandir '+nombre+' en '+this.btnnumber);  	
  	if ((this.btnnumber == nombre)&&(this.on!=true)){
  		this.on=true;
  		this.set_on();
  		return;
  	}
  	//alert("childslength " +this.childs.length);
  	if (this.childs.length){
	  	for (var k=0; k<this.childs.length;k++){
	  		//alert("child "+k+': '+this.childs[k]);
	  		this.childs[k].select_name(nombre);
	  	}
  	}
  	return true;
  }
  function expand_name(nombre){
  //	alert('expandir '+nombre+' en '+this.btnnumber);  	
  	if ((this.btnnumber == nombre)&&(this.expanded!=true)){
  		this.expanded=true;
  		this.expand();
  		return;
  	}
  	//alert("childslength " +this.childs.length);
  	if (this.childs.length){
	  	for (var k=0; k<this.childs.length;k++){
	  		//alert("child "+k+': '+this.childs[k]);
	  		this.childs[k].expand_name(nombre);
	  	}
  	}
  	return true;
  }
  function insertChild(xButton){
  	xButton.ischild = true;
  	xButton._parent = this;
  	this.childs.push(xButton);
  }
  function set_recursive(node,onoff){
  	if (node!==undefined){
  	if (node.nodeType!=1)return;
  	
  	if (node.cssclass==undefined)return;
	
	setClass(node,node.cssclass+onoff);
  	if (node.childNodes.length){
  		
  		for(i = 0; i<node.childNodes.length;i++){
  			this.set_recursive(node.childNodes[i],onoff);
  		}
  		return true;
  	}
  	}
  	
  	return true;
  }
  function set_click(){
  		setClass(this.main,this.main.cssclass+'clicked');
  }
  function set_dblclick(){
  		setClass(this.main,this.main.cssclass+'dblclicked');
  }
  
  function set_on(){
  	
  	this.set_recursive(this.button,'on');
  }
  function set_off(){
		this.set_recursive(this.button,'');
		//setClass(this.main,this.main.cssclass);
	
  }
  function clear(retract){
  	
  	
	this.on=false;
	this.set_off();
  	
  	if ((this.childs.length!==0)){
  		if (retract){
  			 this.retract();
  			 this.on=false;
  			 this.set_off();
  			 this.expanded=false;
  		}
  		else{
	  		for (var j=0;j<this.childs.length;j++){
	  			this.childs[j].clear();
	  		}
  		}
  	}
  	return;
  }
  function mouseout(){
  	//All are out
  	if (!this.on)
  	this.set_off();
  }
  function mouseover(){
	if (!this.on)
	this.set_on();
  }
  function click(){
  		var retract_onclick=true;
  		
  		if (this.on)return;
  		if (this.parentmenu.retract==3) retract_onclick=false;
  		this.parentmenu.clear_selection(retract_onclick);
  		
  		if (this.parentmenu.expand==1){
	  		if (this.parentmenu.retract==3) this.parentmenu.clear_selection(this.ischild);
	  		if (this.childs.length!=0){
	  				  			
	  			if (this.expanded){
	  				if (this.parentmenu.retract==1){
	  					this.expanded = false;
	  					this.retract();
	  				}
	  			}
				else{
					this.expand();
					this.expanded=true;	
				} 
					
			}
			else{
				
			}
  		}
  		if (!this.on){
					this.on = true;
					this.set_on();
					this.parentmenu.selected=this.btnnumber;
		}
		var action = htmlspecialchars_decode(this.action, 'ENT_QUOTES');
		
		eval(action);
		return true;
	
		
  }
  function clickdbl(){
  		
  		if (this.parentmenu.expand==2){
	  		if (this.parentmenu.retract==3) this.parentmenu.clear_selection(this.ischild);
	  		
	  		if (this.childs.length!=0){
	  				if (this.parentmenu.retract==2){
	  					
			  			if (this.expanded)
			  				{
			  					this.expanded = false;
			  					this.retract();
			  				}
			  			
						else{
							this.expand();
							this.expanded=true;	
						}
	  				}  	
				}
  		}
  		
		if (this.dblclickaction!=''){
			this.on = true;
			this.set_dblclick();	
			var dblclickaction = htmlspecialchars_decode(this.dblclickaction, 'ENT_QUOTES');
  			eval(dblclickactin)
		}
		
  	
  }
  function setup_button(where,classadds){
		var main = document.createElement('div');
			var cssboxclass = classadds + this.cssboxclass;
			setClass(main,cssboxclass);
			main.cssclass=cssboxclass;
		var button = document.createElement('div');
			button.cssclass =cssboxclass+'_outer';
		var inner;
		var text;
		
			setClass(button,cssboxclass+'_outer');
			inner= document.createElement('div');
				setClass(inner,cssboxclass+'_inner');
				inner.cssclass=cssboxclass+'_inner';
			text= document.createElement('div');
				setClass(text,cssboxclass+'_text');
				text.innerHTML = this.title;
				text.cssclass=cssboxclass+'_text';
				inner.appendChild(text);
				button.appendChild(inner);
			
			
			
		
		button.on = false;
		button.id = 'mainbutton';
		button._self = this;
		button.action = this.action;
		
		button.onmouseover = function(){
				this._self.set_on();
				//this._self.mouseover();
				this._self.isover=true;
				return true;
		}
		button.ondblclick = function (e){
				
				
				
				if (!e) var e = window.event;
				this._self.clickdbl();
				return true;
		}		
		button.onclick = function (e){
				
				if (!e) var e = window.event;
				if (e.type == 'dblclick') this._self.clickdbl;
				click_object = this._self;
				setTimeout('click_object.click();',300);
				//this._self.click();
				return true;
		}
		

		button.onmouseout = function (e){
			
				this._self.mouseout();
				return true;
		}	
			
		this.button=button;
		this.main = main;
		main.appendChild(button);
		var maincontainer = (where);
		this.menu = main;
		
		if (maincontainer!= null )maincontainer.appendChild(main);
		
	}
	
	function expand(){
		if (this.childs.length!=0){
			//Creamos un div nuevo en el que van englobados los otros
			var divmain = document.createElement('div');
			setClass(divmain,'submenu');
				
			
			for (i=0; i < this.childs.length; i++){
				this.childs[i].setup_button(divmain,'sub');
			}
			this.menu.appendChild(divmain);
			this.parentmenu.set_expanded(this.btnnumber);
		}	
		
	}
	function retract(){
		while(this.menu.lastChild){
			if (this.menu.lastChild.id != 'mainbutton'){
				this.menu.removeChild(this.menu.lastChild);
			}
			else{
				break;
			}
		}
		this.parentmenu.unset_expanded(this.btnnumber);
		
	}
			
}

/*
 * 		Xmenu
 * 
 *		padre: 
 * 		container:div wich contains the menu. Mus have a id
 * 		expand:
 * 				1 if menu expands with click  
 * 				2 if menu expands with dblclick
 * 		retract:
 * 				1 if menu retracts with click  
 * 				2 if menu retracts with dblclick
 * 				3 if menu retracts when we select another element
 * */

function Xmenu(padre,container,expand,retract,formulario){
	
	this.childs = Array();
	this.selected = eval('selected'+container);
	this.expanded = eval('expanded'+container);
	
	this.padre = padre; 
	this.container=findObject(container);
	this.expand = expand;
	this.retract = retract;
	this.count_buttons=0;
	if (formulario!==undefined) this.formulario = formulario; 
	else this.formulario = 'formulario';
	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;
     Xmenu.prototype.setup_button = setup_button;
     Xmenu.prototype.click_button = click_button;
     Xmenu.prototype.set_expanded = set_expanded;
     Xmenu.prototype.unset_expanded = unset_expanded;
     Xmenu.prototype.post_expanded = post_expanded;
     Xmenu.prototype.setup_expanded = setup_expanded;
     Xmenu.prototype.setup_selected = setup_selected;
     
  }
  	function setup_expanded(){
  		var a= setup_expanded.arguments;
  		for (var i=0; i<a.length;i++){
  			for (var j=0;j<this.childs.length;j++){
  				this.childs[j].expand_name(a[i]);
  			}
  		}
  	}
  	function setup_selected(){
  		var a= setup_selected.arguments;
  		for (var i=0; i<a.length;i++){
  			for (var j=0;j<this.childs.length;j++){
  				this.childs[j].select_name(a[i]);
  			}
  		}
  	}
  	function post_expanded(){
  		var out='';
  		for(var i=0; i<this.expanded.length;i++){
  			out+='<#'+this.expanded[i]+'#>';
  		}
  		set_input_value(this.formulario,'xmenu'+container+'_expanded',out);
  		set_input_value(this.formulario,'xmenu'+container+'_selected',this.selected);
  		return true;
  		
  	}
	function set_expanded(exp_elem){
		for (var i=0; i<this.expanded.length;i++){
			if (this.expanded[i]==exp_elem) return ;
		}
		this.expanded.push(exp_elem);
	}
	function unset_expanded(exp_elem){
		for (var i=0; i<this.expanded.length;i++){
			if (this.expanded[i]==exp_elem) {
				this.expanded.splice(i,1);
				return true;
			}
		}
		return true;
	}
	function click_button(count){
		this.childs[count].click();
	}
	function setup_button(){
		var i = 0;
		
		var actlevel = parseInt(this.padre[0]);
		
		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],this.padre[i+1]+':'+this.padre[0]);
		
												 
//alert('actlevel '+actlevel);										 
		for (var j=0 ; j<7; j++){
				this.padre.shift();
		}
//alert('padre length '+this.padre.length);
		var nextlevel = parseInt(this.padre[0]);
		//alert('nextlevel '+nextlevel);	
		while(actlevel < nextlevel){
			//alert('insertado hijo');	
			nbutton.insertChild(this.setup_button());
			nextlevel = parseInt(this.padre[0]);
			
			//alert('siguiente nextlevel '+nextlevel);	
		}
		return nbutton;
		
	}
	
	function setup_menu(){
		var i=0;
		
		while ( i<this.padre.length){
			//XButton(padre,nombre,cssclass,title,action)
			var xbutton = this.setup_button();
			
			this.childs.push(xbutton);
			
		}
		
		for (i=0; i<this.childs.length;i++){
			this.childs[i]._menu = this;
			this.childs[i].setup_button(this.container,'');
			
		}
		
		
		
	}
	
	
	function clear_selection(retract){
		
		for (var 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();
	
}

//-->
