// navigation tree functions


var bDebug = false;
var withPageID = false;
var OpenPageID;

var imgNodeDir = "./library/images/navi/";
var imgWidth = 11;
var imgHeight = 20;
var imgExt = ".gif";

var imgTargetBlank = "";

var sBaustingGesamt;
var iLetztesLevel = 0;
var iLetztesItemLevel = 0;
var lastLinkStyleOnIndex = -1;
var sFolderOpen = "";
var fontOn = '<span class=treetext>'
var fontOnSize = '<span class=treetext>'
var fontOff = '</span>'

var imgNone;
var imgBlank;
var imgHLine;
var imgVLine;
var imgVHalfLine;
var imgPlus;
var imgMinus;


function neuesFenster(url)	{
var fenster = window.open(url,'neuesfenster','status=0,toolbar=0,menubar=0,location=0,scrollbars=0,resizable=1,top=0,left=0'); fenster.focus(); 
}


function preload_image(img_name, img_src) {
	eval(img_name + ' = new Image()');
	eval(img_name + '.src = img_src');
}

function switch_image(img_name, img_name_switch) {
	if (eval(img_name_switch)) eval('document["'+img_name+'"].src='+img_name_switch+'.src');
}

function trView() {
// Eigenschaft
	this.count = -1;
	this.child				= new Array;				// Child nodes = ROOT nodes.
	this.startLevel			= 0;
	this.target				= '_top';
	this.serverurl			= '';
	this.noDocumentWrite	= false;					// Keine Ausgabe, nur String zusammensetzen
// Methode
	this.addItem			= trView_addItem;

	return this;
}


function loadimages() {
// Bilder laden
	if (trView.noDocumentWrite == false) {
		preload_image ("imgNone", imgNodeDir + "tvNone" + imgExt);
		preload_image ("imgBlank", imgNodeDir + "tvBlank" + imgExt);
		preload_image ("imgHLine", imgNodeDir + "tvHLine" + imgExt);
		preload_image ("imgVLine", imgNodeDir + "tvVLine" + imgExt);
		preload_image ("imgVHalfLine", imgNodeDir + "tvVHalfLine" + imgExt);
		preload_image ("imgPlus", imgNodeDir + "tvPlus" + imgExt);
		preload_image ("imgMinus", imgNodeDir + "tvMinus" + imgExt);
		if (document.all) {
			document.getElementById = document.all
		}
	}else{
		imgNone = imgNodeDir + "tvNone" + imgExt;
		imgBlank = imgNodeDir + "tvBlank" + imgExt;
		imgHLine = imgNodeDir + "tvHLine" + imgExt;
		imgVLine = imgNodeDir + "tvVLine" + imgExt;
		imgVHalfLine = imgNodeDir + "tvVHalfLine" + imgExt;
		imgPlus = imgNodeDir + "tvPlus" + imgExt;
		imgMinus = imgNodeDir + "tvMinus" + imgExt;
	}
}

function trNode(parentObj, caption, id, link, level, lastChild) {

//Eigenschaften
	this.index		= trView.count + 1;			// The node's index in respect of it's treeview
	this.parent		= parentObj;				// The node's parent

	this.caption	= caption;					// The node's caption (may also be hyper link)
	this.id			= id;						// The node's id
	this.link		= link;						// The node's link
	if (trView.serverurl && link!='') this.link	= trView.serverurl + link;
	this.level		= eval(level);				// How many levels down the node is from it's ROOT
	if (iLetztesLevel < eval(level)) iLetztesLevel = eval(level); // Last level in
	if (lastChild == "TRUE") {					// is Last Child
		this.isLast = true;
	}else{
		this.isLast = false;
	}
	this.isOpen		= false;					// Node is open	
	this.childCount = 0;						// Node Child Count

	parentObj.childCount++;
	
	return this;
}

function trView_addItem(Parent, caption, id, link, level, lastChild) {
	newNode = new trNode(Parent, caption, id, link, level, lastChild);
	this.child[newNode.index] = newNode;
	this.count++;

	return newNode;
}

