/**
* This is the popjax user id of the person playing the game.  If it is null, then the user playing the game 
* is not logged on.
*/
var m_pjuid = null;

var m_pjuidforinvites = null;

var m_fbname = null;

var m_gameId = null; // 190;

var m_categoryIds = null;

var m_gameType = null;

var m_pjusername = null;

var m_gohome = null;

var m_origin = null;

var m_preview = "false";

var m_webAppSessionId = null;

var m_numberOfSuggestedGamesToShow = Number(6);

var m_gamePageJsp = "/game/standAloneGame.jsp";

var m_suggestedGameLabelColor =  "#000033";

var m_reportingOrigin = null;

var m_reportingRef = null;

var m_isSkipAheadQuestion = false;
var m_didSeekForSkipAhead = false;
var m_loadingVideoOverlayShowing = false;

var m_adDartId = null;
var m_minimumMSToShowAd = 15000;
var m_allowAdSkip = false;

var m_suppressDrawingWinnerAnimations = false;

var m_affiliateId = null;

//var m_numberOfSuggestedGames = 3;

/**
* By default, do show lame animation at end of game.
*/
var m_showEndOfGameAnimation = true;

var m_skinnedGamesChooserUrl = null;

//var m_dataServiceTraceString = "";

/**
* Contains the current score for the game being played.
*/
var m_currentGameScore = 0;

var m_numberOfQuestionsAnswered = 0;

var m_clearTheBoardGame = false;

var m_isFacebookGame = false;

var m_isQuestionPreview = false;

/**
* Only used for single question mode.
*/
var m_singleQuestionId = 0;

var GAMEBOARD_SIZE_3x3 = "3x3";
var GAMEBOARD_SIZE_4x4 = "4x4";

/**
* Is "true" if the Flash gameboard is loaded.
*/
var m_gameBoardLoaded = false;

var m_gameBoardSize = GAMEBOARD_SIZE_4x4;

var m_isSpinGame = "false";

var m_gameboardLeftPos = 100;

var m_gameAchievementMessageContainerWidth = 900;
var m_gameAchievementMessageContainerHeight = 600;
var m_gameAchievementMessageContainerMarginLeft = "auto";

var m_numberOfQuestionBeingAnswered = 0;
var m_questionAnsweredToGoogleAnalyticsCode = null;

/**
* SLW 601.  The path to the popjax video player.
*/
var m_popjaxVideoPlayerUrl = "/content/game/VBChromelessPlayer.swf"; //?_enableJSApi=1"; //http://gdata.youtube.com/apiplayer?enablejsapi=1&playerapiid=ytplayer";

/**
* SLW 601.  Another level of containment to handle switching between you tube player, popjax player, and the other players.
*/
var m_idOfYouTubePlayerHolderDiv = "youTubePlayerHolderDiv";
var m_idOfPopJaxPlayerHolderDiv = "popJaxPlayerHolderDiv";

var YOU_TUBE_LOAD_VIDEO_FUNCTION = "loadVideoById";
var POP_JAX_LOAD_VIDEO_FUNCTION = "loadVideoByUrl";


//var m_doRandomizeGameSuggestions = true;



// ?videoPath=http://67.192.74.244/game/resources.active/largeVideos/28_Family_Movies.flv&caption=Talk Show Moments";

/**
* The associative array of parameters to the "start game" message we send to the back end.
*/
var m_startGameParms = null;

// var m_videoWidth = "500px";
//  var m_videoHeight = "400px";

var ORIGIN_SITE     = "site";
var ORIGIN_FACEBOOK = "facebook";

var LEVEL_NAME_POPQUIZ   = "popquiz";
var LEVEL_NAME_POP4      = "popjax4";
var LEVEL_NAME_POPQUIZ16 = "popquiz16";
var LEVEL_NAME_POPSPIN   = "popspin";

var LEVEL_POP4_GAME    = 1;
var LEVEL_POPSPIN_GAME = 2;
var LEVEL_HIT_GAME     = 3;

var GAME_TYPE_HIT      = "HIT";
var GAME_TYPE_CUSTOM   = "CUSTOM";
var GAME_TYPE_POP4     = "POP4";
var GAME_TYPE_POPSPIN  = "POPSpin";

