/*
	Create an 'ord' number so that it can later be passed to a flash movie. this will enable us to play video ads
	in flash movies.
	
	to fix the caching problem on nick.com right now we're not passing the ord num. to flash movies.
	-=Dhimiter=-
	-=03/01/03=-

	more work will be needed to turn this back on, since we're now using FlashVars to pass other args.
	-=Boyer=-
	-=08/14/04=-
*/



if (isNaN(document.axel))
	document.axel = Math.random() + ""; 
ord = document.axel * 1000000000000000000;

/*
	Construct bandwidth parameter if no throughput can be determined in /common/header/insightfirst.jhtml
	D.Hannon 11.26.03
*/
var bps = document.cSpeed?"&bps="+document.cSpeed:"";

/*
	Embed Object
*/
function embedObject(src,id,width,height,args)
{
	if ((width.indexOf("%")>0)&&(detect.browser.ie == false))
	{
		width = width.substring(0,width.indexOf("%"));
		percent = width / 100;
		width = detect.browser.getWidth() * percent;
	}
	if ((height.indexOf("%")>0)&&(detect.browser.ie == false))
	{
		height = height.substring(0,height.indexOf("%"));
		percent = height / 100;
		height = detect.browser.getHeight() * percent;
	}
	
	if (location.hostname == "www.nick.com" || location.hostname == "www.nick.com") {
		this.staticHost = "http://static.nick.com";
	} else if (location.hostname == "www.nickjr.com") {
		this.staticHost = "http://static.nickjr.com";
	} else {
		this.staticHost = "";	
	}
	
	this.height = height||144;
	this.width = width ||176;
	this.src = src;
	this.id = id;
	this.args = args;
	this.embed = new Function("writeEmbed(this)");
	this.debug = new Function("showDebug(this)");
}

/*
	Write Embed
	Controls what is written

	Alternate Content should go here
*/
function writeEmbed(obj)
{
	document.write(getEmbed(obj));
}

