
SZN.DynamicTree = SZN.ClassMaker.makeClass({
	NAME: "DynamicTree",
	VERSION: "1.0",
	CLASS: "class"
});

SZN.DynamicTree.prototype.$constructor = function(id) {
	this._dom = SZN.gEl(id);
	if(!this._dom) this.$destructor();


	
	this.eventsCache = [];
	this.eventsCache.push(SZN.Events.addListener(window,"unload",this,"$destructor",false,true));
	
	var lis = this._dom.getElementsByTagName("li");
	for (var i = 0; i < lis.length; i++){
		this.eventsCache.push(SZN.Events.addListener(lis[i],"mouseover",this,"showChild",false,true));
		this.eventsCache.push(SZN.Events.addListener(lis[i],"mouseout",this,"hideChild",false,true));		
	}
	
	this.menus = [];

}

SZN.DynamicTree.prototype.$destructor = function() {
	for (var i=0;i<this.eventsCache.length;i++) {
		SZN.Events.removeListener(this.eventsCache[i]);
	}
	for (var p in this) { this[p] = null; }
}

SZN.DynamicTree.prototype.showChild = function(e,elm) {
	this.timerIndex=this.timerIndex+1;
	
	if(elm.hasChildNodes()){
		for (var i = 0; i < elm.childNodes.length; i++){
			if(elm.childNodes[i].nodeName=="UL"){
				
				if(!this.menus[elm.childNodes[i].id]){
					this.menus[elm.childNodes[i].id] = new SZN.DynamicTreeMenu(this,elm.childNodes[i]);	
				}
				else this.menus[elm.childNodes[i].id].show();
				
			}
		}
	}
}
SZN.DynamicTree.prototype.hideChild = function(e,elm) {
	this.timerIndex=this.timerIndex+1;

	if(elm.hasChildNodes()){
		var children = elm.getElementsByTagName("ul");
		for (var i = 0; i < children.length; i++){
			var menuDom = children[i];
			if(this.menus[menuDom.id]){
				this.menus[menuDom.id].hide();
			}
		}
	}
}

SZN.DynamicTreeMenu = SZN.ClassMaker.makeClass({
	NAME: "DynamicTreeMenu",
	VERSION: "1.0",
	CLASS: "class"
});
SZN.DynamicTreeMenu.prototype.$constructor = function(parent,dom) {
	this._parent = parent;
	this._dom = dom;
	if(!this._dom) this.$destructor();
	
	this.direction = 1;
	this.fadeStep= 0.2;
	this.setOpacity(0);
	
	this.eventsCache = [];
	this.eventsCache.push(SZN.Events.addListener(window,"unload",this,"$destructor",false,true));
	
	var menu = this;
	this.timer = setInterval(function(){ menu.tick(); }, 80);
	this.show();	
	
}

SZN.DynamicTreeMenu.prototype.$destructor = function() {
	clearInterval(this.timer);
	this._parent.menus[this._dom.id] = null;
	
	for (var i=0;i<this.eventsCache.length;i++) {
		SZN.Events.removeListener(this.eventsCache[i]);
	}
	for (var p in this) { this[p] = null; }
}

SZN.DynamicTreeMenu.prototype.tick = function() {
	this.setOpacity(this.getOpacity()+parseFloat(this.fadeStep*this.direction));
	
	if(this.getOpacity()>=1){
		
	}
	if(this.getOpacity()<=0){
		this._dom.style.display="none";
		this.$destructor();
	}
	
}
SZN.DynamicTreeMenu.prototype.hide = function() {
	this.direction = -1;
}
SZN.DynamicTreeMenu.prototype.show = function() {
	this.direction = 1;
	this._dom.style.display="block";
}

SZN.DynamicTreeMenu.prototype.setOpacity = function(opacity) {
	
	if(opacity >= 0.95) opacity = 0.95;
	if(opacity <= 0) opacity = 0;
	
	if(SZN.Browser.client=="ie"){
		this._dom.style.filter = "alpha(opacity="+(parseFloat(opacity)*100)+")";
	}else this._dom.style.opacity = opacity;
	
}
SZN.DynamicTreeMenu.prototype.getOpacity = function() {
	if(SZN.Browser.client=="ie"){
		var hell = this._dom.style.filter.match(/alpha\(opacity=(.*)\)/);
		if(hell[1]){
			return parseFloat(hell[1])/100;	
		}
	}else return parseFloat(this._dom.style.opacity);
	return 1.0;
}

