//Quick function to specify a bunch of stuff as data
function JSTreeDataObject(text,xOff,yOff,url)
{
this.text = text;
this.xOff = xOff;
this.yOff = yOff;
this.url = url;
}

//Function (pretty much only good for testing) of quickly creating a new node without DB intervention
function JSTreeQuick(text,id)
{

return new JSTree(text,75,-25,"http://www.google.com",id);   

}

//Standard node constructor
//text: Node text
//xOff: x position offset
//yOff: y blah blah
//url: Node link address
//id: Unique ID useful to find the node
function JSTree(text,xOff,yOff,url,id)
{
	this.children = null;
	this.leaf = true;
	this.data = new JSTreeDataObject(text,xOff,yOff,url);
	this.id = id;
}

//Adds a child node to constructor
JSTree.prototype.addChild = function(node)
{
	if(this.leaf == true)
	{
		this.children = new Array();
		this.children[0] = node;
		this.leaf = false;
	}
	else
	{
		this.children[this.children.length] = node;
	}
}

//Adding a quickly generated node created by text input
//Not really useful other than in testing w/o DB
JSTree.prototype.addNewChild = function(text, id)
{
	if(this.leaf == true)
	{
		this.children = new Array();
		this.children[0] = new JSTreeQuick(text,id);
		this.leaf = false;
	}
	else
	{
		this.children[this.children.length] = new JSTree(text,id);
	}
}

//Removes a child node of the current node
//Not really useful, just added for completeness
JSTree.prototype.removeChild = function(id)
{
	if(this.leaf == true) 
	{
	return false;
	}
	else
	{
		for (child in this.children)
		{
			if(this.children[child].id == id)
			{
				this.children.splice(child,1);
			}		
		}
		if(this.children.length == 0) 
		{
			this.leaf = true;
			
		}
		return true
	}
	return true;
}

//Finds an element in the tree with a given ID. Searches downward from current node.
//Returns the element if found, returns false otherwise.
JSTree.prototype.getElementFromTree = function(id)
{
	if(this.id == id)
	{
	return this;
	}
	else if(this.leaf)
	{
	return false;
	}
	else
	{
		for (var i=0;i<this.children.length;i++)
		{
			if((this.children[i].getElementFromTree(id) != false))
			{
			//alert(this.children[i].data);
				
				return this.children[i].getElementFromTree(id);
			}
		}
	}
	return false;

}

//Wrapper
JSTree.prototype.getData  = function()
{
return this.data;
}