// history
//	07.06.02	changed method treenode_draw to make mac compatible (m.k)

// Tree - js-scripts to generarte dynamic tree-navigation


// global vars
var layerhandler = null;
var layertop = 0;
var layerleft = 0;
var autoids = 1;
var navcounter= 0;
var tpSrc='../../images/icons/tp.gif'; // used for relative positioning (mac)

// class treenav
// class that defines appearance, layer and other settings for navigation

// Parameter:
function treenav(name)
{
	// properties
	// ----------
	// are taken from the global vars defined in the definition file.can be overwritten later	
	this.name 				= name;
	
	
	this.topHeader			= topHeader;		// for description of the properties below see nav_def.js
	this.header 			= header;			  
	this.tr_defstart		= tr_defstart;
	
	this.tr_folderc_nolink	= tr_folderc_nolink;
	this.tr_folderc_link	= tr_folderc_link;
	this.tr_foldero_nolink	= tr_foldero_nolink;
	this.tr_foldero_link	= tr_foldero_link;
	this.tr_subontent		= tr_subontent;
	this.tr_subontent_last	= tr_subontent_last;
	this.tr_doc_link		= tr_doc_link;
	this.tr_doc_nolink		= tr_doc_nolink;
	
	this.tr_defend			= tr_defend;
	this.footer				= footer;
	this.topFooter			= topFooter;
	
	this.status_doc			= status_doc;
	this.status_node_open	= status_node_open;
	this.status_node_close	= status_node_close;
	
	this.std_targrt			= std_target;
	
	this.docpics			= docpics;
	
	this.unique				= unique;
	this.memorize			= memorize;
	
	this.layerwidth			= layerwidth;
	this.layerheight		= layerheight;
	
	this.allnodes			= Array();			// associative array with direct link to every object
	this.styleIsSet			= false;			// used for error-handling
	
	// this properties are set by myNavigation.show and used bei myNavigation.rePositionLayer
	this.ypos				= 0;				// position of the layer
	this.xpos				= 0;		
	this.imagename			= "";				// name of the image that serves as anker for relative positioning
	
	// methods
	this.show		= treenav_show;				// shows the whole navigation and set right position
	this.setPos		= treenav_setPos;			// same as show except that layer visibilit will not be affected
	
	this.directOpen	= treenav_directOpen;		// opens an object of the navigation
	this.directClose= treenav_directClose;		// close an object of the navigation
	this.makeLayer	= treenav_makeLayer;		// prints out the div-tag and navigation
	this.makeTreeStyle = treenav_makeTreeStyle;	// prints out the <style> tag for layer
	this.showLayer	= treenav_showLayer;		// recalcultes position of layer and sets it
	this.hideLayer	= treenav_hideLayer;		// hides Layer
	this.calcPos	= treenav_calcPos;			// recalculates Position (helping method)
	this.openAll	= treenav_openAll;			// opens the whole tree
	this.openAllRec	= treenav_openAllRec;		// recursive helper for openAll
	
	// constructor code
	this.layername			= "treenavlayer"+navcounter++;	// name of the layer used for this navigation
	this.topObject 			= new treenode(this,"topobject",-1);	// start-object of the navigation
	this.topObject.opened	= true;
	
}

// public Methods


// openAll	- opens the whole structure
function treenav_openAll()
{
	this.openAllRec(this.topObject);
	this.topObject.draw();
}

function treenav_openAllRec(myObject)
{
	if(!myObject) return;
	myObject.opened=true;
	this.openAllRec(myObject.topchild);
	this.openAllRec(myObject.next);
}



// show - renders the whole navigation an set layer to the right position
// xpos		: x-position of the layer absolut or relative to the object with id=id
// ypos		: y-position
// imagename: [optional] imagename, which coordinates are the origion of the relativ position
//			  imagename can be any picture in the main document (not on a layer!)

function treenav_show(xpos,ypos,imagename)
{
	if(typeof(xpos)=='undefined' || typeof(ypos)=='undefined') 
	{
		alert("ERROR: myNavigation.show - please specify at least x and y -coordiantes");
		return false;
	}
	
	this.setPos(xpos,ypos,imagename);
	
	this.showLayer();
	
	
}

