var num_mines = 10;
var num_rows = 9;
var num_columns = 9;
var Feld = new Array();
var Minen = 0;
var Zeilen,Spalten,zuWelchem;
var aufgedeckt = 0;
var Status = 0;
var Klick = "links";
var counter = 0;
var NachbarX = new Array();
var NachbarY = new Array();
var darum = 0;
var temp = 0;
var markiert = 0;

var Zeit = 0;
var was = 0;
var blah;

// geklärt
function Uhr()
{
	if (Status >= 2) was = 2;
	if (Zeit <= 999)
		switch (was)
		{
			case 0:		was = 1;
					setTimeout("Uhr()",1000);
					Zeit++;
					break;
			case 1:		setTimeout("Uhr()",1000);
					Zeit++;
					break;
			case 2:		break;
		}
	window.document.out.Zeit.value = Zeit;
}

// geklärt
function reload()
{
	window.open('/eng/games/minesweeper/lx/play',window.name)
}

// geklärt
switch (window.name)
{
	case '2':	num_mines = 40;
			num_rows = 16;
			num_columns = 16;
			window.document.out.Level.value = "0";
			break;
	case '3':	num_mines = 99;
			num_rows = 16;
			num_columns = 30;
			window.document.out.Level.value = "1";
			break;
	case '11':	num_mines = 10;
			num_rows = 9;
			num_columns = 9;
			window.document.out.Level.value = "nix";
			break;
	case '22':	num_mines = 40;
			num_rows = 16;
			num_columns = 16;
			window.document.out.Level.value = "0";
			break;
	case '33':	num_mines = 99;
			num_rows = 16;
			num_columns = 30;
			window.document.out.Level.value = "1";
			break;
	case '99':	Abfragen();
			window.document.out.Level.value = "nix";
			break;
	default:	window.name = '1';
			window.document.out.Level.value = "nix";
			break;
}

// geklärt
function changeLevel()
{
	window.name = window.document.out.difficulty.options[window.document.out.difficulty.selectedIndex].value;
	reload();
}

// geklärt
function Abfragen()
{
	do
	{
		num_rows = eval(prompt('How many rows?\n(3 - 30)',num_rows));
	} while (!((num_rows >= 3) && (num_rows <= 30)))
	do
	{
		num_columns = eval(prompt('How many columns?\n(8 - 30)',num_columns));
	} while (!((num_columns >= 8) && (num_columns <= 30)))
	do
	{
		num_mines = eval(prompt('Hom many mines?\n(' + Math.floor(num_rows * num_columns * 0.3) + ' - ' + Math.floor(num_rows * num_columns * 0.7) + ')',num_mines));
	} while (!((num_mines <= (Math.floor(num_rows * num_columns * 0.7))) && (num_mines >= (Math.floor(num_rows * num_columns * 0.3)))));
}

// geklärt
function allesAufdecken()
{
	for (blah = 0; blah < (num_rows*num_columns); blah++)
		aufdecken(blah);
}

// geklärt
function nachbarfelder(x)
{
	if (!Feld[x][0]) aufdecken(x);
	if (x%num_columns > 0)
	{
		if (x >= num_columns) schauen(x-1-num_columns);
		schauen(x-1);
		if (x < (num_columns*num_rows-num_columns)) schauen(x-1+num_columns);
	}

	if (x >= num_columns) schauen(x-num_columns);
	if (x < (num_columns*num_rows-num_columns)) schauen(x+num_columns);

	if ((x+num_columns)%num_columns < num_columns - 1)
	{
		if (x >= num_columns) schauen(x+1-num_columns);
		schauen(x+1);
		if (x < (num_columns*num_rows-num_columns-1)) schauen(x+1+num_columns);
	}
}

// geklärt
function schauen(x)
{
	if (Status == 0) init(x);
	if (Status >= 2)
		{ }
	else
	{
		if (Klick == "rechts")
		{
			if  (!Feld[x][0])
			{
				window.document.images['Z' + x].src = '/games/minesweeper/lx/flag.gif';
				Feld[x][0] = 2;
				markiert++;
			} else
			if (Feld[x][0] == 2)
			{
				window.document.images['Z' + x].src = '/games/minesweeper/lx/_.gif';
				Feld[x][0] = 0;
				markiert--;
			}
		}
		else
		if (Feld[x][0] != 2)
		{
			if (Feld[x][1] == 'X')
			{
				Status = 4;
				Feld[x][0] = 4;
				window.document.images['Z' + x].src = '/games/minesweeper/lx/aua.gif';
				switch (window.name)
				{
					case '2':	window.name = '1';
							break;
					case '3':	window.name = '2';
							break;
					default:	break;
				}
				allesAufdecken();
			}
			if (!Feld[x][0])
			{
				if (Feld[x][1] == 0)
				{
					nachbarfelder(x);
				}
				else aufdecken(x);
			}
			else if (!temp)
			{
				drumherum(x);
			}
			if (((num_rows * num_columns - num_mines) == aufgedeckt) && (Status != 4))
			{
				if (window.document.out.Level.value == "0" || window.document.out.Level.value == "1") window.document.getElementById('score').style.visibility = 'visible';
				if (Status != 3)
					switch (window.name)
					{
						case '1':	window.name = '2';
								break;
						case '2':	window.name = '3';
								break;
						case '3':	break;
						default:	break;
					}
				Status = 3;
				allesAufdecken();
			}
		}
	}
	if (Status == 4) window.document.images['smiley'].src = '/games/minesweeper/lx/s_aua.gif';
	if (Status == 3) window.document.images['smiley'].src = '/games/minesweeper/lx/s_win.gif';
	window.document.out.put.value = (num_mines - markiert);
}