var m_currentGameType  = null;

var m_gameWidth        = "550px";
var m_gameHeight       = "400px";

/** The size of the embedded video. */
var m_videoWidth       = "512px";
var m_videoHeight      = "380px";

var forceHuluWidth     = null ;


/**
* The SWF name for the Flash gameboard.
*/
var gameboardSwf = "/content/game/GameBoard.swf?gameBoardSize=" + m_gameBoardSize + "&isSpinGame=" + m_isSpinGame;


/**
* The SWF name for the Flash question panel.
*/
var questionPanelSwf = "/content/game/QuestionPanel.swf"; //http://www.frisbeefish.com/popjax/game/QuestionPanel.swf";

/*
skinObject["questionPanelCenterColor"] && skinObject["questionPanelOuterColor"]
*/

var m_questionPanelSkin = null;


var m_hasLeftColumnSkin = false;
var m_hasRightColumnSkin = false;

/**
* This is the id of either the youtube player or the popjax player.
* SLW 601.
*/
var m_youtubeTypeVideoPlayerId = null;

function fromFlash_applyHtmlSkin ( elementId, html ) {
	//alert ( "fromFlash_applyHtmlSkin called for: " + elementId + " HTML: " + html );
	
	try {
		
//alert ( " fromFlash_applyHtmlSkin: elementId: " + elementId +  " html: " + html );

      if ( elementId == "skinLeftColumn" && html != null && html != "null" && html.indexOf ( "<" ) >= 0 ) 
	  {
			//alert ( "IS LEFT COLUMN SKIN html: " + html );
			if ( html.indexOf ( "<" ) > html.indexOf ( "<!--" ) ) {
			   m_hasLeftColumnSkin = true;
			} else {
				//alert ( "COMMENTED OUT LEFT" );
			}
		}

      if ( elementId == "skinRightColumn" && html != null && html != "null" && html.indexOf ( "<" ) >= 0 ) {
			//alert ( "IS RIGHT COLUMN SKIN html: " + html  );
			if ( html.indexOf ( "<" ) > html.indexOf ( "<!--" ) ) {
			m_hasRightColumnSkin = true;
			} else {
				//alert ( "COMMENTED OUT RIGHT" );
			}
		}
		
		/**
		* If there is a left column skin or right column skin or both, adjust the size of the div surrounding the center column div.
		*/
		if ( m_hasLeftColumnSkin == true || m_hasRightColumnSkin == true ) {
			var outerContainerWidth = 900;
			if ( m_hasLeftColumnSkin == true ) {
				outerContainerWidth += 200;
			}
			if ( m_hasRightColumnSkin == true ) {
				outerContainerWidth += 200;
			}
			
			var outsideOfCenterColumn = document.getElementById ( "outsideOfCenterColumn" );
			outsideOfCenterColumn.style.width = outerContainerWidth + "px";
			// alert ("Outside width now " + outerContainerWidth ) ;
		}
		

		//loadHtmlSkin ( "skinBehindGameboard", m_skinName, "behindGameboard.html" );
		var elem = document.getElementById ( elementId ); //"skinAboveMenu" );
		
//alert ( " skin elementId: " + elementId + " element: " + elem );

		elem.innerHTML = html;
	} catch ( exc ) {
	}
	
	// skinAboveMenu
}

function fromFlash_executeHtmlSkinJavascript ( jscode ) {
//	alert ( "fromFlash_executeHtmlSkinJavascript called : " + jscode );
	
	try {
		//var elem = document.getElementById ( elementId ); //"skinAboveMenu" );
		//elem.innerHTML = html;
		eval ( jscode );
	} catch ( exc ) {
	}
	
	// skinAboveMenu
}


function afterLoginCallback ( _pjuid ) {
	m_pjuid = _pjuid;
	
   m_pjusername = document.getElementById("login_userName").value;
	
   document.getElementById("loginform2").style.display = "none";
   document.getElementById("logOutForm2").style.display = "block";	
   document.getElementById("userNameLoggedIn").innerHTML = m_pjusername; //userId.value;	
   
   restartTheGameWithLoginCredentials ();
}

