/*****************************************************************
 *
 * JS_BRAMUS - by Bramus! - http://www.bram.us/
 * hTree: for use with the wTree PHP Class (special version)
 *
 * v 1.0.1 	- 2007.01.10 	- keepopen class added
 * v 1.0 	- 2007.01.09 	- initial release
 *
 * only comment so far (no time yet) is Exodus 20.15 (Google it!)
 *
 *****************************************************************/
 
hTree	= Class.create();

hTree.prototype = {

	initialize	: function(tree, defaultColor, activeColor) {
		this.elements		= tree.getElementsByTagName("li");
		this.previousNode	= null;
		this.defaultColor	= defaultColor;
		this.activeColor	= activeColor;
		if (!$('htree_toggler')) {
			new Insertion.After(tree.parentNode, "<div id=\"htree_toggler\" style=\"clear: both; display: block;\">Safari Debug</div>");
		}
		this.prepTree();
	},
	
	// prepares the tree (click events baby!)
	prepTree	: function() {		
		$A(this.elements).each(function(treenode) {
			if (treenode.childNodes.length == 2) {
				// bind event listener on first item 
				// treenode.childNodes[0].onclick = 	this.toggleTreeNode.bindAsEventListener(this); // disabling for this project
				
				// collapse the second item, only if we don't need to keep it open
				if (treenode.className == "keepOpen") {	// used to be "treenode.hasClassName("keepOpen")" but IE can't handle that :S
					this.showDiv(treenode.childNodes[1].id, treenode.offsetLeft);
				}
				
				// init the subtree, which will be a wTree - freaking out a bit...
				// new wTree(treenode.childNodes[1]);
			} else {
				return;
			}
		}.bind(this));		
	},
	
	// collapses a treenode
	collapseDiv	: function(el, padLeft) {
		Effect.BlindUp(
			$('htree_toggler'), { 
				afterFinish : function() {
					$(this.previousNode).parentNode.childNodes[0].style.color = this.defaultColor;
					if (el != (this.previousNode.toString())) {
						this.showDiv(el, padLeft);
					} else {
						this.previousNode = null;	
					}
				}.bind(this),
				duration: 0.5
			}
		);
	},
	
	// shows a treenode
	showDiv		: function(el, padLeft) {
		padLeft = 0;	// override
		this.previousNode											= el;
		$(this.previousNode).parentNode.childNodes[0].style.color 	= this.activeColor;
		$('htree_toggler').style.paddingLeft						= padLeft + "px";
		$('htree_toggler').innerHTML								= "<ul>" + $(el).innerHTML + "</ul>";
		
		// new wTree($('htree_toggler').childNodes[0]); // - freaking out a bit...
		
		Effect.BlindDown(
			$('htree_toggler'), { 
				afterFinish : function() {
					// $(this.previousNode).parentNode.childNodes[0].style.color = this.activeColor;
				}.bind(this),
				duration: 0.5 
			}
		);
	},
	
	// toggles a treenode 
	toggleTreeNode	: function(evt) {
			
		padLeft	= (Event.element(evt).parentNode.offsetLeft);	
				
		if (this.previousNode != null) {
			this.collapseDiv(Event.element(evt).parentNode.id + "_childTree", padLeft);
		} else {
			this.showDiv(Event.element(evt).parentNode.id + "_childTree", padLeft);
		}
				
		return false;
	}

}