// geklärt
function drumherum(x)
{
	darum = 0;
	temp = 1;

	if (x%num_columns > 0)
	{
		if ((x >= num_columns) && (Feld[x-1-num_columns][0] == 2)) darum++;
		if (Feld[x-1][0] == 2) darum++;
		if ((x < (num_columns*num_rows-num_columns)) && (Feld[x-1+num_columns][0] == 2)) darum++;
	}
	if ((x >= num_columns) && (Feld[x-num_columns][0] == 2)) darum++;
	if ((x < (num_columns*num_rows-num_columns)) && (Feld[x+num_columns][0] == 2)) darum++;
	if ((x+num_columns)%num_columns < num_columns - 1)
	{
		if ((x >= num_columns) && (Feld[x+1-num_columns][0] == 2)) darum++;
		if (Feld[x+1][0] == 2) darum++;
		if ((x < (num_columns*num_rows-num_columns-1)) && (Feld[x+1+num_columns][0] == 2)) darum++;
	}

	if (darum == Feld[x][1])
	{
		if (x%num_columns > 0)
		{
			if (x >= num_columns) schauen(x-1-num_columns);
			schauen(x-1);
			if (x < (num_columns*num_rows-num_columns)) schauen(x-1+num_columns);
		}
		if (x >= num_columns) schauen(x-num_columns);
		if (x < (num_columns*num_rows-num_columns-1)) schauen(x+num_columns);
		if ((x+num_columns)%num_columns < num_columns - 1)
		{
			if (x >= num_columns) schauen(x+1-num_columns);
			schauen(x+1);
			if (x < (num_columns*num_rows-num_columns-1)) schauen(x+1+num_columns);
		}
	}
	temp = 0;
}

// geklärt
function aufdecken(x)
{
	if (!Feld[x][0])
	{
		if ((Status <= 1) || (Status == 4))
		{
			window.document.images['Z' + x].src = '/games/minesweeper/lx/' + Feld[x][1] + '.gif';
			aufgedeckt++;
			Feld[x][0] = 1;
		}
		else
		{
			window.document.images['Z' + x].src = '/games/minesweeper/lx/flag.gif';
			aufgedeckt++;
			Feld[x][0] = 1;
		}
	}
	if ((Feld[x][0] == 2) && (Status >= 2))
	{
		if (Feld[x][1] != 'X')
		{
			window.document.images['Z' + x].src = '/games/minesweeper/lx/wrong.gif';
			Feld[x][0] = 1;
		}
	}
}

// geklärt
function init(x)
{
	document.getElementById('score').style.visibility = "hidden";
	for (blah = 0; blah < (num_rows*num_columns); blah++)
	{
		Feld[blah] = new Array(0,0);
	}

	while (Minen < num_mines)
        {
		for (blah = 0; blah < (num_rows*num_columns); blah++)
	        {
         		if ((Minen < num_mines) && (Feld[blah][1] == 0) && (Math.random() < 0.1) && (blah != x))
			{
				Minen++;
				Feld[blah][1] = 9;
			}
                }
        }

	for (blah = 0; blah < num_rows*num_columns; blah++)
		if (Feld[blah][1] >= 9) korrigiere(blah);

	for (blah = 0; blah < num_rows*num_columns; blah++)
		if (Feld[blah][1] >= 9) Feld[blah][1] = 'X';
	Status = 1;
	Uhr();
}

//geklärt
function korrigiere(x)
{
	if (x%num_columns > 0)
	{
		if (x >= num_columns) Feld[x-1-num_columns][1] ++;
		Feld[x-1][1] ++;
		if (x < (num_columns*num_rows-num_columns)) Feld[x-1+num_columns][1] ++;
	}

	if (x >= num_columns) Feld[x-num_columns][1] ++;
	if (x < (num_columns*num_rows-num_columns)) Feld[x+num_columns][1] ++;

	if ((x+num_columns)%num_columns < num_columns - 1)
	{
		if (x >= num_columns) Feld[x+1-num_columns][1] ++;
		Feld[x+1][1] ++;
		if (x < (num_columns*num_rows-num_columns-1)) Feld[x+1+num_columns][1] ++;
	}
}

