/******************************************************************
*
* IABTREE - dhtml tree implementation
* Copyright (C) 1999-2000, INCORIDA AB - http://www.incordia.se
*
******************************************************************/

// mutual exclusive expansion
var iabtree_singleexpansion = 2; // 0=never, 1=root-children, 2=always

var iabtree_imagepath = "";

var iabtree_selected = null;

var separated_firstlevel = false;

function iabtree_docobj(objid)
{
	try
	{
		return document.all[objid];
	}
	catch(e)
	{
		return null;
	}
}

function iabtree_docwrite(objid, html)
{
	try
	{
		iabtree_docobj(objid).innerHTML = html;
	}
	catch(e)
	{
		//void...
	}
}

function iabtree(nodeid, parent, content, title)
{
	// members
	this.level		= 0;
	this.nodeid		= nodeid;
	this.parent		= parent;
	this.content	= content;
	this.children	= new Array();
	this.expanded	= false;
	this.title		= "<nobr>" + title + "</nobr>";

	eval("nodeObj" + this.nodeid + " = this");

	// methods
	this.addchild = iabtree_addchild;
	this.islast = iabtree_islast;
	this.isroot = iabtree_isroot;
	this.dump = iabtree_dump;
	this.expand = iabtree_expand;
	this.collapse = iabtree_collapse;
	this.toggle = iabtree_toggle
	this.image = iabtree_image;
	this.refresh = iabtree_refresh;
	this.ensurevisible = iabtree_ensurevisible;
	this.select = iabtree_select;

	// come to papa!
	if(parent)
	{
		this.level = parent.level + 1;
		parent.addchild(this);
	}
}
function iabtree_select(refresh)
{
	if(iabtree_selected == this) return;

	if(iabtree_selected)
	{
		iabtree_selected.selected = false;
		iabtree_selected.refresh();
	}

	this.selected = true;
	iabtree_selected = this;

	if(refresh) this.refresh();
}
function iabtree_image(imageid)
{
	return "<img align=\"absmiddle\" border=\"0\" hspace=\"0\" vspace=\"0\" src=\"" + iabtree_imagepath + "\\" + imageid + ".gif\" width=\"17\" height=\"17\">";
}
function iabtree_islast()
{
	return (!this.parent || this.parent.children[this.parent.children.length - 1] == this);
}
function iabtree_addchild(node)
{
	this.children[this.children.length] = node;
	node.parent = this;
}
function iabtree_dump()
{
	var l = (this.islast() ? "last" : "");
	var s = "", t = this.title;
	
	if(this.selected) t = "<b>" + t + "</b>";

	if(this.isroot())
	{
		s += this.image("root") + "&nbsp;" + this.content;
		this.expanded = true; // root always expanded
	}
	else
	{
		var ss = "";

		for(var p = this.parent; !p.isroot(); p = p.parent)
		{
			if(p.islast())
				ss = this.image("null") + ss;
			else if( p.parent.isroot() && separated_firstlevel)
				ss = this.image("null") + ss;
			else
				ss = this.image("blankdots") + ss;
		}

		s += ss;

		if(this.children.length)
		{
			s += "<a class=\"nav\" href=\"#\" onClick=\"nodeObj" + this.nodeid + ".toggle(); return false;\">";
			if( this.parent.isroot() && separated_firstlevel)
				s += this.image(l + (this.expanded ? "freeminus" : "freeplus"));
			else
				s += this.image(l + (this.expanded ? "minus" : "plus"));
			s += "</a>&nbsp;" + this.content + t + "</a>";
		}
		else
			s += this.image(l + "dots") + "&nbsp;" + this.content + t + "</a>";
	}

	if(this.children.length && this.expanded)
	{
		s += "<br>";
		for(var i = 0; i < this.children.length; i++)
			s += "<div class=iabnode id=\"node" + this.children[i].nodeid + "\">" + this.children[i].dump() + "</div>";
	}

	return s;
}
function iabtree_isroot()
{
	return (this.parent == null);
}
function iabtree_refresh()
{
	iabtree_docwrite("node" + this.nodeid, this.dump());
}
function iabtree_ensurevisible(select)
{
	if(this.isroot())
		return;
	else
		this.parent.ensurevisible();

	if(select) this.select(false);
	this.expand();
}
function iabtree_expand()
{
	var p = this.parent;

	if((iabtree_singleexpansion == 2) || (p.isroot() && iabtree_singleexpansion == 1))
	{
		for(var i = 0; i < p.children.length; i++)
		{
			p.children[i].expanded = (p.children[i] == this);
			p.children[i].refresh();
		}
	}
	else
	{
		this.expanded = true;
		this.refresh();
	}	
}
function iabtree_toggle()
{
	if(this.expanded)
		this.collapse();
	else
		this.expand();
}
function iabtree_collapse()
{
	this.expanded = false;
	this.refresh();
}