
// *********** The following code sets up the return calls from Platform to Game
// *********** SetupJSToGameInterface(identifier) should be called once upon page initialization
// ***** Start block *****************************************************************

var g_GameApplicationLang;
var g_mute = "on";
var g_gameContinue = "on";
var g_gameReady=false;
var g_blnSupportsFocus=false;

SetupJSToGameInterface("FLASH5.0")

function WriteAPILog(message)
{
	if(typeof(g_LogAPI)!="undefined")
	{
		WriteLog(message);
	}
	return;
}

function DebugAlert(message)
{
	if(typeof(g_Debug)!="undefined")
	{
		alert(message);
	}
	return;
}


function thisGame(gameName) 
{
		return document.getElementById(gameName);
}

function SetupJSToGameInterface(identifier)
{
	switch(identifier)
	{
		case "FLASH5.0":
			g_GameApplicationLang="FLASH5.0";
			g_blnSupportsFocus=true;			
			break;

		case "ACTIVEX":
			DebugAlert("GameApplicationLang is not supported");
			break;

		default:
			DebugAlert("GameApplicationLang is not supported");
			break;
	}
	return;
}

function ToggleMute(){
	if (g_mute=="off")
		g_mute = "on"
	else
		g_mute="off"	
}


function SetFocusOnGame()
{
	if(g_blnSupportsFocus)
	{
		thisGame("GameObject").focus();
	}
}

// ***** End block *******************************************************************

// ********** The following Javascript functions are called by the Game **************
// ***** Start block *****************************************************************

function CustomEvent(customXML)
{
	if (customXML == "<gamedata><upsell>"+"1"+"</upsell></gamedata>")
	{
		OpenUpsellWindow();
		return;
	}
	if (customXML == "<gamedata><upsell>"+"2"+"</upsell></gamedata>")
	{
		OpenUpsellWindow();
		return;
	}
	if (customXML == "<gamedata><upsell>"+"3"+"</upsell></gamedata>")
	{
		OpenUpsellWindow();
		return;
	}
	return;
}

function GameBreak(customXML)
{
//	GameContinue();
	return;
}

function GameEnd(customXML)
{
//	CustomReturn();
	return;
}

function GameReady(customXML)
{
//	GameStart();
	return;
}

function SessionReady(customXML)
{
//	SessionStart();
	return;
}

function ScoreBroadcast(customXML)
{
	return;
}

function ScoreSubmit(customXML)
{
//	GameMenu();
	return;
}

function GameError(customXML)
{
	return;
}

function DataBroadcast(customXML)
{
	return;
}


function PropertyRead(customXML)
{
	return;
}

function PropertyWrite(customXML)
{
	return;
}

function GameLog(customXML)
{
}

function LoadBroadcast(customXML)
{
	return;
}

// ********** End block **************************************************************

// ********** The following Javascript functions make calls into the Game **************
// ***** Start block *****************************************************************
function CustomReturn(customXML)
{
		try
		{
			WriteAPILog("COMMON:CustomReturn Args" + customXML);
			thisGame("GameObject").SetVariable("CustomData", customXML);
			thisGame("GameObject").TCallLabel("/","CustomReturn");
		}
		catch(e)
		{
			DebugAlert("CustomReturn()" + " " + e +  " " + e.number + " " + e.description);
		}
	return;
}

function GameContinue(customXML)
{
	if (g_gameContinue == "off")
		return;
		
	SetFocusOnGame();
	try
	{
		WriteAPILog("COMMON:GameContinue Args" + customXML);
		thisGame("GameObject").SetVariable("GameContinueData", customXML);
		thisGame("GameObject").TCallLabel("/","GameContinue");
	}
	catch(e)
	{
		DebugAlert("GameContinue()" + " " + e +  " " + e.number + " " + e.description);
	}
	return;
}

function GameMenu()
{
	SetFocusOnGame();
	try
	{
		WriteAPILog("COMMON:GameMenu");
		thisGame("GameObject").TCallLabel("/","GameMenu");	
	}
	catch(e)
	{
		DebugAlert("GameMenu()" + " " + e +  " " + e.number + " " + e.description);
	}
	return;
}

