var game_running = false;
var game_status = 0;			// 0... normal, 1... 
var game_twin_found;			// count of found stones by user in this game
var game_selected = 0;			// count of currently turned stones by user
var game_attempts;				// count of user draws
var game_start_time;
var game_time_counter;
var curr_playfieldsize = 0;
var curr_countstones;
var curr_countcols;
var lang_id = GetQueryValue ("lang_id");
if (lang_id == "") lang_id = "en";
var playfield = "";
var memory_array_id = null;
var memory_array_status = null;

//------------------------------------------------------------------------------------------------//

function SelectPlayfield (playfield_size, count_cols, stone_twin_count)
{
	HideDiv ('memory_select_playfield');
	if (AskNewGame())
	{
		SetPlayfieldSize (playfield_size, count_cols, stone_twin_count);
		StartGame();
	}
}

//------------------------------------------------------------------------------------------------//

function SetPlayfieldSize (playfield_size, count_cols, stone_twin_count)
{
	SetTextColor (FIND ("memory_playfield_" + curr_playfieldsize), "#C3BDB9");

	curr_playfieldsize 	= playfield_size;
	curr_countstones 		= curr_playfieldsize * stone_twin_count;
	curr_countcols			= count_cols;
	curr_twincount			= stone_twin_count;

	SetText ("memory_select_playfield_title", playfield_size + " " + memT ("cards" + stone_twin_count));
	SetTextColor (FIND ("memory_playfield_" + playfield_size), "#E60005");
}

//------------------------------------------------------------------------------------------------//

function SetLang (lang)
{
	window.location.href = "games_noexcuse.asp?lang_id=" + lang;
}

//------------------------------------------------------------------------------------------------//

function AskNewGame()
{
	var result = false;
//	if (!game_running || confirm (memT ("wanttostartgame")))
	{
		game_running = false;
		result = true;
	}
	return result;
}

//------------------------------------------------------------------------------------------------//

function StartGame()
{
	HidePopups();
	
	if (!game_running || AskNewGame())
	{
	 	if (!playfield) playfield = FIND ('playfield');
		game_status = 0;
		game_selected = 0;
		game_running = true;
		SetGameTwinsFound (0);
		SetGameAttempts (0);
		RemoveChildrenNodes (playfield);
		MakePlayfield();
		RandomizePlayfield();
		SetText ("memory_game_time", "0:00");
		ShowHint ("hint_click_image");
	}
}

//------------------------------------------------------------------------------------------------//

function ShowGameTime()
{
	
	var curr_time = new Date();
	var time_diff = Math.round ((curr_time - game_start_time) / 1000);
	
	var mins = Math.floor (time_diff / 60);
	var secs = time_diff % 60;
	if (secs <= 9) secs = "0" + secs;
	SetText ("memory_game_time", mins + ":" + secs);
	
	game_time_counter = window.setTimeout ("ShowGameTime()", 1000);
}

//------------------------------------------------------------------------------------------------//

function StartGameTime()
{
	game_start_time = new Date();
	ShowGameTime();
}

//------------------------------------------------------------------------------------------------//

function StopGameTime()
{
	window.clearTimeout (game_time_counter);
}

//------------------------------------------------------------------------------------------------//

