/*
 
   ======  ==  ==  ======  ====    ==   ====
     ||     \\//     ||    || ))   ||  ((
     ||      ||      ||    ====    ||   \\
     ||     //\\     ||    || \\   ||    ))
     =T    ==  =X    =T    ==  =R  =I  ===S

   v. 0.9.3

   (c) 2002 by zockomat.de/RAGMaN

   spexial thanx to alexej pajitnov, creator of tetris 

*/

var displaystring = ""; // Der angezeigte Inhalt des Spieles
var score;
var level;
var rows;
var now;
var then;
var interval;
var x_pos;
var y_pos;
var ps_toggle = 0;
var gameon;
var gameover = 0;
var ref_fl_row = "";
var ref_mt_row = "";
var ref_tr_row = "";
var gameoverstring = "----------\r----------\r----------\r----------\r----------\r---GAME---\r---OVER---\r----------\r----------\r----------\r----------\r----------\r----------\r----------\r----------\r----------";
var pausestring =    "----------\r----------\r----------\r----------\r----------\r--PAUSE---\r----------\r----------\r----------\r----------\r----------\r----------\r----------\r----------\r----------\r----------";
var act_pc;
var act_ndx = 0;
var norotate = 0;
old_array = new Array(8);
var soundon = 0;

// Allgemeine Spielvariablen (config)

var xspan = 10; // Breite des Spielfelds
var yspan = 16; // Höhe des Spielfelds
var xzero = xspan - 1;
var yzero = yspan - 1;
var gamelength = xspan * yspan;
var useiframe = 1; // 1, wenn der Iframe zur Scorespeicherung benutzt wird
var mtsgn = "~"; // Zeichen für leeres Feld
var flsgn = "O"; // Zeichen für volles Feld
var score1line = 100; // Score für 1,2,... Zeilen
var score2line = 400;
var score3line = 900;
var score4line = 1600;
var lvl0ntrvl = 2000; // Refreshabstand (ms) für Level 0,1,2,3,...
var	lvl1ntrvl = 1700; 
var	lvl2ntrvl = 1500; 
var	lvl3ntrvl = 1300; 
var	lvl4ntrvl = 1100; 
var	lvl5ntrvl = 900; 
var	lvl6ntrvl = 700; 
var	lvl7ntrvl = 500;
var	lvl8ntrvl = 400; 
var	lvl9ntrvl = 300; 
var	lvl10ntrvl = 200; 
var lvlup = 10;		// Benötigte Reihen für nächstes Level


// Block-Definitionen

