//all this is necessary only because of mother f..ign IE mouse layering bug!!!
var browserIsIe = navigator.userAgent.toLowerCase().indexOf("msie") > -1;
// --------------------------- timers [ -----------------------
var YTimer = {
	timers:[],
	nutimer:function(msTime,ontimeout,datas){
		var evst = "YTimer.timers["+YTimer.timers.length+"].oto(YTimer.timers["+YTimer.timers.length+"].dat);";
		YTimer.timers.push({oto:ontimeout,dat:datas});
		return evst;
	},
	timeout:function(msTime,ontimeout,datas){
		return window.setTimeout(YTimer.nutimer(msTime,ontimeout,datas),msTime);
	},
	interval:function(msTime,ontimeout,datas){
		return window.setInterval(YTimer.nutimer(msTime,ontimeout,datas),msTime);
	},
	waitBody:function(onLoaded,datas){
		
		if(document && document.body){
			YLogger.log(" document.body ");
			//YEvt.add(document.body,"load",onLoaded,datas);
			onLoaded(datas);
		}else{
			YTimer.timeout(100,function(datas){YTimer.waitBody(datas.ol,datas.dat);},{dat:datas,ol:onLoaded});
		}
	}
};
// --------------------------- ] timers -----------------------
// --------------------------- events [ -----------------------
var YEvt = {
	listeners:[],
	"add":function(trigger,eventDesc,eventFunk,datas) {
		var evitm = { tr:trigger , de:eventDesc , fu:eventFunk , da:datas};
		var evst = "var funk = function(e){var ls =YEvt.listeners["+(YEvt.listeners.length)+"];"+
					" if(ls){ls.fu(browserIsIe?event:e,ls.da);}	};";
		eval(evst);
		if( navigator.userAgent.toLowerCase().indexOf("firefox") && eventDesc=="mousewheel" )eventDesc = 'DOMMouseScroll';
		try{	if(trigger.addEventListener)	trigger.addEventListener(eventDesc,funk,false);
				else							trigger.attachEvent("on"+eventDesc,funk);
		}catch(e){alert("Error YEvt.add :\n"+e);return null};
		YEvt.listeners.push(evitm);
		return YEvt.listeners.lengt - 1;
	},
	"remove":function(evindx) {
		if(evindx>-1 && evindx<YEvt.pile.length)YEvt.listeners[evindx]=null;
		return evindx;
	}
};
// --------------------------- ] events -----------------------

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

function YtreeNode(linod,ulnod){
	this._parent = null;
	this._li = linod;// structure model : <li> <ul></ul> </li>
	this._ul = ulnod;
	this.childs = [];
	this.rhide_timer = null;
	this.requirehide = function(){
		this.rhide_timer = YTimer.timeout(500,function(owner){
			owner.hide();
		},this);
	};
	this.hide = function(){
		if(this._ul){
			this.rhide_timer = null;
			this.hideChilds();
			this._ul.style.display = "none";
		}
	};
	this.hideChilds = function(){
		for(i in this.childs){
			this.childs[i].hide();
		}
	};
	this.show = function(){
		if(this._ul){
			if(this.rhide_timer)window.clearTimeout(this.rhide_timer);
			this.rhide_timer = null;
			this._ul.style.display = "block";
			if(this._parent)this._parent.show();
		}
	};
	this.build = function(){
		if(this._ul){
			for(i in this._ul.childNodes){
				var cn = this._ul.childNodes[i];
				if(cn.tagName){
					var tagname = cn.tagName.toLowerCase();
					if(tagname=="li"){
						var uls = cn.getElementsByTagName("ul");
						var child = (uls.length>0) ? new YtreeNode(cn,uls[0]) : new YtreeNode(cn,null);
						//var rul = cn.getElementsByTagName("ul")[0];
						child._parent = this;
						this.childs.push(child);
					}
				}
			}
		}
	};
	this.dynamize = function(){
		if(this._li){
			YEvt.add(this._li,'mouseover',function(evt,owner){
				owner.show();
			},this);
			if(this._ul){
				YEvt.add(this._ul,'mouseover',function(evt,owner){
					owner.show();//alert("ul over");
				},this);
				/*YEvt.add(this._ul,'mouseout',function(evt,owner){
					owner.requirehide();
				},this);*/
			}
			YEvt.add(this._li,'mouseout',function(evt,owner){
				owner.requirehide();
			},this);
		}
		for(i in this.childs){
			this.childs[i].dynamize();
		}
	};
	this.build();
}


var YCTree={
	init : function(rootdivId){//main_menu_div
		//alert('YCTree init');
		try{
			var rdiv = document.getElementById(rootdivId);
			var rul = rdiv.getElementsByTagName("ul")[0];
			var nod = new YtreeNode(null,rul);
			nod.hideChilds();
			nod.dynamize();
			return nod;
		}catch(e){alert("YCTree Error:"+e);}
	}
};
/*
$(document).ready(function () {
	YCTree.init("main_menu_div");
});*/


