﻿var crossword;

function initCrossword(grid)
{
	crossword = new Crossword(grid);
}

function Crossword(grid)
{
	this.i = -1;
	this.j = -1;
	this.direction = 1; // 1 - horizontal, 2 - vertical
	this.hlColor = "LightGreen";



	this.grid = grid;
	this.rownum = this.grid.length;
	this.colnum = 0;
	for(var i = 0; i < this.grid.length; i++)
	{
		if(this.colnum < this.grid[i].length) this.colnum = this.grid[i].length;
	}
}

Crossword.prototype.select = function(direction, i, j)
{
	this.direction = direction;
	this.getInput(i, j).focus();
}

Crossword.prototype.cellExists = function(i, j)
{
	return (i >= 0 && j >= 0 && i < this.rownum && j < this.colnum && this.grid[i][j] != " ");
}

Crossword.prototype.checkH = function(i, j)
{
	if(this.cellExists(i,j - 1)) return true;
	if(this.cellExists(i,j + 1)) return true;
	return false;
}

Crossword.prototype.checkV = function(i, j)
{
	if(this.cellExists(i - 1,j)) return true;
	if(this.cellExists(i + 1,j)) return true;
	return false;
}

Crossword.prototype.highlightHWord = function(i, j)
{
	this.highlight(i, j);
	var k = j - 1;
	while(this.cellExists(i, k)) this.highlight(i, k--);
	k = j + 1;
	while(this.cellExists(i, k)) this.highlight(i, k++);
}

Crossword.prototype.highlightVWord = function(i, j)
{
	this.highlight(i, j);
	var k = i - 1;
	while(this.cellExists(k, j)) this.highlight(k--, j);
	k = i + 1;
	while(this.cellExists(k, j)) this.highlight(k++, j);
}

Crossword.prototype.unhighlightAll = function()
{
	for(var i = 0; i < this.rownum; i++)
		for(var j = 0; j < this.colnum; j++)
			if(this.cellExists(i, j))
				this.unhighlight(i, j);
}

Crossword.prototype.highlight = function(i, j)
{
	this.getInput(i, j).style.backgroundColor = this.hlColor;
}

Crossword.prototype.unhighlight = function(i, j)
{
	this.getInput(i, j).style.backgroundColor = "#ffffff";
}

Crossword.prototype.getInput = function(i, j)
{
	return document.getElementById("cwcell_" + i + "_" + j);
}

Crossword.prototype.onkeydown = function(e)
{
	alert(e.keyCode);
	alert(e.which);
}

Crossword.prototype.onkeyup = function(e)
{
	var keyCode = e.keyCode ? e.keyCode : e.which;
	if(keyCode == 37) this.move(this.i, this.j, 0, -1);
	else if(keyCode == 38) this.move(this.i, this.j, -1, 0);
	else if(keyCode == 39) this.move(this.i, this.j, 0, 1);
	else if(keyCode == 40) this.move(this.i, this.j, 1, 0);
	else if(keyCode == 8) this.shift(this.i, this.j, -1);
	else this.shift(this.i, this.j, 1);
}

Crossword.prototype.putLetter = function(c)
{
	if(this.cellExists(this.i, this.j))
	{
		this.getInput(this.i, this.j).value = c;
		this.shift(this.i, this.j, 1);
	}
}

Crossword.prototype.shift = function(i, j, k)
{
	if(this.direction == 1 && this.cellExists(i, j + k)) this.getInput(i, j + k).focus();
	else if(this.direction == 2 && this.cellExists(i + k, j)) this.getInput(i + k, j).focus();
	else this.getInput(i, j).select();
}

Crossword.prototype.move = function(i, j, m, n)
{
	if(this.cellExists(i + m, j + n)) this.getInput(i + m, j + n).focus();
	else this.getInput(i, j).select();
}

Crossword.prototype.onmousedown = function(i, j)
{
	if(i == this.i && j == this.j)
	{
		if(this.direction != 1) this.direction = 1;
		else this.direction = 2;
		this.getInput(i, j).select();
		this.unhighlightAll();
		this.onfocus(i, j);
	}
}

Crossword.prototype.onfocus = function(i, j)
{
	this.getInput(i, j).select();
	if(this.direction == 1) 
	{
		if(this.checkH(i, j)) this.highlightHWord(i, j);
		else 
		{
			this.direction = 2;
			this.highlightVWord(i, j);
		}
	}
	else 
	{
		if(this.checkV(i, j)) this.highlightVWord(i, j);
		else 
		{
			this.direction = 1;
			this.highlightHWord(i, j);
		}
	}
	this.i = i;
	this.j = j;
}

Crossword.prototype.onblur = function()
{
	this.unhighlightAll();
}

Crossword.prototype.check = function()
{
	for(var i = 0; i < this.rownum; i++)
		for(var j = 0; j < this.colnum; j++)
			if(this.cellExists(i,j) && this.getInput(i,j).value.toUpperCase().replace(/Ё/, "Е").replace(/Й/, "И") != this.grid[i][j].toUpperCase())
				return false;
	return true;
}