function RandomizePlayfield()
{
	var i;
	var j;
	var count_images = memory_images.length;

	// ---------------------------------------------------------------------------- init memory array 	
	memory_array_id = null;
	memory_array_status = null;
	memory_array_id = new Array (curr_countstones);
	memory_array_status = new Array (curr_countstones);
	for (i = 0; i != curr_countstones; i++)
	{
		memory_array_id[i] = 0;
		memory_array_status[i] = 0;
	}

	// .........................................init auxiliary array with chosen images for this game
	var chosen_images_array = new Array (count_images);
	var rand_stone_nr;
	var curr_stone_nr;
	
	for (i = 0; i != count_images; i++)
	{
		chosen_images_array[i] = "";
	}
		
	for (i = 0; i != curr_playfieldsize; i++)
	{
		rand_stone_nr = Math.floor (Math.random() * (count_images - i));
		curr_stone_nr = -1;
		
		for (j = 0; j != count_images && curr_stone_nr != rand_stone_nr; j++)
		{
			if (chosen_images_array [j] == "") // stone not chosen yet
			{
				curr_stone_nr++;
				if (curr_stone_nr == rand_stone_nr) 
				{
					chosen_images_array[j] = curr_twincount;
				}
			}
		}
	}

	for (i = 0; i != curr_countstones; i++)
	{
		rand_stone_nr = Math.floor (Math.random() * (curr_playfieldsize - Math.floor (i / curr_twincount)));
		curr_stone_nr = -1;
			
		for (j = 0; j != count_images && curr_stone_nr != rand_stone_nr; j++)
		{
		
			if (chosen_images_array [j] && chosen_images_array [j] > 0)
			{
				curr_stone_nr++;
				if (curr_stone_nr == rand_stone_nr) 
				{
					memory_array_id[i] = j;
					chosen_images_array [j]--;
				}
			}
		}
	}
}

//------------------------------------------------------------------------------------------------//

function MakePlayfield()
{
	var new_table, new_tbody, new_tr, new_td;
	var i = 0;
	
	var new_table = AddTableChildNode (playfield, "memory_stones");

	for (i = 0; i != curr_countstones; i++)
	{
		if (!(i % curr_countcols))
		{
			new_tr = null;
			new_tr = document.createElement("TR"); new_table.appendChild (new_tr);
		}
		new_td = document.createElement("TD"); new_tr.appendChild (new_td);

		MakeStone (new_td, i);
		
	}

}

//------------------------------------------------------------------------------------------------//

function SetGameTwinsFound (num)
{
	game_twin_found = num;
	SetText ("memory_game_twins_found", game_twin_found);
}

//------------------------------------------------------------------------------------------------//

function SetGameAttempts (num)
{
	game_attempts = num;
	SetText ("memory_game_attempts", game_attempts);
}

//------------------------------------------------------------------------------------------------//

function HidePopups()
{
	IndicateTwinIncorrect (false);
	IndicateGameWon (false);
	IndicateCredits (false);
	HideDiv ('memory_select_playfield');
}

//------------------------------------------------------------------------------------------------//

function UserClickStone (id)
{
	var i;
	var twin_img_id = "";

	if (!game_status)
	{
		if (!game_selected && !game_attempts) 
			StartGameTime();
		
		if (game_selected != curr_twincount && memory_array_status[id]==0)
		{
			TurnStone (id, 1);
			game_selected++;
			memory_array_status[id] = 1;
		
			if (game_selected == curr_twincount) //..................turned enough stones of same kind?
			{
				SetGameAttempts (game_attempts + 1);
				
				// user opened twin... check move!
				var is_ok = true;
				for (i = 0; i < curr_countstones; i++)
				{
					if (memory_array_status[i] == 1)
					{
						if (twin_img_id == "")
						{
							twin_img_id = memory_array_id[i];
						}
						else
						{
							if (twin_img_id != memory_array_id[i])
							{
								is_ok = false;
								break;
							}
						}
					}
				}
				
				if (is_ok) //.................................................................stones same
				{
					SetGameTwinsFound (game_twin_found + 1);
					
					if (game_twin_found == curr_playfieldsize) //................................game won!
					{
						GameWon();
					}
					else
					{
						for (i = 0; i < curr_countstones; i++) 
						{ if (memory_array_status[i] == 1) { memory_array_status[i] = 2; } }
						ShowHint ("hint_click_image");
					}
					
					game_selected = 0;
				}
				else //........................................stones different - turn them around again!
				{
					ShowHint ("hint_click_to_continue");
					game_status = 1;
					TwinIncorrect();
				}
			}
			else
			{
				ShowHint ("hint_click_another");
			}
		}
	}
	else if (game_status == 1) //.........................wrong stones --> click for cover them again
	{
		game_selected = 0;
		game_status = 0;
		for (i = 0; i < curr_countstones; i++) 
		{ if (memory_array_status[i] == 1) { memory_array_status[i] = 0; TurnStone (i, 0); } }
		ShowHint ("hint_click_image");
	}
	else if (game_status == 2)
	{
		StartGame();
	}
}

