/*
	detect object

	called by function, like this: detect.flash.installed();

*/

var detect = {};
function detectionObject(type)
{
	this.type = type;
	this.isPlugin = false;
	this.requireVersion;
	this.getPlugin = new Function("return get" + this.type + "Plugin(this.requireVersion)");
	this.installed = new Function("return get" + this.type + "Installed()");
	this.version = new Function("return get" + this.type + "Version()");
}

/* Browser and Platform stuff */
detect.browser = {};
detect.platform = {};
detect.browser.getWidth = function()
{
	if (document.all)
	{
		if (document.body)
		{
			return document.body.clientWidth;
		}
		else
		{
			return -1;
		}
	}
	else
	{
		return window.innerWidth;
	}
}
detect.browser.width = detect.browser.getWidth();
detect.browser.getHeight = function()
{
	if (document.all)
	{
		if (document.body)
		{
			return document.body.clientHeight;
		}
		else
		{
			return -1;
		}
	}
	else
	{
		return window.innerHeight;
	}
}
detect.browser.height = detect.browser.getHeight();

detect.browser.ie = false;
if (document.all)
{
	detect.browser.ie = true;
}

detect.platform.pc = navigator.platform=="Win32";
detect.platform.mac = navigator.platform.indexOf("Mac")>-1;

var pcie = false;

if (detect.browser.ie) {
    if (detect.platform.pc) {
        detect.browserOs = 'ieWin';
        pcie = true;
    }
    else
        detect.browserOs = 'ieMac';
}
else {
    if (detect.platform.pc)
        detect.browserOs = 'netWin';
    else
        detect.browserOs = 'netMac';
}

if (navigator.userAgent)
{
	if (navigator.userAgent.indexOf('NT 5.0')>-1)
	{
		detect.platform.os = 'Win2000';
	}
}

detect.platform.winVersion = getWinVersion();
detect.browser.ieVersion = getIEVersion();

