// This script was written by Marcelino Alves Martins on January of 1997.
// You are free to copy it as long as you keep this copyright notice.
// For more information refer to http://www.geocities.com/Paris/LeftBank/2178/ or mail to
// martins@hks.com

// Modified and enhanced by Mathias C. Hoeschen [maze@bikefreaks.de]
// Original author see above. Find the original code at the above address.
// New functions are (c) 1999 M. C. Hoeschen / Please keep this copyright notice,
// or you are NOT allowed to use the enhanced version of TreeView.

// each node in the tree is an Array with 4+n positions
// node[0] is 0/1 when the node is closed/open
// node[1] is 0/1 when the folder is closed/open
// node[2] is 1 if the children of the node are documents
// node[3] is the name of the folder
// node[4]...node[4+n] are the n children nodes

// Building the data in the tree
function generateTree(){
 var aux1, aux2, aux3, aux4

 foldersTree = folderNode("bikefreaks")
  aux1 = appendChild(foldersTree, leafNode("Reiseberichte"))
   appendChild(aux1, doDocEntry(0, "México", "reports/mexico/index.html", ""))
   appendChild(aux1, doDocEntry(0, "Skandinavien", "reports/scandinavia2001/index.html", ""))
   appendChild(aux1, doDocEntry(0, "Alaska-Arizona", "reports/alaska-arizona/index.html", ""))
   appendChild(aux1, doDocEntry(0, "Neuseeland", "reports/nz/index.html", ""))
   appendChild(aux1, doDocEntry(0, "Kanada/Alaska", "reports/alaska/index.html", ""))
   appendChild(aux1, doDocEntry(0, "externe", "reports/external/index.html", ""));
//   appendChild(aux1, doDocEntry(0, "Kanada", "reports/canada/index.html", ""))
//   appendChild(aux1, doDocEntry(0, "Schweden", "reports/sweden/index.html", ""))
//  aux1 = appendChild(foldersTree, folderNode("Tips"))
//   aux1 = appendChild(foldersTree, leafNode("Planung"));
//    appendChild(aux1, doDocEntry(0, "Transport", "plan/transport.html", ""))
//    appendChild(aux1, doDocEntry(0, "Gesundheit", "plan/health.html", ""))
   aux1 = appendChild(foldersTree, leafNode("Ausrüstung"));
    appendChild(aux1, doDocEntry(0, "Bike", "outfit/bike/index.html", ""))
//    appendChild(aux1, doDocEntry(0, "Werkzeug", "outfit/tools.html", ""))
    appendChild(aux1, doDocEntry(0, "Kleidung", "outfit/clothes/index.html", ""))
    appendChild(aux1, doDocEntry(0, "Foto", "outfit/photo/index.html", ""))
//    appendChild(aux1, doDocEntry(0, "Sonstiges", "outfit/other/index.html", ""))
   aux1 = appendChild(foldersTree, leafNode("Unterwegs"))
    appendChild(aux1, doDocEntry(0, "Verpflegung", "onTrip/supply/index.html", ""))
    appendChild(aux1, doDocEntry(0, "Der Bär", "onTrip/bear/index.html", ""))
    appendChild(aux1, doDocEntry(0, "Insekten", "onTrip/insects/index.html", ""))
    appendChild(aux1, doDocEntry(0, "Extreme Höhen", "onTrip/altitude/index.html", ""))
//    appendChild(aux1, doDocEntry(0, "andere Tiere", "onTrip/animals/index.html", ""))
  aux1 = appendChild(foldersTree, leafNode("Community"))
   appendChild(aux1, doDocEntry(0, "Forum", "board.html", ""))
   appendChild(aux1, doDocEntry(0, "Umfragen", "vote/index.html", ""))
   appendChild(aux1, doDocEntry(0, "Postkarte", "postcard/index.html", ""))
   appendChild(aux1, doDocEntry(0, "Links", "links/links.html", ""))
  aux1 = appendChild(foldersTree, leafNode("über uns"))
   appendChild(aux1, doDocEntry(0, "Das Team", "about/index.html", ""))
   appendChild(aux1, doDocEntry(0, "Impressum", "about/impressum.html", ""))
   appendChild(aux1, doDocEntry(0, "Banner", "about/banner.html", ""))
   appendChild(aux1, doDocEntry(0, "copyright", "about/copy.html", ""))
}

