/*
##############################################################################################
#   $Source: /home/cvs/clientside/cvsroot/jstools/mod/mod.jst_plugins.js,v $
#   $Author: stephenc $
#   $Revision: 1.16 $
#   $Date: 2004/11/25 15:46:26 $
##############################################################################################
*/

bbcjs.trace("<b><font color=green>jst_plugins.js</font> was included.</b>",2);

//Create the main plugins object:
bbcjs.plugins =
{
	flashVersion	: 0,
	flash			: false,
	numFlashMovies	: 0,
	real			: false,
	wwguides		: {
						flash : "<a href=\"/webwise/askbruce/articles/download/howdoidownloadflashplayer_1.shtml\" onclick=\"return wwguide('flash');\">WebWise Flash install guide</a>",
						real  : "<a href=\"/webwise/askbruce/articles/download/howdoidownloadrealplayer_1.shtml\" onclick=\"return wwguide('real');\">WebWise RealPlayer install guide</a>"
				    },
	lowversionStr	: "<p>The Flash movie you are trying to view requires Flash player version <<this.version>>, whilst this computer seems to "+
					  "only have version <<bbcjs.plugins.flashVersion>>. In order to view this movie you must upgrade your Flash "+
					  "player.</p><p> To do this, visit the <<bbcjs.plugins.wwguides.flash>></p>",
	noflashStr		: "<p>You are trying to view a Flash movie although you have no version of Flash installed. To find out how to "+
					  "install Flash, go to the <<bbcjs.plugins.wwguides.flash>></p>"

};

//Flash detect function for browsers other than IE/Windows - IE-Win uses vbs file included.
bbcjs.plugins.getFlashVersion = function()
{
	var s = "";
	var fv, hv = 0;
	for (var i=0; i<navigator.plugins.length; i++)
	{
		s = navigator.plugins[i].description;
		if (s.indexOf("Flash")!=-1)
		{
			fv = parseInt(s.charAt(s.indexOf(".") - 1));
			if (fv > hv) hv = fv;
		}
	}
	return hv;
}
//Only run vbscript on browsers supporting activex controls.
if (navigator.plugins.length==0)
{
	//need to write out vbs function due to failing in NS3...
	document.write("<script language=vbscript>Function vbs_getIEFlashVersion ()\nDim flashversion\nflashversion = 0\n\nOn Error Resume Next\n\nFor i = 2 to 8\nIf Not(IsObject(CreateObject(\"ShockwaveFlash.ShockwaveFlash.\" & i))) Then\n'flashversion = 0\nElse\nflashversion = i\nEnd If\nNext\n\nvbs_getIEFlashVersion = flashversion\nEnd Function</"+"script>");
	bbcjs.plugins.flashVersion = vbs_getIEFlashVersion();
}
else bbcjs.plugins.flashVersion = bbcjs.plugins.getFlashVersion();
if (bbcjs.plugins.flashVersion > 0) bbcjs.plugins.flash = true;

bbcjs.trace("User seems to have <b>flash "+bbcjs.plugins.flashVersion+"</b>",3);


//Realplayer (G2) detection, no version number:
bbcjs.plugins.detectReal = function()
{
	var retval = false;
	for (var i=0; i<navigator.plugins.length; i++)
	{
		if (navigator.plugins[i].description.indexOf("Real")!=-1) retval = true;
	}
	return retval
}
if (navigator.plugins.length==0)
{
	//need to write out vbs function due to failing in NS3...
	document.write("<script language=vbscript>Function vbs_detectReal ()\nOn Error Resume Next\nvbs_detectReal = (IsObject(CreateObject(\"rmocx.RealPlayer G2 Control\")))\nEnd Function</"+"script>");
	bbcjs.plugins.real = vbs_detectReal();
}
else bbcjs.plugins.real = bbcjs.plugins.detectReal();

bbcjs.trace("Real installed: <b>"+bbcjs.plugins.real+"</b>",3);


/* Embedding functions: */

