
var board = null;
var reloadform = false;
var currentPlayer = null;
var searchGame = null;
/**
 * Called when the Flash file sends a message to the html page.
 **/
function Board_DoFSCommand(command, args)
{
	switch (command)
	{
		
		case "Load":
			this.Board_Load();
			break;
	
		case "GameList_BeginUpdate":
			this.GameList_BeginUpdate(args);
			break;
			
		case "GameList_GamesAvailable":
			this.GameList_GamesAvailable(args);
			if(this.searchGame)
			{
				Game_Click(1);
				this.searchGame = "";
			}
			break;

		case "GameInfo_MakeEmpty":
			this.GameInfo_MakeEmpty();
			this.GameList_SelectGame(0);
			break;
		
		case "GameInfo_DataAvailable":
			if (!this.GameInfo_GameHasHeader(args.split('|')))
				this.GameList_SelectGame(0);
				
			this.GameInfo_DataAvailable(args);
			break;
		
		case "GameInfo_MoveNumberChanged":
			this.GameInfo_SelectMove(parseInt(args));
			break;
		
		case "NicKeyTree_NicKeysAvailable":
			this.NicKeyTree_NicKeysAvailable(args);
			break;
		
		case "NicKeyTree_MovesAvailable":
			this.NicKeyTree_MovesAvailable(args);
			break;

		case "ClassificationInfo_NicKeyAvailable":
			this.NicKeyTree_SetRootNode(1, args.split('|'), "ClassificationTreeContainer");
			this.Panel_Show("ClassificationInfoPanel");
			break;

		case "PlayerInfo_DataAvailable":
			this.PlayerInfo_DataAvailable(args);
			break;
			
		case "Players_DataAvailable":
			this.Players_DataAvailable(args);
			break;
		
		case "GetGamesInPGN":
			// set pgn text in form
			var a = pgnview.document.forms[0];
			a.TextBox1.value = args;
			// submit the hidden form
			var theform;
			if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
				theform = pgnview.document.PgnGames;
			}
			else {
				theform = pgnview.document.forms["PgnGames"];
			}
			theform.submit();
			this.GameList_ShowHideButton(false);
			this.reloadform = false;
			break;
		case "Error":
			alert(args);
			break;
	}
}

/**
 * Called when the script is loaded for the first time.
 **/		
function Board_Load()
{
	this.board = MOZILLA ? document.Board : this.GetElementByID("Board");

	// Give Server domain to Flash
	this.Board_SetCommandParameter("Server", location.host.toLowerCase());

	// Ensure netscape compatibility.
	this.Board_SetCommandParameter("EscapeQuotes", MOZILLA ? "True" : "False");
	//return;
	if(this.currentPlayer)
	{
		this.Panel_Show("PlayerInfoPanel");
		this.PlayerInfo_BeginUpdate();

		this.Board_SetCommandParameter("PlayerID", this.currentPlayer);
		this.Board_ExecuteCommand("GetPlayer");
	}
	if(this.searchGame)
	{
		this.Panel_Show("GameInfoPanel");
		this.GameInfo_BeginUpdate();
		this.Search_GetGame(this.searchGame);
	}
}

function Board_ExecuteCommand(command)
{
	if(this.board == null) this.Board_Load();
	this.board.SetVariable("Command", command);
}

function Board_SetCommandParameter(paramName, paramValue)
{
	if(this.board == null) this.Board_Load();
	this.board.SetVariable(paramName, paramValue);
}
// show
function showKey()
{
	var infoPanel = this.GetElementByID("GameInfoPanel");
	if(infoPanel.style.display == "")	
	{
		a = 0;
		switch(event.keyCode)
		{
		case 35 : a = 5555;
			break;
		case 36: a = -5555;
			break;
		case 37: a = -1;
			break;
		case 33:
		case 38: a = -5;
			break;
		case 39: a = 1;
			break;
		case 34:
		case 40: a = 5;
			break;
		}
		this.currentMove += a;
		if(this.currentMove < 0) this.currentMove = 0;
		this.Move_Click(this.currentMove);
	}
}
// Look for Internet Explorer.
var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
//
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && 
	navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1)
{
	document.write('<SCRIPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	document.write('Sub Board_FSCommand(ByVal command, ByVal args)\n');
	document.write('  call Board_DoFSCommand(command, args)\n');
	document.write('end sub\n');
	document.write('</script\> \n');
}