function GameStart()
{
	
	g_gameReady = true; //mute and pause now enabled
	g_gameContinue = "on"; //enable continue state
	SetFocusOnGame();
	try
	{
		WriteAPILog("COMMON:GameStart");
		thisGame("GameObject").TCallLabel("/","GameStart");					
	}
	catch(e)
	{
		DebugAlert("GameStart()" + " " + e +  " " + e.number + " " + e.description);
	}
	return;
}

function GameMute(muteStatus)
{

	if (g_gameReady==false)
		return;

	try
	{
		WriteAPILog("COMMON:GameMute Args " + muteStatus);
		if (muteStatus.toLowerCase() == "on")
			thisGame("GameObject").TCallLabel("/","MuteOn");
		else
			thisGame("GameObject").TCallLabel("/","MuteOff");			
		ToggleMute();	
	}
	catch(e)
	{
		DebugAlert("Mute(" + muteStatus + ")" + " " + e +  " " + e.number + " " + e.description);
	}
	return;
}

function SessionStart()
{
	SetFocusOnGame();
	try
	{
		WriteAPILog("COMMON:SessionStart");
		thisGame("GameObject").TCallLabel("/", "SessionStart");			
	}
	catch(e)
	{
		DebugAlert("SessionStart()" + " " + e +  " " + e.number + " " + e.description);
	}
	return;
}
       
function GamePause(pauseStatus)
{
	if (g_gameReady==false)
		return;			

		if (pauseStatus.toLowerCase() != "on")
			SetFocusOnGame();

		try
		{
			WriteAPILog("COMMON:GamePause Args " + pauseStatus);
			if (pauseStatus.toLowerCase() == "on")
				thisGame("GameObject").TCallLabel("/","PauseOn");
			else
				thisGame("GameObject").TCallLabel("/","PauseOff");
		}
		catch(e)
		{
			DebugAlert("GamePause()" + " " + e +  " " + e.number + " " + e.description);
		}
	return;
}

function PropertyReturn(customXML)
{
	try
	{
		WriteAPILog("COMMON:PropertyReturn Args " + customXML);
		GameObject.SetVariable("MyVar", customXML);
		thisGame("GameObject").TCallLabel("/","PropertyReturn");		
	}
	catch(e)
	{
		DebugAlert("PropertyReturn()" + " " + e +  " " + e.number + " " + e.description);
	}
	return;
}

function GameObject_DoFSCommand(command, arguments)
{
	//alert(command + " " + arguments);
	switch(command)
	{
		case "CustomEvent":
			CustomEvent(arguments);
			break;
		case "WebUpsell":
			WebUpsell(arguments);
			break;
		case "GameBreak":
			GameBreak(arguments)
			break;
		case "GameEnd":
			GameEnd(arguments);
			break;
		case "GameReady":
			GameReady(arguments);
			break;
		case "SessionReady":
			SessionReady(arguments);
			break;
		case "ScoreBroadcast":
			break;
		case "ScoreSubmit":
			ScoreSubmit(arguments);
			break;
		case "GameError":
			GameError(arguments);
			break;
		case "DataBroadcast":
			DataBroadcast(arguments);
			break;
		case "PropertyRead":
			PropertyRead(arguments);
			break;
		case "PropertyWrite":
			PropertyWrite(arguments);
			break;
		case "GameLog":
			GameLog(arguments);
			break;
		case "LoadBroadcast":
			LoadBroadcast(arguments);
			break;
		default:
			DebugAlert("Game function " + command + " is not supported");
			window.external.UnsupportedAPI(command, arguments);
			break;
	}
	return;
}

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 GameObject_FSCommand(ByVal command, ByVal args)\n');
  document.write(' call GameObject_DoFSCommand(command, args)\n');
  document.write('end sub\n');
  document.write('</SCRIPT\> \n');
} 
// ********** End block **************************************************************
