var trees=[];
var images=[
[177,"<img src=pics/trans.gif width=19 height=16 border=0 align=texttop>"],
[195+"+","<a href=JavaScript:trees[$index].toggle([$iam])><img src=pics/img-plus-cont.gif width=19 height=16 border=0 align=texttop></a>"],
[195+"-","<a href=JavaScript:trees[$index].toggle([$iam])><img src=pics/img-minus-cont.gif width=19 height=16 border=0 align=texttop></a>"],
[195,"<img src=pics/img-branch-cont.gif width=19 height=16 border=0 align=texttop>"],
[179,"<img src=pics/img-vert-line.gif width=19 height=16 border=0 align=texttop>"],
[192+"+","<a href=JavaScript:trees[$index].toggle([$iam])><img src=pics/img-plus-end.gif width=19 height=16 border=0 align=texttop></a>"],
[192+"-","<a href=JavaScript:trees[$index].toggle([$iam])><img src=pics/img-minus-end.gif width=19 height=16 border=0 align=texttop></a>"],
[192,"<img src=pics/img-branch-end.gif width=19 height=16 border=0 align=texttop>"],
["!",""]];

var defaulticons=[
"pics/folderClosed.gif",
"pics/folderOpen.gif",
"pics/leaf.gif",
"pics/leaf_u.gif",
"icons/control_pause.png",
"icons/control_play.png",
"icons/control_eject.png"];
var addanchor = "<img src=pics/img-branch-end.gif width=19 height=16 border=0 align=texttop><a href=$url class=TreeAddItem>add</a></div>";

function Replace(s,replacethis,withthis){
	var a=s.split(replacethis);
	return a.join(withthis);
}

// Menu:
//  [title,items]
// Items:
//  [title,icon,url,target,submenu]]
//

