/* Global Variables */
var listRoot;			// reference to the Linked List Root
var counter=0;			// id counter

/* Constructor of listItem */
function listItem()
{
	// public constants

	// public variables
							// Linked List Controlls
	this.parent = null;		// Parent in Level structure
	this.child = null;		// Next Child in Level structure
	this.next = null;		// Next Item in linked-list

	this.id	= null;			// Single List Element properties
	this.caption = null;
	this.href = null;
	this.icon = null;
	this.isOpen = null;
	this.isVisible = null;

	// public methods

	this.createRoot = createRoot;	// Initialize a Linked List and create the ROOT-NODE
	this.insertItem = insertItem;	// Insert a Item into the Linked List
	this.renderAll	= renderAll;	// Render the whole Linked List to HTML
	this.propagateProperties = propagateProperties;		// self-explaining
	this.clickOnItem = clickOnItem; // View logic after a click on an Item

}

function insertItem (tree, parent, caption, href)
{
	itm = new listItem();			// Generate new Item
	itm.parent = parent;		// Set Parent of new Item
	itm.caption = caption;		// Set Caption of new Item
	itm.href = href;
	itm.isOpen = 0;				// All Folders are closed at the begining
	itm.isVisible = 1;			// All Folders are visible at the begining
	
	var cursor = parent;

	if (parent == tree)				// Item belongs to the uppermost level
	{
		while (cursor.next != null)	// Let the new Item descend to the end of the Linked List
			cursor = cursor.next;
		
		cursor.next = itm;			// Link the last Item in the Linked List to the present one.
	} 
	else							// Item has a Parent in a upper level
	{

		while (cursor.parent == parent && cursor.next != null)
			cursor = cursor.next;	// Seek the last Item belonging to that level

		var dummy = cursor.next;	// Insert the new Item at the end of the Level
		cursor.next = itm;			// it belongs to
		itm.next = dummy;
		parent.child = itm;			// Set Parent's child to the new Item
	}
	
	itm.id = ++counter;			// Increase ID and write back to new Item
	
	return (itm);
}

function createRoot ()
{
	itm = new listItem();
	itm.isOpen = 1;
	itm.isVisible = 1;
	itm.id = 0;
	return (itm);
}

function renderAll (tree)
{
	var item, output;
	item = tree;
	output = "";	

	while (item.next != null)
	{
		item = item.next;
		output += "<div id=\""+item.id+"\"";
		if (item.isOpen == 0 && item.parent != tree)
			output += " style='display: none'";
		output += ">";
		output += "<table border=0 cellpadding=0 cellspacing=1><tr>";

		var dummy = item;
		var c=0;
		while (dummy.parent != tree)
		{
			c++;
			dummy = dummy.parent;
		}

		if (item.child != null)
			output += "<td width="+c*20+">&nbsp;</td><td valign=\"middle\" width=25><a href='javascript:clickOnItem(\""+item.id+"\",\"none\")'><img id=\"icon_"+item.id+"\" src=\"ico/ftv2pnode.gif\" border=0></a></td><td>&nbsp;</td>";
		else
			output += "<td width="+c*20+">&nbsp;</td><td width=25>&nbsp;</td><td>&nbsp;</td>";

//		output += "<a href='' target='_blank' OnClick='javascript:clickOnItem(\""+item.id+"\")'>";


		if (item.child != null)
			output += "<td valign=\"middle\"><a href='javascript:clickOnItem(\""+item.id+"\",\""+item.href+"\")'>"+item.caption+"</a></td>";
		else
			output += "<td valign=\"middle\"><a href=\""+item.href+"\" target=\"basefrm\">"+item.caption+"</a></td>";		
		output += "</tr></table>";
		output += "</div>";
	}


	document.write(output);
}

function propagateProperties(item)
{
	var cursor = item.next;
	var isVisible = item.isVisible;
	while (cursor.next != null)
	{
		while (cursor.parent == item)
		{
			cursor.isVisible = isVisible;
	
			obj = document.getElementById(cursor.id);
			if (isVisible)
				obj.style.display = 'block';
			else		
				obj.style.display = 'none';
		
			if (cursor.child != null && cursor.isOpen)
				propagateProperties (cursor);
			
			cursor = cursor.next;
		}
		
		if (cursor.next != null)
			cursor = cursor.next;
	}	

}

function clickOnItem(id, href)
{	
	var cursor = listRoot;

	while (cursor.id != id)			// Descend the List until we've got the Item with
		cursor = cursor.next;		// the coresponding ID

	if (cursor.child != null)
		obj = document.getElementById("icon_"+cursor.id);
	
	if (cursor.isOpen == 1)
	{
		cursor.isOpen = 0;	
		cursor.isVisible = 0;
		obj.src = "ico/ftv2pnode.gif"
	}
	else
	{	
		cursor.isOpen = 1;
		cursor.isVisible = 1;
		obj.src = "ico/ftv2mnode.gif"
	}
	if (cursor.next != null)
		propagateProperties(cursor);
		
	if (href != "none")
		parent.frames[1].location=href;
}

function debug(tree)
{
	var item, output;
	item = tree;
	output = "--------------- debug -----------------<br>";
	while (item.next != null)
	{
		item = item.next;
		output += "id: "+ item.id+" / caption: "+ item.caption+" / parent: "+item.parent.caption+" / child: "+item.child+"<br>";
	}
	output += "---------------------------------------<br>";
	document.write("<div id=\"debug\">"+output+"</div>");
}


