//<SCRIPT LANGUAGE="Javascript">
///////////////////////////////////////////////////////////////////////
// bcTreeNode.js
//
// tree data structure modeled after the W3C DOM Node object.
// Copyright (c) 2000 by Bob Clary. All rights reserved.
//
// requires: oop.js
//

registerFile('bcTreeNode.js', 'oop.js')

_classes.registerClass('bcTreeNode');

function bcTreeNode()
{
	_classes.defineClass('bcTreeNode', _prototype_func);

	this.init();

	function _prototype_func()
	{
		bcTreeNode.prototype.parentNode			= null;
		bcTreeNode.prototype.firstChild			= null;
		bcTreeNode.prototype.lastChild			= null;
		bcTreeNode.prototype.childNodes			= null;
		bcTreeNode.prototype.previousSibling	= null;
		bcTreeNode.prototype.nextSibling		= null;
		

		bcTreeNode.prototype.init = init;
		function init()
		{
			this.parentMethod('init');
			this.childNodes = new bcArray();		
		};
		
		bcTreeNode.prototype.destroy = destroy;
		function destroy()
		{
			var i;
			
			if (this.childNodes)
				this.childNodes.destroy();
				
			this.parentNode			= null;
			this.firstChild			= null;
			this.lastChild			= null;
			this.previousSibling	= null;
			this.nextSibling		= null;
			this.childNodes			= null;
			
			this.parentMethod('destroy');
		}
		
		bcTreeNode.prototype.appendChild = appendChild;
		function appendChild(newChild)
		{
			var oldLastChild = this.lastChild;
			
			this.childNodes.push(newChild);

			if (!this.firstChild)
				this.firstChild = newChild;
					
			if (this.lastChild)
				this.lastChild.nextSibling = newChild;
					
			this.lastChild = newChild;
					
			newChild.parentNode = this;
			newChild.previousSibling = oldLastChild;
			newChild.nextSibling     = null;
			
			return newChild;
		}
		
		bcTreeNode.prototype.insertBefore = insertBefore;
		function insertBefore(newChild, refChild)
		{
			if (!refChild)
			{
				this.appendChild(newChild);
				return newChild;
			}

			var parentNode = newChild.parentNode;
			if (parentNode)
			{
				parentNode.removeChild(newChild);
			}
				
			var ok            = false;
			var tmpChildNodes = new bcArray();
		
			while (this.childNodes.length > 0)
			{
				var tmpChild = this.childNodes.shift();
				if (tmpChild == refChild)
				{
					tmpChildNodes.push(newChild);
					newChild.nextSibling = refChild;
					newChild.previousSibling = refChild.previousSibling;
					refChild.previousSibling = newChild;
					newChild.parentNode = this;
					ok = true;
				}
				tmpChildNodes.push(tmpChild);
			}

			if (!ok)
				throw(new bcException('Not found Error', 'TreeNode.js', 'insertBefore'));
				
			this.childNodes = tmpChildNodes;
			this.firstChild = this.childNodes.item(0);
			this.lastChild = this.childNodes.item(this.childNodes.length-1);
		
			return newChild;
		}
		
		bcTreeNode.prototype.removeChild = removeChild;
		function removeChild(oldChild)
		{
			var ok = false;
			var tmpChildNodes = new bcArray();
		
			while (this.childNodes.length > 0)
			{
				var tmpChild = this.childNodes.shift();
				if (tmpChild == oldChild)
				{
					if (oldChild.nextSibling)
						oldChild.nextSibling.previousSibling = oldChild.previousSibling;
						
					if (oldChild.previousSibling)
						oldChild.previousSibling.nextSibling = oldChild.nextSibling;
				
					ok = true;
				}
				else
					tmpChildNodes.push(tmpChild);
			}
			
			if (!ok)
				throw(new bcException('Not found Error', 'TreeNode.js', 'removeChild'));

				
			this.childNodes     = tmpChildNodes;

			if (this.childNodes.length == 0)
			{
				this.firstChild = null;
				this.lastChild  = null;
			}
			else
			{
				this.firstChild = this.childNodes.item(0);
				this.lastChild = this.childNodes.item(this.childNodes.length-1);
			}
			
			oldChild.parentNode = null;
			oldChild.previousSibling = null;
			oldChild.nextSibling = null;
		
			return oldChild;
		}
		
		bcTreeNode.prototype.replaceChild = replaceChild;
		function replaceChild(newChild, oldChild)
		{
			var tmpChildNodes = new bcArray();
		
			if (!newChild)
			{
				this.removeChild(oldChild);
				return oldChild;
			}
			
			var parentNode = newChild.parentNode;
			if (parentNode)
			{
				parentNode.removeChild(newChild);
			}
				
			var ok = false;
			
			while (this.childNodes.length > 0)
			{
				var tmpChild = this.childNodes.shift();
				if (tmpChild == oldChild)
				{
					newChild.nextSibling = oldChild.nextSibling;
					newChild.previousSibling = oldChild.previousSibling;
					tmpChildNodes.push(newChild);
					ok = true;
				}
				else
					tmpChildNodes.push(tmpChild);
			}
			
			if (!ok)
				throw(new bcException('Not found Error', 'TreeNode.js', 'replaceChild'));

				
			this.childNodes = tmpChildNodes;
			this.firstChild = this.childNodes.item(0);
			this.lastChild  = this.childNodes.item(this.childNodes.length-1);
			
			oldChild.parentNode = null;
		
			return oldChild;
		}
		
		bcTreeNode.prototype.getRoot = getRoot;
		function getRoot()
		{
			var root = this;
			
			while (root.parentNode)
				root = root.parentNode;
				
			return root;
		}
	}
}
// eof: bcTreeNode.js
//</SCRIPT>
