
var selectedMove = null;
var pgnString = null;
var pgnFileName = null;

/**
 * Called when the script is loaded for the first time.
 **/
function PlayerInfo_Load()
{
	var container = this.GetElementByID("PlayerInfoContainer");
	container.innerHTML = "No player selected.";
}

/**
 * Clears the game info.
 **/
function PlayerInfo_MakeEmpty()
{
	var container = this.GetElementByID("PlayerInfoContainer");
	container.innerHTML = "No player selected.";
}

/**
 * Called when the info panel is about to receive a game update.
 * Shows the loading message.
 **/
function PlayerInfo_BeginUpdate()
{
	var container = this.GetElementByID("PlayerInfoContainer");
	container.innerHTML = "<img height=\"11\" class=\"playerInfoLoadingIcon\" src=\"Images/Controls/PlayerInfo/Loading.gif\" width=\"11\" />Loading...";
}

/**
 * Called when there is a game available to show in the control.
 **/
function PlayerInfo_DataAvailable(infoString)
{
	var infoFields = infoString.split('|');
	var container = this.GetElementByID("PlayerInfoContainer");
	
	var output = "";
	
	output += this.PlayerInfo_DrawInfo(infoFields);
	container.innerHTML = output;
	
	this.Panel_Show("PlayerInfoPanel");
	if(this.currentPlayer) 
	{
		this.Board_SetCommandParameter("PlayerID", this.currentPlayer);
		this.Board_ExecuteCommand("GetGamesByID");			
		this.currentPlayer = null;
	}
}

/**
 * Draws the standard info for a game.
 **/
function PlayerInfo_DrawInfo(infoFields)
{
	var output = "<INPUT TYPE=\"HIDDEN\" id=\"playerID\" value=\"" + infoFields[PLAYER_INFO_ID] + "\">";;
	
	output += "<div id=\"PlayerInfo\">";

	output += "<img src=\"../Common/Images/Authors/" + infoFields[PLAYER_INFO_IMAGE] + "\" ";
	output += "border=\"0\" ";
	output += "class=\"playerInfoImage\" ";
	output += "title=\"" + this.PlayerInfo_GeneratePhotoCredits(infoFields[PLAYER_INFO_IMAGE]) + "\" /><br />"; 
	if( infoFields[PLAYER_INFO_TEXT] != "")
	{
		output += "<table><tr><td width=\"150\">";
	}
	else
	{
		output += "<table><tr><td>";
	}	
	output += "<b>" + infoFields[PLAYER_INFO_FIRST_NAME] + " " + infoFields[PLAYER_INFO_LAST_NAME] + "</b>";
	if( infoFields[PLAYER_INFO_TEXT] != "")
	{
		output += "</td><td><img id=\"moreInfoButton\" onclick=\"moreInfo()\" src=\"../Common/Images/MoreInfo.gif\" class=\"imageButton\" alt=\"More info\" height=12; style=\"margin-left: 0px;\" />";
	}
	else
	{
		output += "</td><td><strong>&nbsp;</strong>";
	}
	output += "</td></tr></table>";
	
	output += "</div><div id=\"info1\" style=\"DISPLAY: \"\";\">";
	
	output += "<table border=\"0\" cellpadding=\"1\" cellspacing=\"1\">";
	
	if ((infoFields[PLAYER_INFO_FIDE_ID] != "")
		&& (infoFields[PLAYER_INFO_FIDE_ID] != "-1"))
	{
		output += "<tr>";
		output += "<td width=\"150\">" + "FIDE number" + "</td>";
		output += "<td>" + infoFields[PLAYER_INFO_FIDE_ID] + "</td>";
		output += "</tr>";
	}

	if (infoFields[PLAYER_INFO_BIRTH_DATE] != "")
	{
		output += "<tr>";
		output += "<td width=\"150\">" + "Birthdate" + "</td>";
		output += "<td>" + infoFields[PLAYER_INFO_BIRTH_DATE] + "</td>";
		output += "</tr>";
	}

	if (infoFields[PLAYER_INFO_DEATH_DATE] != "")
	{
		output += "<tr>";
		output += "<td width=\"150\">" + "Deceased" + "</td>";
		output += "<td>" + infoFields[PLAYER_INFO_DEATH_DATE] + "</td>";
		output += "</tr>";
	}

	if (infoFields[PLAYER_INFO_COUNTRY_CODE] != "" && infoFields[PLAYER_INFO_COUNTRY_CODE] != "   ")
	{
		output += "<tr>";
		output += "<td width=\"150\">" + "Nationality" + "</td>";
		output += "<td>" + infoFields[PLAYER_INFO_COUNTRY_CODE];
		output += " <img height=\"10\" class=\"countryFlag\" src=\"Images/Controls/GameInfo/Flags/" + infoFields[PLAYER_INFO_COUNTRY_CODE] + ".gif\" width=\"17\" alt=\""+ infoFields[PLAYER_INFO_COUNTRY_CODE] + "\" />";
		output += "</td>";
		output += "</tr>";
	}

	output += "<tr>";
	output += "<td width=\"150\">Games</td>";
	output += "<td align=\"left\" style=\"padding-top: 5px;\"><img id=\"SearchGamesButton\" onclick=\"Search_Games(" + infoFields[PLAYER_INFO_ID] + ")\" src=\"../Common/Images/SearchButton.gif\" class=\"imageButton\" alt=\"Search\" style=\"margin-left: 0px;\" /></td>";
	output += "</tr>";
	
	if (infoFields[PLAYER_INFO_CURRENT_RANKING_DATE] != "")
	{
		output += "<tr>";
		if ((infoFields[PLAYER_INFO_FIDE_ID] != "")
			&& (infoFields[PLAYER_INFO_FIDE_ID] != "-1"))
		{
			output += "<td width=\"150\">" + "Elo (" + infoFields[PLAYER_INFO_CURRENT_RANKING_DATE] + ")</td>";
		}
		else
		{
			output += "<td width=\"150\">" + "Elo (historical)</td>";
		}
		output += "<td>" + infoFields[PLAYER_INFO_CURRENT_RANKING_ELO] + "</td>";
		output += "</tr>";
	}
	if(infoFields[PLAYER_INFO_CURRENT_ICCF_ELO]  != "")
	{
		iccfDate = "";
		if(infoFields[PLAYER_INFO_CURRENT_ICCF_DATE] != "")
		{
			iccfDate = " (" + infoFields[PLAYER_INFO_CURRENT_ICCF_DATE] + ")";
		}
		output += "<tr>";
		output += "<td>ICCF" + iccfDate + "</td><td>" + infoFields[PLAYER_INFO_CURRENT_ICCF_ELO] + "</td>";
		output += "</tr>";
	}

	output += "</table>";

	if ((infoFields[PLAYER_INFO_CURRENT_RANKING_DATE] != "")
		&& (infoFields[PLAYER_INFO_FIDE_ID] != "")
		&& (infoFields[PLAYER_INFO_FIDE_ID] != "-1"))

	{
		output += "<b>Rating History</b><br />";
		output += "<img border=\"0\" src=\"RankingHistory.aspx?PlayerID=" + infoFields[PLAYER_INFO_ID] + "\" alt=\"rating history\" width=\"250\" height=\"125\" />";
	}
	
	output += "</div>";
	if( infoFields[PLAYER_INFO_TEXT] != "")
	{
		output += "<div id=\"info2\" style=\"DISPLAY: none\";><table border=\"0\" cellpadding=\"1\" cellspacing=\"1\"><tr><td>";
		output += infoFields[PLAYER_INFO_TEXT];
		output += "</td></tr></table></div>";
	}
	return output;
}
/***
* Show more Info if exits
**/
function moreInfo()
{
	if(this.GetElementByID("info2") != null)
	{
		if(this.GetElementByID("info2").style.display == "")
		{
			this.GetElementByID("info1").style.display = "";
			this.GetElementByID("info2").style.display = "none";		
		}
		else
		{
			this.GetElementByID("info2").style.display = "";
			this.GetElementByID("info1").style.display = "none";	
		}
	}
}
/** 
* Search for the games of this player
**********/

