var GAME_STATUS_NORMAL			= 0;
var GAME_STATUS_SELECT			= 1;
var GAME_STATUS_REMOVE			= 2;
var GAME_STATUS_FINISHED		= 3;

var BALL_SELECTED					= 100;
var NO_COLOR						= -1;

var game_running 					= false;
var game_status 					= GAME_STATUS_NORMAL;		
var lang_id 						= GetQueryValue ("lang_id");
if (lang_id == "") 	lang_id 	= "en";

var node_playfield 				= "";
var array_playfield				= null;
var array_undo						= null;
var playfield_xdim				= 11;
var playfield_ydim				= 11;
var playfield_colors			= 5;
var selected_color				= NO_COLOR;
var selected_balls				= 0;
var score_total					= 0;

//------------------------------------------------------------------------------------------------//

function SetLang (lang)
{
	window.location.href = "games_deballizer.html?lang_id=" + lang;
}

//------------------------------------------------------------------------------------------------//

function InitGame()
{
	SetGameStatus ("playfield_size", playfield_xdim + " x " + playfield_ydim);
	
	StartGame();
}

//------------------------------------------------------------------------------------------------//

function SelectPlayfieldSize (xdim, ydim)
{
	HideDiv ('select_playfield');

	playfield_xdim	 = xdim;
	playfield_ydim	 = ydim;

	SetGameStatus ("playfield_size", playfield_xdim + " x " + playfield_ydim);
	StartGame();
}

//------------------------------------------------------------------------------------------------//

function StartGame()
{
 	if (!node_playfield) node_playfield = FIND ('playfield');
	game_status 				= GAME_STATUS_NORMAL;
	selected_color				= NO_COLOR;
	selected_balls				= 0;
	score_total					= 0;
	game_running 				= true;
	MakePlayfield();
	SetGameStatus ("score_total", score_total);
	SetGameStatus ("score_selected", 0);
}

//------------------------------------------------------------------------------------------------//

function MakePlayfield()
{
	var table_node, tr_node, td_node;
	var i, j;
	
	array_playfield = null;
	array_playfield = [];
	array_undo = null;
	array_undo = [];
	
	RemoveChildrenNodes (node_playfield);
	
	table_node = AddTableChildNode (node_playfield, "balls");

	for (j = 0; j < playfield_ydim; j++)
	{
		tr_node 	= joAddHTMLChildNode (table_node, "TR");
		array_playfield[j] = [];
		array_undo[j] = [];
		for (i = 0; i < playfield_xdim; i++)
		{
			array_playfield[j][i] = Math.floor (Math.random() * playfield_colors);
			array_undo[j][i] = array_playfield[j][i];
			td_node 	= joAddHTMLChildNode (tr_node, "TD", "", "f-" + i + "-" + j);
			SetAttrib (td_node, "xpos", i);
			SetAttrib (td_node, "ypos", j);
			
			joAddEvent 	(td_node, "click", function (event) 
			{ 
				ClickField (event.target ? event.target : event.srcElement);
			});
			
			joAddEvent 	(td_node, "dblclick", function (event) 
			{ 
				DblClickField (event.target ? event.target : event.srcElement);
			});

			DrawBall (td_node, array_playfield[j][i]);
			td_node.innerHTML = "";
		}
	}
}

//------------------------------------------------------------------------------------------------//

function DrawBall (field_node, color)
{
	if (color >= 0)
	{
		SetAttrib (field_node, "class", "filled col" + color);		
	}
	else
	{
		SetAttrib (field_node, "class", "nocol");		
	}
}

//------------------------------------------------------------------------------------------------//

function DblClickField (field_node)
{
	ClickField (field_node);
	if (selected_balls) ClickField (field_node);
	
}

//------------------------------------------------------------------------------------------------//

function ClickField (field_node)
{
	xpos = parseFloat (field_node.getAttribute ("xpos"));
	ypos = parseFloat (field_node.getAttribute ("ypos"));
	
	if (array_playfield [ypos][xpos] != NO_COLOR)
	{
		if (game_status == GAME_STATUS_NORMAL	)
		{
			selected_color = array_playfield [ypos][xpos];
			if (HasSameNeighbours (xpos, ypos))
			{
				SelectField (xpos, ypos, selected_color);
				game_status = GAME_STATUS_SELECT;
				SetGameStatus ("score_selected", selected_balls * (selected_balls - 1));
			}
		}
		else if (game_status == GAME_STATUS_SELECT)
		{
			if (array_playfield [ypos][xpos] == selected_color + BALL_SELECTED)
			{
				FillUndo();
				RemoveSelection();
				FillGaps();
				DrawBalls();
				score_total += selected_balls * (selected_balls - 1);
				SetGameStatus ("score_total", score_total);
				if (IsGameOver())
				{
					alert ("Game over. Total score: " + score_total);
					StartGame();
				}
			}
			else
			{
				DeactSelection();
			}
			game_status 		= GAME_STATUS_NORMAL;
			selected_balls 	= 0;
			selected_color		= NO_COLOR;
			SetGameStatus 		("score_selected", 0);
		}
	}
}

//------------------------------------------------------------------------------------------------//

function SetGameStatus (node_id, value)
{
	document.getElementById (node_id).innerHTML = value;
}

//------------------------------------------------------------------------------------------------//