pc = new Array(7);
pc[0] = new Array(4);
pc[0][0] = new Array(8);
pc[0][0][0] = -1;
pc[0][0][1] = 0;
pc[0][0][2] = 0;
pc[0][0][3] = 0;
pc[0][0][4] = 1;
pc[0][0][5] = 0;
pc[0][0][6] = 1;
pc[0][0][7] = -1;
pc[0][1] = new Array(8);
pc[0][1][0] = 0;
pc[0][1][1] = 1;
pc[0][1][2] = 0;
pc[0][1][3] = 0;
pc[0][1][4] = 0;
pc[0][1][5] = -1;
pc[0][1][6] = -1;
pc[0][1][7] = -1;
pc[0][2] = new Array(8);
pc[0][2][0] = 1;
pc[0][2][1] = 0;
pc[0][2][2] = 0;
pc[0][2][3] = 0;
pc[0][2][4] = -1;
pc[0][2][5] = 0;
pc[0][2][6] = -1;
pc[0][2][7] = 1;
pc[0][3] = new Array(8);
pc[0][3][0] = 0;
pc[0][3][1] = -1;
pc[0][3][2] = 0;
pc[0][3][3] = 0;
pc[0][3][4] = 0;
pc[0][3][5] = 1;
pc[0][3][6] = 1;
pc[0][3][7] = 1;
pc[1] = new Array(4);
pc[1][0] = new Array(8);
pc[1][0][0] = -1;
pc[1][0][1] = 0;
pc[1][0][2] = 0;
pc[1][0][3] = 0;
pc[1][0][4] = 1;
pc[1][0][5] = 0;
pc[1][0][6] = -1;
pc[1][0][7] = -1;
pc[1][1] = new Array(8);
pc[1][1][0] = 0;
pc[1][1][1] = 1;
pc[1][1][2] = 0;
pc[1][1][3] = 0;
pc[1][1][4] = 0;
pc[1][1][5] = -1;
pc[1][1][6] = -1;
pc[1][1][7] = 1;
pc[1][2] = new Array(8);
pc[1][2][0] = 1;
pc[1][2][1] = 0;
pc[1][2][2] = 0;
pc[1][2][3] = 0;
pc[1][2][4] = -1;
pc[1][2][5] = 0;
pc[1][2][6] = 1;
pc[1][2][7] = 1;
pc[1][3] = new Array(8);
pc[1][3][0] = 0;
pc[1][3][1] = -1;
pc[1][3][2] = 0;
pc[1][3][3] = 0;
pc[1][3][4] = 0;
pc[1][3][5] = 1;
pc[1][3][6] = 1;
pc[1][3][7] = -1;
pc[2] = new Array(4);
pc[2][0] = new Array(8);
pc[2][0][0] = -1;
pc[2][0][1] = 0;
pc[2][0][2] = 0;
pc[2][0][3] = 0;
pc[2][0][4] = 1;
pc[2][0][5] = 0;
pc[2][0][6] = 0;
pc[2][0][7] = -1;
pc[2][1] = new Array(8);
pc[2][1][0] = 0;
pc[2][1][1] = 1;
pc[2][1][2] = 0;
pc[2][1][3] = 0;
pc[2][1][4] = 0;
pc[2][1][5] = -1;
pc[2][1][6] = -1;
pc[2][1][7] = 0;
pc[2][2] = new Array(8);
pc[2][2][0] = 1;
pc[2][2][1] = 0;
pc[2][2][2] = 0;
pc[2][2][3] = 0;
pc[2][2][4] = -1;
pc[2][2][5] = 0;
pc[2][2][6] = 0;
pc[2][2][7] = 1;
pc[2][3] = new Array(8);
pc[2][3][0] = 0;
pc[2][3][1] = -1;
pc[2][3][2] = 0;
pc[2][3][3] = 0;
pc[2][3][4] = 0;
pc[2][3][5] = 1;
pc[2][3][6] = 1;
pc[2][3][7] = 0;
pc[3] = new Array(4);
pc[3][0] = new Array(8);
pc[3][0][0] = -1;
pc[3][0][1] = 0;
pc[3][0][2] = 0;
pc[3][0][3] = 0;
pc[3][0][4] = 1;
pc[3][0][5] = 0;
pc[3][0][6] = 2;
pc[3][0][7] = 0;
pc[3][1] = new Array(8);
pc[3][1][0] = 0;
pc[3][1][1] = 1;
pc[3][1][2] = 0;
pc[3][1][3] = 0;
pc[3][1][4] = 0;
pc[3][1][5] = -1;
pc[3][1][6] = 0;
pc[3][1][7] = -2;
pc[3][2] = new Array(8);
pc[3][2] = pc[3][0];
pc[3][3] = new Array(8);
pc[3][3] = pc[3][1];
pc[4] = new Array(4);
pc[4][0] = new Array(8);
pc[4][0][0] = 0;
pc[4][0][1] = 0;
pc[4][0][2] = 1;
pc[4][0][3] = 0;
pc[4][0][4] = -1;
pc[4][0][5] = -1;
pc[4][0][6] = 0;
pc[4][0][7] = -1;
pc[4][1] = new Array(8);
pc[4][1][0] = 0;
pc[4][1][1] = 0;
pc[4][1][2] = 0;
pc[4][1][3] = -1;
pc[4][1][4] = -1;
pc[4][1][5] = 1;
pc[4][1][6] = -1;
pc[4][1][7] = 0;
pc[4][2] = new Array(8);
pc[4][2] = pc[4][0];
pc[4][3] = new Array(8);
pc[4][3] = pc[4][1];
pc[5] = new Array(4);
pc[5][0] = new Array(8);
pc[5][0][0] = -1;
pc[5][0][1] = 0;
pc[5][0][2] = 0;
pc[5][0][3] = 0;
pc[5][0][4] = 0;
pc[5][0][5] = -1;
pc[5][0][6] = 1;
pc[5][0][7] = -1;
pc[5][1] = new Array(8);
pc[5][1][0] = 0;
pc[5][1][1] = 1;
pc[5][1][2] = 0;
pc[5][1][3] = 0;
pc[5][1][4] = -1;
pc[5][1][5] = 0;
pc[5][1][6] = -1;
pc[5][1][7] = -1;
pc[5][2] = new Array(8);
pc[5][2] = pc[5][0];
pc[5][3] = new Array(8);
pc[5][3] = pc[5][1];
pc[6] = new Array(4);
pc[6][0] = new Array(8);
pc[6][0][0] = 0;
pc[6][0][1] = 0;
pc[6][0][2] = 1;
pc[6][0][3] = 0;
pc[6][0][4] = 0;
pc[6][0][5] = -1;
pc[6][0][6] = 1;
pc[6][0][7] = -1;
pc[6][1] = new Array(8);
pc[6][1] = pc[6][0];
pc[6][2] = new Array(8);
pc[6][2] = pc[6][0];
pc[6][3] = new Array(8);
pc[6][3] = pc[6][0];