function getWinVersion()
{
	var rv = -1; // Return value assumes failure
	if (navigator.userAgent.indexOf("Windows") != -1) 
	{
    var ua = navigator.userAgent;
    var re  = new RegExp("Windows NT ([5-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

function getIEVersion()
{
  var rv = -1; // Return value assumes failure
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

detect.showPlugins = new Function("getShowPlugins()");
detect.showAll = new Function("getShowAll()");
function getShowPlugins()
{
	for (a in detect)
	{
			if (detect[a].isPlugin)
			{
				document.write("<b>" + detect[a].type + '</b><br>');
				document.write("Installed: <b>" + detect[a].installed() + "</b>");
				document.write("Version: <b>" + detect[a].version() + "</b><P>");
			}
	}
}
function getShowAll()
{
	detect.showPlugins();
	document.write("detect.browser.ie " + detect.browser.ie + "<br>");
	document.write("detect.platform.pc " + detect.platform.pc + "<br>" );
	document.write("pcie " + pcie + "<br>");
}

// VBscript functions
if (pcie)
{

}

//
// Flash
//
detect.flash =  new detectionObject('flash');
detect.flash.isPlugin = true;
//some defaults. these are meant to be overridden
function getflashPlugin(version){}
detect.flash.minVersion = 3;
detect.flash.maxVersion = 10;

// functions
function getflashInstalled(obj)
{
	out = false;
	if (getflashVersion()>0)
	{
		out = true;
	}
	return out;
}
function getflashVersion()
{
	var min = detect.flash.minVersion;
	var max = detect.flash.maxVersion;
	var version = null;
	if (pcie)
	{
		// Call VBscript for PC IE
		version = vbGetFlashVersion(min,max);
	}
	else
	{
		if(navigator.plugins.length)
		{
			var i;
			//loop through all the plugins installed
			for (i=0; i < navigator.plugins.length; i++)
			{
				//put the plugin string in a variable
				var pluginIdent = navigator.plugins[i].description.split(" ");
				//The Flash Player identification string is ([] = the array index) [0]Shockwave [1]Flash [2]6.0 [3]r21
				
				if(pluginIdent[0] == "Shockwave" && pluginIdent[1] == "Flash")
				{
					//an array of the Flash version number (major.minor)
					var versionArray = pluginIdent[2].split(".");
					version = versionArray[0]; // get major version
					break; //need to break this loop as some browsers may have two versions installed
				}
			}
		}
	}
	
	return version;
}


//
// Virtools
// author: Dhimiter Bozo
// Date: 11/10/2005
//
detect.virtools =  new detectionObject('virtools');
detect.virtools.isPlugin = true;
//some defaults. these are meant to be overridden
//version numbers are not needed since the plugin updates itself
function getvirtoolsPlugin(version){}
function getvirtoolsVersion() {} // this is not yet implemented, but needs to be here
detect.virtools.minVersion = 1;
detect.virtools.maxVersion = 1000;

// functions
function getvirtoolsInstalled(obj)
{
	var installed = null;
	
	if (pcie)
	{
		// Call VBscript for PC IE
		
		installed = vbGetVirtoolsVersion();
	}
	else
	{
		if(navigator.plugins.length)
		{
			var i;
			//loop through all the plugins installed
			for (i=0; i < navigator.plugins.length; i++)
			{
				//put the plugin string in a variable
				var pluginIdent = navigator.plugins[i].description.split(" ");
								
				if(pluginIdent[0] == "Virtools")
				{
					installed = true
					break; //need to break this loop as some browsers may have two versions installed
				}
			}
		}
	}
	
	return installed;
}

/*
	Shockwave
	Version numbers aren't accurate
*/
detect.shockwave =  new detectionObject('shockwave');
detect.shockwave.isPlugin = true;

//some defaults. these are meant to be overridden
function getshockwavePlugin(){}
detect.shockwave.minVersion = 3;
detect.shockwave.maxVersion = 100;

// functions
function getshockwaveInstalled(obj)
{
	out = false;
	//("shockwave version: " + getshockwaveVersion());
	if (getshockwaveVersion()>0)
	{
		out = true;
	}
	return out;
}
function getshockwaveVersion()
{
	var min = detect.shockwave.minVersion;
	var max = detect.shockwave.maxVersion;
	var version = null;
	if (pcie)
	{
		// Call VBscript for PC IE
		version = parseInt(vbGetShockwaveVersion(min,max));
	}
	else
	{
		if (navigator.plugins)
		{
			for ( var i = 0 ; i< navigator.plugins.length ; i++)
			{ 
				if ((navigator.plugins[i].name.indexOf('Shockwave') > -1 )&&(navigator.plugins[i].name.indexOf('Flash') == -1))
				{
					for (var a=min ; a<=max ; a++)
					{
						if ( navigator.plugins[i].description.indexOf(a) > -1 ){version = a }
					}
				}
			}
		}
	}	
	return version;
}

/*
	Real
	G2 version = 3
*/
detect.real =  new detectionObject('real');
detect.real.isPlugin = true;

//some defaults. these are meant to be overridden
function getrealPlugin(){}
detect.real.minVersion = 1;
detect.real.maxVersion = 10;

// functions
function getrealInstalled(obj)
{
	out = false;
	if (getrealVersion()>0)
	{
		out = true;
	}
	return out;
}
function getrealVersion()
{
	var min = detect.real.minVersion;
	var max = detect.real.maxVersion;
	var version = null;
	if (pcie)
	{
		// Call VBscript for PC IE
		version = vbGetRealVersion(min,max);
	}
	else
	{
		if (navigator.plugins)
		{
			for ( var i = 0 ; i< navigator.plugins.length ; i++)
			{ 
				if (navigator.plugins[i].name.indexOf('RealPlayer') > -1 )
				{
					for (var a=min ; a<=max ; a++)
					{
						if ( navigator.plugins[i].name.indexOf(a) > -1 ){version = a }
					}
				}
			}
		}
	}
	return version;
}

/*
	All PCIE versions = 4
	Most NS versions = 3
*/
detect.quicktime =  new detectionObject('quicktime');
detect.quicktime.isPlugin = true;

//some defaults. these are meant to be overridden
function getquicktimePlugin(){}
detect.quicktime.minVersion = 1;
detect.quicktime.maxVersion = 10;

// functions
function getquicktimeInstalled(obj)
{
	out = false;
	if (getquicktimeVersion()>0)
	{
		out = true;
	}
	return out;
}
function getquicktimeVersion()
{
	var min = detect.quicktime.minVersion;
	var max = detect.quicktime.maxVersion;
	var version = null;
	if (pcie)
	{
		// Call VBscript for PC IE
		version = vbGetQuicktimeVersion(min,max);
	}
	else
	{
		if (navigator.plugins)
		{
			var isQuickTime = navigator.mimeTypes &&

			navigator.mimeTypes["video/quicktime"] &&
			navigator.mimeTypes["video/quicktime"].enabledPlugin;
			if ( isQuickTime )
			{
				for (var a=min ; a<=max ; a++)
				{
					if ( isQuickTime.name.indexOf(a) > -1 ){version = a }
				}
				if ( isQuickTime.name.indexOf('4.1') > -1 ) version = 4.1 ;
			}
			if (version == null)
			{
				for ( var i = 0 ; i< navigator.plugins.length ; i++)
				{ 
					if (navigator.plugins[i].name.indexOf('QuickTime') > -1 )
					{
						for (var a=min ; a<=max ; a++)
						{
							if ( navigator.plugins[i].description.indexOf(a) > -1 ){version = a }
						}
					}
				}
			}
		}
	}
	return version;
}

/*
	Windows Media Player
	All versions = 6
*/
detect.wmp =  new detectionObject('wmp');
detect.wmp.isPlugin = true;

//some defaults. these are meant to be overridden
function getwmpPlugin(){}
detect.wmp.minVersion = 1;
detect.wmp.maxVersion = 10;

// functions
function getwmpInstalled(obj)
{
	out = false;
	if (getwmpVersion()>0)
	{
		out = true;
	}
	return out;
}
function getwmpVersion()
{
	var min = detect.wmp.minVersion;
	var max = detect.wmp.maxVersion;
	var version = null;
	if (pcie)
	{
		// Instantiate a wmp object
		document.write('<object WIDTH="1" HEIGHT="1" classid="CLSID:760C4B83-E211-11D2-BF3E-00805FBE84A6" id="DrmStore"></object>');
		document.write('<object ID="WMPlay" WIDTH="1" HEIGHT="1" classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" CODEBASE="#Version=6,2,5,410"></object>');
		// Call VBscript for PC IE
		version = vbGetWmpVersion(min,max);
	}
	else
	{
		if (navigator.plugins)
		{
			v = false;
			if(navigator.mimeTypes && navigator.mimeTypes["application/x-drm"] &&  navigator.mimeTypes["application/x-drm"].enabledPlugin){v=true}
			if(navigator.mimeTypes && navigator.mimeTypes["video/x-ms-wm"] &&  navigator.mimeTypes["video/x-ms-wm"].enabledPlugin){v=true}
			if(navigator.mimeTypes && navigator.mimeTypes["video/x-ms-asf-plugin"] && navigator.mimeTypes["video/x-ms-asf-plugin"].enabledPlugin){v=true}
			if (v)
			{
				version = 6;
			}
		}
	}
	return version;
}



/*
	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 (detect.browser.ie) {
    if (detect.platform.pc) {
        detect.browserOs = 'ieWin';
        pcie = true;
    }
    else
        detect.browserOs = 'ieMac';
}
else {
    if (detect.platform.pc)
        detect.browserOs = 'netWin';
    else
        detect.browserOs = 'netMac';
}


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,params)
{
	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 = params;
	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)
{
	(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 (var a in args)
		{
			if (typeof args[a] != "string"){continue;}
			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='';

	if (ext == ".dcr")
	{
		out += embedShockwave(obj);
	}
	if (ext == ".grv")
	{
		out += embedGroove(obj);
	}
	if ((ext == ".cmo") || (ext == ".vmo"))
	{
		out += embedVirtools(obj);
	}
	return out;
}

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="http://www.nick.com/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;
}
