/*----------------------------------------------------|
  | dTree 2.05 | www.destroydrop.com/javascript/tree/ |
  |---------------------------------------------------|
  | Copyright (c) 2002-2003 Geir Landro               |
  |                                                   |
  | This script can be used freely as long as all     |
  | copyright messages are intact.                    |
  |                                                   |
  | Updated: 17.04.2003                               |
  |---------------------------------------------------|
  | Modified for Dokuwiki by                          |
  | Samuele Tognini <samuele@cli.di.unipi.it>         |
  | under GPL 2 license                               |
  | (http://www.gnu.org/licenses/gpl.html)            | 
  | Updated: 25.01.2006                               |
  |---------------------------------------------------|
  | indexmenu  | wiki.splitbrain.org/plugin:indexmenu |
  |--------------------------------------------------*/
function Node(dokuid,id,pid,name,url,title,icon,iconOpen,open){this.dokuid=dokuid;this.id=id;this.pid=pid;this.name=name;this.url=url;this.title=title||name;this.icon=icon;this.iconOpen=iconOpen;this._io=open||false;this._is=false;this._ls=false;this._hc=false;this._ai=0;this._p=false;this._lv=0;this._ok=false;this._cp=false;}function dTree(objName,objImg){var objExt=this.findExt(objImg);this.config ={useCookies:true,scroll:true,max:1};this.icon ={root:objImg+'base.'+objExt,folder:objImg+'folder.'+objExt,folderH:objImg+'folderh.'+objExt,folderOpen:objImg+'folderopen.'+objExt,folderHOpen:objImg+'folderhopen.'+objExt,node:objImg+'page.'+objExt,empty:objImg+'empty.'+objExt,line:objImg+'line.'+objExt,join:objImg+'join.'+objExt,joinBottom:objImg+'joinbottom.'+objExt,plus:objImg+'plus.'+objExt,plusBottom:objImg+'plusbottom.'+objExt,minus:objImg+'minus.'+objExt,minusBottom:objImg+'minusbottom.'+objExt,nlPlus:objImg+'nolines_plus.'+objExt,nlMinus:objImg+'nolines_minus.'+objExt};this.obj=objName;this.aNodes=[];this.aIndent=[];this.root=new Node(false,-1);this.selectedNode=null;this.selectedFound=false;this.completed=false;this.scrllTmr=0;this.pageid=window.indexmenu_ID||'';}dTree.prototype.add=function(dokuid,id,pid,name,url,title,icon,iconOpen,open){this.aNodes[this.aNodes.length]=new Node(dokuid,id,pid,name,url,title,icon,iconOpen,open);};dTree.prototype.openAll=function(){if(!this.getCookie('co'+this.obj)){this.oAll(true);}};dTree.prototype.toString=function(){var str= '';if(this.config.scroll){str += '<div id="cdtree_'+this.obj+'" style="position:relative;overflow:hidden;width:100%;">\n';}str += '<div id="dtree_'+this.obj+'" class="dtree" style="overflow:';if(this.config.scroll){ str += 'visible;position:relative;width:100%"';}else{str += 'hidden;"';}str += '>\n';if(document.getElementById){if(this.config.useCookies){ this.selectedNode=this.getSelected();}str += this.addNode(this.root);}else{str += 'Browser not supported.';}str += '</div>';if(this.config.scroll){str += '<div id="left_'+this.obj+'" style="display:none;position:absolute;filter:alpha(opacity=60);-moz-opacity:.60;opacity:.60;"><img src="'+DOKU_BASE+'lib/plugins/indexmenu/images/scrollleft.gif" alt="Scroll" onMouseOver="javascript:'+this.obj+'.scroll(\'right\',2);" onMouseOut="javascript:'+this.obj+'.stopscroll()" onMouseDown="javascript:'+this.obj+'.scroll(\'right\',6);" onMouseUp="javascript:'+this.obj+'.scroll(\'right\',2);"></div>\n';str += '</div>';}this.completed=true;var nojs=document.getElementById('nojs_'+this.obj);if(nojs){nojs.style.display="none";}return str;};dTree.prototype.addNode=function(pNode){var str='';var n=pNode._ai;var l=pNode._lv+1;for(n;n<this.aNodes.length;n++){if(this.aNodes[n].pid==pNode.id){var cn=this.aNodes[n];cn._p=pNode;cn._ai=n;cn._lv=l;this.setCS(cn);if(cn._hc&&!cn._io&&this.config.useCookies){cn._io=this.isOpen(cn.id);}if(cn.dokuid==this.pageid){cn._cp=true;}else 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;};dTree.prototype.node=function(node,nodeId){var h=false;if(node._lv>this.config.max){node._io=false;h=true;}else{node._ok=true;}var str='<div class="dTreeNode">'+this.indent(node,nodeId);if(!node.icon){node.icon =(this.root.id==node.pid)? this.icon.root:((node._hc)?((!node.url)? this.icon.folder:this.icon.folderH):this.icon.node);}if(!node.iconOpen){node.iconOpen =(node._hc)?((!node.url)? this.icon.folderOpen:this.icon.folderHOpen):this.icon.node;}if(this.root.id==node.pid){node.icon=this.icon.root;node.iconOpen=this.icon.root;}str += '<img id="i'+this.obj+nodeId+'" src="' +((node._io)? node.iconOpen:node.icon)+ '" alt="" />';if(node.url){str += '<a id="s'+this.obj+nodeId+'" class="' +((node._cp)? 'navSel':((node._is)? 'nodeSel':(node._hc)? 'nodeFdUrl':'nodeUrl'));str += '" href="'+node.url+'"'+' title="'+node.title+'"';if(this.config.scroll){str += ' onmouseover="'+this.obj+'.scroll(\'left\',\''+nodeId+'\');" ';}str += ' onclick="javascript:'+this.obj+'.s('+nodeId+');"';str += '>';}else if(!node.url&&node._hc&&node.pid!=this.root.id){str += '<a id="s'+this.obj+nodeId+'" href="javascript:'+this.obj+'.o('+nodeId+');" class="node"';if(this.config.scroll){ str +='onmouseover="'+this.obj+'.scroll(\'left\',\''+nodeId+'\');"';}str +='>';}str += node.name;if(node.url ||(!node.url&&node._hc)){str += '</a>';}str += '</div>';if(node._hc){str += '<div id="d'+this.obj+nodeId+'" class="clip" style="display:' +((this.root.id==node.pid||node._io)? 'block':'none')+ ';">';if(!h){str += this.addNode(node);}str += '</div>';}this.aIndent.pop();return str;};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 )+ '" alt="" />';}if(node._ls){this.aIndent.push(0);}else{this.aIndent.push(1);}if(node._hc){str += '<a href="javascript:'+this.obj+'.o('+nodeId+');"><img id="j'+this.obj+nodeId+'" src="';str +=((node._io)?((node._ls)? this.icon.minusBottom:this.icon.minus):((node._ls)? this.icon.plusBottom:this.icon.plus ));str += '" alt="" /></a>';}else{str += '<img src="' +((node._ls)? this.icon.joinBottom:this.icon.join)+ '" alt="" />';}}return str;};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.getSelected=function(){var sn=this.getCookie('cs'+this.obj);return(sn)? sn:null;};dTree.prototype.s=function(id){var cn=this.aNodes[id];if(this.selectedNode!=id){var eNew=document.getElementById("s"+this.obj+id);if(!eNew ){return;}if(this.selectedNode||this.selectedNode===0){eOld=document.getElementById("s"+this.obj+this.selectedNode);eOld.className="node";}eNew.className="nodeSel";this.selectedNode=id;if(this.config.useCookies){this.setCookie('cs'+this.obj,cn.id);}}};dTree.prototype.o=function(id){var cn=this.aNodes[id];this.nodeStatus(!cn._io,id,cn._ls);cn._io=!cn._io;if(this.config.useCookies){this.updateCookie();}};dTree.prototype.oAll=function(status){for(var n=0;n<this.aNodes.length;n++){if(this.aNodes[n]._hc&&this.aNodes[n].pid!=this.root.id){this.nodeStatus(status,n,this.aNodes[n]._ls);this.aNodes[n]._io=status;}}if(this.config.useCookies){this.updateCookie();}};dTree.prototype.openTo=function(nId,bSelect,bFirst){if(!bFirst){for(var n=0;n<this.aNodes.length;n++){if(this.aNodes[n].id==nId){nId=n;break;}}}this.fill(this.aNodes[nId].pid);var cn=this.aNodes[nId];if(cn.pid==this.root.id||!cn._p){return;}cn._io=true;cn._is=bSelect;if(this.completed&&cn._hc){this.nodeStatus(true,cn._ai,cn._ls);}if(this.completed&&bSelect){this.s(cn._ai);}else if(bSelect){this._sn=cn._ai;}this.openTo(cn._p._ai,false,true);};dTree.prototype.getOpenTo=function(nodes){if(nodes===''){this.openAll();}else if(!this.config.useCookies ||!this.getCookie('co'+this.obj)){var anodes=nodes.split(',');for(var n=0;n<anodes.length;n++){this.openTo(anodes[n],false,true);}}};dTree.prototype.nodeStatus=function(status,id,bottom){if(status&&!this.fill(id)){return;}var eDiv= document.getElementById('d'+this.obj+id);var eJoin= document.getElementById('j'+this.obj+id);var eIcon= document.getElementById('i'+this.obj+id);eIcon.src =(status)? this.aNodes[id].iconOpen:this.aNodes[id].icon;eJoin.src =((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus));eDiv.style.display =(status)? 'block':'none';};dTree.prototype.clearCookie=function(){var now=new Date();var yesterday=new Date(now.getTime()- 1000*60*60*24);this.setCookie('co'+this.obj,'cookieValue',yesterday);this.setCookie('cs'+this.obj,'cookieValue',yesterday);};dTree.prototype.setCookie=function(cookieName,cookieValue,expires,path,domain,secure){document.cookie =escape(cookieName)+ '='+escape(cookieValue)+(expires?';expires='+expires.toGMTString():'')+';path=/' +(domain?';domain='+domain:'')+(secure?';secure':'');};dTree.prototype.getCookie=function(cookieName){var cookieValue='';var posName=document.cookie.indexOf(escape(cookieName)+ '=');if(posName!=-1){var posValue=posName +(escape(cookieName)+ '=').length;var endPos=document.cookie.indexOf(';',posValue);if(endPos!=-1){cookieValue=unescape(document.cookie.substring(posValue,endPos));}else{cookieValue=unescape(document.cookie.substring(posValue));}}return(cookieValue);};dTree.prototype.updateCookie=function(){var str='';for(var n=0;n<this.aNodes.length;n++){if(this.aNodes[n]._io&&this.aNodes[n].pid!=this.root.id){if(str){str += '.';}str += this.aNodes[n].id;}}this.setCookie('co'+this.obj,str);};dTree.prototype.isOpen=function(id){var aOpen=this.getCookie('co'+this.obj).split('.');for(var n=0;n<aOpen.length;n++){if(aOpen[n]==id){return true;}}return false;};dTree.prototype.openCurNS=function(){var cn;var cns=this.pageid;for(var n=0;n<this.aNodes.length;n++){cn=this.aNodes[n];if(cn.dokuid==cns){this.openTo(cn.id,false,true);break;}}};dTree.prototype.fill=function(id){if(id==-1||this.aNodes[id]._ok ){return true;}var a;var n=id;var rd=[];var eDiv;while(!this.aNodes[n]._ok){rd[rd.length]=n;n=this.aNodes[n].pid;}for(var ln=rd.length-1;ln>=0;ln--){id=rd[ln];a=this.aNodes[id];eDiv=document.getElementById('d'+this.obj+id);if(!eDiv){return false;}this.aIndent=[];n=a;while(n.pid>=0){if(n._ls){this.aIndent.unshift(0);}else{this.aIndent.unshift(1);}n=n._p;}eDiv.innerHTML=this.addNode(a);a._ok=true;}return true;};dTree.prototype.openCookies=function(){var aOpen=this.getCookie('co'+this.obj).split('.');for(var n=0;n<aOpen.length;n++){if(aOpen[n]===""){break;}var cn=this.aNodes[aOpen[n]];if(!cn._ok){this.nodeStatus(true,aOpen[n],cn._ls);cn._io=true;}}};dTree.prototype.scroll=function(where,n){var w;var dtree=document.getElementById('dtree_'+this.obj);var cdtree=document.getElementById('cdtree_'+this.obj);this.stopscroll();if(where=="right"){this.scrollRight(parseInt(dtree.offsetLeft,0),n);}else{var nodeId=document.getElementById('s'+this.obj+n);if(nodeId.offsetWidth+nodeId.offsetLeft<=cdtree.offsetWidth+Math.abs(dtree.offsetLeft)){return;}if(nodeId.offsetLeft<55){return;}w=parseInt(nodeId.offsetLeft,0);this.scrollLeft(parseInt(dtree.offsetLeft,0),w);var left=document.getElementById('left_'+this.obj);if(left.style.display=="block"){return;}left.style.top=dtree.offsetHeight/2-left.firstChild.height/2+"px";left.style.display="block";}};dTree.prototype.scrollLeft=function(lft,w){if(lft<54-w){this.scrllTmr=0;return;}var self=this;document.getElementById('dtree_'+self.obj).style.left=lft+"px";this.scrllTmr=setTimeout(function(){self.scrollLeft(lft-4,w);},20);};dTree.prototype.scrollRight=function(lft,speed){var self=this;var dtree=document.getElementById('dtree_'+self.obj);if(dtree.offsetLeft>0){document.getElementById('left_'+this.obj).style.display="none";this.scrllTmr=0;return;}dtree.style.left=lft+"px";this.scrllTmr=setTimeout(function(){self.scrollRight((lft+speed >0)? lft+1:lft+speed,speed);},10);};dTree.prototype.stopscroll=function(){clearTimeout(this.scrllTmr);this.scrllTmr=0;};dTree.prototype.findExt=function(path){var ext="gif";var cext=path.lastIndexOf(".");if( cext>-1){cext++;cext=path.substring(cext,path.length-1).toLowerCase();if((cext=="png")||(cext=="jpg")){ext=cext;}}return ext;};if(!Array.prototype.push){Array.prototype.push=function array_push(){for(var i=0;i<arguments.length;i++){this[this.length]=arguments[i];}return this.length;};}if(!Array.prototype.pop){Array.prototype.pop=function array_pop(){var lastElement=this[this.length-1];this.length=Math.max(this.length-1,0);return lastElement;};}
