/**************************************************************************
	Copyright (c) 2001 Geir Landrö (drop@destroydrop.com)
	JavaScript Tree - www.destroydrop.com/hugi/javascript/tree/
	Version 0.96	
	Additional Scripts (c) 2005 by Patrick Elsing - e-synergy.de
	This script can be used freely as long as all copyright messages are
	intact.
**************************************************************************/
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
// Arrays for nodes and icons
var nodes		= new Array();;
var openNodes	= new Array();
var icons		= new Array(6);
var oldNode = new Array();
var ImgList = new Array();
var isOpen = false;
var park = new Array();
var openNode = new Array();
var openChapter = new Array(1);
var activeButton = new Array(1);
var trackList = new Array();
var myRollover;
var oldBottom;
var aktiv;
var onChange = false;

//Preload all rollover in the tree-------------------------------
function makeImgLib(count){
	myRollover = new Array(count);
	for(i=0; i<count; i++){
		myRollover[i] = new Image();
	}
}

//Preload all rollover in the tree-------------------------------

// Loads all icons that are used in the tree
// Create the tree
function createTree(arrName, startNode, openNode) {
	nodes = arrName;
	if (nodes.length > 0) {
		//preloadIcons();
		makeImgLib(nodes.length);
		if (startNode == null) startNode = 0;
		if (openNode != 0 || openNode != null) setOpenNodes(openNode);

		var recursedNodes = new Array();
		addNode(startNode, recursedNodes);
	}
}
// Returns the position of a node in the array
function getArrayId(node) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==node) return i;
	}
}
// Puts in array nodes that will be open
function setOpenNodes(openNode) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==openNode) {
			openNodes.push(nodeValues[0]);
			setOpenNodes(nodeValues[1]);
		}
	} 
}
// Checks if a node is open
function isNodeOpen(node) {
	for (i=0; i<openNodes.length; i++)
		if (openNodes[i]==node) return true;
	return false;
}
// Checks if a node has any children
function hasChildNode(parentNode) {
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) return true;
	}
	return false;
}
// Checks if a node is the last sibling
function lastSibling (node, parentNode) {
	var lastChild = 0;
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode)
			lastChild = nodeValues[0];
	}
	if (lastChild==node) return true;
	return false;
}
// Adds a new node in the tree
function addNode(parentNode, recursedNodes) {
	for (var i = 0; i < nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) {			
			var ls	= lastSibling(nodeValues[0], nodeValues[1]);
			var hcn	= hasChildNode(nodeValues[0]);
			var ino = isNodeOpen(nodeValues[0]);
			var overImgTemp = nodeValues[2].split("_off");
			var overImg = overImgTemp.join("_on");
			myRollover[i].src = "images/img_navi/"+ overImg;
			if (ls) recursedNodes.push(0);
			else recursedNodes.push(1);
			//document.write("<a href=\"javascript:;\" onclick=\"oc(" + nodeValues[0] + ")");
			document.write("<a href=\""+nodeValues[3]+"\" title=\"" + nodeValues[4] + "\" onclick=\oc(" + nodeValues[0] + ")");
			document.write("; writeImgList('imgBut" + nodeValues[0] + "', '" + nodeValues[2] + "');");
			document.write(" MM_swapImage('imgBut" + nodeValues[0] + "','',''+myRollover["+i+"].src+'',1); return false;\"");
			document.write(" onMouseOut=\"if(!askForClicked('imgBut" + nodeValues[0] + "'))MM_swapImage('imgBut" + nodeValues[0] + "','','images/img_navi/"+ nodeValues[2] +"',1)\"");
			document.write(" onMouseOver=\"MM_swapImage('imgBut" + nodeValues[0] + "','',''+myRollover["+i+"].src+'',1)\"");
			document.write(" onFocus=\"if(document.all) this.blur()\">")
			document.write("<img id=\"stNode" + nodeValues[0] + "\" alt=\"" + nodeValues[4] + "\" name=\"imgBut" + nodeValues[0] + "\" src=\"images/img_navi/"+nodeValues[2]+"\" align=\"absbottom\" /></a><br>");
			if (hcn) {
				document.write("<div id=\"div" + nodeValues[0] + "\"");
					if (!ino) document.write(" style=\"display: none;\"");
				document.write(">");
				addNode(nodeValues[0], recursedNodes);
				document.write("</div>");
			}
			recursedNodes.pop();
		}
	}
}

function oc(node) {
	activeButton[0]="imgBut"+node;	
	if(openNode.length != 0 && !myFamily(node)){
	 	closeAll();
		tidyAllButtons();	 	
	}else if(openNode.length != 0 && myFamily(node)){
		var imgtoClose = closeOtherChilds(node);
		tidyChildImg(imgtoClose);
	}else{
		tidyAllButtons();
	}
	
	if(haveChild(node)){
		var childDiv = MM_findObj("div" + node);
		if(childDiv) childDiv.style.display = '';
		if(!checkOpenNode(node)) openNode.push(node);
	}
	
	if(!checkNode(node)) oldNode.push(node);
	//content Laden
	if(trackList.length==0) loadContent(node);
}           


