var ie=true;
//var theTree;
function TreeView(doc,title,ref,eventHandler,rootname){
  if(rootname==null)
      rootname="root";
  this.rootNode=new TreeNode(doc,title,ref,"nodeRoot",rootname);
  if(navigator.appName=="Netscape")  // Safari also currently has 'Netscape' in its name string
    ie=false;
  if (ie){
    this.rootNode.node.attachEvent("onclick",this.doClick);
    this.rootNode.node.attachEvent("onmouseover",this.doHover);
    this.rootNode.node.attachEvent("onmouseout",this.doUnHover);
    }
  else{
    this.rootNode.node.addEventListener("click",this.doClick,false);
    this.rootNode.node.addEventListener("mouseover",this.doHover,false);
    this.rootNode.node.addEventListener("mouseout",this.doUnHover,false);
    }
  this.rootNode.tree=this;
  this.nodeCount=0;
  this.currentNode=this.rootNode;
  this.treeNodes=new Array();
  this.treeNodes.push(this.rootNode);
  this.evtH=eventHandler;
//  theTree=this;
}

TreeView.prototype.getNode=function(id){
  for(var n=0;n<this.treeNodes.length;n++){
    if(this.treeNodes[n].node.id==id)
      return(this.treeNodes[n]);
  }
  return null;
}

TreeView.prototype.addNode=function(toNode,node){
  node.tree=toNode.tree;
  toNode.children.appendChild(node.node);
  this.treeNodes.push(node);
}

TreeView.prototype.doClick=function(e){
  var tgt;
  if (ie){
    tgt=window.event.srcElement;
  }
  else{
    tgt=e.target;
  }
  if(tgt.tagName=="U")
    tgt=tgt.parentNode;
  var pn=tgt.parentNode;
  if(tgt.tagName=="DIV")
    pn=tgt;
  var nd=pn.childNodes[2];
  if(nd.parentNode.owner.ndType!="leaf"){
    if(nd.className=="show"){
        nd.className="hide"
        pn.childNodes[0].src="images/ps.png";
    }
    else if(nd.className=="hide"){
        nd.className="show"
        pn.childNodes[0].src="images/po.png";
    }
  }
  if(pn.owner.tree.currentNode.node.childNodes[1].className!="tocroot")    
    pn.owner.tree.currentNode.node.childNodes[1].className="toctitle"    
  pn.owner.tree.currentNode=pn.owner;
  if(pn.className!="tocroot" && pn.childNodes[1].className!="tocroot")
    pn.childNodes[1].className="current"
  if(pn.owner.tree.evtH!=null && tgt.tagName!="IMG")
    pn.owner.tree.evtH(tgt);
}

var bgc="";
TreeView.prototype.doHover=function(e){
  var tgt;
  if (ie){
    tgt=window.event.srcElement;
  }
  else{
    tgt=e.target;
  }
  var pn=tgt.parentNode;
  if(tgt.tagName=="DIV")
    pn=tgt;
   bgc=pn.style.backgroundColor;
   if(pn.id.indexOf("area")==-1)
   pn.style.backgroundColor="#d99c52";
}
TreeView.prototype.doUnHover=function(e){
  var tgt;
  if (ie){
    tgt=window.event.srcElement;
  }
  else{
    tgt=e.target;
  }
  var pn=tgt.parentNode;
  if(tgt.tagName=="DIV")
    pn=tgt;
   if(pn.id.indexOf("area")==-1)
   pn.style.backgroundColor=bgc;
}

function TreeNode(doc,title,ref,nodeType,id){
  this.tree=null;
  this.ndType=nodeType;
  this.ref=ref;
  this.node=doc.createElement("div");
  this.node.id=id;
  this.node.owner=this;
  this.icon=document.createElement("img");
  this.icon.width=12;
  this.icon.height=12;
  if(nodeType=="nodeOpen" || nodeType=="nodeRoot")
    this.icon.src="images/po.png";
  else if(nodeType=="nodeClosed")
    this.icon.src="images/ps.png";
  else
    this.icon.src="images/tr.png";
  this.node.appendChild(this.icon);
  if(true) // originally if ie
    this.title=doc.createElement("span");
  else
    this.title=doc.createElement("button");
  if(nodeType=="nodeRoot")
    this.title.className="tocroot";
  else
    this.title.className="toctitle";
  this.title.innerHTML=title;
  if(title.indexOf("<u>")==0)
    this.title.style.cursor="pointer";
  this.node.appendChild(this.title);
  this.children=doc.createElement("div");
  this.node.appendChild(this.children);
  if(nodeType=="nodeOpen" || nodeType=="nodeRoot")
    this.children.className="show";
  else
    this.children.className="hide";
}