// Init-Funktion

function init()	
	{
	play_wav("init");
	this_timer = new Date();
	then = this_timer.getTime();
	displaystring = "";
	for (var ct = 1; ct <= gamelength; ++ct)
		{	displaystring = displaystring + mtsgn;	}
	ref_fl_row = "";
	for (var ct = 1; ct <= xspan; ++ct)
		{ ref_fl_row = ref_fl_row + flsgn; }
	ref_mt_row = "";
	for (var ct = 1; ct <= xspan; ++ct)
		{ ref_mt_row = ref_mt_row + mtsgn; }
	gameon = 1;	
	level = 0;
	rows = 0;
	score = 0;
	interval = lvl0ntrvl;
	x_pos = Math.floor(xzero / 2);
	y_pos = yzero;
	ps_toggle = 0;
	old_x = x_pos;
	old_y = y_pos;
	gameover = 0;
	nu_active();
  write_block(x_pos,y_pos,flsgn);
	draw();
	timer();
	}

// Timer-Funktionen

function now()
	{
		other_timer = new Date();	
		return other_timer.getTime();
	}

function timer()
	{
		if (gameover == 1)
		{ play_wav("over");
			if (score > oldhiscore)
				{ 
					oldhiscore = score;
					document.spielfeldform.hifield.value = oldhiscore
				}
					document.spielfeldform.hifield.value = oldhiscore;
					if (parent.multiframe && useiframe == 1)   
   						 {  zielpfad = 'txt_i_scores.php?score='+score+'&rows='+rows+'&style='+style;
    							parent.multiframe.location.href = zielpfad;}
 					else
   						 { openWindow('txt_scores.php?score='+score+'&rows='+rows,10,10); } 
			document.spielfeldform.spielfeld.value = gameoverstring;
		}
		else
		{ 
			if (ps_toggle != 1) 
			{
				if (now() > then + interval)
					{  then=now(); ctrl_move(0,-1); }
				window.setTimeout('timer()',100); 
			}
		}
	}






function draw() 
			{
			if (displaystring.length != gamelength)	{ gameover = 1; alert('Allgemeine Spielverletzung'); }
			var drawstr = "";
				for (var i=0; i<gamelength; i+=xspan)
					drawstr += displaystring.substr(i, xspan) + "\r";
        document.spielfeldform.spielfeld.value = drawstr;
        document.spielfeldform.scorefield.value = score;
        document.spielfeldform.rowfield.value = rows;
        document.spielfeldform.lvlfield.value = level;
			}

// String-IO-Funktionen

function write_block(x,y,what)
{
		for (ct=0;ct<4;++ct)
			{ 
				w_x = pc[act_pc][act_ndx][ct*2] + x;								// x-wert von subblock ct
				w_y = pc[act_pc][act_ndx][(ct*2)+1] + y;						// y-wert von subblock ct
				if (w_x > xzero || w_x < 0 || w_y > yzero || w_y < 0)
				{}
				else
				{ write(w_x,w_y,what); }
			}
}

function write(x,y,what)
{
			subposition = (-1*(y-yzero)*10)+x;
			displaystring = displaystring.substring(0,subposition) + what + displaystring.substring(subposition+1,gamelength);
}

function read(x,y)
{
		subposition = (-1*(y-yzero)*10)+x;
		rd_rslt = displaystring.substr(subposition,1);
		return rd_rslt;	
}


function del_line(y)
{
		l_subposition = (-1*(y-yzero)*10);
		displaystring = ref_mt_row + displaystring.substring(0,l_subposition) + displaystring.substring(l_subposition+10,gamelength);
}

// Steuerungsfunktionen

function ctrl_sound()
{ 
	if (soundon == 0) { soundon = 1; document.getElementById("snd_state").firstChild.nodeValue = "Ton aus"; }
	else 						  { soundon = 0; document.getElementById("snd_state").firstChild.nodeValue = "Ton an"; }
}

