// These functions create the ad layer.

var popcap_in_focus			= true;
var popcap_time_last_ad		= (new Date()).getTime(); 
var popcap_timer			= null;
var popcap_ad_countdown		= 0;
var popcap_nofocus_endad	= false;

// Utility
function PopcapOnFocus() 
{ 
	popcap_in_focus = true;
	if (popcap_nofocus_endad) PopcapEndad(false);
}

function PopcapOnBlur() 
{ 
	popcap_in_focus = false;
}

// Writes placeholder text into the ad container if ads are not integrated.
function WriteAdText(forceCountdownDisplay)
{
	var ad = document.getElementById('adcell');
	
	ad.innerHTML = '';
	ad.innerHTML = 'This is an in-game ad.<br>\n';
	
	if (popcap_ad_countdown>0 && (popcap_in_focus || forceCountdownDisplay))
		ad.innerHTML += 'It will automatically end in ' + popcap_ad_countdown + ' seconds.<br>\n';

	ad.innerHTML +='<br>\n';
	ad.innerHTML += '<a href="javascript:void(0)" onclick="javascript:window.open(\'http://www.popcap.com\');">Click here to see ad details.</a><br><br>\n';
	
	if (!popcap_runPreroll) // Prevent user from forcing resume on a preroll
		ad.innerHTML += '<a href="javascript:void(0)" onclick="javascript:PopcapEndad(true);">Click here to resume game.</a>\n';
}

// Sets up the ad container.
function MakeAd()
{
	if (popcap_external_ads)
	{
		var ad_tag = '';
		var game_top = '10px';
		var game_left = '10px';

		ad_tag += '<style type="text/css">\n';

		ad_tag += '#adcontainer {\n';
		ad_tag += 'position: absolute;\n';
		ad_tag += 'top: ' + game_top + ';\n';
		ad_tag += 'left: ' + game_left + ';\n';
		ad_tag += 'z-index: 1;\n';	
		ad_tag += 'visibility: hidden;\n';		
		ad_tag += 'display: none;\n';
		ad_tag += 'width: ' + popcap_app_width + ';\n';
		ad_tag += 'height: ' + popcap_app_height + ';\n';
		ad_tag += 'border-collapse: collapse;\n';
		ad_tag += 'border: none;\n';
		ad_tag += 'margin: none;\n';
		ad_tag += 'padding: none;\n';
		ad_tag += 'color: #ffffff;\n';
		ad_tag += '}\n';

		ad_tag += '</style>\n\n';
		
		ad_tag += '<table id=adcontainer bgcolor=black><tbody>\n';
		ad_tag += '<tr><td align=center id="adcell">';
		
		if (popcap_integrated_ads)
		{
			ad_tag += '<iframe id="spot" name="spot" width="' + popcap_app_width + '" height="' + popcap_app_height + '" src="';
			
			if (popcap_runPreroll) ad_tag += 'preroll.html';
			else ad_tag += 'mid.html';
			
			ad_tag += '" scrolling="NO" frameborder="0" border=0 marginheight="0" marginwidth="0" oncontextmenu="return false"/>';
		}
		
		ad_tag += '</td></tr>\n';
		ad_tag += '</tbody></table>\n';

		document.write(ad_tag);

		document.body.onfocus = PopcapOnFocus;
		document.body.onblur = PopcapOnBlur;
	}
}

