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

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

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

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

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() {
  this.count 			= -1;
  this.child			= new Array;
  this.startLevel		= 0;
  this.target			= '_top';
  this.serverurl		= '';
  this.noDocumentWrite	= false;
  this.addItem			= trView_addItem;
  return this;
}

function loadimages() {
  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) {
  this.index		= trView.count + 1;
  this.parent		= parentObj;
  this.caption	= caption;
  this.id			= id;
  this.link		= link;
  if (trView.serverurl && link!='') this.link	= trView.serverurl + link;
  this.level		= eval(level);
  if (iLetztesLevel < eval(level)) iLetztesLevel = eval(level);
  if (lastChild == "TRUE") {					
    this.isLast = true;
  }else{
	this.isLast = false;
  }
  this.isOpen	  = false;	
  this.childCount = 0;			

  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 = '<font class=treetext>';

  if (bDebug) document.writeln("makeWriteString Start " + Date() + "<br>") 
  sBaustingGesamt = sBaustingGesamt + '<table width="100%" '
  if (trView.noDocumentWrite == true) {
	sBaustingGesamt = sBaustingGesamt + 'height="100%" '
  }
  sBaustingGesamt = sBaustingGesamt + 'border="0" cellspacing="0" cellpadding="0" class="navcolor"><tr><td class="weiss" colspan="2"><img src="/library/images/tree/1ptrans.gif" width="1" height="1" alt="" /></td></tr><tr><td colspan="2"><img src="/library/images/tree/1ptrans.gif" width="1" height="11" alt="" /></td></tr><tr><td><img src="/library/images/tree/1ptrans.gif" width="4" height="1" alt="" /></td><td valign="top" height="100%"><table border="0" cellspacing="0" cellpadding="4"><tr>'
  //	sBaustingGesamt = sBaustingGesamt + '<td><img src="/library/images/misc/gif.gif" width="4" height="1" alt="" /></td>'
  //	sBaustingGesamt = sBaustingGesamt + '<td><img src="/library/images/misc/gif.gif" width="6" height="1" alt="" /></td>'
  sBaustingGesamt = sBaustingGesamt + fontOnSize
  sBaustingGesamt = sBaustingGesamt + '<td>'
  if (trView.child[0].link != "" && trView.child[0].link != "index.html") {
    sBaustingGesamt = sBaustingGesamt + '<a '
	if (trView.target != '') sBaustingGesamt = sBaustingGesamt + 'target="' + trView.target + '"'
	  sBaustingGesamt = sBaustingGesamt + ' href="' + trView.child[0].link + '" title="' + trView.child[0].caption + '"  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 != "index.html") sBaustingGesamt = sBaustingGesamt + '</a>'
	sBaustingGesamt = sBaustingGesamt + fontOff	
	sBaustingGesamt = sBaustingGesamt + "</td></tr></table>"

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

    if (bDebug) document.writeln("makeWriteString End " + Date() + "<br>")

    if (bDebug) document.writeln("DocumentWrite Start " + Date() + "<br>") 
	sBaustingGesamt = sBaustingGesamt + "</font>"
	sBaustingGesamt = sBaustingGesamt + "</td></tr></table>"
	if (trView.noDocumentWrite == false) {
	  document.writeln(sBaustingGesamt);
	}
	if (trView.noDocumentWrite == false) {
	  searchAndOpenId(OpenPageID)
	}

    if (bDebug) document.writeln("DocumentWrite End " + Date() + "<br>")
	if (trView.noDocumentWrite == true) {
	  return sBaustingGesamt;
	}
}

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 + ">";	
  }

  if (bDebug) {
	sBaustring = sBaustring + "<table width='100%' border='1' cellpadding='0' cellspacing='0'><tr>";
  }else{
	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/tree/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 != "index.html") {
	sBaustring = sBaustring + ">"
  } else {		
	sBaustring = sBaustring + " onClick='" + nClick + "'";
	sBaustring = sBaustring + " onMouseOver='this.style.cursor=\"hand\"'";
	sBaustring = sBaustring + ">"
  }
  sBaustring = sBaustring + fontOnSize
  if (dItem.link != "" && dItem.link != "index.html") {
	sBaustring = sBaustring + '<a '
	if (trView.target != '') sBaustring = sBaustring + 'target="' + trView.target + '"'
	sBaustring = sBaustring + ' href="' + dItem.link + '" title="' + dItem.caption + '"  class="treetext">'
  }	
  sBaustring = sBaustring + dItem.caption
  if (withPageID) sBaustring = sBaustring + "(" + dItem.id + ")"
  if (bDebug) sBaustring = sBaustring + "(" + dItem.index + ")"
  if (bDebug) sBaustring = sBaustring + "[" + sFolderOpen + "]"
  if (dItem.link != "" && dItem.link != "index.html") sBaustring = sBaustring + '</a>'
  sBaustring = sBaustring + fontOff
  sBaustring = sBaustring + "</td><td><img src='/library/images/tree/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 + "'";
	if (trView.noDocumentWrite == false) {
	  sBaustring = sBaustring + " style='display:none'";
	}
	sBaustring = sBaustring + ">";
  }
  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"');
  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 = "";

  if (trView.child[0].id == id) {
	return
  }

  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")
    if (bDebug) alert("start parent")
	if (searchNode != null) {
	  sIndexPath = searchParent(searchNode, "");
	}
    if (bDebug) alert("ende parent")
    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")
}

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(241,241,241)';
  document.getElementById("linkid" + linkIndex).style.fontWeight='bold';
  document.getElementById("linkid" + linkIndex).style.paddingLeft='2px';
  lastLinkStyleOnIndex = linkIndex;
}

function NNonload() {
}