//Creates a new flash movie object (not actual flash) to represent a movie. Default settings are below:
bbcjs.plugins.FlashMovie = function (src, v)
{
	this.src = src;
	this.width = "100%";
	this.height = "100%";
	this.quality = "autohigh";
	this.loop = false;
	this.play = true;
	this.menu = true;
	this.htmlString = "";
	this.align = "";
	
	this.version = 5;
	if (v) this.version = v;
	
	this.flashvars = "";
	this.bgcolor = "";
	
	this.lowversion = bbcjs.plugins.lowversionStr;
	this.noflash	= bbcjs.plugins.noflashStr;

	//Store a reference of the number of flash movies we have on a page, then set up an id to reference this movie.
	bbcjs.plugins.numFlashMovies++;
	this.id = "flashmovie_"+bbcjs.plugins.numFlashMovies;

	//Allow us to embed a movie by use of 'flashMovie.embed()'
	this.embed = function ()
	{
		return bbcjs.plugins.embedFlashMovie(this);
	}
}


//Main function to embed a movie onto the page. Uses document.write to provide either the movie, or an error message.
bbcjs.plugins.embedFlashMovie = function(movObj,v)
{
	//If we've just taken in a url [and a version number], create a new movie object.
	if (typeof(movObj)=="string") movObj = new bbcjs.plugins.FlashMovie(movObj);
	if (typeof(v)!="undefined") movObj.version = v;

	bbcjs.trace("<b>bbcjs.plugins.embedFlashMovie</b> called...",3);
	movObj.htmlString = bbcjs.plugins.getFlashEmbedString(movObj,v);
	document.write(movObj.htmlString);
}

bbcjs.plugins.getFlashEmbedString = function (movObj, v)
{
	var outStr = "";
	var fvStr = "";
	var fvStrE= "";
	var alStr = "";

	//If the user has a version capable of playing this file, embed it onto the page
	if (movObj.version <= bbcjs.plugins.flashVersion)
	{
		if (movObj.flashvars != "")
		{
			fvStr  = '<param name="flashvars" value="'+movObj.flashvars+'">'; //for object tag
			fvStrE = ' flashvars="'+movObj.flashvars; //for embed tag
		}
		if (movObj.align!="") alStr =  'align="'+movObj.align+'"';
		bbcjs.trace("Correct version of flash found (need v"+movObj.version+"), embedding movie",2);
		outStr = ('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+movObj.width+'"'+
					' height="'+movObj.height+'" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"'+
					' id="'+movObj.id+'"'+alStr+'>'+
					'<param name="movie" value="'+movObj.src+'">'+
					'<param name="loop" value="'+movObj.loop+'">'+
					'<param name="quality" value="'+movObj.quality+'">'+
					'<param name="menu" value="'+movObj.menu+'">'+
					'<param name="autoplay" value="'+movObj.play+'">');

		if (movObj.flashvars != "") outStr += '<param name="flashvars" value="'+movObj.flashvars+'">';
		if (movObj.bgcolor != "")   outStr += '<param name="bgcolor" value="'+movObj.bgcolor+'">';
					
		outStr += ('<embed src="'+movObj.src+'" width="'+movObj.width+'"'+
					' height="'+movObj.height+'" autoplay="'+movObj.play+'" loop="'+movObj.loop+'"'+
					' quality="'+movObj.quality+'" menu="'+movObj.menu+'"');

		//if (movObj.flashvars != "") outStr += ' flashvars="'+movObj.flashvars+'"';
		if (movObj.bgcolor != "")   outStr += ' bgcolor="'+movObj.bgcolor+'"';
		
		outStr += (' pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"'+
					' type="application/x-shockwave-flash"></embed>'+
					'</object>');
		bbcjs.trace("Full flash output:<br />"+bbcjs.HTMLOut(outStr),5);
		return outStr;
	}
	//If we do not have a correct flash version, display an error message.
	else
	{
		bbcjs.trace("Incorrect or no flash found! Showing error message:",2);
		var errorstr = movObj.noflash; //for if there is no flash installed
		if (bbcjs.plugins.flash) errorstr = movObj.lowversion; //for if there is a flash version too low to play

		//Simple variable replacement, using <<variable>>:
		var tvi = "";
		var tv = errorstr.match(/\<\<([a-zA-Z0-9\._]*)\>\>/g);
		if(tv)
		{
			for (var i=0; i<tv.length; i++)
			{
				tvi = tv[i].replace(/[\<\>]/g,"");
				tvi = tvi.replace(/this/g, "movObj");
				bbcjs.trace("flashEmbed:: replacing '"+tvi+"'",6);
				tvi = eval(tvi);
				errorstr = errorstr.replace(tv[i],tvi);
			}
		}

		//Write out the error to the page.
		//document.write(errorstr);
		return errorstr;
	}
}

// REAL PLAYER EMBED TO BE PLACED HERE