﻿//-----------------------------------------------------------
// Functions called by the site that do not communicate w/ the api/minigame
//-----------------------------------------------------------
// 100518
//Remove div w/ embedded game and replace it with an empty element
//
function removeGame(gameDivName)
{
	var el = document.getElementById(gameDivName);
   
   if(el){
   
	  var div = document.createElement("div");
	  el.parentNode.insertBefore(div, el);
 
	  //Remove the SWF
	  swfobject.removeSWF(gameDivName);
   
	  //Give the new DIV the old element's ID
	  div.setAttribute("id", gameDivName);
	  div.style.display = 'none';  
   }
   
}
// Embed game swf into the game div
//
function loadGame(url, basePath, gameWidth, gameHeight, apiUrl, apiConfigUrl, gameConfigUrl, containerSize, gameDivName)
{
	if (typeof containerSize == "undefined") {
		containerSize = "none";
	}
	
   //Check for existing SWF
   if(isObject(gameDivName)){
   
	  //replace object/element with a new div
	  removeGame(gameDivName);
	  
   }
   
   if (containerSize == "standard" || containerSize == "xlarge")
   {
	   if (containerSize == "standard")
	   {
		   containerWidth = 580;
		   containerHeight = 410;
	   }
	   else if (containerSize == "xlarge")
	   {
		   containerWidth = 750;
		   containerHeight = 500;  
	   }
		   
	   if ((containerWidth - gameWidth) < (containerHeight - gameHeight))
		  scaleFactor = containerWidth/gameWidth;
	   else
		  scaleFactor = containerHeight/gameHeight;
				
	   gameWidth = gameWidth * scaleFactor;
	   gameHeight = gameHeight * scaleFactor;
   }
			
	var flashvars = {};
	flashvars["apiUrl"] = escape(apiUrl);
	flashvars["apiConfigUrl"] = escape(apiConfigUrl);
	flashvars["gameConfigUrl"] = escape(gameConfigUrl);
	var attributes = {
		styleclass: gameDivName,
		wmode: "opaque"
	};
	var params = {
		menu: "false",
		bgcolor: "#000000",
		quality: "high",
		base: basePath,
		allowScriptAccess: "always"
	};

	  swfobject.embedSWF(url, gameDivName, gameWidth, gameHeight, "10.0.0", "expressInstall.swf", flashvars, params, attributes);
	  
	  // re-position the player
	  var gc = document.getElementById(gameDivName);
	  gc.style.left = adjustGCX(gc.style.width) + 'px';
}

// Toggles the visibility of the game
//
function toggleGameVisibility(gameDivName, isVisible)
{
	var gc = document.getElementById(gameDivName);
	
	if (isVisible == true)
		gc.style.visibility = 'visible';
	else
		gc.style.visibility = 'hidden';
}

//Support function: checks to see if target
//element is an object or embed element
//
function isObject(targetID)
{

   var isFound = false;
   var el = document.getElementById(targetID);
   
   if(el && (el.nodeName === "OBJECT" || el.nodeName === "EMBED")){
   
	  isFound = true;
   
   }
   
   return isFound;
}

//-----------------------------------------------------------
// Functions called by the api/minigame to communicate with the site
//-----------------------------------------------------------

// notify the site that the game has destroyed and can be removed
//
function onDestroy(siteDivName)
{
	document[siteDivName].onDestroy();
}

// notify the site that the game has responded to the setPause() request
//
function onSetPause(siteDivName, paused)
{
	document[siteDivName].onSetPause(paused);
}

// notify the site that the user has changed the in-game volume
//
function onSetVolume(siteDivName, volume)
{
	document[siteDivName].onSetVolume(volume);
}

// request the site volume level
//
function getVolume(siteDivName)
{
	return document[siteDivName].getVolume();
}

// request the username
//
function getUsername(siteDivName)
{
	return document[siteDivName].getUsername();
}

// request the userId
//
function getUserId(siteDivName)
{
	return document[siteDivName].getUserId();
}

// send startGame() metagame message to site
//
function startGame(siteDivName, message)
{
	return document[siteDivName].startGame(message);
}

// send endGame() metagame message to site
//
function endGame(siteDivName, message) // call made for conduit
{
	endGameOpen(siteDivName,message); // trigger conduit on game page
	return document[siteDivName].endGame(message);
}

// send startLevel() metagame message to site
//
function startLevel(siteDivName, message)
{
	return document[siteDivName].startLevel(message);
}

// send endLevel() metagame message to site
//
function endLevel(siteDivName, message)
{
	return document[siteDivName].endLevel(message);
}

// send submitGameEvent() metagame message to site
//
function submitGameEvent(siteDivName, message)
{
	return document[siteDivName].submitGameEvent(message);
}

// attempt to save data on the site's server
//
function storeData(siteDivName, data)
{
	document[siteDivName].storeData(data);
}

// attempt to retrieve data on the site's server
//
function retrieveData(siteDivName)
{
	document[siteDivName].retrieveData();
}

// attempt to delete data on the site's server
//
function deleteData(siteDivName)
{
	document[siteDivName].deleteData();
}

//-----------------------------------------------------------
// Functions called from the site to communicate info to the api/minigame
//-----------------------------------------------------------

// notify api that the game needs to destroy
//
function destroy(gameDivName)
{
	document[gameDivName].destroy();
}

// notify the api that the game needs to change its pause state
//
function setPause(gameDivName, paused)
{
	document[gameDivName].setPause(paused);
}

// notify the api that the game needs to change its volume
//
function setVolume(gameDivName, volume)
{
	document[gameDivName].setVolume(volume);
}

// notify the api of the success or failure of the server save
//
function onStoreData(gameDivName, saved, err)
{
	document[gameDivName].onStoreData(saved, err);
}

// notify the api of the success or failure of the server retrieval
//
function onRetrieveData(gameDivName, data, err)
{
	document[gameDivName].onRetrieveData(data, err);
}

// notify the api of the success or failure of the server delete
//
function onDeleteData(gameDivName, deleted, err)
{
	document[gameDivName].onDeleteData(deleted, err);
}

