<!--

/** default icon for a leaf (node with no child) */
var defaultLeafIcon = 'images/tree/leaf.gif';

/** default icon for a opened node with at least one child */
var defaultOpenedNodeIcon = 'images/tree/opened.gif';

/** default icon for a closed node with at least one child */
var defaultClosedNodeIcon = 'images/tree/closed.gif';

/**
 * Konstruktor.
 */
function Tree()
{
	this.m_Children = new Array();
	this.m_Link = ''; // '?uid='
	this.m_AppendUid = false;

	this.setAppendUid = Tree_setAppendUid;
	this.getAppendUid = Tree_getAppendUid;
	this.appendChild = Tree_appendChild;
	this.aC = Tree_appendChild;
	this.create = Tree_create;
	this.getPanel = Tree_getPanel;
	this.setLink = Tree_setLink;
	this.getLink = Tree_getLink;
	this.getNodeLink = Tree_getNodeLink;
	this.openTo = Tree_openTo;
}

/**
 * 
 */
function Tree_setAppendUid()
{
	this.m_AppendUid = true;
}

/**
 * 
 */
function Tree_getAppendUid()
{
	return(this.m_AppendUid);
}

/**
 * Appends a tree node to the tree.
 * @param node the tree node
 */
function Tree_appendChild(node)
{
	node.setFirstRow();
	this.m_Children[this.m_Children.length] = node;
	node.setTree( this );
}

/**
 * 
 * @param string Link
 */
function Tree_setLink(link)
{
	this.m_Link = link;
}

/**
 * 
 * @param string
 */
function Tree_getLink()
{
	return(this.m_Link);
}

/**
 * 
 * @param int uid
 */
function Tree_getNodeLink(uid)
{
	if(this.m_AppendUid)
	{
		return(this.m_Link + uid);
	}
	else
	{
		return(this.m_Link);
	}
}

/**
 * Creates and shows the tree.
 */
function Tree_create()
{
	document.body.appendChild(this.getPanel());
}

/**
 * Creates and returns the tree.
 */
function Tree_getPanel()
{
	var panel = document.createElement('DIV');
	panel.className = 'Tree';

	for(var i=0; i < this.m_Children.length; i++)
	{
		panel.appendChild(this.m_Children[i].create());
	}
	return(panel);
}

/**
 * Öffnen des Baumes bis zur angegebenen UID.
 */
function Tree_openTo(uid)
{
	Tree_openToExecute(this.m_Children, uid, false);
}

/**
 * Öffnet den Baum bis zur angegebenen UID.
 * static
 */
function Tree_openToExecute(children, uid, found)
{
	var i = 0;
	var countChilds = children.length
	while( (i < countChilds) && ( ! found) )
	{
		if(children[i].m_Uid == uid)
		{
			found = true;
			children[i].m_CurrentLine = true;
			if(children[i].m_Children.length > 0)
			{
				// Nur öffnen falls der Eintrag Subeinträge hat.
				children[i].m_IsOpened = true;
			}
		}
		else
		{
			if(children[i].m_Children.length > 0)
			{
				found = Tree_openToExecute(children[i].m_Children, uid, found);
				if(found)
				{
					children[i].m_ActiveLine = true;
					children[i].m_IsOpened = true;
				}
			}
			i++;
		}
	} // while
	return(found);
}

// --------------------------------------------------------------------------------------------------------------------------------------------

/**
 * Creates a new tree node for the tree.
 */