function afterRegistrationCallback ( _pjuid ) {
	
	m_pjuid = _pjuid;
	
	m_pjuidforinvites = _pjuid;
	
	//if ( m_pjuidforinvites == null ) {
	//	alert ( "NULL PJUID" );
	//}
	
//alert ( "AFTER REG: " + m_pjuid );	
	
   m_pjusername = document.getElementById("reg_registrationUserName").value;
	
   document.getElementById("loginform2").style.display = "none";
   document.getElementById("logOutForm2").style.display = "block";	
   document.getElementById("userNameLoggedIn").innerHTML = m_pjusername; //userId.value;	
   
   restartTheGameWithLoginCredentials ();
	
}


/**
* Test for whether or not we're running within IE.
*/
function isIE () {
	
	var _isIE = false;
	
	    var agt=navigator.userAgent.toLowerCase();
        var myApp = navigator.appName;
		if ( myApp.indexOf ( "Microsoft" ) >= 0 && agt.indexOf ("msie") >= 0  ) {
		   _isIE = true;
		}
		
	return  _isIE;	
}


/*
level="3" levelName="popquiz" origin="site" gameType="HIT" gameId="140" preview="false"
*/

function createStartFacebookHitGameParms ( gameId, preview ) {
   var parms = new Object ();
  
   parms.level = LEVEL_HIT_GAME;
   parms.levelName = LEVEL_NAME_POPQUIZ;
   parms.origin = ORIGIN_FACEBOOK;
   parms.gameType = GAME_TYPE_HIT;
   parms.gameId = gameId;
   parms.preview = preview;
  
   m_origin = ORIGIN_FACEBOOK;
   
   if ( m_pjuid == null || m_pjuid == "null" ) {
	  parms.pjuid = -1;
   } else {
	  parms.pjuid = m_pjuid;
   }
  
   return parms;
}

function createStartSiteHitGameParms ( gameId, preview ) {
   var parms = new Object ();
  
   parms.level = LEVEL_HIT_GAME;
   parms.levelName = LEVEL_NAME_POPQUIZ;
   parms.origin = ORIGIN_SITE;
   parms.gameType = GAME_TYPE_HIT;
   parms.gameId = gameId;
   parms.preview = preview;
   
   m_origin = ORIGIN_SITE;
  
   if ( m_pjuid == null || m_pjuid == "null" ) {
	  parms.pjuid = -1;
   } else {
	  parms.pjuid = m_pjuid;
   }
  
   return parms;
}

function createStartSitePop4GameParms ( categoryIdsString, preview ) {
   var parms = new Object ();
  
   parms.level = LEVEL_POP4_GAME;
   parms.levelName = LEVEL_NAME_POP4;
   parms.origin = ORIGIN_SITE;
   parms.categories = categoryIdsString;
  // parms.gameType = GAME_TYPE_HIT;
 // parms.gameId = gameId;
   parms.preview = preview;
   
   m_origin = ORIGIN_SITE;
  
   if ( m_pjuid == null || m_pjuid == "null" ) {
	  parms.pjuid = -1;
   } else {
	  parms.pjuid = m_pjuid;
   }
  
   return parms;
}

function createStartFacebookPopQuiz16GameParms ( categoryIdsString, preview ) {
   var parms = new Object ();
  
   parms.level = "3"; // What does 3 mean any more?!!! LEVEL_POP4_GAME;
   parms.levelName = LEVEL_NAME_POPQUIZ16;
   parms.origin = ORIGIN_FACEBOOK;
   parms.categories = categoryIdsString;
   parms.gameType = GAME_TYPE_CUSTOM;
   parms.suggestedName = m_fbname;
 // parms.gameId = gameId;
   parms.preview = preview;
   
   m_origin = ORIGIN_FACEBOOK;
  
   if ( m_pjuid == null || m_pjuid == "null" ) {
	  parms.pjuid = -1;
   } else {
	  parms.pjuid = m_pjuid;
   }
  
   return parms;
}

