function node(id,parent,stat,name,href)
{
	this.id=id;
	this.open=stat;
	this.stat=0;
	this.parent=parent;
	this.path=new Array();
	this.child=new Array();
	this.href=href;
	this.url=href.substring(document.location.hostname.length+7);
	this.name=name;
	this.count=0;
	this.actv=0;
}
 
function tree(varname,hand,name)
{
	this.pict="/styles/2009/tree";
	this.name=name;
	this.varname=varname;
	this.node=new Array();
	this.count=0;
	this.indx=new Array();
	this.select=-1;
	this.fclick=null;
	this.forceopen=0;
	this.b_rest=0;
	this.autoid=1;
	this.node[0]=new node(0,0,1,"Root","");
	this.count=1;
}

tree.prototype.add = function(id,parent,stat,name,href)
{
	if(!id)
		return;
	if(id) id=id+"S";
	if(parent) parent=parent+"S";
	if(!this.node[parent])
		parent=0;
	if(this.node[id])
	{
		delete this.node[id];
		this.count--;
	}
	this.node[id]=new node(id,parent,stat,name,href);
	this.count++;
}

tree.prototype.calc = function()
{
	var i,p,j;

	if(this.b_rest==0)
		this.rest();
	if(this.forceopen)
	{
		if(this.node[this.forceopen])
		{
			this.node[this.forceopen].open=1;
			for(p=this.node[this.forceopen].parent;p;p=this.node[p].parent)
			{
				this.node[p].open=1;
			}
		}
	}

	for(i in this.node)
	{
		p=this.node[i].parent;
		if(p==0)
		{
			this.node[i].stat=1;
			this.node[p].open=1;
		}
		this.node[p].child[this.node[p].count]=i;
		this.node[p].count++;
	}
	for(i in this.node)
	{
		for(j=0;j<this.node[i].child.length;j++)
			this.node[ this.node[i].child[j] ].stat=this.node[i].open;
	}

	for(i in this.node)
	{
		this.node[i].level=1;
		this.node[i].ender=0;
		this.node[i].path = new Array();
		this.node[i].npath = 0;

		for(p=this.node[i].parent;p;p=this.node[p].parent)
		{
			if(p==this.node[p].parent)
			{
				this.node[p].parent=0;
				break;
			}
			this.node[i].level++;
			this.node[i].path[this.node[i].npath++]=p;
		}
		this.node[i].path[this.node[i].npath++]=0;
	}
	this.node[0].level=0;
	j=0;
	for(i in this.node)
	{
	    this.indx[j]=this.node[i].id;
	    this.node[i].i=j;
	    j++;
	}
}

tree.prototype.click = function(l,type)
{

	if(!this.node[l].open)
	{
		for(i in this.node)
		{
			this.node[i].stat=1;
			this.node[i].open=0;
			if(this.node[i].level <= 1)
			       continue;
			this.node[i].stat=0;
			this.node[i].open=0;
		}
	}

	if(type==0)
	{
		this.select=l;
		/*
		if(this.fclick)
			this.fclick(l,this.node[l].data);
		*/
		return;
	}
	if(this.node[l].open)
	{
		this.node[l].open=0;
		for(var i=this.node[l].i+1;i<this.indx.length;i++)
		{
			if(this.node[this.indx[i]].level <= this.node[l].level)
				break;
			this.node[this.indx[i]].stat=0;
			this.node[this.indx[i]].open=0;
		}
	}
	else
	{
		var flag=0;
		var level=0;
		this.node[l].open=1;

		for(var i=this.node[l].i+1;i<this.indx.length;i++)
		{
			if(this.node[this.indx[i]].level <= this.node[l].level)
				break;
			if(level && this.node[this.indx[i]].level <= level)
				flag=0;
			if(flag)
				continue;
			if(this.node[this.indx[i]].count && this.node[this.indx[i]].open==0)
			{
				level=this.node[this.indx[i]].level;
				flag=1;
			}
			this.node[this.indx[i]].stat=1;
		}
	}
	this.save();
	this.draw();
}

