// JavaScript Document
// mod 2009-05-25
MenuTree = new Class({
	submenus:[]
	,usesubmenu:null
	,cache:[]
	,el:null
	,options: {
		onStart: Class.empty
		,onComplete: Class.empty
		,onCancel: Class.empty
		,onRequest: Class.empty
		,onFailure: function(transport){if(transport)alert("Error:\n"+transport.responseText);}
		,loading_html: '<span style="color:#999999;font-size:10px;font-family:Verdana, Arial">Loading...</span>'
		,id:''
		/*,duration:300*/
	}
	,initialize: function(el,options)
	{
		this.setOptions(options);
		this.el = $(el);
		if(this.el.id){this.options.id = this.el.id;}
		if(this.options.id)this.cache = $COOKIE(this.options.id).split(",");
		var elul = $E('ul', this.el);
		var submenu = null
		if ( elul ){
			for (var i=0; i<elul.childNodes.length; i++)
			{
				var element = $(elul.childNodes[i]);
				if ( !element )continue;
				if ( String(element.nodeName).toLowerCase() != 'li' )continue;
				var item = $E('a', element);
				
				if ( item.rel && !item.myAjax ){
					item.options = Json.evaluate(item.rel);
					item.options.onComplete= function (text,xml){
						//alert(this.owner.MenuTree);
						/*if(!this.owner.MenuTree){
							this.owner.MenuTree = new MenuTree(this.owner.childs_div);
						}*/
						if(this.owner.childs_div==this.options.update)new MenuTree(this.options.update,{id:this.options.id});
						this._ctrl_class.onComplete(text,xml);
					};
					item.options.onRequest= this.onRequest.bind(this);
					item.options.onFailure= this.onFailure.bind(this);
					item.options.onCancel= this.onCancel.bind(this);
					item._ctrl_class = this;
					//item.childs_div = new Element('div',{});
					item.childs_div = new Element('div');
					if(!item.options.update)item.options.update = item.childs_div;
					else item.options.update = $(item.options.update);
					item.childs_div.setStyle('display','none');
					item.childs_div.setStyle('margin','0px');
					//item.childs_div.setStyle('border','1px #cccccc solid');
					item.childs_div.injectAfter(item);
					
					if(!item.myAjax){
						item.myAjax = new Ajax(item.options.url, {
						method: 'get'
						,update:item.options.update
						,onComplete: item.options.onComplete
						,onRequest: item.options.onRequest
						,onFailure: item.options.onFailure
						,onCancel: item.options.onCancel
						,autoCancel:true
						,evalScripts:true
						,id:this.options.id
						});
						item.myAjax.owner = item;
						item.myAjax._ctrl_class = item._ctrl_class;
					}
					
					item.addEvent("click",MenuTree.click);
					if(this.options.id){
						//alert([this.options.id,item.options.url]);
						if( this.cache.contains(item.options.url) ){
							MenuTree.click.call(item);
						}
					}
						

				}
				//element.childs_div.innerHTML = "123";
				
				//catmenu-tree
			}
		}// if (elul)  \
		
	}
	,savecache:function(url,io){
			if(this.options.id){
				if ( io ){
					if( !this.cache.contains(url) ){
						this.cache.push(url);
					}
				}else{
					if( this.cache.contains(url) ){
						this.cache.remove(url);
					}
				}
				setcookie(this.options.id,this.cache.join(","));
			}
		
	}
	,onComplete:function(text,xml){
		this.fireEvent('onComplete', [text,xml]);	
	}
	,onCancel:function(){
		this.fireEvent('onCancel');	
	}
	,onRequest:function(){
		this.fireEvent('onRequest');	
	}
	,onFailure:function(transport){
		this.fireEvent('onFailure',transport);	
	}
	
});
MenuTree.implement(new Chain, new Events, new Options);
MenuTree.click = function(event){
	if ( event ){
		event = new Event(event);
		event.stop();
	}
	if( this.childs_div.getStyle("display") == 'none' || this.options.update != this.childs_div ){
		this.options.update.innerHTML = this._ctrl_class.options.loading_html;
		
		this.myAjax.request();
		this.childs_div.setStyle('display','');
		this._ctrl_class.savecache(this.options.url,true);
	}else{
		if(this.options.update == this.childs_div)this.childs_div.setStyle('display','none');
		this._ctrl_class.savecache(this.options.url,false);
	}
}