// Debug
function showDebug(obj)
{
	alert(getEmbed(obj));
}
/*
	Print parameters.

Since the syntax of the object and embed tags are stable, parameters to them can be passed as an array.
embed syntax is name=value
object syntax is <param name= value= >

*/
function getArgs(obj)
{
    var args = obj.args;
	var out = '';
	if(args!=null)
	{
		// Sets wmode to opaque by default.
		if (args['wmode'])
		{
		}

		else
		{
			args['wmode'] = "opaque";
		}

		// Set wmode to opaque by default (except on win2000)
		// wmode has a tendency to crash IE on win2000
		/*
		if (detect.platform.os == 'Win2000')
		{
			delete args['wmode'];
		}
		*/
		for (a in args)
		{
			if (a == "codebase"){}
			else
			if (pcie)
			{
				out += ' <param name="' + a + '" value="' + args[a] + '">';
			}
			else
			{
				out += ' ' + a + '=' + args[a] + "\n";
			}
		}
		if(args["useStaticDomain"] == "true"){
			if (pcie){
				out += ' <param name="domain" value="' + obj.staticHost + '">';
			}else{
				out += ' domain=' + obj.staticHost + "\n";
			}
		}
	}
	return out;
}
/*
	Figure out which Plugin and what type of embed to use
	This may eventually be replaced by checking the dot-extension rather than having type passed in.
	It could also be a little faster using if/elseif.
*/
function getEmbed(obj)
{
	var s = obj.src.split("?");
	var ext = s[0].substring(s[0].lastIndexOf('.'),s[0].length);
	var out='';
/*
	checking for jhtml - assuming its the asxmaker call for adding in video intro or outro ads.
	- greg graf
*/	
	if (ext == ".jhtml")
	{
		out += embedWmp(obj);
	}	
	if (ext == ".swf")
	{
		if(obj.args["useContainer"] == "false"){
				out += embedFlash(obj);
		}else{
				out += embedFlashContainer(obj);
		}
	}
	if ((ext == ".rm")||(ext == ".ra")||(ext==".smil"))
	{
		out += embedReal(obj);
	}

/* erena added the .wmv -- hoping to add that extention to detection */

	if ((ext == ".asf")||(ext==".asx") || (ext == ".wmv") || (ext == ".wma") )
	{ 
		if  (ext == ".asx") 
		{ 
			var handle = obj.src.substring(0,obj.src.lastIndexOf('.asx'));
			obj.src = handle + ".asx";
		}

		out += embedWmp(obj);


	}
	if (ext == ".mov")
	{
		out += embedQuicktime(obj);
	}
	if (ext == ".dcr")
	{
		out += embedShockwave(obj);
	}
	if (ext == ".grv")
	{
		out += embedGroove(obj);
	}
	if ((ext == ".cmo") || (ext == ".vmo"))
	{
		out += embedVirtools(obj);
	}
	return out;
}
function embedFlash(obj)
{
	var flArgs = obj.args;	
	var out = '';
	var swfSrc;
	
	if(flArgs["useStaticDomain"] == "true"){
		swfSrc = obj.staticHost.concat(obj.src);
	}else{
    swfSrc = obj.src;
	}
	
	
	// lego fix //
	var testArray = Array("jsString",
					  "atoro",
					  "ewkii",
					  "uparu",
					  "aller",
					  "kongu",
					  "test"
					  );
	
	var found;
	var testString = top.document.location.pathname;
	var temp = testString.lastIndexOf("/");
	var tempString = testString.substr(temp+1);
	var temp2 = tempString.lastIndexOf(".");
	var temp3 = (tempString.lastIndexOf("_")+1);
	tempString = tempString.substr(temp3, temp2-temp3);
	
	var swfFound;
	var swfTestString = swfSrc;
	var swfTemp = (swfTestString.lastIndexOf(".")-6);
	var swfTempString = swfTestString.substr(swfTemp+1);
	var swfTemp2 = swfTempString.lastIndexOf(".");
	swfTempString = swfTempString.substr(0, swfTemp2);
	for(i = 0; i < testArray.length; i++) {
		if(swfTempString == testArray[i]) {
			alert("ding!")
			found = testArray[i];
			break;
		}
	}
	
	if (pcie && found != swfTempString)	{
	out += '<OBJECT ' + 
	'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '+
	'codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash_dir/swflash.cab#version=4,0,2,0" '+
	'width="'+ obj.width + '" '+
	'height="'+ obj.height + '" '+
	'id="' + obj.id + '" ' +
	'><param  name="movie" value="' + swfSrc + '">'+
	/*'><param  name="FlashVars" value="_ord=' + ord  + bps +'">'+*/
	'><param  name="menu" value="false">'+
	"\n";
	}
	else if(found != swfTempString) 
	{
	out += "<EMBED " + 
	'name="' + obj.id + '" ' +
	'pluginspage="http://www.macromedia.com/shockwave/download/" ' +
	'type="application/x-shockwave-flash" ' + "\n" +
	'width="' + obj.width + '" \n' +
	'height="' + obj.height + '" \n' +
	/*'flashvars="_ord=' + ord + bps +'" \n' +*/
	'menu="false" \n' +
	'src=' + swfSrc + 
	" \n";
	} else if(found == swfTempString) {
			alert(obj.id);
			out += "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' "+
			"codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' "+
			"width='500' "+
			"height='300' "+
			"id='bionicle_game' "+
			"align='middle'>"+
			"<param name='allowScriptAccess' value='always' />"+
			"<param name='movie' value='"+swfSrc+"' />"+
			"<param name='quality' value='high' />"+
			"<param name='bgcolor' value='#000000' />"+
			"<param name='flashvars' value='advert=yes' />"+
			"<param name='wmode' value='transparent' />"+
			"<embed 	src='"+swfSrc+"' "+
			"quality='high' "+
			"wmode='transparent'"+
			"bgcolor='#000000' "+
			"width='500' "+
			"height='300' "+
			"name='bionicle_game' "+
			"align='middle' "+
			"allowScriptAccess='always' "+
			"type='application/x-shockwave-flash' "+
			"pluginspage='http://www.macromedia.com/go/getflashplayer' "+
			"flashvars='advert=yes' />"+
			"</object>";
	}
	out += getArgs(obj);
	out += getClose();
	return out;
}
function embedFlashContainer(obj)
{
	var out = '';
	var flArgs = obj.args;
	var swfSrc;
	var domain;
	var containerPath;
		if (flArgs["wmode"])
		{
		}

		else
		{
			flArgs["wmode"] = "opaque";
		}

	if(flArgs["useStaticDomain"] == "true"){
		swfSrc = obj.staticHost.concat(obj.src);
		domain = obj.staticHost;
		containerPath = obj.staticHost.concat("/dynamo/droplets/detect/multimedia/loader.swf");
	}else{
    swfSrc = obj.src;
		domain = "";
		containerPath = "/dynamo/droplets/detect/multimedia/loader.swf";
	}

	flArgs["paramList"] += ":domain";

	out += '<object type="application/x-shockwave-flash" ';
	if(!pcie){
 		out += 'data="' + containerPath + '" ';
	}
	out += 'width="'+ obj.width + '" '+
	'height="'+ obj.height + '" '+
	'id="' + obj.id + '" >';
	if(pcie){
		out += '<param  name="movie" value="' + containerPath + '">';
	}
	out += '<param name="FlashVars" value="path=' + swfSrc + '&paramList=' + flArgs["paramList"] + '&domain=' + domain + '&' + flArgs["FlashVars"] + '"/>'+'<param  name="menu" value="false"><param name="scale" value="noscale"><param name="wmode" value="' + flArgs["wmode"] + '">';
	if (flArgs["allowScriptAccess"])
	{
		out += '<param name="allowScriptAccess" value="' + flArgs["allowScriptAccess"] + '">';
	}
	out += '</object>';
	return out;
}
function embedReal(obj)
{

	if ((obj.src.indexOf('://')<0)||(obj.src.indexOf('://')>4))
	{
		obj.src = getPluginServerPath('real') + obj.src;
	}

	// Everything gets a smil, mainly so MacIE doesn't pop 2 players.
	var autosmil = "/dynamo/droplets/detect/autosmil.smil?src=";
	obj.src = autosmil + obj.src;

	var out = '';
	// True for now <-----------------------------------------
	var showControls = true;

	// Real uses 2 embeds (player and controller) so we dump it into a table
	var tableCenter = '</td></tr><tr><td align="center">';

	out += '<table border="0" cellpadding="0" cellspacing="0"><tr><td>';

	if (pcie)
	{
		// Image Window
		out += '<OBJECT ' + 
		'classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" '+
		'width="'+ obj.width + '" '+
		'height="'+ obj.height + '" '+
		'id="' + obj.id + '" ' +
		'>'+
		'<param name="src" value="' + obj.src + '">'+ "\n"+
		'<param name="controls" value="ImageWindow">'+ "\n"+
		'<param name="console" value="_master">'+ "\n"+
		'<param name="autoStart" value="true">'+ "\n"+
		'<param name="nolabels" value="true">'+ "\n";
		out += getArgs(obj);
		out += getClose();
		// Controller Window
		if (showControls)
		{
			//
			out += tableCenter;
			//
			out += '<OBJECT ' +  ' ' +
			'classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" '+
			'width="'+ obj.width + '" '+
			'height="25" '+
			'id="' + obj.id + '" ' +
			'>'+
			'<param name="src" value="' + obj.src + '">'+ "\n"+
			'<param name="controls" value="ControlPanel">'+ "\n"+
			'<param name="console" value="_master">'+ "\n"+
			'<param name="autoStart" value="true">'+ "\n"+
			'<param name="nolabels" value="true">'+ "\n";
			out += getArgs(obj);
			out += getClose();
		}
	}
	else
	{
		// Image Window
		out += "<EMBED " + 
		'name=' + obj.id + " " +
		'TYPE="audio/x-pn-realaudio-plugin" ' + "\n" +
		'width="' + obj.width + '" \n' +
		'height="' + obj.height + '" \n' +
		'src=' + obj.src + " \n" +
		'CONTROLS="ImageWindow" '+ " \n" +
		'CONSOLE="_master" '+ " \n" +
		'autoStart="true" '+ " \n" +
		'NOLABELS="true" '+ " \n" +
		" ";
		out += getArgs(obj);
		out += getClose();
		// Controller Window
		if (showControls)
		{
			//
			out += tableCenter;
			//
			out += "<EMBED " + 
			'name=' + obj.id + " " +
			'TYPE="audio/x-pn-realaudio-plugin" ' + "\n" +
			'width="' + obj.width + '" \n' +
			'height="25" \n' +
			'src=' + obj.src + " \n" +
			'CONTROLS="ControlPanel" '+ " \n" +
			'CONSOLE="_master" '+ " \n" +
			'autoStart="true" '+ " \n" +
			'NOLABELS="true" '+ " \n" +
			" ";
			out += getArgs(obj);
			out += getClose();
		}
	}
	out += '</td></tr></table>';
	return out;
}

