
var gamesArray = null;
var hasPreviousPage = false;
var hasNextPage = false;
var selectedGame = null;
var gameclick = false;

/**
 * Parses a game string into a game array.
 **/
function GameList_Parse(gamesString)
{
	this.gamesArray = gamesString.split("|*");
	this.hasPreviousPage = (this.gamesArray[0] == 1);
	this.hasNextPage = (this.gamesArray[1] == 1);

	this.gamesArray = this.gamesArray.slice(2, this.gamesArray.length);

	if (gamesString.length == 6)
	{	
		this.gamesArray = null;
	}
	//alert(gamesString);
}

/**
 * Clears the game list and sets a 'No games found' message.
 **/
function GameList_Clear()
{
	var container = this.GetElementByID("GameListContainer");
	container.innerHTML = "";
}

/**
 * Returns s cut off at length 'length'. If s is cut off, then the last three
 * characters are '...'.
 **/
function GameList_CutOffString(s, length)
{
	if (s.length > length)
	{
		s = s.substr(0, length - 3);
		return s + "...";
	}
	
	return s;
}

/**
 * Called when the script is loaded for the first time.
 **/
function GameList_Load()
{
	var panel = this.GetElementByID("GameListPanel");
	panel.style.display = "";
}

/**
 * Called when the list is about to receive a games update.
 * Shows the loading message.
 **/
function GameList_BeginUpdate()
{
	var container = this.GetElementByID("GameListContainer");
	container.innerHTML = "<img height=\"11\" class=\"gameListSearchingIcon\" src=\"Images/Controls/GameList/Searching.gif\" width=\"11\" />Searching for games...";
			
	this.GameList_SelectGame(0);
}

/**
 * Select the game at the specified index.
 **/
function GameList_SelectGame(gameIndex)
{
	if (this.selectedGame != null)
	{
		this.selectedGame.className = "game";
	}

	if (gameIndex == 0) return;
	
	this.selectedGame = this.GetElementByID("Game" + (gameIndex));
	this.selectedGame.className = "gameSelected";
}

/**
 * Called when there are games available to show in the control.
 **/
function GameList_GamesAvailable(gamesString)
{
	var container = this.GetElementByID("GameListContainer");
	
	this.GameList_Parse(gamesString);
	output = "";	

	if (this.gamesArray == null)
	{
		output += "No games found.";
	}
	else
	{
		output += this.GameList_DrawNavigationControls(true);

		output += "<table id=\"GameList\" cellpadding=\"0\" cellspacing=\"0\">";

		// Draw each game.
		for (var i = 0; i < this.gamesArray.length; i++)
		{
			output += this.GameList_DrawItem(i);
		}

		output += "</table>";
		output += this.GameList_DrawNavigationControls(false);
	}
	
	container.innerHTML = output;
	this.GameList_Check();
}

/**
 * Draws a single game.
 **/
