/**
 * @file treeView.js
 * @date Aug 2007
 */	
 
var TreeView = Class.create();
TreeView.prototype = {
		
	initialize: function (id)
	{
		this.top = id;
		
		var h = this.nodeClick;
		
		$$('#' + id + ' li.branch').each(function (el) {
			el.observe('mousedown', h.bind(el));
			// make the branch open if not already specified
			if (!el.hasClassName('open') && ! el.hasClassName('close'))
				el.addClassName('open');
		});			
		
		// clean up empty branches
		$$( '#' + id + ' li.branch ul' ).each( function ( el ) {
			if (!el.down('li')) 
			{
				// add an item to indicate the lack of items
				li = document.createElement('li');
				li.appendChild(document.createTextNode('No Topics to Display'));
				el.appendChild(li);
				el.addClassName('empty');	
				// close the branch				
				el.up('li').removeClassName('open').addClassName('close');
			}
		});
	},
	
	nodeClick: function (e)
	{
		var el = Event.element(e);
		
		el.blur();
		
		if (el != this) 
			return;
		
		if (el.hasClassName('open'))
			el.removeClassName('open').addClassName('close');
		else if (el.hasClassName('close'))
			el.removeClassName('close').addClassName('open');
	},
	
	toogleBranch: function ( el )
	{
		if (el.hasClassName('open'))
			el.removeClassName('open').addClassName('close');
		else if ( el.hasClassName('close'))
			el.removeClassName('close').addClassName('open');
	}
	
};
	