tree.prototype.draw = function()
{
	var c='',p='';

	for(var i in this.node)
	{
		if(this.node[i].count==0)
			continue;
		for(var j=this.count-1;j>=0;j--)
			if(this.node[this.indx[j]].parent == this.node[i].id)
			{
				this.node[this.indx[j]].ender=1;
				break;
			}
	}
	var b=[];
	b.push("<table border=0 cellpadding=0 cellspacing=0 width='100%'>");

	for(var l in this.node)
	{
		if(this.node[l].href==document.location.href)
		{
			this.node[l].actv=1;
			for(p=this.node[l].parent;p;p=this.node[p].parent)
			{
				this.node[p].actv=1;
			}
			break;
		}
	}

	for(var l in this.node)
	{
		if(l==0)
			continue;
		if(this.node[l].stat==0)
			continue;
		if(this.node[l].href==document.location.href || parent_href && this.node[l].href==parent_href)
			c="actv";
		else
			c="norm";
		b.push("<tr id=R"+l+" class="+c+" onclick="+this.varname+".click(\""+l+"\",0);>");
		b.push("<td class=row><table border=0 cellpadding=0 cellspacing=0><tr>");
		for(var i=this.node[l].level-2;i>=0;i--)
		{
			if(this.node[this.node[l].path[i]].ender)
				b.push("<td><div class=space></div></td>");
			else
				b.push("<td class=vert><div class=space></div></td>");
		}
		if(this.node[l].ender)
		{
			c='';
			if(this.node[l].count)
				if(this.node[l].open)
					p='minusend';
				else
					p='plusend';
			else
				p='ends';
		}
		else
		{
			c=' class=vert ';
			if(this.node[l].count)
				if(this.node[l].open)
					p='minus';
				else
					p='plus';
			else
				p='cros';
		}
		b.push("<td valign=top "+c+"><img src='"+this.pict+"/"+p+".gif' width=16 height=14");
		if(this.node[l].count)
			b.push(" class=ico onclick="+this.varname+".click(\""+l+"\",1);");
		b.push("></td><td valign=top><img src='"+this.pict+"/folder.gif' width=9 height=16></td>");
		if(this.node[l].actv)
			b.push("<td class='page l"+this.node[l].level+"'><a class='link' href='"+this.node[l].href+"'>"+this.node[l].name+"</a></td></tr></table></td></tr>");
		else
			b.push("<td class='stat l"+this.node[l].level+"'><a class='link' href='"+this.node[l].href+"'>"+this.node[l].name+"</a></td></tr></table></td></tr>");
	}
	b.push("</table>");
	//document.getElementById(this.name).innerHTML=b.join("");
	$("#"+this.name).html(b.join(""));
	onload_linkthis();
}


tree.prototype.exec = function(cmd)
{
	var i;

	this.node[0].stat=1;
	this.node[0].open=1;

	switch(cmd)
	{
	case "all":
		break;
	case "close":
	case "level1":
		for(i in this.node)
		{
			this.node[i].stat=1;
			this.node[i].open=0;
			if(this.node[i].level <= 1)
			       continue;
			this.node[i].stat=0;
			this.node[i].open=0;
		}
		break;
	case "level2":
		for(i in this.node)
		{
			if(this.node[i].level <= 2)
			       continue;
			this.node[i].stat=0;
			this.node[i].open=0;
		}
		break;
	case "level3":
		for(i in this.node)
		{
			if(this.node[i].level <= 3)
			       continue;
			this.node[i].stat=0;
			this.node[i].open=0;
		}
		break;
	case "onlyactv":
		for(i in this.node)
		{
			if(this.node[i].href==document.location.href)
				this.forceopen=i;
			this.node[i].stat=1;
			this.node[i].open=0;
			if(this.node[i].level <= 1)
			       continue;
			this.node[i].stat=0;
			this.node[i].open=0;
		}
		this.b_rest=1;
		this.calc();
		break;
	}
	this.save();
	this.draw();
}

tree.prototype.save = function()
{
	var b="TREE__"+this.name+"=0";
	for(l in this.node)
		if(this.node[l].open==1 && this.node[l].count>0)
			b+="#"+l;
	b+="; domain=" + document.domain +"; path=/";
	document.cookie=b;
}

tree.prototype.rest = function()
{
return;
	var beg=0;
	var end=0;
	var d=document.cookie;
	var treename="TREE__"+this.name+"=";
	if((beg=d.indexOf(treename))!=-1)
	{
		if((end=d.indexOf(";",beg+treename.length))==-1) end=d.length;
		var str=unescape(d.substring(beg+treename.length,end));
		var dat=str.split("#");
		if(dat.length)
		{
			for(var l in this.node)
				this.node[l].open=0;
			for(var i=0;i<dat.length;i++)
			{
				if(typeof(this.node[dat[i]])!="undefined")
					this.node[dat[i]].open=1;
			}
		}
	}
}

tree.prototype.loadul = function(e,p,hand)
{
	this.fclick=hand;
	this.loadul_r(e,p);
	this.calc();
	this.draw();
}

tree.prototype.loadul_r = function(e,p)
{
	if(!e)
		return;
	var i,a=e.getElementsByTagName("a"),id,pid,p;
	for(var i=0; i<a.length; i++)
	{
		id=a[i].getAttribute("i");
		pid=a[i].getAttribute("p");
		this.add(id,pid,0,a[i].innerHTML,a[i].href);
		if(a[i].href==document.location.href)
			this.forceopen=id+"S";
	}
}
