var boolMenuLoaded = false;
var currentActiveId = "";
var boolToggle = true;

window.onload = function()
{
	var arrElems = document.getElementById('navigation').getElementsByTagName('div');
	for (var i = 0; i < arrElems.length; i++)
	{
		var currAttributeNode = arrElems[i].getAttribute("name");
		if (currAttributeNode == "nav")
		{
			arrElems[i].onclick = function(){toggleTree(this.parentNode)};
		}
	}
	boolMenuLoaded = true;
}

window.onunload = function()
{
	return;
}

function toggleTree(node, toggle)
{
	var nodeChild	= node.getElementsByTagName('div')[1];

	if (!nodeChild)
	{
		window.setTimeout("toggleTree('" + node + "');", 100);
	}
	else
	{
		if (isVisible(nodeChild) && boolToggle)
		{
			closeAllSubElems(node);			
		}
		else
		{
			openAllSubElems(node);
		}
	}
}

function openAllSubElems(node)
{
	var currentId 	= node.id;
	if (currentId)
	{
		var nodeEntry 	= node.getElementsByTagName('div')[0];
		var nodeChild 	= node.getElementsByTagName('div')[1];
		var elemToggle 	= nodeEntry.getElementsByTagName('img')[0];
		var elemNav			= nodeEntry.getElementsByTagName('img')[1];

		if (!nodeChild)
		{
			window.setTimeout("openAllSubElems('" + node + "');", 100);
		}
		else
		{
			elemToggle.src = "minus.gif";
			nodeChild.style.display = "block";
			entryParent = node.parentNode.parentNode;

			if (elemNav.className == "folderIcon")
			{
				elemNav.src = "folder_open.gif";
			}

			if (node.parentNode.id != "navigation")
			{
				if (typeof(entryParent.subfolders) == "undefined")
				{
					entryParent.subfolders = new Array();
				}
				entryParent.subfolders[entryParent.subfolders.length] = currentId;
			}
		}
	}
}

function closeAllSubElems(node)
{
	var currentId 			= node.id;
	var nodeEntry 			= node.getElementsByTagName('div')[0];
	var nodeChild 			= node.getElementsByTagName('div')[1];
	var elemToggle 			= nodeEntry.getElementsByTagName('img')[0];
	var elemNav					= nodeEntry.getElementsByTagName('img')[1];
	var tempArray				= new Array();
	var currSubFolders 	= new Array();
	
	if (!nodeChild)
	{
		window.setTimeout("closeAllSubElems('" + elem + "');", 100);
	}
	else
	{
		elemToggle.src = "plus.gif";
		nodeChild.style.display = "none";
		entryParent 			= node.parentNode.parentNode;
		currSubFolders 	= node.subfolders; 
		
		if (elemNav.className == "folderIcon")
		{
			elemNav.src = "folder_close.gif";
		}		
		
		if (node.parentNode.id != "navigation")
		{
			for (var i=0; i < entryParent.subfolders.length; i++)
			{
				if (entryParent.subfolders[i] != currentId)
				{
					tempArray[tempArray.length] = entryParent.subfolders[i];
				}
			}
			entryParent.subfolders = tempArray;
		}
			
		if (currSubFolders)
		{
			var laenge = currSubFolders.length;
			for (var i = 0; i < laenge; i++)
			{
				if (currSubFolders[i])
				{
					closeAllSubElems(document.getElementById(currSubFolders[i]));
				}
			}
		}
	}
}

function openAllParents(node)
{
	elem = node.parentNode;
	openAllSubElems(node);	
	if (elem.id != 'navigation' && elem.id != '')
	{
		openAllParents(elem.parentNode);
	}
}

function isVisible(elem)
{
	if (elem == null)
	{
		return false;
	}
	else if (elem.style.display == "none" || elem.style.display == "")
	{
		return false;
	}
	else
	{
		return true;
	}
}

function loadElem(folder, name)
{
	boolToggle = false;
	if (name == "")
	{
		if (typeof(top.model) == "undefined")
		{
			window.setTimeout("loadElem('" + folder + "', '" + name + "');", 100);
		}
		else
		{
			top.model.location.href = "../" + folder + "/" + folder + ".html";
		}
	}
	else
	{
		instanceWindow = window.open("../" + folder + "/" + name + ".html", "InstanceWindow", "scrollbars=yes, resizable=yes, width=400, height=600, dependent=yes");
		instanceWindow.focus();
	}
	boolToggle = true;
}

function showElemInTree(id)
{
	var elemNode = document.getElementById(id);
	if (elemNode)
	{
		openAllParents(elemNode);
		if (currentActiveId != "")
		{
			document.getElementById(currentActiveId + "Entry").getElementsByTagName('a')[0].className = "inActiveLink";
		}
		document.getElementById(id + "Entry").getElementsByTagName('a')[0].className = "activeLink";
		currentActiveId = id;
		
		// Link in sichtbaren Bereich verschieben
		abstand_y = elemNode.offsetTop;
		parentTag	=	elemNode.offsetParent;
		while (parentTag)
		{
			abstand_y = abstand_y + parentTag.offsetTop;
			parentTag = parentTag.offsetParent;
		}		
		self.scrollTo(0, abstand_y - 20);
		return true;
	}
	else
	{
		return false;
	}
}