function treenav_setPos(xpos,ypos,imagename)
{
	this.xpos = xpos;
	this.ypos = ypos;
	this.imagename = imagename;

	this.calcPos();
}

function treenav_calcPos()
{
	if(typeof(this.imagename)!='undefined')
	{
		if(myImg = document.images[this.imagename])
		{
			left = getImageXfromLeft(this.imagename)+this.xpos;
			toppos=getImageYfromTop(this.imagename)+this.ypos;
		}
		else
		{
			//alert("makeTreeStyle error: "+this.imagename+" is not a picture");
			return;
		}
		
	}
	else
	{
		left = this.xpos;toppos=this.ypos;
	}


	setXPos(this.layername,left);
	setYPos(this.layername,toppos);
}

function treenav_showLayer()
{
	this.calcPos();
	showLayer(this.layername);
}

function treenav_hideLayer()
{
	hideLayer(this.layername);
}


function treenav_directOpen(id)
{
	var node = this.allnodes[id];
	if(node)
	{
		if(!node.topchild)
		{
			node=node.parent;
		}
		if(this.unique)
		{
			if(!this.memorize) node.getTree().closeChildren();
			else node.closeBrothers();
		}
		node.openDadies();
		node.open();
	}
}

function treenav_directClose(id)
{
	node= this.allnodes[id];
	if(node) node.close();
}


function treenav_makeLayer()
{
	if(!this.styleIsSet) 
	{
		alert ("ERROR: there was no style-sheet set for the layer. Use myNavigation->makeTreeStyle() in your head-sction");
		return false;
	}
	
	document.write("<div id=\""+this.layername+"\">");
	document.write(this.topHeader);
	document.write(this.topObject.getTable());	
	document.write(this.topFooter);
	document.write("</div>");
	
}

// method to draw head-sction of navigation
function treenav_makeTreeStyle()
{
	var width="";
	var height="";
	var top = 0;
	var left = 0;
	
	if(this.layerwidth>0)
	{
		width = " width: "+this.layerwidth+"px;";
	}
	if(this.layerheight>0)
	{
		height = " height: "+this.layerheight+"px;";
	}
	
	
	document.write("<style media=\"screen\" type=\"text/css\"><!--");
	document.write("#"+this.layername+" { position: absolute; top: "+top+"px; left: "+left+"px; "+width+" "+height+" visibility: hidden }");
	document.write("--></style>");
	
	this.styleIsSet=true;

}





// class treenode
function treenode(treenav,description1,id1,url1,statusline,target1)
{
	// properties
	this.topchild = null;
	this.parent = null;
	this.top = null;
	this.next = null;
	this.previous = null;
	this.id = -1;
	this.url = "";
	this.target = "";
	this.statuso = "";
	this.statusc = "";
	this.statusd = "";
	this.opened = false;
	this.docpic = "standard";
	this.treenav = treenav;
	
	
	var docpic = "";
	var description = "";

	// methods
	this.draw 		= treenode_draw;				// rewrites navigation layer with content (getTable)
	this.addChild 	= treenode_addChild;			// adds a ne node to an other
	this.parseIt	= treenode_parseIt;				// parses the defiend variables 
	this.getTable	= treenode_getTable;			// returns the whole layercontent (without div) of a node and its children if opened.does not rewrite layer
	this.open		= treenode_open;				// sets the opened flag and rewrites layer
	this.getTree	= treenode_getTree;				// gets top Object of navigation
	this.openDadies = treenode_openDadies;			// sets opened flag on all parent object of the node
	this.closeChildren = treenode_closeChildren;	// sets all opened flag of any supcontent to false 
	this.close		= treenode_close;				// closes all subcontent and corrent node and rewrites layer
	this.closeBrothers= treenode_closeBrothers		// sets all opened flags of nodes with the same level to false
	
	// constructor code
	this.description = description1;
	if(typeof (id1)!="undefined" && id1!="" && id1!=0 && id1!=null)	this.id = id1;
	else this.id= "auto"+autoids++;

	if(typeof (url1)!="undefined") this.url = url1;
	if(typeof (statusline)!="undefined"&&statusline!=null)
	{
		this.statuso = this.statusc = this.statusd = statusline;
	}
	else
	{
		// use standards
		this.statuso = this.treenav.status_node_open.replace(/\$desc/g,this.description);
		this.statusc = this.treenav.status_node_close.replace(/\$desc/g,this.description);
		this.statusd = this.treenav.status_doc.replace(/$desc/g,this.description);
	}
	
	if(typeof (target1)!="undefined" &&target1!="" && target1!=null) this.target = " TARGET=\""+target1+"\"";
	else if(std_target!="")
	{
		this.target=" TRAGET=\""+this.treenav.std_target+"\"";
	}
}



