var Size = new Number(6);
var Moves = new Array();
var noGame = 1;
var FD;
var Board = new function()
{
	this.init = function()
	{
		var a;
		Solve.init();
		for(var i = 0; i < Size * Size; this.set(i++,0));
		for(var i = 0; i < 3 + 3 * FD.selectedIndex; i++)
		{
			Board.clicked(a = Math.floor(Math.random() * Size) + "-" + Math.floor(Math.random() * Size),1);
			Solve.add(a);
		}
	}
	this.clicked = function(square,c)
	{
		var x,y;
		this.change(x = parseInt(square.split("-")[0]),y = parseInt(square.split("-")[1]),c);
		this.change(x - 1,y,c);
		this.change(x + 1,y,c);
		this.change(x,y - 1,c);
		this.change(x,y + 1,c);
	}
	this.change = function(x,y,c)
	{
		if(this.check(x,y))
			this.set(x + Size * y,(parseInt(this.get(x,y)) + c) & 3);
	}
	this.set = function(square,val)
	{
		window.document.forms["playForm"].elements[square].value = val;
	}
	this.get = function(x,y)
	{
		return (this.check(x,y) ? window.document.forms["playForm"].elements[x + Size * y].value : ' ');
	}
	this.check = function(x,y)
	{
		return (x >= 0 && y >= 0 && x < Size && y < Size);
	}
}
var Counter = new function()
{
	this.counter = 0;
	this.counterID = 0;
	this.set = function(i)
	{
		this.stop();
		this.counter = i;
		this.show();
	}
	this.get = function()
	{
		return this.counter;
	}
	this.show = function()
	{
		window.document.forms["CounterForm"]["Counter"].value = this.counter;
	}
	this.add = function(i)
	{
		if((this.counter += i) <= 0)
			this.set(0);
		this.show();
	}
	this.start = function()
	{
		this.set(1000);
		this.counterID = setInterval('Counter.add(-1)',166);
	}
	this.stop = function()
	{
		if(this.counterID)
			clearInterval(this.counterID);
		this.counterID = 0;
	}
}
var Solve = new function()
{
	this.solve = new Array();
	this.solveID = 0;
	this.init = function()
	{
		this.stop();
		this.solve.length = 0;
		for(var i = 0; i < 9; window.document.forms["hintForm"].elements[i++].value = ' ');
	}
	this.add = function()
	{
		for(var i = 0; i < arguments.length; this.solve.push(arguments[i++]));
		this.solve.sort();
		for(var i = 0; i <= this.solve.length - 4; i++)
			if(this.solve[i] == this.solve[i + 1] && this.solve[i] == this.solve[i + 2] && this.solve[i] == this.solve[i + 3])
				this.solve.splice(i--,4);
		return this.solve.length;
	}
	this.start = function()
	{
		this.solveID = setInterval('Solve.run()',window.document.forms["solveForm"].SolveSpeed.options[window.document.forms["solveForm"].SolveSpeed.selectedIndex].value);
	}
	this.stop = function()
	{
		if(this.solveID)
			clearInterval(this.solveID);
		this.solveID = 0;
	}
	this.run = function()
	{
		var i;
		if(i = this.solve.pop())
			return Board.clicked(i,3);
		this.stop();
	}
	this.hint = function()
	{
		var i = this.solve[Math.floor(Math.random() * this.solve.length)];
		var x = parseInt(i.split("-")[0]);
		var y = parseInt(i.split("-")[1]);
		window.document.forms["hintForm"].elements[0].value = Board.get(x - 1,y - 1);
		window.document.forms["hintForm"].elements[1].value = Board.get(x,y - 1);
		window.document.forms["hintForm"].elements[2].value = Board.get(x + 1,y - 1);
		window.document.forms["hintForm"].elements[3].value = Board.get(x - 1,y);
		window.document.forms["hintForm"].elements[4].value = Board.get(x,y);
		window.document.forms["hintForm"].elements[5].value = Board.get(x + 1,y);
		window.document.forms["hintForm"].elements[6].value = Board.get(x - 1,y + 1);
		window.document.forms["hintForm"].elements[7].value = Board.get(x,y + 1);
		window.document.forms["hintForm"].elements[8].value = Board.get(x + 1,y + 1);
	}
}
var Highscores = new function()
{
	this.highscores = new Array();
	this.init = function()
	{
		for(var i = 0; i < FD.length; i++)
		{
			this.highscores[i] = new Array(10);
			for(var j = 0; j < 10; this.highscores[i][j++] = 1);
		}
	}
	this.add = function(score)
	{
		this.highscores[FD.selectedIndex].push(score);
		this.highscores[FD.selectedIndex].sort();
		this.highscores[FD.selectedIndex].reverse();
		this.highscores[FD.selectedIndex].pop();
		this.show(score);
	}
	this.show = function(score)
	{
		for(var i = 0, t = ''; i < 10; t += (i < 9 ? ' ' : '') + (i + 1) + ': ' + this.highscores[FD.selectedIndex][i] + (score == this.highscores[FD.selectedIndex][i++] ? '  <--' : '') + '\n');
		window.document.forms["scoreForm"].Scores.value = t;
	}
}
function buttonNew()
{
	Board.init();
	Moves.length = window.document.forms["clicksForm"].Clicks.value = 0;
	Highscores.show(0);
	Counter.start();
	noGame = 0;
}
function buttonMove(x)
{
	if(noGame)
		return;
	Board.clicked(x,3);
	window.document.forms["clicksForm"].Clicks.value = Moves.push(x);
	if(Solve.add(x,x,x))
		return;
	noGame = 1;
	Counter.stop();
	if((i = 3 + 3 * FD.selectedIndex - Moves.length) < 0)
		Counter.add(7 * i);
	Highscores.add(Counter.get());
}
function buttonUndo(flag)
{
	if(noGame)
		return;
	Counter.add(-20);
	while(flag-- && (i = Moves.pop()))
		Board.clicked(i,1),Solve.add(i);
	window.document.forms["clicksForm"].Clicks.value = Moves.length;
}
function buttonHint()
{
	if(noGame)
		return;
	Counter.add(-35);
	Solve.hint();
}
function buttonSolve()
{
	if(noGame)
		return;
	noGame = 1;
	Counter.set(0);
	Solve.start();
}
function doOnload()
{
	FD = window.document.forms["settingsForm"]["Difficulty"];
	Highscores.init();
}