// Auxiliary function to build the node
function folderNode(name){
 var arrayAux
 arrayAux=new Array
 arrayAux[0]=0
 arrayAux[1]=0
 arrayAux[2]=0
 arrayAux[3]=name
 return arrayAux
}

// Auxiliary function to build the node
// The entries in arrayAux[4]..array[length-1] are strings built in generate doc entry
function leafNode(name){
 var arrayAux
 arrayAux=new Array
 arrayAux[0]=0
 arrayAux[1]=0
 arrayAux[2]=1
 arrayAux[3]=name
 return arrayAux
}

//this way the generate tree function becomes simpler and less error prone
function appendChild(parent, child){
 parent[parent.length]=child
 return child
}

//these are the last entries in the hierarchy, the local and remote links to html documents
function doDocEntry(icon, docDescription, link){
 var r=""
 if (icon==0)
  r="<a href='"+link+"'><img src='/images/doc.gif' width='24' height='20' alt='anzeigen'"
 else
  r="<a href='http://"+link+"' target='_blank'><img src='/images/link.gif' width='24' height='20' alt='anzeigen'"
 r=r + " border='0'></a><td nowrap>"
 r=r + "<a href='"+link+"'>" + docDescription + "</a>"
 return r
}

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

//redraws the left frame
function redrawTree(){
 var doc=top.treeFrame.window.document;
 doc.open("text/html", "replace");
 doc.writeln('<html><head><meta http-equiv="content-type" content="text/html; charset=iso-8859-1">\
<base href="http://www.bikefreaks.de/" target="mainFrame">\n<style type="text/css">\n\
A:link, A:visited {font-family:Arial,sans-serif; font-size:12px; color:black; text-decoration:none}\n\
A:hover {color: blue; text-decoration: underline}\nA:active {color: blue; text-decoration: none}\n\
.f {font-weight:bold; font-size:12px}\nb {font-weight:normal; background-color: rgb(255,255,210)}\n\
body {background-attachment:fixed; background-repeat:repeat-y}\n\
</style></head>\n<body bgcolor="white" background="/images/treeBack.jpg">');
 redrawNode(foldersTree, doc, 0, 1, "");
 doc.writeln('<form method="get" action="http://www.bikefreaks.de/cgi-bin/search/search.pl">\
<table border="0" cellspacing="0"><tr>\
<td><input type="text" name="q" style="width:100px; font-family:sans-serif; font-size:8pt" size="10" maxlength="80" value="Suche" onFocus="if(this.value==\'Suche\'){this.value =\'\';}else{this.select()}"></td>\
<td><input type="image" src="/images/search.gif" alt="suchen" width="40" height="25" border="0"></td>\
</tr></table></form></font></body></html>');
 doc.close();
}