// public methods
function treenode_open()
{
	this.opened=true;
	this.getTree().draw();	
}

function treenode_close()
{
	this.closeChildren();
	this.opened = false;
	this.getTree().draw();
}

function treenode_getTree()
{
	return this.treenav.topObject;
}

function treenode_closeBrothers()
{
	var tmp = this.top;
	var merker = this.opened;
	
	
	while(tmp)
	{
		tmp.opened=false;
		tmp = tmp.next;
	}
	
	this.opened=merker;
	
}


function treenode_getTable()
{
	var content = this.treenav.header;
	var node = this.topchild;
	
	while(node)
	{
		content += this.treenav.tr_defstart;
		// get type
		if(node.topchild)
		{
			if(node.opened) type = "tr_foldero_"; else type ="tr_folderc_";
			if(node.url!="") type += "link"; else type += "nolink";
		}
		else
		{
			if(node.url!="") type = "tr_doc_link";
			else type = "tr_doc_nolink";
		}
		

		eval("myTDSet=this.treenav."+type+";");
		
		if(node.next) content += node.parseIt(myTDSet[1]);else content += node.parseIt(myTDSet[0]);
		if(myTDSet[2]!="") content += node.parseIt(myTDSet[2]);
		content += node.parseIt(myTDSet[3]);
		
		if(node.opened && node.topchild)
		{
			content += tr_defstart;
			if(node.next) 	content += this.treenav.tr_subontent.replace(/\$subcontent/g,node.getTable());
			else 			content += this.treenav.tr_subontent_last.replace(/\$subcontent/g,node.getTable());
			
			content += this.treenav.tr_defend;
		}
		content += this.treenav.tr_defend;
		
		node=node.next;
	} 

	content += this.treenav.footer;
	return content;
}

posTableStart='<table border="0" cellpadding="0" cellspacing="0" width="1%"><tr><td><img src="tpSrc" width="xPos" height="yPos" border="0"></td><td><img src="tpSrc" width="1" height="yPos" border="0"></td></tr><tr><td><img src="tpSrc" width="xPos" height="1" border="0"></td><td>dynSource</td></tr></table>';

function treenode_draw()
{
	myLayer = getLayer(this.treenav.layername);
	if(!myLayer)
	{
		alert("error: configuration of treenav went wrong.\nPlease check if 'initTreeStyle(...)' is in embedded in the <head>-sction of your document");
		return;
	}
	
	if(myLayer.innerHTML)
	{
		if(navigator.appVersion.indexOf('Macintosh')!=-1 && document.all)	// flick wegen verschissenem IE5 auf Mac 7.6.02 (m.k)
		{
			hideLayer(this.treenav.layername);
			dynVal=this.treenav.topHeader+this.getTable()+this.treenav.topFooter;
			document.all['dynPart'].innerHTML=(this.treenav.xpos>0 || this.treenav.ypos>0)?(posTableStart.replace(/xPos/g,(this.treenav.xpos>0?this.treenav.xpos:1)).replace(/yPos/g,(this.treenav.ypos>0?this.treenav.ypos:1)).replace(/dynSource/,dynVal).replace(/tpSrc/g,tpSrc)):dynVal;
		}
		else
		{
			myLayer.innerHTML=this.treenav.topHeader+this.getTable()+this.treenav.topFooter;
		}
	}
	else if(myLayer.document)
	{
		myLayer.document.open();
		myLayer.document.write(this.treenav.topHeader);
		myLayer.document.write(this.getTable());
		myLayer.document.write(this.treenav.topFooter);
		myLayer.document.close();
	}

	
}