function embedWmp(obj)
{
	var htmlCode = '';
	
	if ((obj.src.indexOf('://')<0)||(obj.src.indexOf('://')>4))
	{
/*
	unless its the asxmaker in which case, don't prepend the akamai string.  for adding in video intro or outro ads.
	- greg graf
*/		
		if (obj.src.indexOf('asxmaker')<0) {
			obj.src = getPluginServerPath('wmp') + obj.src;
		} else {
		/* create an alternate plug in like on vh1 */
		
      htmlCode += '<object id="' + obj.id + '" ';
      htmlCode += '        name="' + 'test' + '" ';
      htmlCode += '        width="' + obj.width + '" ';
      htmlCode += '        height="' + (parseInt(obj.height) + 50) + '" ';
      htmlCode += '        classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" ';
      htmlCode += '        standby="Loading Windows Media Player components..." ';
      htmlCode += '        type="application/x-oleobject">';
      htmlCode += '  <param name="FileName" value="' + obj.src + '">';
      htmlCode += '  <param name="ShowControls" value="true">';	  
      htmlCode += '  <param name="ShowStatusBar" value="false">';	  
      htmlCode += '  <param name="EnableContextMenu" value="false">';	  
      htmlCode += '  <param name="AutoSize" value="false">';	
      htmlCode += '  <param name="DisplaySize" value="0">';		  
	  htmlCode += '  <embed name="' + obj.id + '" src="' + obj.src + '" type="application/x-mplayer2" width="' + obj.width + '" height="' + (parseInt(obj.height) + 50) + '" ShowControls="1" ShowStatusBar="0" AutoSize="true" EnableContextMenu="0" DisplaySize="0" pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/"></embed>';
      htmlCode += '</object>';  
		}
	}
	
	var out = '';

	/* Adjust for controller height. */
	var Height = parseInt(obj.height) + 50;
	
	if (pcie)
	{
		// Image Window
		out += '<OBJECT ' + 
		'classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" '+
		'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715" '+
		'standby="Loading Microsoft® Windows Media™ Player components..." '+
		'type="application/x-oleobject" '+
		'width="'+ obj.width + '" '+
		'height="'+ Height + '" '+
		'id="' + obj.id + '" ' +
		'>'+
		'<param name="FileName" value="' + obj.src + '">'+ "\n"+
		'<param name="showControls" value="true">'+ "\n"+
		'<param name="autoStart" value="true">'+ "\n"+
		'<param name="autoRewind" value="true">'+ "\n";
		out += getArgs(obj);
		out += getClose();
	}
	else
	{
		// Image Window
		out += "<EMBED " + 
		'name=' + obj.id + " " +
		'TYPE="application/x-mplayer2" ' + "\n" +
		'pluginspage="http://www.microsoft.com/isapi/redir.dll?prd=windows&sbp=mediaplayer&ar=media&sba=plugin&" ' + "\n" +
		'width="' + obj.width + '" \n' +
		'height="' + Height + '" \n' +
		'filename=' + obj.src + " \n" +
		'showControls="true" '+ " \n" +
		'autoStart="true" '+ " \n" +
		'autoRewind="true" '+ " \n" +
		" ";
		out += getArgs(obj);
		out += getClose();
	}
	if (htmlCode != '') out = htmlCode;
	return out;
}