function writeImgList(param1, param2){
	var pointer;
	var inside = false;
	if(!ImgList.length){
		pointer = 0;
	}else{
		pointer = ImgList.length;
		for(var i=0; i<ImgList.length; i++){
			if(ImgList[i][0]==param1) inside = true;
		}
	}
	if(!inside){
		ImgList[pointer] = new Array(2);
		ImgList[pointer][0] = param1;
		ImgList[pointer][1] = param2;
	}
}

function myFamily(nowIs){
	var tempChap;
	for(i=openNode.length-1; i >= 0; i--){
		tempChap = nodes[nowIs-1].split("|")[1];
		if(tempChap==openNode[i]){
			return true; break;
		}
	}
}

function closeAll(){
	var theoldDiv;
	for(i=openNode.length-1; i >= 0; i--){
		theoldDiv = MM_findObj("div" + openNode[i]);
		theoldDiv.style.display = 'none';
		openNode.pop();
	}
	return true;
}

function closeOtherChilds(nowIs){
	var theoldDiv;
	var tooMuchImg = ImgList.length-openNode.length;
	var counter = 0;
	var myChapter = nodes[nowIs-1].split("|")[1];
	for(i=openNode.length-1; i >= 0; i--){
		if(openNode[i] != myChapter){
			theoldDiv = MM_findObj("div" + openNode[i]);
			theoldDiv.style.display = 'none';
			openNode.pop();
			counter++;
		}else{
			break;
		}
	}
	var imgToChange = tooMuchImg+counter;
	return imgToChange;
}

function tidyAllButtons(){	
	for(i=ImgList.length-1; i >= 0; i--){
		if(ImgList[i][0] != activeButton[0]){
			MM_swapImage(ImgList[i][0],'','images/img_navi/'+ImgList[i][1]+'',1);
			ImgList.pop();
		}
	}
}

function tidyChildImg(imgtoClose){
	for(i=1; i<=imgtoClose; i++){
		MM_swapImage(ImgList[ImgList.length-1][0],'','images/img_navi/'+ImgList[ImgList.length-1][1]+'',1);
		ImgList.pop();
	}
}

function askForClicked(nowIs){
	for(var i=0; i<ImgList.length; i++){
		if(ImgList[i][0]==nowIs) return true; 	
	}
}

function checkOpenNode(nowIs){
	for(var i=0; i<openNode.length; i++){
		if(openNode[i]==nowIs) return true; 	
	}
} 

function checkNode(nowIs){
	for(var i=0; i<oldNode.length; i++){
		if(oldNode[i]==nowIs) return true; 	
	}
} 

function haveChild(nowIs){
	if(nowIs < nodes.length){
		if(nodes[nowIs].split("|")[1] == nowIs){ return true};
	}
}

function haveParent(nowIs){
	if(nodes[nowIs].split("|")[1] != 0) return true;
}

//Erfragt den Ursprungsknoten eines Childs
function searchMyRoots(nowIs){
	var myIndex = nowIs-1
	var myParent = nodes[myIndex].split("|")[1];
	var i = 1;
	trackList[0]=nowIs;
	while(myParent != 0){
		myIndex = nodes[myParent-1].split("|")[0];
		trackList[i] = myIndex;
		myParent = nodes[myParent-1].split("|")[1];
		
		i++;
	}
	trackList.reverse();
	return trackList;
}

//Öffnet den Tree wenn neue Seite geladen werden soll
function openContent(nowIs,is,ext){ 
	if(is == 'no') {onChange = true;}
	else {onChange = false;}
	trackList = searchMyRoots(nowIs);
	var nodeValues, myIndex;
	var i = 0;
	while(i < trackList.length){
		myIndex = trackList[i];
		nodeValues = nodes[myIndex-1].split("|");			
		var overImgTemp = nodeValues[2].split("buttons_");
		var overImg = overImgTemp.join("buttons_on_");
		oc(nodeValues[0]);
		writeImgList("imgBut" + nodeValues[0],nodeValues[2]);
		MM_swapImage('imgBut' + nodeValues[0] + '','',myRollover[myIndex-1].src,1);
		i++;
	}
	loadContent(nodeValues[0],onChange,ext);
	for(a=trackList.length; a > 0; a--){
		trackList.pop();
	}
}

function loadContent(nowIs,onChange,ext){
	if(contID != nowIs){
		var myURL= nodes[nowIs-1].split("|")[3];
		//location.href = myURL;
	}
}

function timeShift(nowIs,onChange, ext){
	if(parent.counter == 2){
		parent.lz=nowIs;
		parent.frames[0].changeHead(nowIs);
		parent.frames[3].changeImages(nowIs);
		if(!onChange && !ext){
			var myURL= nodes[nowIs-1].split("|")[3];
			//window.location.href = myURL;
			onChange = false;		
		}		
		window.clearInterval(aktiv);
	}
}


// Push and pop not implemented in IE(crap! don´t know about NS though)
if(!Array.prototype.push) {
	function array_push() {
		for(var i=0;i<arguments.length;i++)
			this[this.length]=arguments[i];
		return this.length;
	}
	Array.prototype.push = array_push;
}
if(!Array.prototype.pop) {
	function array_pop(){
		lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
		return lastElement;
	}
	Array.prototype.pop = array_pop;
}