//recursive function over the tree structure called by redrawTree
function redrawNode(foldersNode, doc, level, lastNode, leftSide){
 var j=0, i=0;
 doc.write('<table border="0" cellspacing="0" cellpadding="0"><tr><td valign="middle" nowrap>');
 doc.write(leftSide);

 if (level>0)
  if (lastNode){ //the last 'brother' in the children array
   doc.write('<img src="/images/lastnode.gif" width="16" height="20">');
   leftSide=leftSide+'<img src="/images/blank.gif" width="16" height="20">';
  }else{
   doc.write('<img src="/images/node.gif" width="16" height="20">');
   leftSide=leftSide+'<img src="/images/vertline.gif" width="16" height="20">';
  }

 displayIconAndLabel(foldersNode, doc);
 doc.writeln('</table>');

 if (foldersNode.length > 4 && foldersNode[0]){ //there are sub-nodes and the folder is open
  if (!foldersNode[2]){ //for folders with folders
   level++;
   for (i=4; i<foldersNode.length;i++)
    if (i==foldersNode.length-1)
     redrawNode(foldersNode[i], doc, level, 1, leftSide)
    else
     redrawNode(foldersNode[i], doc, level, 0, leftSide)
  }else{ //for folders with documents
   for (i=4; i<foldersNode.length;i++){
    doc.write('<table border="0" cellspacing="0" cellpadding="0">')
    doc.write('<tr><td nowrap>')
    doc.write(leftSide)
    if (i==foldersNode.length - 1)
     doc.write('<img src="/images/lastnode.gif" width="16" height="20">')
    else
     doc.write('<img src="/images/node.gif" width="16" height="20">')
    position=foldersNode[i].indexOf(activeBranch,1)
    if (0<position && activeBranch!="")
     doc.write(foldersNode[i].substring(0, position)+'<b>'+activeBranch+'</b>')
    else
     doc.write(foldersNode[i])
    doc.writeln('</table>')
   }
  }
 }
}

//builds the html code to display a folder and its label
function displayIconAndLabel(foldersNode, doc){
 doc.write("<a href='javascript:top.openBranch(\"" + foldersNode[3] + "\")'><img src='/images/");
 if (foldersNode[1])
  doc.write("openfolder.gif' alt='schließen'")
 else
  doc.write("closedfolder.gif' alt='öffnen'");
 doc.write(" width='24' height='20' border='0'></a><td valign='middle' nowrap>");
 if (foldersNode[3]==activeBranch)
  doc.write("<b>");
 doc.write("<a href='/");
 if (foldersNode[3]=="Ausrüstung")
  doc.write("Outfit")
 else{
  if (foldersNode[3]=="über uns")
   doc.write("Impressum")
  else
   doc.write(foldersNode[3]);
 }
 doc.write(".html' class='f'>"+foldersNode[3]+"</a>");
 if (foldersNode[3]==activeBranch)
  doc.write("</b>");
}

//********************
// Recursive functions

//when a parent is closed all children also are
function closeFolders(foldersNode){
 var i=0
 if (!foldersNode[2]){
  for (i=4; i < foldersNode.length; i++)
   closeFolders(foldersNode[i])
 }
 foldersNode[0]=0
 foldersNode[1]=0
}

//recursive over the tree structure (called by openbranch)
function clickOnFolderRec(foldersNode, folderName){
 var i=0
 if (foldersNode[3]==folderName){
  if (foldersNode[0])
   closeFolders(foldersNode)
  else{
   foldersNode[0]=1
   foldersNode[1]=1
  }
 }else{
  if (!foldersNode[2])
   for (i=4; i< foldersNode.length; i++)
    clickOnFolderRec(foldersNode[i], folderName)
 }
}

// **************
// Event handlers

//called when user clicks on folder
function openBranch(branchName){
 if (!semaphor){
  semaphor++
  clickOnFolderRec(foldersTree, branchName)
  redrawTree()
  semaphor--
 }
}

//called after index.html is loaded
function initializeTree(){
 generateTitle();
 generateTree();
 ready=1;
}

//new function: (c) 1999 M. C. Hoeschen / Keep this copyright notice,
//or you are NOT allowed to use the enhanced version of TreeView.

//make sure the active branch is visible (called from other frame)
function showBranch(branchName){
 if (!semaphor && ready){
  semaphor++;

  if (branchName!="" && branchName!=activeBranch){
   activeBranch=branchName;
   findNode(foldersTree);
   redrawTree();
  }
  semaphor--;
 }else
  timeOutId=setTimeout("showBranch('"+branchName+"')",100);
}

//new function: (c) 1999 M. C. Hoeschen / Keep this copyright notice,
//or you are NOT allowed to use the enhanced version of TreeView.

