//each node in the tree is an Array with 4+n positions 
//    node['open'] is 0/1 when the node is closed/open 
//    node['access'] is for user access 0 = normal; 1+2=under construction 3 admin
//    node['path'] is the Link
//    node['php'] is wheter PHP (1) or not (0)
//    node['extension'] is for PHP the ?GET-text
//    node['id'] is the ID
//    node['parent'] is the parent node
//    node['topic'] is the text in contenttopic
//    node['pathname'] is the text in contenttopic
//    node['children'][0-anzchildren] are the anzchildren children nodes


// ***************
// Building the data in the tree


// Auxiliary function to build the node

function folderNode(id, topic, path, access, php)
{
		
		//alert("foldersNode")
	var arrayAux =  new Array
	arrayAux['open'] = 0
	arrayAux['access'] = access
	arrayAux['path'] = path
	arrayAux['php'] = php
	arrayAux['id'] = id
	arrayAux['parent'] = 0
	arrayAux['topic'] = topic
	arrayAux['pathname'] = topic
	arrayAux['children'] = new Array 
	
	// Bildergalerie
	if(php==2)
	{
		arrayAux['extension']="Type=0&Seite=1&GalerieID=" + id	
		if(id==301)
		{
			var date = new Date()
			
			arrayAux['extension']+="&Datum=" + date.getMonth() + "-" + date.getFullYear() 
		}
	}
	return arrayAux
}

//this way the generate tree function becomes simpler and less error prone
function appendChild(parent, child)
{
	// Das Child beim Parent-Node hinzufügen
	parent['children'][parent['children'].length]=child
	
	// Den Pfad des Child-Nodes anpassen
	if(child['path'])
	{
		child['path']=parent['path']+"/"+child['path']
	}else{
		child['path']=parent['path']	
	}
	if(parent['id']!=0)
	{
		child['pathname']=parent['pathname']+ " &gt; " +child['topic']
	}
			
	// Dem Child den richtigen Parent zuweisen
	child['parent'] = parent
	return child
	
}

function appendExistingChild(parent, child)
{
	//alert("appendExistingChild")
	var clone=folderNode(child['id'], child['path'], child['topic'], child['access'], child['php'])
	appendChild(parent, clone)
	
	return clone
}

// **********************
// display functions

//redraws the frame
function redrawTree(sitemap, aktNode)
{
//alert("redrawTree")
	var doc //Ausgabedokument
	if (sitemap)
	{
		doc=parent.contentmain.document
	}
	else
	{
		doc=parent.menumain.document
	}
	  
	doc.clear()
	
	//Stylesheetaufruf
	if (sitemap==0)
	{
		doc.write("<head><link href='/css/menumain.css' rel='stylesheet' type='text/css'></head>")
	}
	else
	{
		doc.write("<head><link href='/css/contentmain.css' rel='stylesheet' type='text/css'></head>")
	}
	
	doc.write("<body>")	
	
	// 0 ist Level, 1 ist lastNode
	redrawNode(foldersTree, doc, 0, 1, "", sitemap, aktNode)
	
	doc.write("</body>")
	
	
	//if(!sitemap)
//	{
//		
//		var nav=navigator.appName.indexOf("Netscape");
//		if(nav!= -1)
//		{
//			doc.getElementById('akt').scrollIntoView(true)
//		}
//	}

	doc.close()
	
}

//zeichnet einen Node und alle Unternodes
// foldersNode: der zu zeichnende Node
// doc: das Dokument
// level: die Ebene
// lastNode: ob der letzte Node
// leftside: die bilder, die links angezeigt werden
// sitemap: ob Sitemap
// aktNode: der aktuell ausgewählte Node
function redrawNode(foldersNode, doc, level, lastNode, leftSide, sitemap, aktNode)
{
	
	
	// Seiten für Admin verbergen
	if(foldersNode['access'] != 3 || admin)
	{
		if (foldersNode['topic']!="Startseite") // Startseite nicht einrücken
		{
			
			
			doc.write("<table border=0 cellspacing=0 cellpadding=0>")
			doc.write("<tr><td valign = middle nowrap>")
		
			doc.write(leftSide)
				
			if (lastNode) 
			{
				if(sitemap)
				{
					doc.write("<img src='/constant/lastnodesitemap.gif' width=16 height=22>")
				}
				else
				{
					doc.write("<img src='/constant/lastnode.gif' width=16 height=22>")
				}
				
				leftSide = leftSide + "<img src='/constant/blank.gif' width=16 height=22>" 
			}
			else
			{
				if(sitemap)
				{
					doc.write("<img src='/constant/nodesitemap.gif' width=16 height=22>")
					leftSide = leftSide + "<img src='/constant/vertlinesitemap.gif' width=16 height=22>"
				}
				else
				{
					doc.write("<img src='/constant/node.gif' width=16 height=22>")
					leftSide = leftSide + "<img src='/constant/vertline.gif' width=16 height=22>"
				}
			}
		}
	
	
		// Den Link anzeigen
		displayIconAndLabel(foldersNode, doc, sitemap, aktNode)
		
		doc.write("</table>")
	
		var drawSubnodes=0
		if(foldersNode['children'].length > 0)
		{
			if(sitemap)
			{
				drawSubnodes=1;
			}
			else
			{
				drawSubnodes=foldersNode['open']
			}
		}
		
		if (drawSubnodes) 
		{
			level=level+1
			
			// zeichenbare Subnodes ermitteln
			var subnodesToDraw= new Array
			for (var subnode=0; subnode < foldersNode['children'].length; subnode++)
			{
				if(foldersNode['children'][subnode]['access'] !=3	|| admin)
				{
					//alert("AddNode: " + subnodesToDraw.length + foldersNode['children'][subnode]['topic'])
					subnodesToDraw[subnodesToDraw.length]=foldersNode['children'][subnode]	
				}
			}
			
			//alert("StartDraw " + subnodesToDraw.length);
			for (var subnode=0; subnode < subnodesToDraw.length; subnode++)
			{
				//alert("DrawNode:" + subnodesToDraw[subnode]['topic']);
				redrawNode(subnodesToDraw[subnode], doc, level, (subnode==subnodesToDraw.length-1) , leftSide, sitemap, aktNode)			
			}
		}
	}
}