// Hides the ad and resumes the game. Called when the ad display timer has expired, or when the user clicks to continue.
function PopcapEndad(force)
{
	//if window is not in focus don't end ad... user must manually click to restart game
	if (popcap_in_focus || force)
	{
		popcap_nofocus_endad = false;
		if (!popcap_runPreroll)
		{
			//unpause the game
			if (popcap_enableZone)			
			{
				if (popcap_isFlash && popcap_isGameOver)
				{
					if (popcap_isPCAPIonly) 
						thisObject('GameObject').TCallLabel('/','PauseOff');
					else
						thisObject('GameObject').TCallLabel('/','GameMenu');
				}
				else if (popcap_isFlash)
				{				
					if (popcap_isPCAPIonly) 
						thisObject('GameObject').TCallLabel('/','PauseOff');
					else
						thisObject('GameObject').TCallLabel('/','GameContinue');
				}
				else if (popcap_isPCAPIonly)
				{
					thisObject('GameObject').PopcapNotify('pc_pause','0');
				}
				else if (popcap_isGameOver)
				{
					if (popcap_gamename == 'astropop') 
						thisObject('GameObject').PopcapNotify('GameStart','');
					else 
						thisObject('GameObject').PopcapNotify('GameMenu','');
				}
				else
					thisObject('GameObject').PopcapNotify('GameContinue','');
			}
			else
			{
				if (popcap_isFlash)
				{
					if (popcap_isZoneAPIonly)
						thisObject('GameObject').TCallLabel('/','GameContinue');
					else
						thisObject('GameObject').TCallLabel('/','PauseOff');
				}
				else if (popcap_isZoneAPIonly)
				{
					if (popcap_isGameOver)
						thisObject('GameObject').PopcapNotify('GameMenu','');
					else
						thisObject('GameObject').PopcapNotify('GameContinue','');
				}
				else
					thisObject('GameObject').PopcapNotify('pc_pause','0');
			}
			
			popcap_isGameOver = false; // Always set to false in case a post-roll was ended			
		}
		
		//show the game
		thisObject('gamediv').style.visibility = 'visible';
			
		//hide the ad
		thisObject('adcontainer').style.visibility = 'hidden';
		
		if (popcap_integrated_ads) frames['spot'].location.href = 'about:blank';
		
		thisObject('adcontainer').style.display = 'none';
		
		clearTimeout(popcap_timer);
		
		// make sure focus is on the game now
		thisObject('GameObject').focus();
	}
	else
	{
		popcap_nofocus_endad = true;
	}
}

// Timer for ad display.
function PopcapAdTimer(forceCountdownDisplay)
{
	if (popcap_ad_countdown > 0) popcap_ad_countdown--;
	
	if (!popcap_integrated_ads) WriteAdText(forceCountdownDisplay);
		
	if (popcap_ad_countdown == 0)
	{
		PopcapEndad(false);
		if (popcap_runPreroll) popcap_runPreroll = false;
	}
	else popcap_timer = setTimeout('PopcapAdTimer(false)',1000); 
}

// Pauses and hides the game, displays an ad.
function PopcapAdGameBreak()
{
	var current_date = new Date(); 
	var current_time = current_date.getTime(); 
	var ad_interval = current_time - popcap_time_last_ad;
	
	if (ad_interval > popcap_ad_threshold*1000) 
	{
		if (popcap_integrated_ads) 
		{
			if (popcap_isGameOver) frames['spot'].location.href = 'postroll.html';
			else frames['spot'].location.href = 'mid.html';
		}
		
		// Zone automatically pauses the game upon a GameBreak; game only needs to be paused manually if using the PopCap API.
		if (!(popcap_enableZone || popcap_isZoneAPIonly) || popcap_isPCAPIonly)
		{		
			if (popcap_isFlash) thisObject('GameObject').TCallLabel('/','PauseOn');
			else thisObject('GameObject').PopcapNotify('pc_pause','1');
		}

		thisObject('gamediv').style.visibility = 'hidden';
	
		//show the ad
		thisObject('adcontainer').style.visibility = 'visible';
		thisObject('adcontainer').style.display = 'block';
	
		//use setTimeout to end ad (maybe also be overriden by ad itself)
		if (popcap_ad_length > 0)
		{
			popcap_ad_countdown = popcap_ad_length+1;
			PopcapAdTimer(true);
		}
	
		//store the time this ad was displayed to test against the next threshold
		popcap_time_last_ad = current_time;
	}
}

// Runs a preroll ad
function PopcapPreroll()
{
	var current_date = new Date(); 
	var current_time = current_date.getTime();
	
	//show the ad
	thisObject('adcontainer').style.visibility = 'visible';
	thisObject('adcontainer').style.display = 'block';

	//use setTimeout to end ad (maybe also be overriden by ad itself)
	if (popcap_ad_length > 0)
	{
		popcap_ad_countdown = popcap_ad_length+1;
		PopcapAdTimer(true);
	}

	//store the time this ad was displayed to test against the next threshold
	popcap_time_last_ad = current_time;
}

// Checks if a preroll can be run on Firefox; don't run it if the plugin's not installed!
function CanRunPreroll()
{
	if (popcap_runPreroll)
	{
		if (!popcap_isIE && popcap_isAX)
		{
			try
			{
				var pluginType = 'application/x-popcaploader;version=1.0.0.1';
				mimetype = navigator.mimeTypes[pluginType];
				
				if (!(mimetype && mimetype.enabledPlugin && mimetype.type==pluginType)) return false;
				else return true;
			}
			catch (e)
			{
			}
		}
		else return true;
	}
	else return false;
}

MakeAd();

if (CanRunPreroll()) PopcapPreroll();
