// global vars
var gameParams = {};

/** game API **/
function SDG_closeAndReturn(gameResults)
{
	// send the results to the world (main.swf)
	getChatApp().closeGame(gameResults);
  
	// close the game and re-show the world (main.swf)
	cleanUpExternalGame();
}

SDG_loadComplete = function()
{
    // send the game attributes to the gameSwf
	getExternalGame().SDG_init(gameParams);
}

SDG_getState = function()
{
	var result = "";
	result = getExternalGame().SDG_getState();
	return result;
}

SDG_pause = function()
{
	getExternalGame().SDG_pause();
}

SDG_resume = function()
{
	getExternalGame().SDG_resume();
}

SDG_destroy = function()
{
	getExternalGame().SDG_destroy();
}
/** End game API **/

function cleanUpExternalGame()
{
    $("#gameDiv").hide();
	$("#gameDiv").empty();
	showChatApp();
}

function showChatApp()
{
    fit();
   	swffitOn(); 
}

function hideChatApp()
{
    $("#chatAppSwf").height(0);
  	swffitOff();
}

function swffitOn()
{
   	window.onresize = fit; 
}

function swffitOff()
{
   	window.onresize = null; 
}

function getChatApp()
{
   //return top.document.chatAppSwf;
   return document.getElementById("chatAppSwf");
}

function getExternalGame()
{
	//return top.document.extGame;
	return document.getElementById("extGame");
}

function loadExternalGame(src, w, h, params)
{
    // save the game params here.  We'll pass them to SDG_Init when load_complete is called
    gameParams = params;

    // hide the main world swf
	hideChatApp();

	// add id for swf to load into
	$("<div id=\"gameSwf\"></div>").appendTo("#gameDiv"); 

    // center the game
    var winWidth = $(window).width(); 
    var winHeight  = $(window).height(); 
    var cssW = Math.round((winWidth - w) / 2);
    winWidth -= cssW;
    $("#gameDiv").css({ position:"relative", left:cssW, width:winWidth});
    $("#gameDiv").show();
  
    // now embed the game
	var attributes = {id:"extGame"};
	swfobject.embedSWF(src, "gameSwf", w, h, "9.0.0", "expressInstall.swf", false, {}, attributes);
}



