
var popcap_ad_length			= 15;		// in seconds
var popcap_ad_threshold			= 0;		// in seconds
var popcap_in_focus				= true;
var popcap_time_last_ad			= (new Date()).getTime(); 
var popcap_timer				= null;
var popcap_ad_countdown			= 0;

function PopcapOnFocus() 
{ 
	popcap_in_focus = true; 
//	PopcapDebug('Focus'); 
}

function PopcapOnBlur() 
{ 
	popcap_in_focus = false; 
//	PopcapDebug('Blur'); 
}

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';
	ad.innerHTML += '<A href="javascript:void(0)" onclick="javascript:PopcapEndad(true);">Click here to resume game.</A>\n';
}

function MakeAd()
{
	if (popcap_external_ads)
	{
		var ad_tag = "";
		var game = document.getElementById('gamediv');
		var game_top = "10px";
		var game_left = "10px";
		var game_width = popcap_app_width;
		var game_height = popcap_app_height;

		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: ' + game_width + ';\n';
		ad_tag += 'height: ' + game_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 += '}\n';

		ad_tag += '</style>\n\n';

		ad_tag += '<TABLE id=adcontainer bgcolor=#CCCCCC>\n';
		ad_tag += '<TR><TD align=center ID="adcell">';
		ad_tag += '</TD></TR>\n';
		ad_tag += '</TABLE>\n';

//		PopcapDebug(ad_tag);
		document.write(ad_tag);

		document.body.onfocus = PopcapOnFocus;
		document.body.onblur = PopcapOnBlur;
	}
}

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) {
		var game;
		game = document.getElementById('GameObject');
		gameDiv = document.getElementById('gamediv');
		adDiv = document.getElementById('adcontainer');

		//unpause the game
		if (popcap_isFlash)
			game.TCallLabel("/","PauseOff");
		else
			game.PopcapNotify('pc_pause','0');

		//hide the ad
		adDiv.style.visibility = 'hidden';
		adDiv.style.display = 'none';

		//show the game
		gameDiv.style.visibility = 'visible';
		clearTimeout(popcap_timer);
	}
}

function PopcapAdTimer(forceCountdownDisplay)
{
	if (popcap_ad_countdown>0)
		popcap_ad_countdown--;
	
	WriteAdText(forceCountdownDisplay);
	if (popcap_ad_countdown==0)
		PopcapEndad(false);
	else
		popcap_timer = setTimeout("PopcapAdTimer(false)",1000); 
}

function PopcapAdGameBreak(method,param)
{
	var game;
	game  = document.getElementById('GameObject');

	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_isFlash)
			game.TCallLabel("/","PauseOn");
		else
			game.PopcapNotify('pc_pause','1');
			 
		var game_style = document.getElementById('gamediv').style;
		game_style.visibility = 'hidden';
	
		//show the ad
		var ad_style = document.getElementById('adcontainer').style;
		ad_style.visibility = 'visible';
		ad_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;
	}
}

MakeAd();
