/*
Script: TreeGroup.js
	Contains <TreeGroup> Class.

Author:
	Darryl Yip, <http://www.mindspeed.com>

License:
	GNU/GPL license.
	
Dependencies:
	Mootools Core
	Mootools Native
	Mootools Addons
*/

/*
Class: TreeGroup
	Creates a tree interface. 

Arguments:
	options - an Object, see options below.

Options:
	id - the css id of the tree group
	expandAll - true/false to expand all initially
	expandFirst - true/false to expand first level initially (if expandAll is true, this is ignored)
	handleClass - css class name for the tree handlers (default is 'handle')

Example:
	(start code)
	<div id="treeGroup">
		<h3 class="handle">Branch 1</h3>
		<ul>
			<li>Item 1</li>
			<li>Item 2</li>
			<li>
				<h3 class="handle">Item 3</h3>
				<ul>
					<li>Item 3 - a</li>
					<li>Item 3 - b</li>
					<li>Item 3 - c</li>
				</ul>
			</li>
			<li>Item 4</li>
			<li>Item 5</li>
		</ul>
	</div>
	
	<script type="text/javascript">
	var treeGroup = new TreeGroup({
		id: 'treeGroup',
	});
	</script>
	(end)
*/
var TreeGroup = new Class({
	initialize: function(options){
		if( $(options.id) ) {
			this.options 											= options;
			this.options.handleClass 					= (this.options.handleClass) ? '.'+this.options.handleClass : '.handle';
			this.options.expandAll	 					= (this.options.expandAll) ? true : false;
			this.options.expandFirst	 				= (this.options.expandFirst) ? true : false;
			this.handles											= $ES(this.options.handleClass, this.options.id); //$S('#'+this.options.id+' '+this.options.panelClass);
			this._build();
		}
	},
	_build: function() {
		if( this.handles ) {
			for(i=0; i<this.handles.length; i++) {
				var handleId = this.options.id + '_handle_' + i;
				if( !this.options.expandAll ) { // if NOT expand all, then check to see if first level needs expanding
					if( !this.options.expandFirst || (this.handles[i].getParent().getProperty('id') != this.options.id) ) {
						this._display(i);
					} 
				}
				this.handles[i].setProperty('id', handleId);
				this.handles[i].addEvent('click', this.toggle.pass([i], this));
			}
		}
	},
	_display: function(i) {
		if( i < this.handles.length ) {
			var panel	= this.handles[i].getNext();
			if( panel.getStyle('display') == 'none' ) {
				this.handles[i].addClass('current');
				panel.setStyle('display', '');
			} else {
				this.handles[i].removeClass('current');
				panel.setStyle('display', 'none');
			}
		}
	},
	/*
   Function: toggle
		Display the selected branch.

   Parameters:
      iToToggle - integer or array of integers to display, first index is 0.

	*/
	toggle: function(iToToggle) {
		if($type(iToToggle)=='array') {
			iToToggle.each(function(i){
				this._display(i); //this.handles[i].getNext());
			}, this);
		} else {
			this._display(iToToggle); //this.handles[iToToggle].getNext());
		}
	},
	/*
   Function: expandAll
		Expand and display the entire tree.
	*/
	expandAll: function() {
		for(i=0; i<this.handles.length; i++) {
			this.handles[i].setStyle('display', '');
			this.handles[i].addClass('current');
			this.handles[i].getNext().setStyle('display', '');
		}
	},
	/*
   Function: expandAll
		Expand and display the entire tree.
	*/
	closeAll: function() {
		for(i=0; i<this.handles.length; i++) {
			this.handles[i].setStyle('display', '');
			this.handles[i].removeClass('current');
			this.handles[i].getNext().setStyle('display', 'none');
		}
	}
});