/*jeisso@hotmail.it
 * 19 de Agosto de 2009
 * menu vertical, ajax, json
 * */
var Menu = function(url){
	this.Item = Array();
	this.CategActiva='-1';
	this.Cached=[];
	this.url=url;
	this.ItemAbierto=null;
};
Menu.prototype={
	Mostrar : function(id){		
		if(!this.Cached[id]){
			this.Cached[id]=1;
			new Ajax.Request(this.url, {asynchronous:true, parameters: {padre:id}, evalScripts: true,	
				onLoading: this.Loading.bind(this,id),
				onSuccess: this.Procesar.bind(this,id),
		    	onException: function(e){
		    		$('loading-'+id).update(e);
	    		},
	    		onFailure: function(error){
	    			$('loading-'+id).update("Peticcion fallo");	    		
	    		}
		    });
		}else{
			this.Toggle('loading-'+id,false);
			this.Toggle('hijo-'+id,true);
		}
	},
	Show: function(id){	
		this.Toggle('hijo-'+id,true);
		this.Mostrar(id);
	},
	Procesar: function(id,res){
		try{
			var result= res.responseText.evalJSON();
			for(var j=0; j<result.length; j++){
				new Insertion.Bottom('hijo-'+id, "" +
						"<li class='"+((j+1)%2==0?"claro":"oscuro")+"'" +							
							(result[j].categ_hijos>0||result[j].categ_mostrar==1?" onmouseout=\"objetomenu.Toggle('hijo-"+result[j].categ_id+"',false);\" onmouseover=\"objetomenu.Show('"+result[j].categ_id+"');\"":"") +">"+
							"<a class='menu_text_item' href='"+result[j].categ_url+"'><b>"+result[j].categ_descripcion+"</b></a>" +(result[j].categ_hijos>0||result[j].categ_mostrar==1?"<img src='images/menu_mas.png' border='0' class='info' alt='+'>":"")+
							(result[j].categ_hijos>0||result[j].categ_mostrar==1?"<ul class='hijo' id='hijo-"+result[j].categ_id+"'>" +
											"	<li class='loading' id='loading-"+result[j].categ_id+"'></li>" +
											"</ul>":"")+
						"</li>");
			}
			this.Toggle('loading-'+id, false);			
		}catch(e){
			$('loading-'+id).update("Error response JSON");	
		}
	},
	Loading: function(id){	
		this.Toggle('loading-'+id,true);		
		this.SetMensaje("<img src='images/loading-categorias.gif'>Cargando Categorias...",'loading-'+id);
	},
	Toggle: function(id,estado){
		$(id).style.display=estado?'block':'none';
	},
	SetMensaje: function(text,id){		
		$(id).update(text);
	}
};
var objetomenu=new Menu('Modulos/Menu/Mod.QueryMenu.php');