function TreeNode(uid, popupMenu, Text)
{
	this.m_Panel = null;
	this.m_Icon = null;
	this.m_Link = '';
	this.m_FirstRow = false;
	this.m_ActiveLine = false;
	this.m_CurrentLine = false;
	this.m_IconVisible = true;
	
	this.m_Uid = uid;
	this.m_Target = '';
	this.m_Text = Text;
	this.m_LeafIcon = defaultLeafIcon;
	this.m_OpenedIcon = defaultOpenedNodeIcon;
	this.m_ClosedIcon = defaultClosedNodeIcon;
	this.m_Children = new Array();
	this.m_IsLeaf = true;
	this.m_IsOpened = false;
	this.m_Tree = null;
	this.m_PopupMenu = popupMenu;
	
	this.setFirstRow = TreeNode_setFirstRow;
	this.setUid = TreeNode_setUid;
	this.setTarget = TreeNode_setTarget;
	this.setText = TreeNode_setText;
	this.setIcon = TreeNode_setIcon;
	this.hideIcon = TreeNode_hideIcon;
	this.setLeafIcon = TreeNode_setLeafIcon;
	this.setLink = TreeNode_setLink;
	this.setOpenedIcon = TreeNode_setOpenedIcon;
	this.setClosedIcon = TreeNode_setClosedIcon;
	this.setOpened = TreeNode_setOpened;
	this.isOpened = TreeNode_isOpened;
	this.getPanel = TreeNode_getPanel;
	this.setTree = TreeNode_setTree;
	this.getTree = TreeNode_getTree;
	this.getLink = TreeNode_getLink;
	this.appendChild = TreeNode_appendChild;
	this.aC = TreeNode_appendChild;
	this.create = TreeNode_create;
	this.createIcon = TreeNode_createIcon;
	this.createIconLink = TreeNode_createIconLink;
	this.createLink = TreeNode_createLink;
	this.openOrClose = TreeNode_openOrClose;
}

/**
 * Markiert den Eintrag als Eintrag in der ersten Spalte (wegen anderer CSS-Eigenschaften)
 * @param int the uid
 */
function TreeNode_setFirstRow()
{
	this.m_FirstRow = true;
}

/**
 * Setzt die uid für den Eintrag.
 * @param int UID des Eintrages.
 */
function TreeNode_setUid(uid)
{
	this.m_Uid = uid;
}

/**
 * Sets the target frame for the tree node.
 * @param target the target frame
 */
function TreeNode_setTarget(target)
{
	this.m_Target = target;
}

/**
 * Sets the tree node text.
 * @param text the text
 */
function TreeNode_setText(text)
{
	this.m_Text = text;
}

/**
 * Sets the leaf, opened and closed icon.
 * @param icon the icon
 */
function TreeNode_setIcon(icon)
{
	this.m_LeafIcon = icon;
	this.m_OpenedIcon = icon;
	this.m_ClosedIcon = icon;
}


/**
 * 
 * @param bool
 */
function TreeNode_hideIcon()
{
	this.m_IconVisible = false;
}


/**
 * Sets the leaf icon.
 * @param icon the leaf icon
 */
function TreeNode_setLeafIcon(icon)
{
	this.m_LeafIcon = icon;
}

/**
 * Sets the opened icon.
 * @param icon the icon
 */
function TreeNode_setOpenedIcon(icon)
{
	this.m_OpenedIcon = icon;
}

/**
 * Sets the closed icon.
 * @param icon the icon
 */
function TreeNode_setClosedIcon(icon)
{
	this.m_ClosedIcon = icon;
}

/**
 * Sets if a tree node is opened.
 * @param opened true, if the tree node is opened
 */
function TreeNode_setOpened(opened)
{
	this.m_IsOpened = opened;
}

/**
 * Returns if the tree node is opened.
 * @return true, if the tree node is opened.
 */
function TreeNode_isOpened()
{
	return this.m_IsOpened;
}

/**
 * Returns the html panel of the tree node.
 * @return the panel
 */
function TreeNode_getPanel()
{
	return this.m_Panel;
}

/**
 * Sets the parent tree.
 * @param tree the parent tree
 */
function TreeNode_setTree( tree )
{
	this.m_Tree = tree;
}

/**
 * Returns the parent tree.
 * @return the parent tree
 */
function TreeNode_getTree()
{
	return this.m_Tree;
}

/**
 * Returns the link.
 * @return the link
 */
function TreeNode_getLink(uid)
{
	if(this.getTree().getAppendUid())
	{
		return(this.m_Link + uid);
	}
	else
	{
		return(this.m_Link);
	}
}

/**
 * Returns the link.
 * @return the link
 */
function TreeNode_setLink(link)
{
	this.m_Link = link;
}

/**
 * Appends a tree node to the current node, current node is no more a leaf.
 * @param node the tree node
 */