function addCheckIsLastItem(index, parent) {
	var oItem = new Object();
	oItem.index = index;
	oItem.parent = parent;
	return oItem;
}
function checkIsLastItem() {
// Letzte Items  isLast auf True
	var sLevelArray = new Array();
	var iLevelItem;
	var iLevel;
	var iLevelCount;
	var iLastItem = trView.count;

	for (iLevel = 1; iLevel <= iLetztesLevel; iLevel++) {
		for (i = 0; i <= iLastItem; i++) {
			if (trView.child[i].level == iLevel) {
				sLevelArray[sLevelArray.length] = addCheckIsLastItem(i, trView.child[i].parent.id);
			}
		}
		iLevelCount = sLevelArray.length - 1;
		if (iLevelCount > 0) {
			iLevelItem = sLevelArray[iLevelCount].index;
			trView.child[iLevelItem].isLast = true;
			for (i = 0; i < iLevelCount; i++) {
				if (sLevelArray[i].parent != sLevelArray[i + 1].parent) {
					iLevelItem = sLevelArray[i].index;
					trView.child[iLevelItem].isLast = true;
				}
			}
		}
	}
}

function trViewdraw() {
	loadimages();
	if (trView.count > 0) {checkIsLastItem();}

	sBaustingGesamt = '<div id="treebox">';


// 1. Knoten wir Portal

	
		//sBaustingGesamt = sBaustingGesamt + '<div id="treeinner">'
	
			sBaustingGesamt = sBaustingGesamt + '<table border="0" cellspacing="0" cellpadding="0"><tr><td valign="top">'
			//sBaustingGesamt = sBaustingGesamt + '<table width="90%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top">'

	

				sBaustingGesamt = sBaustingGesamt + '<div id="treetitle">'
		
		if (trView.child[0].link != "" && trView.child[0].link != "/") {
			sBaustingGesamt = sBaustingGesamt + '<a '
			
			if (trView.target != '') sBaustingGesamt = sBaustingGesamt + 'target="' + trView.target + '"'
			sBaustingGesamt = sBaustingGesamt + ' href="' + trView.child[0].link + '" class="treetext">'
		}	
		sBaustingGesamt = sBaustingGesamt + "<b>" + trView.child[0].caption + "</b>"
		if (withPageID) sBaustingGesamt = sBaustingGesamt + "(" + trView.child[0].id + ")"
	if (bDebug) sBaustingGesamt = sBaustingGesamt + "(" + trView.child[0].index + ")"
		if (trView.child[0].link != "" && trView.child[0].link != "/") sBaustingGesamt = sBaustingGesamt + '</a>'
		
				sBaustingGesamt = sBaustingGesamt + "</div>"

		
// alle anderen Knoten
	forcount = trView.count;
	for (i=1; i <= forcount; i++) drawItem(trView.child[i]);

	

			sBaustingGesamt = sBaustingGesamt + '</td></tr></table>'
		
		//sBaustingGesamt = sBaustingGesamt + '</div>'
	
	sBaustingGesamt = sBaustingGesamt + '</div>'
	
	if (trView.noDocumentWrite == false) {
		document.writeln(sBaustingGesamt);
	}
	

// 1. Knoten öffnen  -   nodeClick(1);

//Übergabepage öffnen
	if (trView.noDocumentWrite == false) {
		searchAndOpenId(OpenPageID)
	} 
}