//builds the html code to display a folder and its label
function displayIconAndLabel(foldersNode, doc, sitemap, aktNode)
{
	//alert("displayIconAndLabel")
	//alert("displayIaL:FN " + foldersNode[2])
	//alert("displayIaL:AN " + aktNode[2])
	
	
	doc.write("<td valign=middle align=left nowrap width='3'>")
	doc.write("<td valign=middle align=left nowrap>")
//	
//	

//	// Ermitteln der class
	var cssClass=""
		
	if(aktNode == foldersNode)
	{
		cssClass="aktNode id=\"akt\""
	}
	else
	{
		if (sitemap)
		{
			cssClass="sitemap"
		}
		else
		{
			cssClass="menumain"
		}
		
		if(admin)
		{
			
			// Baustellen auf empty setzen
			if (foldersNode['access'] == 1 || foldersNode['access'] == 2)
			{
				cssClass+="empty"
			}
		}
	}
	
	doc.write("<a href='javascript:top.openBranch(\"" + foldersNode['id'] + "\")'>")
	doc.write("<span class='" + cssClass + "'>"+foldersNode['topic']+"</span></a>")
}

//when a parent is closed all children also are
function closeFolders(foldersNode)
{
	//msg = "NeucloseFolder " + foldersNode['topic'] + " \n " + foldersNode['children'].length + " \n "
	if(foldersNode['children'].length > 0)
	{
		for (var i=0; i< foldersNode['children'].length; i++)
		{
			//msg+= i + ": " + foldersNode['children'][i]['topic'] + "\n "
			closeFolders(foldersNode['children'][i])
		}
	}
	foldersNode['open'] = 0
	//alert(msg)
}

//called when the user clicks on a folder
var newAktNode

function openBranch(branchID)
{
	
	if (String(branchID).slice(0, 1)!="s")
	{
   		openBranchCore(branchID, 1, "");
	}else{
		alert("Ungültiger Link: Link muss eine Zahl sein");
	}
}

function openBranchExtension(branchID, extension)
{
	openBranchCore(branchID, 1, extension);
}

function openBranchCore(branchID, addHyperlink, extension)
{
	//alert("OpenBranch:" + branchName)
	newAktNode=0
	
	searchClickedFolder(foldersTree, branchID)
	//alert("gefunden: " + newAktNode['topic'])
	
	if(newAktNode)
	{
		if(extension)
		{
			newAktNode['extension']=extension
		}
		closeFolders(foldersTree)
		openBranchBackwards(newAktNode)
		redrawTree(0, newAktNode)
		if (addHyperlink)
			addLink(newAktNode);
	}
	else
	{
		openBranch(0) // Startseite öffnen ID=0
		var invalidLinkNode=folderNode(0, "/constant/invalidlink", "Link nicht gefunden", 0, 1);
		invalidLinkNode['extension']="link=" + branchID + "&site=" + contentmain.location;
		addLink(invalidLinkNode);
	}
}
//recursive over the tree structure
//called by openbranch
function searchClickedFolder(foldersNode, folderID)
{
	//alert("searchClickedFolder")
	// Prüfen ob aktueller Node der gesuchte ist
	if (foldersNode['id'] == folderID)
	{
		//alert("gefunden: " + foldersNode['topic'])
		newAktNode=foldersNode
	}
	else
	{
		// nächstes child nehmen
		for (var i=0; i< foldersNode['children'].length; i++)
		{
			searchClickedFolder(foldersNode['children'][i], folderID)
		}
	}
}

function openBranchBackwards(foldersNode)
{
//alert("openBranchBackwards Node:" + foldersNode['topic'] + " Parent:" + foldersNode['parent']['topic'])
foldersNode['open']=1

	if (foldersNode['parent'])
	{
		openBranchBackwards(foldersNode['parent'])
	}

}

//called after this html file is loaded
function initializeTree()
{
	//alert("initializeTree")
	if(initializeTree.arguments.length==1)
	{
		admin=initializeTree.arguments[0]
	}
	
	
	generateTree()
	openBranch(0) // Startseite öffnen
	
	//foldersTree['open']=1
//	redrawTree(0, foldersTree)
//	
//	addLink("startseite",0,"Startseite")
	
}

function drawSitemap()
{
	//alert("drawSitemap")
	redrawTree(1, foldersTree)
}

var admin=0
var foldersTree = 0
var timeOutId = 0