//------------------------------------------------------------------------------------------------//

function TwinIncorrect()
{
	IndicateTwinIncorrect (true);
}



//------------------------------------------------------------------------------------------------//

function GameWon()
{
	StopGameTime();
	SetText ("won_info_playfield", curr_playfieldsize + " " + memT ("cards" + curr_twincount));
	SetText ("won_info_attempts", game_attempts);
	SetText ("won_info_time", GetText ("memory_game_time"));
	ShowHint ("congratulations");

	IndicateGameWon (true);
	game_status = 2;
	game_selected = 0;
	game_running = false;
}

//------------------------------------------------------------------------------------------------//

function TurnStone (id, show_it)
{
	var tdnode = FIND ('td_' + id);
	
	if (tdnode)
	{
		tdnode.style.backgroundColor = show_it ? stone_img_bg_color : stone_bg_color;
	}
	var imgnode = FIND ('stone_' + id);
	if (imgnode)
	{
		imgnode.src = show_it ? memory_images[memory_array_id[id]] : game_folder + "images/stone_bg.jpg";
	}

}

//------------------------------------------------------------------------------------------------//

function MakeStone (parent_node, stone_id)
{
	var new_table = AddTableChildNode (parent_node, "memory_stone");
	
	// top	
	new_tr = document.createElement("TR"); new_table.appendChild (new_tr);
	AddStonePart (new_tr, game_folder + "images/stone_lo.jpg", "", "", "");
	AddStonePart (new_tr, game_folder + "images/s.gif", game_folder + "images/stone_o.jpg", "", "x");
	AddStonePart (new_tr, game_folder + "images/stone_ro.jpg", "", "", "");

	// middle	
	new_tr = document.createElement("TR"); new_table.appendChild (new_tr);
	AddStonePart (new_tr, game_folder + "images/s.gif", game_folder + "images/stone_l.jpg", "", "y");
	AddStonePart (new_tr, game_folder + "images/stone_bg.jpg", "", "_" + stone_id, "");
	AddStonePart (new_tr, game_folder + "images/s.gif", game_folder + "images/stone_r.jpg", "", "");
	
	// bottom	
	new_tr = document.createElement("TR"); new_table.appendChild (new_tr);
	AddStonePart (new_tr, game_folder + "images/stone_lu.jpg", "", "", "");
	AddStonePart (new_tr, game_folder + "images/s.gif", game_folder + "images/stone_u.jpg", "", "");
	AddStonePart (new_tr, game_folder + "images/stone_ru.jpg", "", "", "");
}

function AddStonePart (node_tr, img_src, bg_src, stone_id, setsize)
{
	var new_td 	= document.createElement("TD");
	var new_a 	= document.createElement("A");
	var new_img;

	node_tr.appendChild (new_td);

	new_td.appendChild (new_a);
	if (bg_src == "")
		SetAttrib (new_a, "href", "javascript:UserClickStone (" + stone_id.substr (1) + ")");
	SetAttrib (new_a, "width", "100%");
	SetAttrib (new_a, "height", "100%");
	new_img = AddImageChildNode (new_a, img_src, "stone" + stone_id);

	if (bg_src != "")
	{
		new_td.style.backgroundImage = "url(" + bg_src + ")";
	}

	
	if (stone_id != "")
	{
		SetAttrib (new_td, "id", "td" + stone_id);
	}
	
	if (setsize == "x")
	{
		SetAttrib (new_img, "width", stone_img_size_x);
		SetAttrib (new_img, "height", 1);
	}
	if (setsize == "y")
	{
		SetAttrib (new_img, "width", 1);
		SetAttrib (new_img, "height", stone_img_size_y);
	}
}

//------------------------------------------------------------------------------------------------//

function ShowHint (hinttype)
{
	switch (hinttype)
	{
		case "": SetText ("game_hints", " "); break;
		default:
		{
			SetText ("game_hints", memT (hinttype));
		}
	}
}