﻿/*----------------------------------------------------------------------------------------+
| dTree 2.05   www.destroydrop.com/javascript/tree/   Copyright (c) 2002-2003 Geir Landrö |
| This script can be used freely as long as all copyright messages are intact.            |
| This script has been updated for MyPitSelf                                              |
|----------------------------------------------------------------------------------------*/
//================================================ Node object
function Node(id,pid,name,text){
 this.id=     id;
 this.pid=    pid;
 this.name=   name;
 this.text=   text;
 this._io=    false;
 this._is=    false;
 this._ls=    false;
 this._hc=    false;
 this._ai=    0;
 this._p;
};
//================================================ Tree object
function dTree(objName,current){
 this.icon={
  empty:       'dtre0.gif',
  line:        'dtre1.gif',
  join:        'dtre2.gif',
  joinBottom:  'dtre3.gif',
  plus:        'dtre4.gif',
  plusBottom:  'dtre5.gif',
  minus:       'dtre6.gif',
  minusBottom: 'dtre7.gif',
  beep:        'bip-tree.gif'
 };
 this.obj=           objName;
 this.current=       current;
 this.isLoaded=      0;
 this.aNodes=        [];
 this.aIndent=       [];
 this.defer=         false;
 this.subload=       false;
 this.root=          new Node(-1);
 this.selectedNode=  null;
 this.selectedFound= false;
 this.completed=     false;
 this.list=          '';
 this.upperParent=objName.substr(6);
};
//================================================ Adds a new node to the node array
dTree.prototype.add = function(id,pid,name,text){
 if((this.defer&&!this.subload)||(!this.defer&&this.subload)){
  this.aNodes[this.aNodes.length]=new Node(id,pid,name,text);
 }
}
//================================================
dTree.prototype.emptyNodes=function(){
 this.aNodes=[];
 this.aIndent=[];
}
//================================================
dTree.prototype.setUndefer=function(){
 this.defer=false;
}
//================================================
dTree.prototype.setDefer=function(){
 this.defer=true;
}
//================================================
dTree.prototype.setSubload=function(){
 this.subload=true;
}
//================================================
dTree.prototype.SetLoaded=function(){
 this.isLoaded=1;
}
//================================================ Outputs the tree to the page
dTree.prototype.toString=function(){
 var str='<br />';
 if(this.aNodes.length>2){
  str+='<div class="zz_list">\n';
  if(document.getElementById){
   str+=this.addNode(this.root);
  }else{
   str+='Browser not supported.';
  }
  str+='</div>';
 }
 str+='<br /><br />';
 if(!this.selectedFound) this.selectedNode=null;
 this.completed=true;
 return str;
};
//================================================ Creates the tree structure
dTree.prototype.addNode = function(pNode){
 var str = '';
 var n=0;
 var max=this.aNodes.length;
 for(n=0;n<max;n++){
  if(this.aNodes[n].pid==pNode.id){
   var cn= this.aNodes[n];
   cn._p=pNode;
   cn._ai= n;
   this.setCS(cn);
   if(cn.id==this.selectedNode&&!this.selectedFound){
    cn._is = true;
    this.selectedNode=n;
    this.selectedFound=true;
   }
   str+=this.node(cn,n);
   if(cn._ls) break;
  }
 }
 return str;
};

//================================================ show Text
dTree.prototype.removeOlfBlink=function(old,ne){
 var t=this;
 var n='';
 try{
  var x=document.getElementById('tree_'+this.obj+'_'+old);
  var p=x.innerHTML.indexOf(this.icon.beep);
  if(p>=0){
   for(var i=p;i>0;i--){
    if(x.innerHTML.substr(i,1)=='<') break;
   }
   n=x.innerHTML.substr(0,i-1);
   x.innerHTML=n;
  }
 }catch(e){};
 try{
  var y=document.getElementById('tree_'+this.obj+'_'+ne);
  y.innerHTML = y.innerHTML + '<img src="' + this.icon.beep + '" />';
 }catch(e){};
}