function createStartFacebookPop4GameParms ( categoryIdsString, preview ) {
   var parms = new Object ();
  
   parms.level = LEVEL_POP4_GAME;
   parms.levelName = LEVEL_NAME_POP4;
   parms.origin = ORIGIN_FACEBOOK;
   parms.categories = categoryIdsString;
   parms.gameType = GAME_TYPE_CUSTOM;
   parms.suggestedName = m_fbname;
 // parms.gameId = gameId;
   parms.preview = preview;
   
   m_origin = ORIGIN_FACEBOOK;
  
   if ( m_pjuid == null || m_pjuid == "null" ) {
	  parms.pjuid = -1;
   } else {
	  parms.pjuid = m_pjuid;
   }
  
   return parms;
}

function createStartFacebookPopSpinGameParms ( categoryIdsString, preview ) {
   var parms = new Object ();
  
   parms.level = "1"; // What does 1 mean any more?!!! LEVEL_POP4_GAME;
   parms.levelName = LEVEL_NAME_POPSPIN;
   parms.origin = ORIGIN_FACEBOOK;
   parms.categories = categoryIdsString;
   parms.gameType = GAME_TYPE_CUSTOM;
   parms.suggestedName = m_fbname;
 // parms.gameId = gameId;
   parms.preview = preview;
   
   m_origin = ORIGIN_FACEBOOK;
  
   if ( m_pjuid == null || m_pjuid == "null" ) {
	  parms.pjuid = -1;
   } else {
	  parms.pjuid = m_pjuid;
   }
  
   return parms;
}

function createStartSitePopSpinGameParms ( categoryIdsString, preview ) {
   var parms = new Object ();
  
try {
   parms.level = LEVEL_POPSPIN_GAME;
   parms.levelName = LEVEL_NAME_POPSPIN;
   parms.origin = ORIGIN_SITE;
   parms.categories = categoryIdsString;
  // parms.gameType = GAME_TYPE_HIT;
 // parms.gameId = gameId;
   parms.preview = preview;
   
   m_origin = ORIGIN_SITE;
  
   if ( m_pjuid == null || m_pjuid == "null" ) {
	  parms.pjuid = -1;
   } else {
	  parms.pjuid = m_pjuid;
   }
} catch ( exc ) {
	alert ( "EXC: " + exc );
}
   return parms;
}

function addOriginReporting ( _startGameParms ) {
	
	if ( m_reportingOrigin != null ) {
		_startGameParms.reportingOrigin = m_reportingOrigin;
	}
	
	if ( m_reportingRef != null ) {
		_startGameParms.reportingRef = m_reportingRef;
	}
	
}

function initFacebookPopQuiz16Game ( categoryIds ) {
// 54%2055%2098%2099
   m_startGameParms = createStartFacebookPopQuiz16GameParms ( categoryIds, false );  //  "54 55 98 99", false );
	
	
	
   m_currentGameType = GAME_TYPE_POP4; // m_startGameParms.gameType;
   m_isSpinGame = "false";
   m_gameBoardSize = GAMEBOARD_SIZE_4x4;
  
   m_clearTheBoardGame = true;
   
   gameboardSwf = "/content/game/GameBoard.swf?gameBoardSize=" + m_gameBoardSize + "&isSpinGame=" + m_isSpinGame;
}

function initPop4Game ( categoryIds ) {
// 54%2055%2098%2099
   m_startGameParms = createStartSitePop4GameParms ( categoryIds, false );  //  "54 55 98 99", false );
   m_currentGameType = GAME_TYPE_POP4; // m_startGameParms.gameType;
   m_isSpinGame = "false";
   m_gameBoardSize = GAMEBOARD_SIZE_4x4;
  
   gameboardSwf = "/content/game/GameBoard.swf?gameBoardSize=" + m_gameBoardSize + "&isSpinGame=" + m_isSpinGame;
}

function initFacebookPopSpinGame ( categoryIds ) {
   m_startGameParms = createStartFacebookPopSpinGameParms ( categoryIds, m_preview ); //"20 45 46 47", false );
   
   m_currentGameType = GAME_TYPE_POPSPIN; // m_startGameParms.gameType;
   m_isSpinGame = "true";
   m_gameBoardSize = GAMEBOARD_SIZE_4x4;
  
   gameboardSwf = "/content/game/GameBoard.swf?gameBoardSize=" + m_gameBoardSize + "&isSpinGame=" + m_isSpinGame;
}