function GameList_DrawItem(index)
{
	var output = "";
	var game = this.gamesArray[index].split('|');
	
	// Watch the length of the strings
	var checkbox = "<input id=\"check" + index + "\" width=12px type=\"checkbox\" onclick=\"GameList_Check()\" onmouseout=\"GameList_mouseout()\">";
	var whitePlayer = this.GameList_CutOffString(game[GAME_LAST_NAME_WHITE] + ", " + game[GAME_FIRST_NAME_WHITE], 20);
	var blackPlayer = this.GameList_CutOffString(game[GAME_LAST_NAME_BLACK] + ", " + game[GAME_FIRST_NAME_BLACK], 20);
	var place = this.GameList_CutOffString(game[GAME_PLACE], 20);
	var result = game[GAME_RESULT] == "&frac12;-&frac12;" ? "&frac12;-&frac12;" : this.GameList_CutOffString(game[GAME_RESULT], 3);
	
	index += 1;

	if (MOZILLA)
	{
		output += "<tr id=\"Game" + index + "\" onclick=\"Game_Click(" + index + ")\" onmouseover=\"Game_MouseOver(this)\" onmouseout=\"Game_MouseOut(this)\">";

		output += "<td>" + game[GAME_NIC_KEY_CODE] + "</td>";
		output += "<td>" + game[GAME_ECO_KEY_CODE] + "</td>";
		output += "<td>&nbsp;" + whitePlayer + "</td>";
		output += "<td>&nbsp;" + blackPlayer + "</td>";
		output += "<td align=\"center\">" + result + "</td>";
		output += "<td>&nbsp;" + place + "</td>";
		output += "<td>&nbsp;" + this.GameList_CreateSource(game) + "</td>";
		output += "<td>" + this.GameList_ParseDate(game[GAME_DATE]) + "</td>";

		output += "</tr>";

	}
	else
	{
	
		output += "<tr id=\"Game" + index + "\" onclick=\"Game_Click(" + index + ")\" onmouseover=\"Game_MouseOver(this)\" onmouseout=\"Game_MouseOut(this)\">";
		output += "<td width=14px>" + checkbox + "</td>";
		output += "<td>" + game[GAME_NIC_KEY_CODE] + "</td>";
		output += "<td>" + game[GAME_ECO_KEY_CODE] + "</td>";
		output += "<td>&nbsp;" + whitePlayer + "</td>";
		output += "<td>&nbsp;" + blackPlayer + "</td>";
		output += "<td align=\"center\">" + result + "</td>";
		output += "<td>&nbsp;" + place + "</td>";
		output += "<td>&nbsp;" + this.GameList_CreateSource(game) + "</td>";
		output += "<td>" + this.GameList_ParseDate(game[GAME_DATE]) + "</td>";

		output += "</tr>";
	}
	
	return output;
}
function GameList_mouseout()
{
	this.gameclick = false;
}
function GameList_ParseDate(date)
{
	var items = date.split('-');
	if(items.length > 1) return items[2];
	return date;
}
function GameList_CreateSource(game)
{
	if(game[GAME_MAGAZINE_NAME] == "" && game[GAME_YEARBOOK_NAME] == "") return "";
	if(game[GAME_MAGAZINE_NAME] == null && game[GAME_YEARBOOK_NAME] == null) return "";
	var source = null;
	var ret = "";
	if(game[GAME_MAGAZINE_NAME] != null && game[GAME_MAGAZINE_NAME] != "")
	{
		source = game[GAME_MAGAZINE_NAME].split(" ");
		ret = "M-" + source[1];
	}	
	if(game[GAME_MAGAZINE_NAME] != null && game[GAME_YEARBOOK_NAME] != "")
	{
		source = game[GAME_YEARBOOK_NAME].split(" ");
		ret += " YB-" + source[1];
	}
	
	return ret;
}
// Show or hide download image
function GameList_Check()
{
	if (MOZILLA) return;
	this.gameclick = true;
	var hasiframe = false;
	if(this.GetElementByID("centercellGameListContainer").innerHTML == "Games" || this.reloadform == false)
	{
		// preload the iframe
		this.GetElementByID("centercellGameListContainer").innerHTML = "Games&nbsp;&nbsp;&nbsp;  <iframe id=\"pgnview\" name=\"pgnview\" width=\"0\" src=\"PgnGames.aspx\"height=\"0\" frameborder=\"0\" style=\"BACKGROUND-COLOR: transparent\"></iframe><input id=dlgames type=\"image\" border=\"0\" class=\"imageButton\" onclick=\"javascript:GameList_DownloadPgnGameFile()\" src=\"../Common/Images/DownloadIcon.gif\" width=\"16\" height=\"16\" style=\"margin-right: 5px;\" alt=\"Download selected games as PGN\" style=\"DISPLAY: none;\" />";
		//pgnview.location.href = "PgnGames.aspx";
		this.reloadform = true;
	}
	for (var i = 0; i < this.gamesArray.length; i++)
	{
		if(this.GetElementByID("check"+i).checked == true)
		{
			hasiframe = true;
			break;
		}
	}
	if(hasiframe == false)
	{
		this.GetElementByID("centercellGameListContainer").innerHTML = "Games";
	}
	else
	{
		this.GameList_ShowHideButton(hasiframe);
	}
	this.gameclick = false;
}
function GameList_ShowHideButton(showButton)
{
	var b = this.GetElementByID("dlgames");
	if(b == null) return;
	if(showButton == false)
	{
		b.style.display = "none";
	}
	else
	{
		b.style.display = "";
	}	
}
// collect the selected gamesID's
function GameList_DownloadPgnGameFile()
{
	var s = "";
	for (var i = 0; i < this.gamesArray.length; i++)
	{
		if(this.GetElementByID("check"+i).checked == true)
		{
			if(s == "") s = i.toString();
			else s = s + ";" + i;
			this.GetElementByID("check"+i).checked = false;
		}
	}
	// and sent it to flash
	this.Board_SetCommandParameter("Pgn", s);
	this.Board_ExecuteCommand("GetGamesInPGN");
}

/**
 * Draws the controls needed to navigate between pages.
 **/
function GameList_DrawNavigationControls(top)
{
	var output = "";
	
	var className = top ? "gameListNavigationPanelTop" : "gameListNavigationPanelBottom";
	
	if (this.hasPreviousPage || this.hasNextPage)
	{
		output += "<table class=\"" + className + "\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">";
		output += "<tr>";
		
		if (this.hasPreviousPage)
		{
			output += "<td><a href=\"javascript:GameList_PreviousPageClick()\">previous page</a></td>";
		}
		
		if (this.hasNextPage)
		{
			output += "<td align=\"right\"><a href=\"javascript:GameList_NextPageClick()\">next page</a></td>";
		}
		
		output += "</tr>";
		output += "</table>";
	}
	return output;
}
/**
 * Called when the user clicks the 'previous page' link.
 * Sends a message to the board to fetch the previous page.
 */
function GameList_PreviousPageClick()
{
	this.Board_ExecuteCommand("PreviousGames");
}

/**
 * Called when the user clicks the 'next page' link.
 * Sends a message to the board to fetch the next page.
 */
function GameList_NextPageClick()
{
	this.Board_ExecuteCommand("NextGames");
}

/**
 * Called when the user clicks on a game.
 * Sends a message to the board to view the game.
 */
function Game_Click(index)
{
	if(this.gameclick == true)
	{
		this.gameclick = false;
		return;
	}
	this.Panel_Show("GameInfoPanel");
	this.GameInfo_BeginUpdate();
//	this.Board_SetCommandParameter("GameString", this.gamesArray[index - 1]);
	this.Board_SetCommandParameter("GameIndex", index - 1);
	this.Board_ExecuteCommand("ViewGame");
	
	this.GameList_SelectGame(index);
}

/**
 * Called when the user moves the mouse over a game.
 */
function Game_MouseOver(tableRow)
{
	tableRow.className = "gameRowOver";
}

/**
 * Called when the mouse is no longer over a game.
 */
function Game_MouseOut(tableRow)
{	
	tableRow.className = (tableRow == selectedGame) ? "gameSelected" : "";
}
