var searchMonth;
var searchDay;

var playersArray = null;

/**
 * Called when the script is loaded for the first time.
 **/
 
function Today_Load()
{
	var d = new Date()
	searchMonth = d.getMonth() + 1;
	searchDay = d.getDate();
} 
function Today_Draw(playerString)
{
	this.playerArray = playerString.split("|*");
	var players = "";
	if(playerString.length > 0)
	{
		players = this.playerArray.length + " players";
	}
	var container = this.GetElementByID("TodayContainer");
	var output = "<table cellspacing=0>";

	output += "<tr><td>&nbsp;" + this.make_Monthselector(searchMonth-1) + "&nbsp;";
	
	output += "&nbsp;<SELECT id=\"day\" >\r" + this.make_Dayselector(searchMonth-1,searchDay-1) + "</SELECT>\r</td>";

	output += "&nbsp;<td align=\"right\" style=\"padding-top: 5px;\">&nbsp;<img id=\"SearchButtonPlayers\" onclick=\"SearchPlayers_Execute()\" src=\"../Common/Images/SearchButton.gif\" class=\"imageButton\" alt=\"Search\" style=\"margin-right: 10px;\" />";
	output += "&nbsp;" + players + "</td></tr>";
	output += "<tr><td>&nbsp;</td></tr>";
	if(playerString.length > 0)
	{
		for(var i = 0; i < this.playerArray.length; i++)
		{
			output += this.DrawLine(i);
		}
	}
	output += "</table>";
	container.innerHTML = output;
}

function make_Monthselector(selMonth)
{
	var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	
	var output = "<SELECT id=\"month\" >\r";
	var selected = "";
	for(var i = 0;i<12;i++)
	{
		if(i == selMonth) selected = "selected";
		else selected = "";
		output += "<OPTION value=\"" + (i+1) + "\" " + selected + " >" + months[i] + "</OPTION>\r";
	}
	output += "</SELECT>\r";
	return output;
}

function make_Dayselector(selMonth,selDay)
{
	var output = "";
	var selected = "";
	for(var i = 0;i < 31;i++)
	{
		if(i == selDay) selected = "selected";
		else selected = "";
		output += "\t<OPTION value=\"" + (i+1) + "\" " + selected + " >" + (i+1) + "</OPTION>\r";
	}
	return output;
}

function Players_DataAvailable(infoString)
{
	Today_Draw(infoString);
}

function SearchPlayers_Execute()
{
	var a;
	if(this.GetElementByID("month") != null)
	{
		a = this.GetElementByID("month").selectedIndex;
		searchMonth = Math.floor(this.GetElementByID("month").options[a].value);
	}
	if(this.GetElementByID("day") != null)
	{
		a = this.GetElementByID("day").selectedIndex;
		searchDay = Math.floor(this.GetElementByID("day").options[a].value);
	}
	Board_SetCommandParameter("PlayerDate", (searchMonth * 100) + searchDay);
	Board_ExecuteCommand("GetPlayers");	
	this.GetElementByID("TodayContainer").innerHTML = "<img height=\"11\" class=\"gameListSearchingIcon\" src=\"Images/Controls/GameList/Searching.gif\" width=\"11\" />Searching...";;
}

function DrawLine(index)
{
	var output = "";
	var player = this.playerArray[index].split('|');

	if (MOZILLA)
	{
		output += "<tr><td>";
		output += "<a width=\"100px\" id=\"Player" + index + "\" class=\"game\" href=\"javascript:PlayerList_Click('" + player[PLAYER_INFO_ID] + "')\">";
		
		output += "&nbsp;" + player[PLAYER_INFO_FIRST_NAME] + " " + player[PLAYER_INFO_LAST_NAME] + "&nbsp;";
		output += "&nbsp;(" + player[PLAYER_INFO_BIRTH_DATE] + ")&nbsp;";
		output += "</a><td align=center><img height=\"10\" class=\"countryFlag\" src=\"Images/Controls/GameInfo/Flags/" + player[PLAYER_INFO_COUNTRY_CODE] + ".gif\" width=\"17\" alt=\""+ player[PLAYER_INFO_COUNTRY_CODE] + "\" /></td>";
		if(player[PLAYER_INFO_IMAGE] != "NoPictureAvailable.jpg")
		{
			output += "<td><img  class=\"ImageAvailable\" src=\"Images/Controls/GameInfo/Image.gif\" width=\"17\" alt=\"Image available\" /></td>";
		}
		output += "</td></tr>";
	}
	else
	{
		output += "<tr id=\"Player" + index + "\" onclick=\"PlayerList_Click(" + player[PLAYER_INFO_ID] + ")\" onmouseover=\"Player_MouseOver(this)\" onmouseout=\"Player_MouseOut(this)\">";

		output += "<td>" + player[PLAYER_INFO_FIRST_NAME] + " " + player[PLAYER_INFO_LAST_NAME] + "</td>";
		output += "<td>" + player[PLAYER_INFO_BIRTH_DATE] + "</td>";
		if(player[PLAYER_INFO_COUNTRY_CODE]!= "   ")
		{
			output += "<td align=center><img height=\"10\" class=\"countryFlag\" src=\"Images/Controls/GameInfo/Flags/" + player[PLAYER_INFO_COUNTRY_CODE] + ".gif\" width=\"17\" alt=\""+ player[PLAYER_INFO_COUNTRY_CODE] + "\" /></td>";
		}
		else
		{
			output += "<td></td>";
		}
		if(player[PLAYER_INFO_IMAGE] != "NoPictureAvailable.jpg")
		{
			output += "<td><img  class=\"ImageAvailable\" src=\"Images/Controls/GameInfo/Image.gif\" width=\"17\" alt=\"Image available\" /></td>";
		}
		else
		{
			output += "<td>&nbsp;</td>";
		}
		output += "<td>" + player[PLAYER_INFO_CURRENT_RANKING_ELO] + "</td>";		

		output += "</tr>";
	}
	return output;
}
/**
 * Called when the user moves the mouse over a game.
 */
function Player_MouseOver(tableRow)
{
	tableRow.className = "playerRowOver";
}

/**
 * Called when the mouse is no longer over a game.
 */
function Player_MouseOut(tableRow)
{	
	tableRow.className = (tableRow == selectedGame) ? "gameSelected" : "";
}
/**
 * Called when the user clicks on a player name.
 * Sends the player id to the server.
 **/
function PlayerList_Click(playerID)
{
	this.Panel_Show("PlayerInfoPanel");
	this.PlayerInfo_BeginUpdate();
	
	this.Board_SetCommandParameter("PlayerID", playerID);
	this.Board_ExecuteCommand("GetPlayer");
}