function ctrl_pause()
{
		if (gameon == 1 && gameover != 1)
		{
			if (ps_toggle == 0) { ps_toggle = 1; tm_paused = interval - (now() - then); document.spielfeldform.spielfeld.value = pausestring;}
			else								{ ps_toggle = 0; draw(); window.setTimeout('timer()',tm_paused); }
			play_wav("pause");
		}
}

function ctrl_move(x,y)
{ 
		blstay = 0;
		if (gameon == 1 && gameover != 1 && ps_toggle != 1)
	  {
			old_x = x_pos;
			old_y = y_pos;
			x_pos = x_pos + x;
			y_pos = y_pos + y;
			if (x_pos < 0) { x_pos = 0; play_wav("denied"); }
			if (x_pos > xzero) { x_pos = xzero; play_wav("denied"); }
			if (y_pos < 0) { blstay = 1;  }
			for (ct=0;ct<4;++ct)
			{ 
				this_x = pc[act_pc][act_ndx][ct*2] + x_pos;
				this_y = pc[act_pc][act_ndx][(ct*2)+1] + y_pos;
				old_array[ct*2] = pc[act_pc][act_ndx][ct*2] + old_x;
				old_array[(ct*2)+1] = pc[act_pc][act_ndx][(ct*2)+1] + old_y;
				if (this_x < 0 || this_x > xzero) { x_pos = old_x; play_wav("denied"); }
				if (this_y < 0) { y_pos = old_y; blstay = 1; }
			}
			for (ct=0;ct<4;++ct)
			{ 
				this_x = pc[act_pc][act_ndx][ct*2] + x_pos;
				this_y = pc[act_pc][act_ndx][(ct*2)+1] + y_pos;
				this_sgn = read(this_x,this_y);
				if (this_sgn == flsgn && y == -1 && (this_x != old_array[0] || this_y != old_array[1]) && (this_x != old_array[2] || this_y != old_array[3]) && (this_x != old_array[4] || this_y != old_array[5]) && (this_x != old_array[6] || this_y != old_array[7])) { y_pos = old_y; blstay = 1; }
				if (this_sgn == flsgn && y != -1 && (this_x != old_array[0] || this_y != old_array[1]) && (this_x != old_array[2] || this_y != old_array[3]) && (this_x != old_array[4] || this_y != old_array[5]) && (this_x != old_array[6] || this_y != old_array[7])) { x_pos = old_x; play_wav("denied");}
			}
			if (blstay == 1)
				{
					blstay = 0; 
					check_line();
					y_pos = yzero; 
					x_pos = Math.floor(xzero / 2); 
					nu_active();
				}
			else
				{
					write_block(old_x,old_y,mtsgn);
					play_wav("move")
				}
				
			write_block(x_pos,y_pos,flsgn);
			draw();
		}
}

function ctrl_rotate(rt)
{
	if (gameon == 1 && gameover != 1 && ps_toggle != 1)
	  {
		for (ct=0;ct<4;++ct)
				{
					check_ndx = act_ndx+rt;
					if (check_ndx > 3) check_ndx = 0;
					if (check_ndx < 0) check_ndx = 3;
					old_array[ct*2] = pc[act_pc][act_ndx][ct*2] + x_pos;
					old_array[(ct*2)+1] = pc[act_pc][act_ndx][(ct*2)+1] + y_pos;
					if (pc[act_pc][check_ndx][ct*2] + x_pos < 0 || pc[act_pc][check_ndx][ct*2] + x_pos > xzero || pc[act_pc][check_ndx][(ct*2)+1] + y_pos < 0) { norotate = 1; }
				}
			for (ct=0;ct<4;++ct)
				{
					check_ndx = act_ndx+rt;
					if (check_ndx > 3) check_ndx = 0;
					if (check_ndx < 0) check_ndx = 3;
					this_x = pc[act_pc][check_ndx][ct*2] + x_pos;
					this_y = pc[act_pc][check_ndx][(ct*2)+1] + y_pos;
					this_sgn = read(this_x,this_y);
					if (this_sgn == flsgn && (this_x != old_array[0] || this_y != old_array[1]) && (this_x != old_array[2] || this_y != old_array[3]) && (this_x != old_array[4] || this_y != old_array[5]) && (this_x != old_array[6] || this_y != old_array[7])) { norotate = 1; }
					}
		if (norotate == 0)
			{
				write_block(x_pos,y_pos,mtsgn);
				act_ndx = act_ndx + rt;
				if (act_ndx > 3) act_ndx = 0;
				if (act_ndx < 0) act_ndx = 3;
				write_block(x_pos,y_pos,flsgn);
				play_wav("rotate");
				draw();
			}
		else { play_wav("denied"); }
		norotate = 0;
	}
}