function initFacebookPop4Game ( categoryIds ) {
   m_startGameParms = createStartFacebookPop4GameParms ( categoryIds, false );  //  "54 55 98 99", false );
   m_currentGameType = GAME_TYPE_POP4; // m_startGameParms.gameType;
   m_isSpinGame = "false";
   m_gameBoardSize = GAMEBOARD_SIZE_4x4;
   
   gameboardSwf = "/content/game/GameBoard.swf?gameBoardSize=" + m_gameBoardSize + "&isSpinGame=" + m_isSpinGame;
}



function initPopSpinGame ( categoryIds ) {
   m_startGameParms = createStartSitePopSpinGameParms ( categoryIds, m_preview ); //"20 45 46 47", false );
   
   m_currentGameType = GAME_TYPE_POP4; // m_startGameParms.gameType;
   m_isSpinGame = "true";
   m_gameBoardSize = GAMEBOARD_SIZE_4x4;
  
   gameboardSwf = "/content/game/GameBoard.swf?gameBoardSize=" + m_gameBoardSize + "&isSpinGame=" + m_isSpinGame;
}

function initHitGame () {   
   m_startGameParms = createStartSiteHitGameParms ( m_gameId, m_preview );
   m_currentGameType = m_startGameParms.gameType;
   m_isSpinGame = "false";
   m_gameBoardSize = GAMEBOARD_SIZE_3x3;
  
   m_clearTheBoardGame = true;
   
   gameboardSwf = "/content/game/GameBoard.swf?gameBoardSize=" + m_gameBoardSize + "&isSpinGame=" + m_isSpinGame;
}

function initFacebookHitGame () {   
   m_startGameParms = createStartFacebookHitGameParms ( m_gameId, m_preview );
   m_currentGameType = m_startGameParms.gameType;
   m_isSpinGame = "false";
   m_gameBoardSize = GAMEBOARD_SIZE_3x3;
  
   m_clearTheBoardGame = true;
   
   gameboardSwf = "/content/game/GameBoard.swf?gameBoardSize=" + m_gameBoardSize + "&isSpinGame=" + m_isSpinGame;
}


/**
* This is the generic function used all over the place to load flash content into divs.
*
* NOTE: This needs to live here because it is used before all the page scripts have been loaded (therefore, it can't be stored
* in one of the page scripts files).
*/   
function OLD_embedFlashContent ( flashContentId, parentDivId, swfPath, width, height, backColor, windowMode, scaleMode ) {

alert ( "SWFOBJECT" );

   var theScaleMode = scaleMode;
   if ( theScaleMode == null || theScaleMode == "undefined" ) {
	   theScaleMode = "noScale";
   }
   
   var so = new SWFObject( swfPath, flashContentId, width, height, "8" ); //, backColor );
   so.addParam("quality", "high");
   so.addParam("wmode", windowMode ); // "transparent" ); //window");
   so.addParam("allowScriptAccess", "always");
   so.addParam("salign", "tl");
   so.addParam("align", "tl");
   if ( theScaleMode ) {
	  so.addParam("scale", theScaleMode );
   } else {
	  so.addParam("scale", "noScale");
   }

   so.addVariable("id", "ytplayer");

   //so.useExpressInstall('expressinstall.swf');
   so.write( parentDivId );	
}

var m_flashPlayerClsid = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000";