//================================================ show Text
dTree.prototype.showText1=function(nodeId,parentNode){
 var x=String(document.location);
 if(x.toLowerCase().indexOf('www.mypitself.com')>0||x.toLowerCase().indexOf('/webmps/')>0){
  var y=x.indexOf('&sub=');
  if(y>=0){
   x=x.substr(0,y);
  }
  x=x+'&sub='+nodeId;
  document.location=x;
 }else{
  this.showText(nodeId);
 }
}
//================================================ show Text
dTree.prototype.showText=function(nodeId){
 var old=this.current;
 var n=this.aNodes.length-1;
 if(n>1){
  var fait=0;
  for(n=n;n>=0;n--){
   if(this.aNodes[n].id==nodeId){
    if(this.aNodes[n].text==''){
     var max=this.aNodes.length;
     for(var m=0;m<max;m++){
      if(this.aNodes[m].pid==nodeId){
       this.showText(this.aNodes[m].id);
       break;
      }
     }
     break;
    }else{
     document.getElementById('Textbox').innerHTML='<p style="text-align:justify;">&nbsp;<img src="my1515.gif" /> <span class="zz_echo">'+this.aNodes[n].name+'</span><br /><br />\n'+this.aNodes[n].text+'\n</p>';
     fait=this.aNodes[n].id;
     break;
    }
   }
  }
  if(fait){
   document.getElementById('x3tree'+this.obj+this.current).className='LIST';
   document.getElementById('x3tree'+this.obj+fait).className='ECHO';
   this.current=fait;
   this.removeOlfBlink(old,nodeId);
  }
 }
}
//================================================ Creates the node icon, and text
dTree.prototype.node = function(node,nodeId){
 var str = '<div class="zz_list" id="tree_'+this.obj+'_'+node.id+'">' + this.indent(node, nodeId);
// str+='<a class="zz_list" href="javascript:'+this.obj+'.showText('+node.id+');" style="border:0;padding:0;">';
 str+='<a class="zz_list" href="javascript:'+this.obj+'.showText1('+node.id+','+this.upperParent+');" style="border:0;padding:0;">';
 str += '<span id="x3tree'+this.obj+node.id+'" class="';
 if(this.current==node.id){
  str+= 'zz_echo';
 }else{
  str+= 'zz_list';
 }
 str+='" style="padding:0px;">';
 str+=node.name.replace(/&lt;/g,'<').replace(/&gt;/g,'>');
 str+='</span>';
 str+='</a>';
 if(node.pid==-1&&this.aNodes.length>15)  str+=' <a style="border:0;padding:0;" href="javascript:'+this.obj+'.fold();"><img src="au.gif" /></a> <a style="border:0;padding:0;" href="javascript:'+this.obj+'.unfold();"><img src="ad.gif" /></a>';
 str+=' </div>';
 if(node._hc){
  node._io=true;
  str+='<div id="d'+this.obj+nodeId+'" class="zz_list" style="display:block;">';
  str+=this.addNode(node);
  str+='</div>';
 }
 this.aIndent.pop();
 return str;
};
//================================================ fold all
dTree.prototype.fold=function(){
 var max=this.aNodes.length;
 var cn=null;
 for(var n=0;n<max;n++){
  cn=this.aNodes[n];
  if(cn.pid>0&&cn._io) this.o(n);
 }
};
//================================================ unfold all
dTree.prototype.unfold=function(){
 var max=this.aNodes.length;
 var cn=null;
 for(var n=0;n<max;n++){
  cn=this.aNodes[n];
  if(cn.pid>0&&cn._hc&&!cn._io) this.o(n);
 }
};
//================================================ Adds the empty and line icons
dTree.prototype.indent=function(node,nodeId){
 var str = '';
 if(this.root.id!=node.pid){
  for(var n=0; n<this.aIndent.length; n++) 
   str+='<img src="'+((this.aIndent[n]==1)?this.icon.line:this.icon.empty)+'" style="vertical-align:top;" />';
  (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
  if(node._hc){
   str+='<a id="x1tree'+this.obj+nodeId+'" class="zz_list" href="javascript:'+this.obj+'.o('+nodeId+');" style="border:0;padding:0;">';
   str+='<img id="x2tree'+this.obj+nodeId+'" src="'+(node._ls?this.icon.minusBottom:this.icon.minus)+'" style="vertical-align:top;" />';
   str+='</a>';
  }else{
   str += '<img src="'+((node._ls)?this.icon.joinBottom:this.icon.join)+'" style="vertical-align:top;" />';
  }
 }
 return str;
};
//================================================ Toggle Open or close
dTree.prototype.o=function(id){
 var cn=this.aNodes[id];
 this.nodeStatus(!cn._io, id, cn._ls,cn);
 cn._io=!cn._io;
};
//================================================ Checks if a node has any children and if it is the last sibling
dTree.prototype.setCS=function(node){
 var lastId;
 for(var n=0;n<this.aNodes.length;n++){
  if(this.aNodes[n].pid==node.id) node._hc=true;
  if(this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
 }
 if(lastId==node.id) node._ls=true;
};
//================================================ 
dTree.prototype.nodeStatus=function(status,id,bottom,cn){
 eDiv=document.getElementById('d'+this.obj+id);
 eJoin=document.getElementById('x2tree'+this.obj+id);
 eJoin.src=status?(cn._ls?this.icon.minusBottom:this.icon.minus):(cn._ls?this.icon.plusBottom:this.icon.plus);
 eDiv.style.display=(status)?'block':'none';
};

