// $Id: playfield.js,v 1.5 2004/07/13 20:39:17 kai Exp $
// $Log: playfield.js,v $
// Revision 1.5  2004/07/13 20:39:17  kai
// Spezieller Spezialfall für khtml eingebaut
//
// Revision 1.4  2004/07/12 20:10:15  kai
// HTML-Struktur Spielfeld überarbeitet
//
// Revision 1.3  2004/07/10 21:13:57  kai
// Refaktorisiert und HTML-Element-Stuktur überarbeitet
//
// Revision 1.2  2004/07/09 23:20:31  kai
// Dummy Hiscoreliste eingefügt, fertige Reihen beschleunigen die Steine
//
// Revision 1.1.1.1  2004/07/09 22:36:28  kai
// Erster Import
//

// function playfield(
// left = absolute position left of playfield
// top = absolute position top of playfield
// bwidth = width of brick in pixel
// bheight = height of brick in pixel
// brows = height of playfield in bricks
// bcols = width of playfield in bricks
// pborder = border-size in pixel
function playfield(left, top, bwidth, bheight, brows, bcols, pborder)
{
	// Members
	this.left;
	this.top;
	this.brickwidth;
	this.brickheight;
	this.brickrows;
	this.brickcols;
	this.pborder;
	this.playarea;
	this.myscore;

	// Member functions
	this.getpos = playfield_getpos;
	this.draw = playfield_draw;
	this.showbrick = playfield_showbrick;
	this.makeimgid = playfield_makeimgid;
	this.removelines = playfield_removelines;

	// Init
	this.brickwidth = bwidth;
	this.brickheight = bheight;
	this.brickrows = brows;
	this.brickcols = bcols;
	this.pborder = pborder;
	this.left = left;
	this.top = top;

	this.draw();
	this.getpos();
	this.myscore = new score(0, 0);

	this.playarea = new Array(this.brickcols);
	for(i = 0; i < this.brickcols; i++)
	{
		this.playarea[i] = new Array(this.brickrows);
		for(j = 0; j < this.brickrows; j++)
		{
			this.playarea[i][j] = 0;

			img = document.createElement("img");
			img.id = this.makeimgid(i, j);
			with(img.style)
			{
				left = i * this.brickwidth;
				top = j * this.brickheight;
				width = this.brickwidth;
				height = this.brickheight;
				border = 0;
				position = "absolute";
				visibility = "hidden";
			}
			document.getElementById("playfield").appendChild(img);
		}
	}
	return;
}

function playfield_showbrick(left, top, show, imgname)
{
	imgid = this.makeimgid(left, top);
	myvalue = "hidden";
	if(show)
	{
		myvalue = "visible";
		document.getElementById(imgid).src = imgname;
	}
	document.getElementById(imgid).style.visibility = myvalue;
	this.playarea[left][top] = show;
	return;
}

function playfield_makeimgid(left, top)
{
	return("imgx" + left + "y" + top);
}


function playfield_removelines()
{
	linecounter = 0;
	for(i = 0; i < this.brickrows; i++)
	{
		linefull = true;
		for(j = 0; j < this.brickcols; j++)
		{
			if(!this.playarea[j][i])
			{
				linefull = false;
				break;
			}
		}
		if(linefull)
		{
			linecounter++;
			for(ii = i; ii; ii--)
			{
				for(jj = 0; jj < this.brickcols; jj++)
				{
					this.showbrick(jj, ii, this.playarea[jj][ii - 1], document.getElementById(this.makeimgid(jj, ii - 1)).src);
				}
			}
		}
	}
	this.myscore.add((linecounter * linecounter) * 100, linecounter);
	return linecounter;
}

function playfield_draw()
{
	if(!document.getElementById("tetris"))
	{
		div = document.createElement("div");
		div.id = "tetris";
		div.style.position = "absolute";
		div.style.left = this.left;
		div.style.top = this.top;
		document.getElementsByTagName("body")[0].appendChild(div);

		if(navigator.userAgent.indexOf("KHTML") != -1)
		{
			textarea = document.createElement("textarea");
			textarea.id = "khtmlspecial";
			document.getElementsByTagName("body")[0].appendChild(textarea);
			document.getElementById("khtmlspecial").focus();
			textarea.style.visibility = "hidden";
		}
	}

	if(document.getElementById("playfield"))
	{
		document.getElementById("playfield").style.left = this.left;
		document.getElementById("playfield").style.top = this.top;
	}
	else
	{
		div = document.createElement("div");
		div.id = "playfield";
		div.style.position = "absolute";
		div.style.left = 110;
		div.style.top = 0;
		div.style.width = (this.brickwidth * this.brickcols) + (2 * this.pborder);
		div.style.height = (this.brickheight * this.brickrows) + this.pborder;
		div.style.margin = 0;
		div.style.padding = 0;
		document.getElementById("tetris").appendChild(div);

		// left border
		div = document.createElement("div");
		div.style.position = "absolute";
		div.style.margin = 0;
		div.style.padding = 0;
		div.style.top = 0;
		div.style.left = -(this.pborder);
		div.style.width = this.pborder;
		div.style.height = this.brickheight * this.brickrows;
		div.style.backgroundColor = "#000000";
		document.getElementById("playfield").appendChild(div);

		// right border
		div = document.createElement("div");
		div.style.position = "absolute";
		div.style.margin = 0;
		div.style.padding = 0;
		div.style.top = 0;
		div.style.left = this.brickwidth * this.brickcols;
		div.style.width = this.pborder;
		div.style.height = this.brickheight * this.brickrows;
		div.style.backgroundColor = "#000000";
		document.getElementById("playfield").appendChild(div);

		// bottom border
		div = document.createElement("div");
		div.style.position = "absolute";
		div.style.margin = 0;
		div.style.padding = 0;
		div.style.top = this.brickheight * this.brickrows;
		div.style.left = -(this.pborder);
		div.style.width = (this.brickwidth * this.brickcols) + (2 * this.pborder);
		div.style.height = this.pborder;
		div.style.backgroundColor = "#000000";
		div.innerHTML = "<img src='' height='1' width='1'>";
		document.getElementById("playfield").appendChild(div);
	}
	return;
}

function playfield_getpos()
{
	this.left = parseInt(document.getElementById("playfield").style.left);
	this.top = parseInt(document.getElementById("playfield").style.top);

	return;
}
