// These functions instantiate the Flash object for Flash games.

var popcap_tag_container_begin = '';
var popcap_tag  = '';
var popcap_tag_container_end = '';
var popcap_flash_ready	= false;		
popcap_isFlash			= true;	

function WriteDoc()
{	
    for (var a = 0; a < arguments.length; a++)
		popcap_tag += arguments[a];
}

function WriteParam(key,val)
{
	WriteDoc('<param name="', key, '" value="', val, '">\n');
}

function WriteFlashVars(querystr)
{
	if (popcap_isIE) WriteParam('FlashVars', querystr);
	else WriteDoc('FlashVars="', querystr, '"\n');
}

function CreateGameTags()
{
	popcap_tag_container_begin = '<div id="gamediv">\n';
	
	if (popcap_isIE)
	{
		WriteDoc('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"\n');
		WriteDoc('codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"\n');
		WriteDoc('width="', popcap_app_width, '"\n');
		WriteDoc('height="', popcap_app_height, '"\n');
		WriteDoc('id="GameObject"\n');
		WriteDoc('>\n');

		WriteParam('movie',popcap_swf_url);		
		WriteParam('loop','false');
		WriteParam('menu','false');
		WriteParam('play','true');
		WriteParam('quality','high');
		WriteParam('bgcolor','#000000');
		WriteFlashVars(popcap_flashvars);
		//CUSTOMIZED: enable full path to webgame files
		if (popcap_swf_base) {
			WriteParam("base",popcap_swf_base);
		}
		WriteDoc('</object>\n');
	}
	else
	{
		WriteDoc('<embed name="GameObject" id="GameObject" src="', popcap_swf_url, '"\n');
		WriteDoc('width="', popcap_app_width, '"\n');
		WriteDoc('height="', popcap_app_height, '"\n');
		WriteDoc('play="true"\n');
		WriteDoc('loop="false"\n');
		WriteDoc('menu="false"\n');
		WriteDoc('quality="high"\n');
		WriteDoc('bgcolor="#000000"\n');
		WriteDoc('swLiveConnect="true"\n');
		WriteFlashVars(popcap_flashvars);
		//CUSTOMIZED: enable full path to webgame files
		if (popcap_swf_base) {
			WriteDoc('base=', popcap_swf_base, '\n');
		}
		WriteDoc('pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">');
		WriteDoc('</embed>\n');
	}

	popcap_tag_container_end = '</div>\n'; //WriteDoc('</div>\n');
	
	//CUSTOMIZED: 
	//firefox needs tags written after preroll runs/fails in ad.js
	//ie writes tag to trigger install prompts
	document.write(popcap_tag_container_begin);
	if (!popcap_runPreroll || popcap_isIE)	document.write(popcap_tag);
	document.write(popcap_tag_container_end);
}

function CheckForFlash(mode) {
	flash_installed = true;
	if (popcap_isIE) 
	{
		if (mode != 'retry') mode = 'default';
		try
		{
			var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.9');
			success = true;
			$("#plugin_dialog_ie").hide();
			scroll(0,0);
		}
		catch (e)
		{
			flash_installed = false;
			success = false;
			if (mode == 'default') 
			{
				$("#plugin_dialog_ie").slideDown("slow");
				scroll(0,0);
			}
		}
		if (mode == 'default')
		{
			CreateGameTags();
			if (popcap_runPreroll) thisObject('gamediv').style.visibility = 'hidden';
		}
		//if no preroll, ie flash control install needs additional time to be recognized
		if (!popcap_runPreroll && !success) setTimeout('CheckForFlash("retry")',1000);
	}
	else
	{
		if (!$.plugdetect('flash') && $.browser.mozilla && $.platform.win) {
			$("#plugin_dialog_ff_flash").slideDown("slow");
			scroll(0,0);
			flash_installed = false;
		} 
		else if (!$.plugdetect('flash') && $.browser.mozilla && $.platform.mac) {
			$("#plugin_dialog_mac").slideDown("slow");
			scroll(0,0);
			flash_installed = false;
		}

		
		CreateGameTags();
		if (popcap_runPreroll) thisObject('gamediv').style.visibility = 'hidden';
	}
	return flash_installed;
}

function GameObject_DoFSCommand(command,args)
{
	ReceiveNotification(command,args);
}

if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) 
{	
	document.write('<script event=FSCommand(command,args) for=GameObject>\n');
	document.write('GameObject_DoFSCommand(command,args);\n');
	document.write('</script>\n');
}

flash_installed = CheckForFlash();