function treenode_parseIt(myString)
{
	var docpic=docpics['standard'];
	myString = myString.replace(/\$id/g,this.id);	
	myString = myString.replace(/\$desc/g,this.description);	
	myString = myString.replace(/\$url/g,this.url);	
	myString = myString.replace(/\$target/g," target=\""+this.target+"\"");	
	myString = myString.replace(/\$statuso/g,this.statuso);	
	myString = myString.replace(/\$statusc/g,this.statusc);	
	myString = myString.replace(/\$statusd/g,this.statusd);	
	myString = myString.replace(/\$treenav/g,this.treenav.name);	
	
	if(docpics[this.docpic]) docpic =docpics[this.docpic];
	myString = myString.replace(/\$docpic/g,docpic);
	return myString;
}

function treenode_addChild(description,id,url,status,target)
{
	myChild = new treenode(this.treenav,description,id,url,status,target);
	
	if(this.topchild==null) this.topchild=myChild;
	else
	{
		temp = this.topchild;
		while(temp.next)
		{
			temp = temp.next;
		}
		temp.next = myChild;
		myChild.previous=temp;
	}
	myChild.parent = this;
	myChild.top = this.topchild;
	
	
	this.treenav.allnodes[myChild.id] = myChild;
	return myChild;
}

function treenode_openDadies()
{
	tmp=this;
	while(tmp.parent)
	{
		tmp = tmp.parent;
		tmp.opened=true;
	}
}

function treenode_closeChildren()
{
	var tmp=this.topchild;
	
	if(!tmp) return;
	do
	{
		tmp.opened = false;
		if(tmp.topchild) tmp.closeChildren();
		tmp = tmp.next;
	} while (tmp);
	
}


//------------------------------------------------- layer and pic functions -------------------------------
function getLayer(name)
{
	if(document.layers)
	{
		if(!document.layers[name]) return null;
		return document.layers[name];
	}
	else if(document.all)
	{
		if(!document.all[name]) return null;
		return document.all[name];
	}
	else if(document.getElementById)
	{
		return document.getElementById(name);
	}
	
	return null;
}

function getLayerObject(name)
{
	if(document.layers)
	{
		if(!document.layers[name]) return null;
		return document.layers[name];
	}
	else if(document.all)
	{
		if(!document.all[name]) return null;
		return document.all[name].style;
	}
	else if(document.getElementById)
	{
		return document.getElementById(name).style;
	}
	
	return null;
}

function showLayer(layerName){
        myLayer = getLayerObject(layerName);
		myLayer.visibility="visible";
 }
        
 function hideLayer(layerName){
        myLayer = getLayerObject(layerName);
		myLayer.visibility="hidden";
 }

function setXPos(layerName, xpos) {
    myLayer = getLayerObject(layerName);
	myLayer.left=xpos;
}

function setYPos(layerName, ypos) {
    myLayer = getLayerObject(layerName);
	myLayer.top=ypos;
}

		
function docjslib_getRealLeft(imgElem) {
	xPos = document.images[imgElem].offsetLeft;
	tempEl = document.images[imgElem].offsetParent;
  	while (tempEl != null) {
  		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
  	}
	return xPos;
}

function docjslib_getRealTop(imgElem) {
	yPos = document.images[imgElem].offsetTop;
	tempEl = document.images[imgElem].offsetParent;
	while (tempEl != null) {
  		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
  	}
	return yPos;
}

function getImageXfromLeft(imgID) {
  if (document.images[imgID].x) return document.images[imgID].x
  else return docjslib_getRealLeft(imgID);
}

function getImageYfromTop(imgID) {
  if (document.images[imgID].y) return document.images[imgID].y
  else return docjslib_getRealTop(imgID);
}


