var buffer = "";

function lt(l, t)
{
	this.l = l;
	this.t = t;
}

var lts = new Array();

var zindex = -10;

function xImage(w, h)
{
	this.src = "";
}

var pixel = new Image(1, 1);
pixel.src = "./images/pt000000.bmp";
var lineh020 = new Image(20, 1);
lineh020.src = "./images/pt000000_h020.bmp";
var lineh160 = new Image(80, 1);
lineh160.src = "./images/pt000000_h160.bmp";
var linev005 = new Image(1, 5);
linev005.src = "./images/pt000000_v005.bmp";
var linev020 = new Image(1, 20);
linev020.src = "./images/pt000000_v020.bmp";

function Vykresli()
{
	document.getElementsByTagName("body")[0].innerHTML += buffer;
	for (var i = 0; i < lts.length; i++)
	{
		var s = document.getElementById("xp_" + lts[i].l.toString() + "_" + lts[i].t.toString()).style;
		s.position = "absolute";
		s.left = lts[i].l.toString() + "px";
		s.top = lts[i].t.toString() + "px";
//		s.zIndex = 0;
	}
}

function SpojBunky(a, b, c)
{
	//popis vnejsi
	var tableX = document.getElementById("xfttable").offsetLeft;
	var tableY = document.getElementById("xfttable").offsetTop;
	//popis vnitrni
	var verticalTop = b.offsetTop + b.offsetHeight / 2;
	var verticalLeft = b.offsetLeft + b.offsetWidth + ((c.offsetLeft - (b.offsetLeft + b.offsetWidth)) / 2);
	var verticalBottom = a.offsetTop;
	var verticalRight = verticalLeft;
	var horizontalTop = verticalTop;
	var horizontalLeft = b.offsetLeft + b.offsetWidth;
	var horizontalBottom = horizontalTop;
	var horizontalRight = c.offsetLeft;
	//vykreslit
	while ((verticalBottom - verticalTop) / 20 >= 1)
	{
		verticalBottom -= 20;
		lts.push(new lt(tableX + verticalLeft, tableY + verticalBottom));
		buffer += "<img id='xp_" + (tableX + verticalLeft).toString() + "_" + (tableY + verticalBottom).toString() + "' src='" + linev020.src + "' />";
	}
	while ((verticalBottom - verticalTop) / 5 >= 1)
	{
		verticalBottom -= 5;
		lts.push(new lt(tableX + verticalLeft, tableY + verticalBottom));
		buffer += "<img id='xp_" + (tableX + verticalLeft).toString() + "_" + (tableY + verticalBottom).toString() + "' src='" + linev005.src + "' />";
	}
	while (verticalBottom - verticalTop > 1)
	{
		verticalBottom --;
		lts.push(new lt(tableX + verticalLeft, tableY + verticalBottom));
		buffer += "<img id='xp_" + (tableX + verticalLeft).toString() + "_" + (tableY + verticalBottom).toString() + "' src='" + pixel.src + "' />";
	}
	while ((horizontalRight - horizontalLeft) / 160 >= 1)
	{
		lts.push(new lt(tableX + horizontalLeft, tableY + horizontalBottom));
		buffer += "<img id='xp_" + (tableX + horizontalLeft).toString() + "_" + (tableY + horizontalBottom).toString() + "' src='" + lineh160.src + "' />";
		horizontalLeft += 160;
	}
	while ((horizontalRight - horizontalLeft) / 20 >= 1)
	{
		lts.push(new lt(tableX + horizontalLeft, tableY + horizontalBottom));
		buffer += "<img id='xp_" + (tableX + horizontalLeft).toString() + "_" + (tableY + horizontalBottom).toString() + "' src='" + lineh020.src + "' />";
		horizontalLeft += 20;
	}
	while (horizontalRight - horizontalLeft > 0)
	{
		lts.push(new lt(tableX + horizontalLeft, tableY + horizontalBottom));
		buffer += "<img id='xp_" + (tableX + horizontalLeft).toString() + "_" + (tableY + horizontalBottom).toString() + "' src='" + pixel.src + "' />";
		horizontalLeft ++;
	}
}

function Ini()
{
	//vytvorit trojuhelnikovou matici prave s bunkami zucastnenymi ve strome
	var pole = new Array();
	var raw_tr = document.getElementById("xfttable").getElementsByTagName("tr");
	for (var i2 = 0; i2 < raw_tr.length; i2 ++)
	{
		var raw_td = raw_tr[i2].getElementsByTagName("td");
		var x = 0;
		for (var i3 = 0; i3 < raw_td.length; i3 ++)
		{
			if (raw_td[i3].className == "xfttd")
			{
				if (x ++ == 0) pole.push(new Array());
				pole[pole.length-1].push(raw_td[i3]);
			}
		}
	}
	//poslat vykreslit
	for (var j2 = pole.length-1; j2 > 0; j2 --)
	{
		for (var j3 = 0; j3 < pole[j2].length; j3 ++)
		{
			SpojBunky(pole[j2][j3], pole[j2-1][j3*2], pole[j2-1][j3*2+1]);
		}
	}
	//vykresli
	Vykresli();
}

