var activeNode = 0;

var indentPixels = 10;
//var imgPath = "/bp/";
var imgPath = "/bp/images/";
var imInactSrc = imgPath + "blues1.gif";
var imActSrc = imgPath + "blues2.gif";
var imOpnSrc = imgPath + "yellows2.gif";

// pre-load all images into cache
var fillerImg = new Image(1,1);
fillerImg.src = imgPath + "filler.gif";
var imInact = new Image(11,11);
imInact.src = imInactSrc;
var imAct = new Image(11,11);
imAct.src = imActSrc;
var imOpn = new Image(11,11);
imOpn.src = imOpnSrc;

function img_opn(imgName, frameName)
{ if (typeof frameName == "undefined")	
			frameName = "TREE";
	try
	{ 	s = frames[frameName].document.images[imgName].src;
			if (s.indexOf( imOpnSrc ) < 0)
				frames[frameName].document.images[imgName].src = imOpnSrc;
	 } catch (e) {}
}

function img_act(imgName, frameName)
{ if (typeof frameName == "undefined")	
			frameName = "TREE";
	try
	{ 	s = frames[frameName].document.images[imgName].src;
			if (s.indexOf( imOpnSrc ) < 0)
				frames[frameName].document.images[imgName].src = imActSrc;
	 } catch (e) {}
}

function img_inact(imgName,frameName)
{ if (typeof frameName == "undefined")	
			frameName = "TREE";
	try
	{ 	window.status= "...";
			s = frames[frameName].document.images[imgName].src;
			if (s.indexOf( imOpnSrc ) < 0)
				frames[frameName].document.images[imgName].src = imInactSrc;
	 } catch (e) {}
}

var beforeTree = "<HTML><HEAD><META http-equiv=Content-Type content='text/html; charset=windows-1251'></HEAD><BODY class=tree>"
var afterTree = "<hr>Âõîä<HR></html>";

var tr = new Array();
var opn=[0];

function getNode(id)
{ for (n=0; n<tr.length; n++)
	{	if (tr[n][0] == id)
				return tr[n];
	}
	return null;
}

function openNode(id)
{	openCont(id);
  opn = new Array();
  i = 0;
	var nod = getNode(id)
	while (nod != null)
	{	opn[i++] = nod[0];
			nod = getNode(nod[1])
	}
	outTree();
  activeNode = id;
}

function openCont(id)
{ /* should be overwritten in "TOP" */
}

function indent_ (ind)
{	return "<IMG SRC='" + imgPath + "filler.gif' class=fi WIDTH=" + (indentPixels*ind) + ">";
}

function wid1(id)
{ return "<IMG class=toc SRC='" + imgPath + "blues1.gif' NAME='im" + id + "'>"; }

function wid2(id)
{ return "<IMG class=toc SRC='" + imgPath + "yellows2.gif' NAME='im" + id + "'>"; }

function anc(id, name, stat)
{ s = "<A class=toc HREF=\"javascript:top.openNode('" + id
		+ "');\" onMouseOver=\"top.img_act('im" + id
//		+ "');\" "
		+ "'); window.status='Îòêðûòü ðàçäåë `"+ stat + "`'; return true;\""
		+ " onMouseout=\"top.img_inact('im" + id + "','TREE');\">"
		+ name + "</a>";
		return s;
}

var act1 = "<font color=FFFF00><b><i>";
var act2 ="</b></i></font>";

function outNode(n)
{ var s = "";
	var id = tr[n][0];
	var pid = tr[n][1];
	var nam = tr[n][2];
	var lev = nodeLevel(id);
	var plev = nodeLevel(pid);	

	if ( lev == 0)			// root of the tree
		s = anc(id, "<b>" + nam + ":</b><br>", nam);

	if ( lev == 1)			// opened root node
		s = anc(id, wid2(id) + act1 + nam + act2, nam);
		
	else if ( lev > 1)			// opened not root node
		s = indent_(lev) + anc(id, act1 + "- " + nam  + act2, nam);

	else if (plev == 0)		//closed root node
		s = anc(id, wid1(id) + nam, nam);
	
	else if  (plev > 0)			// closed non-root node
		s = indent_(plev+1)  + anc(id, "- " + nam, nam);
	
	if (s.length > 0)
	{ // alert(s);
		frames["TREE"].document.writeln("<br>" + s);
	}
}

function nodeLevel(id)
{ for (k=0; k<opn.length; k++)
			if (opn[k] == id)		return (opn.length-k-1);
		return -1;
}


function outTree()
{ if (tr.length < 1)
  { opn = new Array("0");
    top.frames["TREE"].window.location.href="/bp?c=tree&tm=" + (new Date()).getTime();
  }
  else 
  { top.frames["TREE"].document.write(beforeTree);
	for (n=0; n<tr.length; n++)
		outNode(n);
		
  top.frames["TREE"].document.write(afterTree);
// debugPrint();
	top.frames["TREE"].document.close();
  }
}

function debugPrint()
{
top.frames["TREE"].document.write("<hr><xmp>");
top.frames["TREE"].document.write("beforeTree:" + beforeTree);
top.frames["TREE"].document.write("<br>tr.length:" + tr.length + "<br>");
 for (n=0; n<tr.length; n++)
	outNode(n);
top.frames["TREE"].document.write("</xmp>");

top.frames["TREE"].document.write(opn,"<p>");
 		
for (i=0; i<tr.length; i++)
  {var str="Node " + i + ": ";
		for (j=0; j < tr[i].length; j++)
	      str += tr[i][j] + " / " ;
 		top.frames["TREE"].document.write(str,"<br>");
  }
}