//recursive over tree structure, find Node & open all parent folders
function findNode(foldersNode){
 var i=0, found=0;

 if (foldersNode[3]==activeBranch)
  found=1
 else
  for (i=4; i < foldersNode.length; i++)
   if (foldersNode[2]){                                //children are documents
    position=foldersNode[i].indexOf(activeBranch,1)
    if (0<position)
     found=1
   }else                                                //children are folders
    if (findNode(foldersNode[i]) == 1)
     found=1
 if (found){
  foldersNode[0]=1
  foldersNode[1]=1
 }
 return found;
}

var semaphor=0, ready=0, foldersTree=0, timeOutId=0, activeBranch="?";
// generateTree() //sometimes when the user reloads the document Netscape 3.01 does not trigger the onLoad event (!!)

//create title-frame:
function generateTitle(){
 var doc=top.titleFrame.window.document;
 doc.open("text/html", "replace");
 doc.write('<html><head><title>bikefreaks</title>\
<style type="text/css">div {position:absolute; left:9; top:8}</style></head><body bgcolor="#FFFFFF">\
<table border="0" cellspacing="0" cellpadding="0" height="100%" width="100%"><tr><td width="279" valign="top" nowrap>');
 if (document.layers)
  doc.write('<layer left="0" top="0" width="279" height="80" background="/images/treeTop.gif"></layer>')
 else
  doc.write('<img src="/images/treeTop.gif" width="279" height="80">');
 doc.write('</td><td width="100%" align="center" valign="top"><a href="/bikefreaks.php" target="mainFrame">\
<img src="/images/bikefreaks.gif" height="64" width="288" border="0" alt="www.bikefreaks.de"></a></td>\
<td><img src="/images/top_right.jpg" height="80" width="100"></td></tr></table>\
<div id="oben"><img name="obenI" src="/images/top_bike.gif" height="52" width="158"></div>\
</body></html>');
 doc.close();
}

//replace small image in title-frame:
function replaceImage(newImage){
 var doc=parent.titleFrame.document;
 if (doc.title == "bikefreaks"){  //check, if title is already loaded
  if (document.getElementById)
   doc.obenI.src="/images/"+newImage
  else
   if (!document.layers){
    if (doc.images[3].src.search(newImage) == -1)
     doc.images[3].src="/images/"+newImage;
   }else
    if (doc.oben.document.images[0].src.search(newImage) == -1){
     doc.oben.document.open();
     doc.oben.document.write('<img src="/images/'+newImage+'" height="52" width="158">');
     doc.oben.document.close();
    }
 }
}

// JS-Errorhandler
// (c) 2000 M.C. Hoeschen [maze@bikefreaks.de]

function reportError(msg,url,line){
 par_url = parent.location.href;
 par_url = (par_url == location.href) ? "no parent" : par_url;
 query_string = "http://www.bikefreaks.de/cgi-bin/errorDocs/js_error.pl?"
 + "url=" + escape(location.href)
 + "&par=" + escape(par_url)
 + "&ref=" + escape(document.referrer)
 + "&bro=" + escape(navigator.appName + " " + navigator.appVersion);
 if (navigator.appName=="Netscape")
  query_string += "&siz=" + escape("InnerWidth: "+innerWidth+" InnerHeight: "+innerHeight)
 if (navigator.appName=="Microsoft Internet Explorer")
  query_string += "&siz=" + escape("InnerWidth: "+document.body.offsetWidth+" InnerHeight: "+document.body.offsetHeight)
 query_string += "&lan=" + escape(navigator.language)
 + "&msg=" + escape(msg)
 + "&lin=" + escape(line)
 + "&cou=" + (++jserror_count);
 if (document.images){
  reporter=new Image();
  reporter.src = query_string;
  if (jserror_count<2)
   alert('Leider ist ein JavaScript-Fehler aufgetreten.\nDie bikefreaks wurden automatisch über diesen Fehler informiert.');
 }
 return true;
}
jserror_count = 0;
window.onerror = reportError;

// Back-In-Frameset function:
var file=self.location.search;
if (file != "")
 file = "/" + file.substring(1,file.length);
