/***
 * TreeCollection.js
 *
 * Author: 	Cory Mawhorter
 * Version:	1.0.1
 *
 *	Released by Ephective.  More information for this and other programs can be found at http://www.ephectiveinnovations.com/
 * 
 * =====
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

function TreeCollection() {
	this.nodes=new Array();
}

TreeCollection.prototype.addChild = function(tn) { this.nodes[this.nodes.length] = tn; return this.nodes[this.nodes.length-1]; };
TreeCollection.prototype.getNodeById = function(findNode) {
	var arr = (typeof arguments[1] == 'undefined') ? this : arguments[1];

	for(var i in arr.nodes) {
		if(arr.nodes[i].id==findNode) {
			return arr.nodes[i];
		}
	
		if(arr.nodes[i].hasChildren()) {
			var tn = this.getNodeById(findNode,arr.nodes[i]);
			if(tn) { return tn; }
		}
	}
	
	return false;
};

TreeCollection.prototype.getNodeByName = function(findNode) {
	var arr = (typeof arguments[1] == 'undefined') ? this : arguments[1];

	for(var i in arr.nodes) {
		if(arr.nodes[i].name==findNode) {
			return arr.nodes[i];
		}
	
		if(arr.nodes[i].hasChildren()) {
			var tn = this.getNodeByName(findNode,arr.nodes[i]);
			if(tn) { return tn; }
		}
	}
	
	return false;
};

TreeCollection.prototype.removeNode	= function(findNode) {
	for(var i in this.nodes) {
		if(this.nodes[i].id==findNode) {
			this.nodes.splice(i, 1);
			return true;
		}
	
		if(this.nodes[i].hasChildren()) {
			this.nodes[i].removeNode(findNode);
		}
	}	
};
										
TreeCollection.prototype.hasChildren = function() { return (this.nodes.length > 0); }






// TreeNode

var __treenodes = [];
function TreeNode() {
	var id;
	while(id = this.generateID())
		if(!__treenodes[id]) break;
	this.id=__treenodes[id]=id;
	
	this.name=(typeof arguments[0] == 'undefined') ? null : arguments[0]; 
	this.data=(typeof arguments[1] == 'undefined') ? null : arguments[1];
	this.nodes=(typeof arguments[2] == 'undefined') ? new Array() : arguments[2];
}

TreeNode.prototype.generateID = function() { return Math.floor(Math.random()*999999-100000+1)+999999; };

TreeNode.prototype.addChild = function(tn) { this.nodes[this.nodes.length] = tn; return this.nodes[this.nodes.length-1]; };
TreeNode.prototype.addChildren = function(tn_col) { for(var i in tn_col) { this.nodes[this.nodes.length] = tn_col[i]; } };

TreeNode.prototype.removeNode	= function(findNode) {
	for(var i in this.nodes) {
		if(this.nodes[i].id==findNode) {
			this.nodes.splice(i, 1);
			return true;
		}
	
		if(this.nodes[i].hasChildren()) {
			this.nodes[i].removeNode(findNode);
		}
	}	
};

TreeNode.prototype.clearNodes	= function() { this.nodes.length = 0; };

TreeNode.prototype.hasChildren = function() { return (this.nodes.length > 0); };

TreeNode.prototype.toString = function() { return this.id; };