function drawItem(dItem, vRoot) {
	var sBaustring = "";
	var imgPicName = imgNone;
	var imgPicLineHalf = "X";
	var nClick = "";

	iIndex = dItem.index + 1;
	iCLevel = dItem.level;
	iCCount = dItem.childCount;
	iCisLast = dItem.isLast;

	if (iLetztesItemLevel > iCLevel) for (var ix = iCLevel;  ix < iLetztesItemLevel; ix++) sBaustring = sBaustring + "</div>"

	if (iIndex == 0) {
		sBaustring = sBaustring + "<div id='childObj" + iIndex + "'";
		if (trView.noDocumentWrite == false) {
			if (dItem.level != 0) sBaustring = sBaustring + " style='display:none'";
		}
		sBaustring = sBaustring + " style='border:0px solid red'>";	
	}
	
	
	
		sBaustring = sBaustring + "<table width='189' border='0' cellpadding='0' cellspacing='0'><tr>";
		//sBaustring = sBaustring + "<table width='100%' border='0' cellpadding='0' cellspacing='0'><tr>";


//	if (iCLevel != 0) sBaustring = sBaustring + "<td width='" + imgWidth + "' NOWRAP>" + drawImg(imgBlank, imgWidth) + "</td>"

	for (var ix=0; ix < iCLevel - 1; ix++) {
		sBaustring = sBaustring + "<td width='" + imgWidth + "' nowrap"
		imgPicName = imgBlank;
		if (ix >= trView.startLevel && sFolderOpen.substr(ix, 1) == "N") {
			if (trView.noDocumentWrite == false) {
				sBaustring = sBaustring + " background='" + imgVLine.src + "'"
			}else{
				sBaustring = sBaustring + " background='" + imgVLine + "'"
			}
		}
		sBaustring = sBaustring + ">" + drawImg(imgPicName, imgWidth) + "</td>";
	}

	imgPicName = imgNone;
	nClick = "";
	if (iCLevel == 0) {
		imgPicName = imgMinus;
		nClick = "nodeClick(" + iIndex + ")";
	}else if (iCCount == 0 && !iCisLast) {
		imgPicName = imgHLine;
		imgPicLineHalf = "N";
	}else if (iCCount == 0 && iCisLast) {
		imgPicName = imgHLine;
		imgPicLineHalf = "J";
	}else if (iCCount != 0 && !iCisLast) {
		imgPicName = imgPlus;
		imgPicLineHalf = "N";
		nClick = "nodeClick(" + iIndex + ")";
	}else if (iCCount != 0 && iCisLast) {
		imgPicName = imgPlus;
		imgPicLineHalf = "J";
		nClick = "nodeClick(" + iIndex + ")";
		if (iCLevel == trView.startLevel + 1) trView.startLevel = iCLevel;
	}	
	if (trView.noDocumentWrite == true) {
		imgPicName = imgHLine;
	}
	if (iCisLast) {
		sFolderOpen = setzteBuchstabe(sFolderOpen, iCLevel, "J")
	}else{
		sFolderOpen = setzteBuchstabe(sFolderOpen, iCLevel, "N")
	}
	sBaustring = sBaustring + "<td width='" + imgWidth + "' NOWRAP valign='top'"
	
	if (trView.noDocumentWrite == false) {
		if (imgPicLineHalf == "N") sBaustring = sBaustring + " background='" + imgVLine.src + "'"
	}else{
		if (imgPicLineHalf == "N") sBaustring = sBaustring + " background='" + imgVLine + "'"
	}
	
	sBaustring = sBaustring + ">" + drawImg(imgPicName, imgWidth, iIndex, nClick) + "</td>"

	sBaustring = sBaustring + "<td><img src='./library/images/1ptrans.gif' width='2' height='1' alt='' /></td><td valign='top' style='padding-top:2px;' width='100%' id='linkid" + dItem.index + "'"
	if (dItem.link != "" && dItem.link != "/") {
		sBaustring = sBaustring + ">"
	} else {		
		sBaustring = sBaustring + " onClick='" + nClick + "'";
		sBaustring = sBaustring + " onMouseOver='this.style.cursor=\"hand\"'";
		sBaustring = sBaustring + ">"
	}
	sBaustring = sBaustring + fontOnSize
	if (dItem.link != "" && dItem.link != "/") {
		sBaustring = sBaustring + '<a '
		if (trView.target != '')
		{
			if (dItem.id != 'target_blank')
			{ 
				sBaustring = sBaustring + 'target="' + trView.target + '"'
				sBaustring = sBaustring + ' href="' + dItem.link + '" class="treetext">'
			}
			else
			{
				//sBaustring = sBaustring + 'target="_blank"'
				//sBaustring = sBaustring + ' href="' + dItem.link + '" class="treetext">'
				sBaustring = sBaustring + ' href="javascript:neuesFenster(\'' + dItem.link + '\')" class="treetext">'
			}
		}
	}	
	sBaustring = sBaustring + dItem.caption
	if (withPageID) sBaustring = sBaustring + "(" + dItem.id + ")"
	//if (dItem.id == 'extern') sBaustring = sBaustring + imgExtLink
	if (dItem.id == 'target_blank') sBaustring = sBaustring + imgTargetBlank
if (bDebug) sBaustring = sBaustring + "(" + dItem.index + ")"
if (bDebug) sBaustring = sBaustring + "[" + sFolderOpen + "]"
	if (dItem.link != "" && dItem.link != "/") sBaustring = sBaustring + '</a>'
	sBaustring = sBaustring + fontOff
	sBaustring = sBaustring + "</td><td><img src='./library/images/1ptrans.gif' width='2' height='1' alt='' /></td>"

	sBaustring = sBaustring + "</tr></table>"

	iLetztesItemLevel = iCLevel;
	
	if (iCCount != 0) {
		sOpenDiv = true
		sBaustring = sBaustring + "\n<div id='childObj" + iIndex + "' style='display:none; border:solid 0px aqua;'>";
	}	
	
	sBaustingGesamt = sBaustingGesamt + sBaustring;
	
	
	
}