function embedQuicktime(obj)
{
	if ((obj.src.indexOf('://')<0)||(obj.src.indexOf('://')>4))
	{
		obj.src = getPluginServerPath('quicktime') + obj.src;
	}
	var out = '';
	if (pcie)
	{
		// Image Window
		out += '<OBJECT ' + 
		'classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" '+
		'codebase="http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0" '+
		'width="'+ obj.width + '" '+
		'height="'+ obj.height + '" '+
		'id="' + obj.id + '" ' +
		'>'+
		'<param name="SRC" value="' + obj.src + '">'+ "\n"+
		'<param name="AUTOPLAY" value="true">'+ "\n"+
		'<param name="CONTROLLER" value="true">'+ "\n";
		out += getArgs(obj);
		out += getClose();
	}
	else
	{
		// Image Window
		out += "<EMBED " + 
		'name=' + obj.id + " " +
		'pluginspage="http://www.apple.com/quicktime/download/" ' + "\n" +
		'width="' + obj.width + '" \n' +
		'height="' + obj.height + '" \n' +
		'SRC=' + obj.src + " \n" +
		'AUTOPLAY="true" '+ " \n" +
		'CONTROLLER="true" '+ " \n" +
		" ";
		out += getArgs(obj);
		out += getClose();
	}
	return out;
}

