// Tree Client Script Library
// Alfa-XP, Michael Isipciuc, 2006-2007.
//
Type.registerNamespace("FM");
Type.registerNamespace("FM.UI");

FM.UI.Tree= function(element) {
		FM.UI.Tree.initializeBase(this, [element]);
		
		this._SelectedNode= null;
		this._allowClick= true;
}

FM.UI.Tree.prototype= {

  initialize : function() {
		FM.UI.Tree.callBaseMethod(this, 'initialize');
		var o= this.get_element();
		$addHandlers(o, {
			'click': this._click,
			'dblclick': this._dblclick,
			'selectstart' : this._cancelEvent
		}, this);

  },

  dispose : function() {
		if (this.get_element() != null) $clearHandlers(this.get_element());
		
    FM.UI.Tree.callBaseMethod(this, 'dispose');
  },
  
  _click : function(e) {
		if (!this._allowClick) return;

		var o= e.target;
		switch (o.tagName) {
			case "SPAN" : o= o.parentNode;
			case "IMG" : 
				if (o.className == "tv_icon") { o= o.parentNode; break; }
				if (o.className == "tv_j") { this.toggleNode(o.nextSibling); return false; }
			case "A": break;
		}
		if (o.tagName == "A" && o.getAttribute('disabled') != '1') {
			this.SelectNode(o);
			return true;
		}
		return false;
  },
  
  _dblclick : function(e) {
		this._click(e);
  },


  SelectNode : function(htmlNode) {
		if (!this.raiseOnBeforeSelect(htmlNode)) return;
		this.SelectNodeWithoutRaising(htmlNode);
		this.raiseOnSelect();
	},
  
  SelectNodeWithoutRaising : function(htmlNode) {
		if (htmlNode == null) return;
		this.UnselectHtmlNode(this.getSelectedHtmlNode());

		this._SelectedNode= htmlNode;
		var Item= this._getNodeSpan(htmlNode);
		if (Item) Item.className= "tv_ns";
	},
	

	UnselectHtmlNode : function(htmlNode) {
		var Item= this._getNodeSpan(htmlNode);
		if (Item) Item.className= "tv_n";
	},
	
	getSelectedHtmlNode : function() {
		if (!this._SelectedNode) {
			var nodeID= this.get_element().getAttribute('SelectedNodeID');
			this._SelectedNode= this._findNodeByID(nodeID);
		}
		return this._SelectedNode;
	},
	
	GetSelectedNode : function() {
		var htmlNode= this.getSelectedHtmlNode();
		if (htmlNode) return FM.UI.TreeNode.GetOrCreate(htmlNode);
		else return null;
	},
	
	_getNodeSpan : function(htmlNode) {
		if (htmlNode == null) return null;
		return htmlNode.childNodes[htmlNode.childNodes.length - 1];
	},
	
	toggleNode : function(htmlNode) {
		var JunctionNode= htmlNode.previousSibling;
		var src= JunctionNode.src;
		var isExpanded= src.indexOf("minus.gif") > 0;
		this.changeImages(JunctionNode, isExpanded);
		
		var htmlDiv= htmlNode.parentNode;
		var childDivNodes= htmlDiv.childNodes;
		for (var i= 0; i < childDivNodes.length; i++) {
			var div= childDivNodes[i];
			if (div.tagName != "DIV") continue;
			div.style.display= (isExpanded) ? "none": "block";
		}
	},
	
	changeImages : function(JunctionNode, isExpanded) {
		var src= JunctionNode.src;
		JunctionNode.src= (isExpanded) ? src.replace("minus.gif", "plus.gif") : src.replace("plus.gif", "minus.gif");
	},
	
	_findNodeByID : function(id) {
		var nodes= this.get_element().getElementsByTagName("A");
		var o;
		for (var i= 0; i < nodes.length; i++) {
			o= nodes[i];
			if (o.getAttribute('nid') == id) return o;
		}
		return null;
	},


 
	_cancelEvent : function(e) {
		e.preventDefault();
		return false;
	},
	
	Enable : function() {
		this._allowClick= true;
	},
	Disable : function() {
		this._allowClick= false;
	},
	
	GetTreeHtmlNodes : function() {
		var o= this.get_element();
		if (!o) return []; // to prevent some error cases
		else return o.getElementsByTagName("A");
	},

  // Events
  add_onSelect: function(h) { this.get_events().addHandler("onSelect", h); },
  remove_onSelect: function(h) { this.get_events().removeHandler("onSelect", h); },
  raiseOnSelect: function() { 
		var h= this.get_events().getHandler("onSelect"); 
		if (h) h(this.GetSelectedNode());
	},
	
	add_onBeforeSelect: function(h) { this._onBeforeSelectEvent= h; },
  raiseOnBeforeSelect: function(htmlNode) { 
		return (this._onBeforeSelectEvent) ? this._onBeforeSelectEvent(FM.UI.TreeNode.GetOrCreate(htmlNode)) : true;
	},

	EndFunc : function() { }

}

FM.UI.Tree.inheritsFrom(Sys.UI.Control);
FM.UI.Tree.registerClass('FM.UI.Tree', Sys.UI.Control);

FM.UI.TreeNode= function(htmlNode) {
	this.ID= htmlNode.getAttribute('nid');
	this.Data= htmlNode.getAttribute('data');

	this.HtmlNode= htmlNode;
	
	this.GetDefaultText= function() {
		return this.HtmlNode.getAttribute('tooltip');
	}
	
	this.GetText= function() {
		if (this.HtmlNode.childNodes.length == 0) return "";
		var o= this.HtmlNode.childNodes[this.HtmlNode.childNodes.length - 1];
		return o.innerHTML;
	}
	
	this.SetText= function(s) {
		if (this.HtmlNode.childNodes.length == 0) return;
		var o= this.HtmlNode.childNodes[this.HtmlNode.childNodes.length - 1];
		o.innerHTML= s;
	}
}

FM.UI.TreeNode.GetOrCreate= function(htmlNode) {
	if (htmlNode.TreeNode == null) htmlNode.TreeNode= new FM.UI.TreeNode(htmlNode);
	return htmlNode.TreeNode;
}

Sys.Application.notifyScriptLoaded();