function setzteBuchstabe(sInString, iInStelle, sInBuchstabe) {
	var sTmpString = "";
	if (iInStelle < 0) return null;
	
	if (iInStelle > sInString.length) {
		sTmpString = sInString + sInBuchstabe;
		return sTmpString;
	}
	
	sTmpString = sInString.substr(0, iInStelle - 1);
	sTmpString = sTmpString + sInBuchstabe + sInString.substr(iInStelle, sInString.length - iInStelle);
	return sTmpString;
}

function drawImg(image, width, id, onClick){
	if (trView.noDocumentWrite == false) {
		imagesrc = image.src;
	}else{
		imagesrc = image;
	}
	imageCode = new String('<img src="' + imagesrc + '" border="0" alt=""');
	if (width) imageCode = imageCode + ' width="' + width + '"';
	if (id) imageCode = imageCode + ' id="iconimg' + id + '" name="iconimg' + id + '"';

	if (trView.noDocumentWrite == false && onClick) {
		imageCode = imageCode + ' onClick="' + onClick + '"';
		imageCode = imageCode + ' onMouseOver="this.style.cursor=\'hand\'"';
	}
	
	imageCode = imageCode + '>';
		
	return imageCode;
}

function nodeClick(nodeIndex) {
	var cItem = document.getElementById("childObj" + nodeIndex);
	
	var sImage = document.getElementById("iconimg" + nodeIndex).src;

	if (trView.child[nodeIndex - 1].isOpen) {
		trView.child[nodeIndex - 1].isOpen = false
		cItem.style.display = "none"
		switch_image ("iconimg" + nodeIndex, "imgPlus")
	}else{
		trView.child[nodeIndex - 1].isOpen = true
		cItem.style.display = ""
		switch_image ("iconimg" + nodeIndex, "imgMinus")
	}
}


function searchAndOpenId(id) {
	var searchIndex = -1;
	var searchNode;
	var sIndexPath = "";
//1. Knoten nicht suchen
	if (trView.child[0].id == id) {
		return
	}
//Suche die Seite
if (bDebug) alert("start search-ID")
	var forcount = trView.count;
	for (i = 0; i <= forcount; i++) {
		if (trView.child[i].id == id) {
			searchIndex = i;
			searchNode = trView.child[i];
			break;
		}
	}
if (bDebug) alert("end search-ID")
//Suche die Eltern
if (bDebug) alert("start parent")
	if (searchNode != null) {
		sIndexPath = searchParent(searchNode, "");
	}
if (bDebug) alert("ende parent")
//Öffne Pfad
if (bDebug) alert("start open")
	if (sIndexPath != "") {
		if (searchNode.childCount > 0) sIndexPath = sIndexPath + "," + searchIndex
		sIndexArray = sIndexPath.split(",");
		for (i = 1; i < sIndexArray.length; i++) {
if (bDebug) alert("start node")

			if (!trView.child[eval(sIndexArray[i])].isOpen) nodeClick(eval(sIndexArray[i]) + 1);
			if (!trView.child[sIndexArray[i]].isOpen) nodeClick(eval(sIndexArray[i]) + 1);
if (bDebug) alert("end node")
		}
		setStyleOn(searchIndex)
	}
if (bDebug) alert("ende open")

}
//Suche die Eltern
function searchParent(cNode, sIndexPath) {
	var aString = sIndexPath;
	var pNode = cNode.parent;
	
	if (pNode.index != 0) {
		aString = searchParent(pNode, sIndexPath);
	}

	if (aString != "") aString = aString + ",";
	aString = aString + pNode.index;
	return aString;
}
function setStyleOn(linkIndex) {
	if (lastLinkStyleOnIndex > -1) {
		document.getElementById("linkid" + lastLinkStyleOnIndex).style.background='none';
	}
	document.getElementById("linkid" + linkIndex).style.background='rgb(255,255,255)';
	document.getElementById("linkid" + linkIndex).style.fontWeight='bold';
	document.getElementById("linkid" + linkIndex).style.paddingLeft='2px';
	lastLinkStyleOnIndex = linkIndex;
}

function NNonload() {
}


