
// Show an ad to a user every fixed number of plays
var globalAdFrequency = 4;
var showAd = false;
var adPlayPosition;

var playDiv;

/**
 * Checks the ad-play frequency, then plays an ad if appropriate (followed by the game) or just loads the game
 * This can be called on page load but can also be accessed by calling this function directly from the Flash game or an AJAX call to this method
 */
function playAdOrGame() {

	// First write out the div holder for the ad or the game
	document.write('<div id="play1"></div>');
	playDiv = document.getElementById("play1");

	// Check the ad frequency on page load
	checkAdFrequency();

	//alert("should we show an ad?: " + showAd);
	if (showAd) {
		// can add additional logic here to load different types of ads

		// Load a video preplay ad (then the game)
		playVideoAd();
	} else {

		// Just load the game
		embedFlashGame();
	}
}

/**
 * Sets whether or not a pre-play ad should run based on play frequency.
 */
function checkAdFrequency() {

	// See if an ad frequency cookie is already set
	if ( $.cookie("adPlayPosition") ) {
		adPlayPosition = $.cookie("adPlayPosition");
		//alert('we have a cookie: ' + $.cookie("adPlayPosition") );

	// Otherwise set one
	} else {

		// Randomly select a user's starting point so ALL users don't have to play [globalAdFrequency] times before seeing the 1st ad
		// start at position [globalAdFrequency-1] or [globalAdFrequency] so the user has 0 or 1 gameplay before seeing 1st ad
		var adPlayPositionStart = Math.floor(Math.random()*(globalAdFrequency - (globalAdFrequency-2) )) + (globalAdFrequency-1);
		$.cookie("adPlayPosition", adPlayPositionStart);
		adPlayPosition = adPlayPositionStart;
		//alert("just set cookie position to: " + adPlayPositionStart);
	}

	// Make sure we're dealing with a number (not a string) for the addition just below
	adPlayPosition = parseInt(adPlayPosition);

	// If it's time to play an ad, set that up and restart the position counter
	if ( adPlayPosition == globalAdFrequency ) {
		showAd = true;
		adPlayPosition = 1;

	// Otherwise just increment the position
	} else {
		adPlayPosition += 1;
	}

	// Update the adPlayCount cookie
	$.cookie("adPlayPosition", adPlayPosition);
}

//holds a reference to the video player.
var player;
var timeoutID;

/**
 * Function to set up and call the video ad - timeout or ad complete will trigger the game to load
 *
 * This function requires these varibles set at embedding:
 * 	dartCategory (the section/channel keyword)
 * 	gameId (the game keyword)
 * 	gameDisplayName (self explanatory)
 */
function playVideoAd(){

	// Blank out the playDiv to hold the video ad
	playDiv.innerHTML = '<div id="flashVideo"></div>';

	// Global variables for the video ad player
	var yumeUrl = "/jsource/plugins/yumesdk3";
	var dartSite = "ag.nol";
	var mode = "mode.live"; // - possible values are "mode.dev" or "mode.live"
	var adComplete = "embedFlashGame"; // - name of a javascript function to call when the ad is done playing.

	// Page local variables for the video ad player
	//var rugrat // - demo identifier, will trigger "under13" for ad calls

	var flashVars = {
		yumeUrl: yumeUrl,
		dartSite: dartSite,
		mode: mode,
		gameName: gameId, //gameDisplayName,
		gameType: dartCategory,
		gameId: gameId,
		rugrat: "false",
		adComplete: adComplete
	};
	var flashParams = {
		allowScriptAccess: "always",
		bgcolor: "#000000",
		quality: "high"
	};
	swfobject.embedSWF("/jsource/GamePlayer.swf", "flashVideo", "480", "340", "9", "", flashVars, flashParams);

	player = window.document.flashVideo;

	// Set a default timeout to ensure the game is shown if ad call fails
	timeoutID = setTimeout( embedFlashGame, 36 * 1000 );
}

/**
 * Function to clear any ad on the page and load the game
 *
 * This function requires these varibles set at embedding:
 * 	gameName (reference to the Flash game to be played).
 * 	gameWidth (self explanatory)
 * 	gameHeight (self explanatory)
 */
function embedFlashGame(){

	// Clear the timeout just in case
	if(timeoutID) {clearTimeout(timeoutID);}

	// Blank out the playDiv and set up the game container
	playDiv.innerHTML = "<div id='FlashContent'></div>";

	// Load the game
	var flashVars = {};
	var flashParams = {
		AllowScriptAccess: "never",
		quality: "high"
	};
	
	gameName+=getURL();
	
	swfobject.embedSWF(gameName, "FlashContent", gameWidth, gameHeight, "9", "", flashVars, flashParams);
}

/**
 * Reskin the backround of the site
 * @param bgImageUrl, the url to the image to apply to the background
 * @param bgColor, the color to apply to the backround behind the image
 */
reskinBackground = function( bgImageUrl, bgColor  ) {
	$("body").css("background-image","url("+bgImageUrl+")");
	$("body").css("background-color",bgColor);
	var reskinIsActive = true;
}