// Not done yet.
function embedShockwave(obj)
{
	var out = '';
	if (pcie)
	{
		// Image Window
		out += '<OBJECT ' + 
		'classid="clsid:166B1BCA-3F9C-11CF-8075-444553540000" '+
		'codebase="http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=7,0,0" '+
		'width="'+ obj.width + '" '+
		'height="'+ obj.height + '" '+
		'name="' + obj.id + '" ' +
		'>'+
		'<param name="SRC" value="' + obj.src + '">'+ "\n";
		out += getArgs(obj);
		out += getClose();
	}
	else
	{
		// Image Window
		out += "<EMBED " + 
		'name=' + obj.id + " " +
		'pluginspage="http://www.macromedia.com/shockwave" ' + "\n" +
		'width="' + obj.width + '" \n' +
		'height="' + obj.height + '" \n' +
		'z-index="-100" \n' +
		'SRC=' + obj.src + " \n" +
		" ";
		out += getArgs(obj);
		out += getClose();
	}
	return out;
}
function embedGroove(obj)
{
	var out = '';
	if (pcie)
	{

		out += '<OBJECT ' + 
		'classid="clsid:77e32299-629f-43c6-ab77-6a1e6d7663f6" '+
		'id="GrooveControl" ' + 
		'width="'+ obj.width + '" '+
		'height="'+ obj.height + '" '+
		'align="middle" '+
		'border="0" " '+
		'codebase="http://www.nick.com/common/groove/gx/GrooveAX25.cab#version=1,0,25,0" ' +
		'>';
		out += getArgs(obj);
		out += getClose();
	}
	else
	{
		out += "<EMBED " + 
		'SRC="/common/groove/gx/Groove.dcr" ' +
		'bgColor="#000000" ' +
		'border="0" ' +
		'WIDTH="' + obj.width + '" ' +
		'HEIGHT="' + obj.height + '" ' +
		'swStretchStyle="none" ' +
		'TYPE="application/x-director" ' +
		'PLUGINSPAGE="http://www.macromedia.com/shockwave/download/" ' +
		'componentURL="/common/groove/gx/GrvComponents.gff" ';
		out += getArgs(obj);
		out += getClose();
	}
	return out;
}

function embedVirtools(obj)
{	
	//var codebase = "http://a532.g.akamai.net/f/532/6712/4h/player.virtools.com/downloads/player/Install3.0/Installer.exe#Version=3,0,0,100";
	var codebase = obj.args['codebase'];
	var out = '';
	if (pcie)
	{
	out += '<OBJECT ' + 
	'classid="clsid:C4925E65-7A1E-11D2-8BB4-00A0C9CC72C3" '+
	'width="'+ obj.width + '" '+
	'height="'+ obj.height + '" '+
	'id="' + obj.id + '" ' +
	'codebase="' + codebase + '" ' +
	'><param  name="SRC" value="' + obj.src + '">'+
	"\n";
	}
	else
	{
	out += "<EMBED " + 
	'name="' + obj.id + '" ' +
	'pluginspage="http://player.virtools.com/" ' +
	'type="application/x-virtools" ' + "\n" +
	'width="' + obj.width + '" \n' +
	'height="' + obj.height + '" \n' +
	'src=' + obj.src + 
	" \n";
	}
	out += getArgs(obj);
	out += getClose();
	return out;
}

function getClose()
{
	var out = pcie ? '</OBJECT>' : '></EMBED>'
	return out;
}