function HasSameNeighbours (xpos, ypos)
{
	var curr_col = array_playfield [ypos][xpos];
	
	if (xpos > 0 && array_playfield [ypos][xpos - 1] == curr_col) { return true; }
	if (ypos > 0 && array_playfield [ypos - 1][xpos] == curr_col) { return true; }
	if (xpos < playfield_xdim - 1 && array_playfield [ypos][xpos + 1] == curr_col) { return true; }
	if (ypos < playfield_ydim - 1 && array_playfield [ypos + 1][xpos] == curr_col) { return true; }
	
	return false; 
}

//------------------------------------------------------------------------------------------------//

function IsGameOver()
{
	var game_over = true;
	
	var i, j;
	for (j = 0; j < playfield_ydim && game_over; j++)
	{
		for (i = 0; i < playfield_xdim && game_over; i++)
		{
			if (array_playfield [j][i] != NO_COLOR && HasSameNeighbours (i, j)) game_over = false;
		}
	}
	
	return game_over;
}

//------------------------------------------------------------------------------------------------//

function DeactSelection()
{
	var field_node;
	var i, j;
	for (j = 0; j < playfield_ydim; j++)
	{
		for (i = 0; i < playfield_xdim; i++)
		{
			if (array_playfield [j][i] >= BALL_SELECTED)
			{
				field_node = document.getElementById ("f-" + i + "-" + j);
				DrawBall (field_node, selected_color);
				array_playfield [j][i] -= BALL_SELECTED;
			}
		}
	}
}

//------------------------------------------------------------------------------------------------//

function SelectField (xpos, ypos, selected_color)
{
	var field_node = document.getElementById ("f-" + xpos + "-" + ypos);
	
	if (array_playfield [ypos][xpos] == selected_color)
	{
		selected_balls	++;
		array_playfield [ypos][xpos] += BALL_SELECTED;
		field_node.className = "selected col" + selected_color;

		if (xpos) SelectField (xpos - 1, ypos, selected_color);
		if (ypos) SelectField (xpos, ypos - 1, selected_color);
		if (xpos < playfield_xdim - 1) SelectField (xpos + 1, ypos, selected_color);
		if (ypos < playfield_ydim - 1) SelectField (xpos, ypos + 1, selected_color);
	}
}

//------------------------------------------------------------------------------------------------//

function RemoveSelection()
{
	var i, j;
	for (j = 0; j < playfield_ydim; j++)
	{
		for (i = 0; i < playfield_xdim; i++)
		{
			if (array_playfield [j][i] >= BALL_SELECTED)
			{
				field_node = document.getElementById ("f-" + i + "-" + j);
				array_playfield [j][i] = NO_COLOR;
				DrawBall (field_node, NO_COLOR);
			}
		}
	}
}

//------------------------------------------------------------------------------------------------//

function DrawBalls()
{
	var i, j;
	for (j = 0; j < playfield_ydim; j++)
	{
		for (i = 0; i < playfield_xdim; i++)
		{
			field_node = document.getElementById ("f-" + i + "-" + j);
			DrawBall (field_node, array_playfield [j][i]);
		}
	}
}

//------------------------------------------------------------------------------------------------//

function FillUndo()
{
	var i, j;
	for (j = 0; j < playfield_ydim; j++)
	{
		for (i = 0; i < playfield_xdim; i++)
		{
			array_undo [j][i] = array_playfield [j][i];
			if (array_undo [j][i] >= BALL_SELECTED)
			{
				array_undo [j][i] -= BALL_SELECTED;
			}
		}
	}
}

//------------------------------------------------------------------------------------------------//

function UndoMove()
{
	var i, j;
	for (j = 0; j < playfield_ydim; j++)
	{
		for (i = 0; i < playfield_xdim; i++)
		{
			field_node = document.getElementById ("f-" + i + "-" + j);
			array_playfield [j][i] = array_undo [j][i];
			DrawBall (field_node, array_playfield [j][i]);
		}
	}
}

//------------------------------------------------------------------------------------------------//

function FillGaps()
{
	var i, j, k, column_empty;
	
	//............................................................................let balls fall down
	for (i = 0; i < playfield_xdim; i++)
	{
		for (k = playfield_ydim - 1; k >= 0; k--)
		{
			for (j = playfield_ydim - 1; j > 0; j--)
			{
				if (array_playfield [j][i] == NO_COLOR)
				{
					array_playfield [j][i] = array_playfield [j - 1][i];
					array_playfield [j - 1][i] = NO_COLOR;
				}
			}
		}
	}
	
	//............................................................................fill empty columns
	for (i = playfield_xdim - 1; i >= 0; i--)
	{
		column_empty = true;
		for (j = 0; j < playfield_ydim && column_empty; j++)
		{
			if (array_playfield [j][i] != NO_COLOR) column_empty = false;
		}
		
		if (column_empty)
		{
			for (k = i; k > 0; k--)
			{
				for (j = 0; j < playfield_ydim; j++)
				{
					array_playfield [j][k] = array_playfield [j][k - 1];
					
				}
			}
			i++;	
			
			//.............................................................................new column
			for (j = 0; j < playfield_ydim; j++)
			{
				array_playfield [j][0] = NO_COLOR;
			}

			
			var count_filled_rows = Math.floor (Math.random() * playfield_ydim - 1) + 1;

			for (j = 0; j < count_filled_rows; j++)
			{
				array_playfield [playfield_ydim - 1 - j][0] = Math.floor (Math.random() * playfield_colors);				
			}
//			DrawBalls();
		}
	}
}