function TreeNode_appendChild(node)
{
	this.m_Children[this.m_Children.length] = node;
	node.setTree(this.getTree());
	this.m_IsLeaf = false;
}

/**
 * Creates the tree node.
 * @return the tree node html element
 */
function TreeNode_create()
{
	this.m_Panel = document.createElement('DIV');
	if(this.m_FirstRow)
	{
		this.m_Panel.className = 'TreeNodeFirst';
	}
	else
	{
		this.m_Panel.className = 'TreeNode';
	}
	var table = document.createElement('TABLE');
	table.className = 'TreeNode';
	table.cellPadding = '0';
	table.cellSpacing = '0';

	var row = table.insertRow(table.rows.length);
	row.className = 'TreeNode';

	var cell = row.insertCell(row.cells.length);
	cell.className = 'TreeNode';

	if(this.m_IconVisible)
	{
		this.m_Icon = this.createIconLink();
		cell.appendChild(this.m_Icon);
	}

	cell = row.insertCell(row.cells.length);
	cell.className = 'TreeNode';
	link = this.createLink();
	cell.appendChild(link);

	this.m_Panel.appendChild(table);

	if(this.isOpened())
	{
		for(i=0; i < this.m_Children.length; i++)
		{
			this.m_Panel.appendChild(this.m_Children[i].create());
		}
	}

	return this.m_Panel;
}

/**
 * Creates an icon for the tree node.
 * @return the icon html element
 */
function TreeNode_createIcon()
{
	var icon = document.createElement('IMG');
	if(this.m_IsLeaf)
	{
		icon.src = this.m_LeafIcon;
	}
	else if (this.m_IsOpened)
	{
		icon.src = this.m_OpenedIcon;
	}
	else if ( ! this.m_IsOpened)
	{
		icon.src = this.m_ClosedIcon;
	}
	icon.className = 'TreeImage';
	return icon;
}

/**
 * Creates a link for the tree node.
 * @return the link html element
 */
function TreeNode_createIconLink()
{
	var link = document.createElement('A');
	link.href = '#';
	link.treeNode = this;

	if ( ! this.m_IsLeaf)
	{
		link.onclick = function() { this.treeNode.openOrClose(); };
	}
	link.onfocus = function() { this.blur(); };

	icon = this.createIcon();
	
	link.appendChild(icon);

	return link;
}

/**
 * Creates a link for the tree node.
 * @return the link html element
 */
function TreeNode_createLink()
{
	var link = document.createElement('A');
	if(this.m_Link.length > 0)
	{
		link.href = this.getLink(this.m_Uid);
	}
	else
	{
		link.href = this.getTree().getNodeLink(this.m_Uid);
	}
	link.target = this.m_Target;
	link.treeNode = this;

	if(this.m_IsLeaf)
	{
		link.className = 'TreeLeaf';
	}
	else if ( ! this.m_IsLeaf && this.m_IsOpened)
	{
		link.className = 'TreeOpenedNode';
	}
	else if ( ! this.m_IsLeaf && !this.m_IsOpened)
	{
		link.className = 'TreeClosedNode';
	}

	if(this.m_ActiveLine)
	{
		link.className += 'Active';
	}
	else
	{
		if(this.m_CurrentLine)
		{
			link.className += 'Current';
		}
	}
	
	if ( ! this.m_IsLeaf)
	{
		link.onclick = function() { this.treeNode.openOrClose(); };
	}
	link.onfocus = function() { this.blur(); };

	link.appendChild(document.createTextNode(this.m_Text));

	return link;
}

/**
 * Open a menu.
 */
function TreeNode_openOrClose()
{
	if(this.m_IsOpened)
	{
		var i;
		for(i=0; i < this.m_Children.length; i++)
		{
			this.m_Panel.removeChild(this.m_Children[i].getPanel());
		}
		this.setOpened(false);
		this.m_Icon.firstChild.src = this.m_ClosedIcon;
	}
	else
	{
		var i;
		for(i=0; i < this.m_Children.length; i++)
		{
			this.m_Panel.appendChild(this.m_Children[i].create());
		}
		this.setOpened(true);
		this.m_Icon.firstChild.src = this.m_OpenedIcon;
	}
}

//-->