/**
* Did this cause swfobject was accessing the document.location object in its CTOR which would blow up security in certain embedding situations.
*/
function embedFlashContent ( flashContentId, parentDivId, swfPath, width, height, backColor, windowMode, scaleMode, flashVars ) {

   var parentDiv = document.getElementById ( parentDivId );
   
   var theScaleMode = scaleMode;
   if ( theScaleMode == null || theScaleMode == "undefined" ) {
	   theScaleMode = "noScale";
   }
   
   var embed = "<embed type='application/x-shockwave-flash' ";
   embed += " src='" + swfPath + "' width='" + width + "' height='" + height + "' ";
   embed += " id='" + flashContentId + "' name='" + flashContentId + "' ";
   embed += " quality='high' wmode='transparent' allowScriptAccess='always' salign='tl' align='tl' ";
   if ( flashVars ) {
      embed += " FlashVars='" + flashVars + "' ";
   }
   embed += " scale='" + theScaleMode + "' />";
   
   var objectTag = "<object id='" + flashContentId + "' name='" + flashContentId + "' ";
   objectTag += " classid='" + m_flashPlayerClsid + "' width='" + width + "' height='" + height + "' >";
   objectTag += " <param name='movie' value='" + swfPath + "' /> ";
   objectTag += " <param name='quality' value='high' /> ";
   objectTag += " <param name='wmode' value='transparent' /> ";
   objectTag += " <param name='allowScriptAccess' value='always' /> ";
   objectTag += " <param name='salign' value='tl' /> ";
   objectTag += " <param name='align' value='tl' /> ";
   objectTag += " <param name='scale' value='" + theScaleMode + "' /> ";
   if ( flashVars ) {
      objectTag += " <param name='FlashVars' value='" + flashVars + "' /> ";
   }
   objectTag += " </object>";
   
   if ( isIE () ) {
      parentDiv.innerHTML = objectTag;
   } else {
	   parentDiv.innerHTML = embed;
   }
   
}

// isIE


/**
* Once a Flash component (one of the pieces of the game) loads into the page, it calls this "moduleLoaded" function.
* The "moduleId" allows us to know what module is loaded so we can execute the appropriate code.
*
* NOTE!!! Once all the scripts load, this version of this function will be replaced with another version!!!
*/
function moduleLoaded ( moduleId ) {

   /**
   * The gameboard has been loaded.
   */
   if ( moduleId == "gameboard" ) {
	  m_gameBoardLoaded = true;   
   }
  
}


/**
* You can subclass this if you want to do something when the end of game divs are showing.
*/
function handleEndOfGameScreenShowing () {
}

/**
* You can subclass this if you want to do something when a video and the question panel are being shown.
*/
function handleVideoAndQuestionShowing () {
}

/**
* You can subclass this if you want to do something when the gameboard is showing.
*/
function handleGameBoardIsShowing () {
}



/*  New stuff added March 2009 for new End-of-Game skin and Hulu recommendations.
    Pass a bucket-full of params over to the gameRecommendations.jsp page.
	It will load in the same Window/IFrame as the one that the caller is in...
	
     numRecs=m_numRecommendedGames
     gameId=m_gameId
     playerId=m_pjuid
     affiliateId=SKINNED_GAME_AFFILIATE_ID
     affiliateName=SKINNED_GAME_AFFILIATE_NAME
	 skinFile=m_ContentPath+EndGameRecs.xml
     gamePage=THIS_PAGE_ON_POPJAX
     homePage=m_skinnedGamesChooserUrl
*/

var endGameRecspage = "/game/gameRecommendations.jsp";  

function showVersion2_EndGame ( playedgameId ) 
{
   var pjuidCookie = getCookie ( "pjuid" ) ;
   if ( (pjuidCookie != null) && (m_pjuid == null) )
     m_pjuid = pjuidCookie ;
   
   var ref = window ;
   var params  = "?numRecs="        + m_numRecommendedGames  +
                 "&gameId="         + m_gameId + 
				 "&score="          + m_currentGameScore +
				 "&recScore="       + m_currentGameScore +
                 "&playerId="       + m_pjuid + 
                 "&affiliateId="    + SKINNED_GAME_AFFILIATE_ID + 
                 "&affiliateName="  + m_gaAffName + 
                 "&skinFile="       + m_ContentPath + "EndGameRecs" + 
                 "&gamePage="       + escape (THIS_PAGE_ON_POPJAX) +
                 "&homePage="       + escape (m_skinnedGamesChooserUrl) ; 
				 
   /* Game was ABANDONED if this is blank */			 
   if ( playedgameId != "" )
     params += ( "&result=score&playedgameId=" + playedgameId ) ;
   else
     params += "&result=abandon" ;
				 
   ref.location.href = ( endGameRecspage + params ) ;  
}
