// These functions instantiate the ActiveX object for ActiveX games.

var popcap_tag_container_begin = '';
var popcap_tag  = '';
var popcap_tag_container_end = '';
popcap_isAX		= true;

function WriteDoc()
{	
	for (var a = 0; a < arguments.length; a++)
		popcap_tag += arguments[a];
}

function WriteParam(key,val)
{
	if (popcap_isIE) // Object
		WriteDoc('<param name="', key, '" value="', val, '">\n');
	else		 // Embed
		WriteDoc(key, '="', val, '"\n');
}

function CreateGameTags()
{
	popcap_tag_container_begin += '<div id="gamediv">\n';
	
	if (popcap_isIE) 
	{
		WriteDoc('<object\n');
		WriteDoc('id="GameObject"\n');
		WriteDoc('classid="clsid:DF780F87-FF2B-4DF8-92D0-73DB16A1543A"\n');
		WriteDoc('codebase="', popcap_loadercab_url, '"\n');		
		WriteDoc('width=', popcap_app_width, '\n');
		WriteDoc('height=', popcap_app_height, '\n');
		WriteDoc('>\n');
	}
	else
	{
		WriteDoc('<embed \n');
		WriteDoc('type="application/x-popcaploader;version=1.0.0.1"\n');
		WriteDoc('width=', popcap_app_width, '\n');
		WriteDoc('height=', popcap_app_height, '\n');
		WriteDoc('id="GameObject"\n');
	}

	WriteParam('numhosts',popcap_ax_hosts.length);
	
	for (var i=0; i < popcap_ax_hosts.length; i++)
	{
		WriteParam('host' + (i+1), popcap_ax_hosts[i]);
		WriteParam('hostsig' + (i+1), popcap_ax_hostsigs[i]);
	}

	WriteParam('gamename',popcap_gamename);
	WriteParam('partnername',popcap_partnername);
	WriteParam('displayname',popcap_displayname);
	WriteParam('gamecab',popcap_gamecab);

	if (popcap_logo_url != null)
		WriteParam('logocab',popcap_logo_url);
	
	if (popcap_cheats)
	{
		popcap_params.push('cheats');
		popcap_paramvals.push('true');
	}

	if (popcap_score_broadcast != 0)
	{
		popcap_params.push('pc_scorebroadcast');
		popcap_paramvals.push(popcap_score_broadcast);
	}

	popcap_params.push('ShowUpsell');
	
	if (!popcap_internal_ads) popcap_paramvals.push('false');
	else popcap_paramvals.push('true');
	
	if (popcap_enableZone || popcap_isZoneAPIonly)
	{
		popcap_params.push('ZoneScript');
		popcap_paramvals.push('true');
	}		
	
	if (popcap_params.length>0)
	{
		var params = '';
		
		for (var i=0; i < popcap_params.length; i++)
		{
			if (i!=0) params += ',';
		
			params += popcap_params[i];
		}

		WriteParam('params',params);
		
		for (var i=0; i < popcap_params.length; i++)
			WriteParam(popcap_params[i],popcap_paramvals[i]);
	}

	if (popcap_isIE)
	{
		WriteDoc('</object>\n');
		WriteDoc('<script for="GameObject" event="PopcapNotification(method,param)" language="JavaScript">\n');
		WriteDoc('ReceiveNotification(method,param);\n');
		WriteDoc('</scr','ipt>\n');
	}
	else
	{
		WriteDoc('>\n');
		WriteDoc('</embed>\n');
	}
	
	popcap_tag_container_end = '</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 ReloadPlugin()
{
	if (!popcap_isIE) navigator.plugins.refresh(true);
	window.location.reload(false);
}

//CUSTOMIZED: 
//CheckForNetscapePlugin() + new CheckForIEControl() combined 
//with added handling for plugin dialog hide and slideDown show animation
function CheckForPopcapLoader()
{
	var control_installed = false;
	if (popcap_isIE) 
	{
		try
		{
			var controlType = new ActiveXObject("PopCapLoader.PopCapLoaderCtrl2"); 
			if (!controlType)
			{
				$("#plugin_dialog_ie").slideDown("slow");
				//document.write('<p>Click the text above and choose "Install ActiveX Control"<br/>');
				//document.write('to install the PopCap ActiveX Control and run this game.<br/>');
				//document.write('Troubles? Get the manual installer <a href="">here</a>!</p>');
			}
			else
			{
				$("#plugin_dialog_ie").hide();
				control_installed = true;
			}
		}
		catch (e)
		{
			$("#plugin_dialog_ie").slideDown("slow");
		}
		CreateGameTags();
		if (popcap_runPreroll) thisObject('gamediv').style.visibility = 'hidden';
	}
	else 
	{
		try
		{
			var pluginType = 'application/x-popcaploader;version=1.0.0.1';
			mimetype = navigator.mimeTypes[pluginType];
			
			if (!(mimetype && mimetype.enabledPlugin && mimetype.type==pluginType))
			{
				$("#plugin_dialog_ff_activex").slideDown("slow");
				//document.write('<p>You need to get the PopCap Plugin to run this game.<br>');
				//document.write('Get the PopCap Plugin <a href="', popcap_plugin_url, '">here</a>!<br>');
				//document.write('After installing the plugin, launch the game <a href="javascript:;" onclick="reloadPlugin()">here</a>!</p>');
			}
			else
			{
				$("#plugin_dialog_ff_activex").hide();
				CreateGameTags();
				if (popcap_runPreroll) thisObject('gamediv').style.visibility = 'hidden';
				control_installed = true;
			}
		}
		catch (e)
		{
			$("#plugin_dialog_ff_activex").slideDown("slow");
		}
	}
	return control_installed;
}

control_installed = CheckForPopcapLoader();

function PopcapNotification_GameObject(method,param) 
{
	ReceiveNotification(method,param);
}