function Tree(title,items){
	this.defaulturl="";
	this.title=title;
	this.drag=false;
	this.allowadd = true;
	this.allowadd = false;
	this.index=trees.length;
	this.name='tree'+"_"+Replace(this.title," ","_");
	trees[trees.length]=this;
	
	this.expanded=[];
	
	this.items=(items)?items:[];
	this.savedmousedownhandler=null;
	
	this.mousedown=function(e){
		if (!e) e = window.event;
		//if((!e)||(e.button!=2))return;
		
		var x, y;
		if (window.opera) {
			x = e.clientX;
			y = e.clientY;
		} else if (document.all) {
			x = document.body.scrollLeft + event.clientX;
			y = document.body.scrollTop + event.clientY;
		} else if (document.layers || document.getElementById) {
			x = e.pageX;
			y = e.pageY;
		}
		

		
      layer=document.createElement('div');
      document.body.appendChild(layer);   
      
      
      layer.style.position = 'absolute';
      
      
      
      layer.style.left = x;
      layer.style.top = y;    
      layer.style.width = 150;
      layer.style.height = 200;
	  layer.style.backgroundColor="red";

	  
	  this.savedmousedownhandler=document.onmousedown;
	  document.onmousedown=function(){
	  }
		
				e.cancelBubble = true;
				if (e.stopPropagation) e.stopPropagation();
				return false;
	
	}


	this._draw=function(items,level,toggle,parent){
		var s="";
	

		for(var i=0;i<items.length;i++){
			var iam=toggle+((toggle!="")?",":"")+i;
			var islast=(i==items.length-1)&&(!this.allowadd);
			var h=new String(level+(islast?192:195));
			var url=(items[i].url)?items[i].url:"";
			var expanded=items[i].title.indexOf("!")==0;
			var haschildren=items[i].submenu!=null;

			if(items[i].icon){
				var icons=items[i].icon.split(",");
				var icon=((expanded)?icons[icons.length-1]:icons[0]);
			}
			else {
				var icon=(haschildren)?((expanded)?defaulticons[1]:defaulticons[0]):((expanded)?defaulticons[3]:defaulticons[2]);
			}
			if(icon!="")icon="<img src="+icon+" width=16 height=16 border=0 align=texttop>";


			h+=((haschildren)?((expanded)?"-":"+"):"")+icon+"<a class=TreeItem"+(items[i].active?"":"Disabled")+" "+
			"href="+((url!="")?url:((items[i][items[i].length-1])?"JavaScript:trees["+this.index+"].toggle(["+iam+"])":"JavaScript:void(0)"))+">"+items[i].title+"</a>";

			for(var j=0;j<images.length;j++)h=Replace(h,images[j][0],Replace(Replace(images[j][1],"$index",this.index),"$iam",iam));
			
			if(items[i].active){
			  h="<a href=\"JavaScript:trees["+this.index+"].disable(["+iam+"])\"><img src="+defaulticons[4]+" alt='DISABLE' width=16 height=16 border=0 align=right></a>"+h;
			}
			else {
				
				
			  h="<a href=JavaScript:void(0) onClick=\"JavaScript:trees["+this.index+"].enable(["+iam+"])\"><img src="+defaulticons[5]+" alt='ENABLE' width=16 height=16 border=0 align=right></a>"+
			    "<a href=JavaScript:void(0) onClick=\"JavaScript:{if(confirm('Are you sure?'))trees["+this.index+"].deleteitem(["+iam+"]);}\"><img src="+defaulticons[6]+" alt='DELETE' width=16 height=16 border=0 align=right></a>"+
			    h;
			}
			
			s+="<div>"+h+"</div>";


			if((haschildren)&&(expanded))s+=this._draw(items[i].submenu,level+(islast?177:179),iam,this.id);


			if(this.allowadd){
				url=QueryReplace(QueryReplace(QueryReplace(url,"watte","ADD"),"ID",""),"ParentID",items[i].id);
				h="<div><img src=pics/img-vert-line.gif width=19 height=16 border=0 align=texttop>"+level+Replace(Replace(Replace(addanchor,"$index",this.index),"$iam",iam),"$url",url);
				for(var j=0;j<images.length;j++)h=Replace(h,images[j][0],Replace(Replace(images[j][1],"$index",this.index),"$iam",iam));

				s+=h;
			}
		}

		if((level=="")&&(this.allowadd)){
			url=QueryReplace(QueryReplace(QueryReplace(this.defaulturl,"watte","ADD"),"ID",""),"ParentID",parent);
			s+="<div>"+level+Replace(Replace(Replace(addanchor,"$index",this.index),"$iam",toggle),"$url",url);
		}	


		return s;
	}

	this._toggle=function(item,expand){
		if(item.submenu){
			if(expand==null)item.title=((item.title.indexOf("!")==0)?item.title.substr(1):"!"+item.title);
			else if((!expand)&&(item.title.indexOf("!")==0))item.title=item.title.substr(1);
			else if((expand)&&(item.title.indexOf("!")!=0))item.title="!"+item.title;
		}
	}

	this._toggleall=function(a,expand){
		for(var i=0;i<a.length;i++){
			var item=a[i];
			var items=item.submenu;
			if(items){
				this._toggle(item,expand);
				this._toggleall(items,expand);
			}
		}
	}
	
	this.finditem = function (index){
		var item=null;
		var items=this.items;
		for(var i=0;i<index.length;i++){
			item=items[index[i]];
			items=item.submenu;
		}		
		return item;
	}

	this.enable=function (index){
		var item=this.finditem(index);
		if(!item)return false;
		var tn=QueryValue(item.url,"Tablename");
		var id=QueryValue(item.url,"ID");
		item.active=true;
		this.redraw();
		SQLExecute("update "+tn+" set Actief=true where ID="+id);		
	}
	this.disable=function (index){
		var item=this.finditem(index);
		if(!item)return false;
		var tn=QueryValue(item.url,"Tablename");
		var id=QueryValue(item.url,"ID");
		item.active=false;
		this.redraw();
		SQLExecute("update "+tn+" set Actief=false where ID="+id);		
	}
	
	
	this._deleteitem = function (items,item){
		for(var i=0;i<items.length;i++){
			if(item==items[i]){
				for(var j=i+1;j<items.length;j++)items[j-1]=items[j];
				items.length=items.length-1;
				return;
			}			
			if(items[i].submenu)this._deleteitem(items[i].submenu,item);
		}				
	}	
	this.deleteitem=function (index){
		var item=this.finditem(index);
		if(!item)return false;
		var tn=QueryValue(item.url,"Tablename");
		var id=QueryValue(item.url,"ID");
		
		SQLExecute("delete from ["+tn+"] where ID="+id);
		SQLExecute("delete from [Taal_"+tn+"] where OwnerID="+id);
		this._deleteitem(this.items,item);
		this.redraw();
	}

	this.toggle=function (index){
		var item=this.finditem(index);
		if(!item)return false;
		this._toggle(item,null);
		this.redraw();
	}
	
	
	this.expandall=function(){
		this._toggleall(this.items,true);
		this.redraw();
	}
	this.collapseall=function(){
		this._toggleall(this.items,false);
		this.redraw();
	}
	this.toggleadd=function(){
		this.allowadd=!this.allowadd;
		this.redraw();
	}

	this.redraw=function(){
		var l=document.getElementById("Tree"+this.index);
		var s=this._draw(this.items,"","",0);
		if(l)l.innerHTML=s;
        this.cookieexpanded();		
	}

	this._expandcookied=function(items,expanded){
		if(items)for(var i=0;i<items.length;i++)if(items[i].submenu){
			this._toggle(items[i],indexOf(expanded,items[i].id)>=0);
			this._expandcookied(items[i].submenu,expanded);
		}	
		
	}
	this.expandcookied=function(){
		var preset=readCookie(this.name);
		if(preset){
			this.expanded=preset.split("|");
			this._expandcookied(this.items,this.expanded);
		}	
	}
	this._cookieexpanded=function(items){
		var s="";		
		if(items)for(var i=0;i<items.length;i++)if(items[i].submenu){
			if(items[i].title.indexOf("!")==0) s=StrAdd(s,items[i].id,"|");
			if(items[i].submenu)s=StrAdd(s,this._cookieexpanded(items[i].submenu),"|");
		}
		return s;
	}
	this.cookieexpanded=function(){
		writeCookie(this.name,this._cookieexpanded(this.items),7);
	}
	
	this.draw=function(){
		this.expandcookied();
		var s="<div class=Tree><div class=TreeCaption>"+
		"<a href='JavaScript:trees["+this.index+"].expandall()'><img align=right width=16 height=16 border=0 src="+defaulticons[1]+"></a>"+
		"<a href='JavaScript:trees["+this.index+"].collapseall()'><img align=right width=16 height=16 border=0 src="+defaulticons[0]+"></a>"+
		"<a href='JavaScript:trees["+this.index+"].toggleadd()'><img align=right width=16 height=16 border=0 src=icons/add.png></a>"+
		this.title+"</div><div style='font-size:9px;font-family:MS Sans Serif;' id=Tree"+this.index+">"+this._draw(this.items,"","",0)+"</div></span>";
		this.cookieexpanded();
		return s;
	}
}


/*
var T=new Tree("test",  [
["Test 1","icons/anchor.png",,,null],
["!Test 2","",,,[
["Test 2-1","",,,null],
["Test 2-2","",,,null],
["Test 2-3",,,,[
["Test 2-3-1",,,,null],
["Test 2-3-2",,,,null],
["Test 2-3-3",,,,null]]
]]],
["!Test 3","icons/award_star_silver_1.png,icons/award_star_bronze_1.png",,,[
["Test 3-1",,,,null],
["Test 3-2",,,,null],
["Test 3-3",,,,null]
]]
]);

document.write(T.draw());
T.expandall();
*/