function check_line()
{	
	dellines = new Array(4);
	dellines[0] = -1;
	dellines[1] = -1;
	dellines[2] = -1;
	dellines[3] = -1;
	linecombo = 0;
	for (ct2 = 0; ct2 < yspan; ++ct2)
	{
		thisrow="";
		for (var ct = 0; ct < xspan; ++ct)
		{ thisrow = thisrow + read(ct,ct2); }
		if (thisrow == ref_fl_row)
		{
			dellines[linecombo] = ct2;
			linecombo = linecombo+1; 
			rows = rows+1; 
		}
	}
	for (dct = 0; dct < 4; ++dct)
	{
	this_del = dellines[dct];
	if (this_del != -1) del_line(this_del-dct);
  }	
  played = 0;
	oldlevel = level;
	level = Math.floor(rows/lvlup);
	if (oldlevel < level) { play_wav("up"); played = 1; }
	if (level == 1) interval = lvl1ntrvl;
	if (level == 2) interval = lvl2ntrvl;
	if (level == 3) interval = lvl3ntrvl;
	if (level == 4) interval = lvl4ntrvl;
	if (level == 5) interval = lvl5ntrvl;
	if (level == 6) interval = lvl6ntrvl;
	if (level == 7) interval = lvl7ntrvl;
	if (level == 8) interval = lvl8ntrvl;
	if (level == 9) interval = lvl9ntrvl;
	if (level >= 10) interval = lvl10ntrvl;
	
	if (linecombo == 1) { score = score + score1line; if (played == 0) play_wav("one"); played=1; }
	if (linecombo == 2) { score = score + score2line; if (played == 0) play_wav("two"); played=1;  }
	if (linecombo == 3) { score = score + score3line; if (played == 0) play_wav("three"); played=1;  }
	if (linecombo == 4) { score = score + score4line; if (played == 0) play_wav("four"); played=1;  }
	if (played == 0) play_wav("tdown");
}

function nu_active()
{
	act_pc = Math.floor(Math.random()*7); // Random-Integer zwischen 0 und 6
	if (act_pc == 7) act_pc = 0;					// Könnte ja sein...
	act_ndx = 0;
	for (ct = 0; ct < 8; ++ct)
	{ if (read(pc[act_pc][act_ndx][ct*2] + x_pos,pc[act_pc][act_ndx][(ct*2)+1] + y_pos) == flsgn) gameover = 1; }
}

// Maskierte Control-Funktionen

function ctrl_left()
	{ ctrl_move(-1,0); }

function ctrl_right()
	{ ctrl_move(1,0); }

function ctrl_down()
	{ ctrl_move(0,-1); }

function ctrl_rleft()
	{ ctrl_rotate(-1); }

function ctrl_rright()
	{ ctrl_rotate(1); }


// Fenster-Funktion (von Knightrun geklaut)

function openWindow(URL, xsize, ysize) {  // open a centered subwindow
	var xy='';
	if (document.all) { // MSIE?
	  xy = ',left=' + Math.floor(window.screenLeft + document.body.offsetWidth / 2 - xsize / 2);
	  xy += ',top=' + Math.floor(window.screenTop + document.body.offsetHeight / 2 - ysize / 2);
	} else {
		if (document.layers) { // NS?
		  xy = ',screenX=' + Math.floor(window.screenX + window.outerWidth / 2 - xsize / 2);
		  xy += ',screenY=' + Math.floor(window.screenY + window.outerHeight / 2 - ysize / 2);
		}
	}
	options = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width='+xsize+',height='+ysize+xy;
	window.open(URL, 'subwin', options);
}

// Sound-Funktionen

function play_wav(which)

{ 
	if (soundon == 1)
	{
		switch(which) 
		{
		case "one":
 		document.embeds[4].play();
 		break;
		case "two":
 		document.embeds[5].play();
 		break;
 		case "three":
 		document.embeds[6].play();
 		break;
 		case "four":
 		document.embeds[7].play();
 		break;
 		case "over":
 		document.embeds[3].play();
 		break;
 		case "pause":
 		document.embeds[10].play();
 		break;
 		case "init":
 		document.embeds[11].play();
		break;
 		case "denied":
 		document.embeds[1].play();
 		break;
// 		case "rotate":
//		document.embeds[2].play();
// 		break;
 		case "up":
 		document.embeds[0].play();
 		break;
// 		case "move":
// 		document.embeds[8].play();
// 		break;
 		case "tdown":
 		document.embeds[9].play();
 		break;
 		default:
 		break;
		}
	}
}