function Search_Games(playerID)
{
	if(this.GetElementByID("playerID") != null)
	{
		Board_SetCommandParameter("PlayerID", playerID);
		Board_ExecuteCommand("GetGamesByID");			
	}
}

/**
 * Returns a string with the photo credit according 
 * to the ImageFileName.
 **/
function PlayerInfo_GeneratePhotoCredits(imageFileName)
{
	var value = "";
	
	// Cut off the '.jpg'
	imageFileName = imageFileName.substr(0, imageFileName.length-4);
	
	var a = imageFileName.split('-');
	
	// If no credits available return 'Photo'
	if (a.length != 3) return "Photo";
	
	// Set year
	if (a.length > 2 && parseInt(a[2]) > 0)
	{
		value += a[2] + " ";
	}
	
	// Set author
	switch (a[1])
	{
		case "A":
			value += "Rupert van der Linden";
			break;
			
		case "B":
			value += "Gerard de Graaf";
			break;
		
		case "D":
			value += "Frits Agterdenbosch";
			break;

		case "E":
			value += "Bas Beekhuizen";
			break;

		case "F":
			value += "Hartmut Metz";
			break;

		case "G":
			value += "Asher Molet";
			break;

		case "H":
			value += "Sergey Sorokktis";
			break;

		case "I":
			value += "Joris van Velzen";
			break;

		case "J":
			value += "Ren&eacute; Olthof";
			break;

		case "K":
			value += "Rosa de las Nieves";
			break;

		case "L":
			value += "Jerome Bibuld";
			break;

		case "M":
			value += "Anne Hees";
			break;

		case "O":
			value += "Sabine Kaufman";
			break;

		case "P":
			value += "Arvind Aaron";
			break;

		case "Q":
			value += "Dagobert Kohlmeyer";
			break;
			
		case "T":
			value += "schakers-info";
			break;

		case "U":
			value += "Nofzai archive";
			break;

		case "Z":
			value += "unknown";
			break;
			
		case "S":
			value += "John Fernandez";
			break;
	
		case "C":
		default:
			value += "NIC Archief";
			break;
	}
	
	value += ". &copy; Interchess B.V.";

	return value;
}

