/******************************************************************
*
*	Flash class
*	Description
*		Class to determine what version is installed on the client machine.
*
*	Usage
*		// Following will write "Current version is: X" where X is the version of your flash player.
*		// If no flash is installed, then major version will be 0.
*		document.write("Current version is: " + String(flash.Version.Major))
*
*******************************************************************/

Version = function(Major, Minor, Revision)
{
	this.Major = Major;
	this.Minor = Minor;
	this.Revision = Revision;
}

Flash = function()
{
	this.isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	this.isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
	this.isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

	this.plugin = null;
	if ( navigator.plugins != null && navigator.plugins.length > 0 )
	{
		if ( navigator.plugins["Shockwave Flash 2.0"] )
			this.plugin = navigator.plugins["Shockwave Flash 2.0"];
		else if ( navigator.plugins["Shockwave Flash"] )
			this.plugin = navigator.plugins["Shockwave Flash"];
	}
	this.sUserAgent = navigator.userAgent.toLowerCase();
	this.Version = this.getVersion();
}

Flash.prototype.tryXVersion = function(iVersion, sDefaultVersion, bSetScriptAccess, bGetVersion)
{
	var sVersion = "";
	var ex;
	
	var sFullName = "ShockwaveFlash.ShockwaveFlash";
	if ( iVersion > 0 ) sFullName += "." + String(iVersion);
	try
	{
		var activeXObj = new ActiveXObject(sFullName);
		if ( sDefaultVersion.length > 0 )	sVersion = sDefaultVersion;
		if ( bSetScriptAccess )				activeXObj.AllowScriptAccess = "always";
		if ( bGetVersion )					sVersion = activeXObj.GetVariable("$version");
	}
	catch (ex)
	{
		sVersion = "";
	}
	return sVersion;
}

Flash.prototype.getActiveXVersion = function()
{
	var sVersion;

	// 7+
	sVersion = this.tryXVersion(7, "", false, true);
	if ( sVersion.length == 0 ) sVersion = this.tryXVersion(6, "6,0,21,0",	true,	true);
	if ( sVersion.length == 0 ) sVersion = this.tryXVersion(3, "",			false,	true);
	if ( sVersion.length == 0 ) sVersion = this.tryXVersion(3, "3,0,18,0",	false,	false);
	if ( sVersion.length == 0 ) sVersion = this.tryXVersion(0, "2,0,0,11",	false,	false);

	// Remove the WIN keyword
	if ( sVersion.length > 0 && sVersion.indexOf("WIN") != -1 ) sVersion = sVersion.split(" ")[1];
	sVersion = sVersion.replace(/,/g, ".");
	return this.parseStrVersion(sVersion);
}

Flash.prototype.getVersion = function()
{
	var ver = new Version(0, 0, 0);
	if ( this.plugin )
	{
		var arrDescription = this.plugin.description.split(" ");
		var arrVersion = arrDescription[2].split(".");

		ver.Major = arrVersion[0];
		ver.Minor = arrVersion[1];

		var iRevision = parseInt((arrDescription[3].length > 0 ? arrDescription[3] : arrDescription[4]).split("r")[1]);
		ver.Revision = iRevision > 0 ? iRevision : 0;
	}
	else if ( this.sUserAgent.indexOf("webtv/2.6") != -1 )	ver.Major = 4;
	else if ( this.sUserAgent.indexOf("webtv/2.5") != -1 )	ver.Major = 3;
	else if ( this.sUserAgent.indexOf("webtv") != -1 )		ver.Major = 2;
	else if ( this.isIE && this.isWin && !this.isOpera ) ver = this.getActiveXVersion();

	return ver;
}

Flash.prototype.parseStrVersion = function(sVersion)
{
	var ver = new Version(0, 0, 0);
	if ( sVersion.length > 0 )
	{
		arrVersion = sVersion.split(".");
		ver.Major = arrVersion[0];
		ver.Minor = arrVersion[1];
		ver.Revision = arrVersion[2];
	}
	return ver;
}

Flash.prototype.hasRequiredVersion = function(reqMajorVer, reqMinorVer, reqRevision)
{
	var ver = this.Version
	if (ver.Major > parseFloat(reqMajorVer))
	{
		return true;
	}
	else if (ver.Major == parseFloat(reqMajorVer))
	{
		if (ver.Minor > parseFloat(reqMinorVer))
		{
			return true;
		}
		else if (ver.Minor == parseFloat(reqMinorVer))
		{
			if (ver.Revision >= parseFloat(reqRevision))
				return true;
		}
	}
	return false;
}

window.flash = new Flash();
