//** $Id: assistant.js,v 1.6 2006/06/07 14:57:19 jau Exp $//**/
//
// Versions object. Holds the currently-required version of each of the
// multimedia helpers we support.
//
//
versions = {
	toolbar: '1.0',
	real:    '6.0.12.000',
	helix:   '0.4.0',
	flash:   '7.0',
	shock:   '8.5',
	quick:   '7.1',
	wmp:     '10.0',
	activex: '1.0'
};
//
// This variable holds the toolbar object, if it's installed. Otherwise, null.
//
var toolbar=null;

//
// This is called on page load by the toolbar with the toolbar scripting object
// as an argument.
//
function ToolBarInit(t)
{
	toolbar=t;
}

//
// Set a cookie
//
function set_cookie(name, value, expires, path, domain, secure)
{
	document.cookie = name + "=" + escape(value) + 
	((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
	((path == null) ? "" : "; path=" + path) +
	((domain == null) ? "" : "; domain=" + domain) +
	((secure == null) ? "" : "; secure");
}

//
// Checks for plugins installed, returns true is present; false, otherwise
//
// @mime string passed mime type.
//
function check_plugin(mime)
{
        var plugin;

        plugin = false;

        try {
                if (navigator.mimeTypes)
                {
                        plugin = navigator.mimeTypes[mime].enabledPlugin;
                }

        } catch (oError) {
                return false;
        }
        return (plugin) ? true : false;
}

//
// Return true if the user's browser can do ActiveX; false, otherwise
//
function has_activex()
{
	var retval=false;
	var ie_control; // Internet Explorer control object
	
	try
	{
		// Shell.Explorer should always be available if anything is.
		ie_control = new ActiveXObject('Shell.Explorer');
		retval=true;
	}
	catch(e)
	{
		retval=false;
	}
	return retval;
}

//
// Get the version of RealPlayer the user has installed.
//
function getver_real()
{
	var retval='Unknown';
	var player=null; // RealPlayer object
	if (navigator.plugins && navigator.plugins.length)
	{
		for (var i=0; i<navigator.plugins.length; i++)
		{
			// Recent RealPlayers come with a 'RealPlayer Version Plugin'
			// for Gecko browsers. Isn't that nice of them? If there isn't a
			// RealPlayer Version plugin, their RealPlayer isn't recent enough
			// for us (it might work, but go ahead and tell 'em to upgrade it
			// anyway).
			if (navigator.plugins[i].name.match(/RealPlayer Version/gi))
			{
				player=navigator.plugins[i];
				retval=navigator.plugins[i].description;
				break; // Found it. Don't need to check anymore.
			}
		}
	}
	else if (has_activex())
	{
		// If they have ActiveX, we can get the RealPlayer information by
		// instantiating it as an ActiveXObject
		try
		{
			player = new ActiveXObject('rmocx.RealPlayer G2 Control.1');
			retval=player.GetVersionInfo();
		}
		catch(e)
		{
			retval='None';
		}
	}
	return retval;
}

//
// Get the version of the Helix DNA plugin (RealPlayer-compatible plugin for
// Linux etc.)
//
function getver_helix()
{
	var retval='None';
	if (navigator.plugins && navigator.plugins.length)
	{
		for (var i=0; i<navigator.plugins.length; i++)
		{
			if (navigator.plugins[i].name.match(/RealPlayer G2/gi))
			{
				retval = (navigator.plugins[i].description.match(/version [0-9.]+/i)[0]).match(/[0-9.]+/)[0];
			}
		}
	}
	return retval;
}

//
// Get the version of Flash Player the user has installed.
//
// This actually only works for Flash Player 7, and only under 
//
function getver_flash()
{
	var retval='Unknown';
	var flash;  // Our flash object, if any

	// If we're running a Gecko derivative, we'll have a navigator.plugins array
	if (navigator.plugins && navigator.plugins.length)
	{
		flash = navigator.plugins['Shockwave Flash'];
		// Found a Navigator plugin, so use it to grab the version number
		if (flash && flash.description)
		{
			retval = flash.description.match(/[0-9.]+/)[0];
		}
		else
		{
			retval='None';
		}
	}
	// Uh oh. We couldn't find it in navigator.plugins. Maybe this is an IE
	// derivative? Check for ActiveX
	else if (has_activex())
	{

		try
		{
			flash = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.7');
			retval='7.0';
		}
		catch(e)
		{
			retval = 'None';
		}
	}
	
	return retval;
}

//
// Get the version of Shockwave the user has installed.
//
function getver_shock()
{
	var retval='None';
	var shock; // Our shockwave object, if any
	
	// Gecko-style plugin detect
	if (navigator.plugins && navigator.plugins.length)
	{
		shock = navigator.plugins['Shockwave for Director'];
		if (shock && shock.description)
		{
			retval = shock.description.match(/version ([0-9.]*)/)[1];
		}
	}
	// IE-style plugin detect
	else if (has_activex())
	{
		try
		{
			shock = new ActiveXObject("SWCtl.SWCtl");
			retval = shock.ShockwaveVersion('');
		}
		catch(e)
		{
			retval = 'None';
		}
	}
	// If it doesn't use Gecko or IE style plugin detect, we can't be sure
	// the user doesn't have it.
	else
	{
		retval='Unknown';
	}
	return retval;
}

//
// Get the version of QuickTime the user has installed.
//
function getver_quick()
{
	var retval='None';
	var quick; // Our QuickTime object, if any
	
	// Gecko-style plugin detect
	if (navigator.plugins && navigator.plugins.length)
	{
	   if (navigator.plugins != null && navigator.plugins.length > 0) {
		  for (i=0; i < navigator.plugins.length; i++ ) {
			 var plugin = navigator.plugins[i];
			 if (plugin.name.indexOf("QuickTime") > -1) {
				retval = parseFloat(plugin.name.substring(18));
				break;
			 }
		  }
	   }
	}
	// IE-style plugin detect
	else if (has_activex())
	{
		try
		{
			quick = new ActiveXObject("QuickTimeCheckObject.QuickTimeCheck.1");
			retval = quick.QuickTimeVersion.toString(16) / 1000000;
		}
		catch(e)
		{
			retval = 'None';
		}
	}
	// If it doesn't use Gecko or IE style plugin detect, we can't be sure
	// the user doesn't have it.
	else
	{
		retval='Unknown';
	}
	return retval.toString();
}
//
// Get the version of Windows Media Player the user has installed.
//
function getver_wmp()
{
	var retval = 'None';
	var wmp = null;
	
	// Check if we can make an IE ActiveX object of it
	if (has_activex())
	{
		try
		{
			wmp = new ActiveXObject('WMPlayer.OCX');
			retval=wmp.versionInfo;
		}
		catch(err)
		{
			retval='Unknown';
		}
	}
	// Give the GeckoActiveXObject a shot
	else if (window.GeckoActiveXObject)
	{
		try
		{
			wmp = new GeckoActiveXObject('WMPlayer.OCX.7');
			retval=wmp.versionInfo;
		}
		catch(e)
		{
			retval='None';
		}
	}
	
	// If both of those failed, try scraping the plugins array. We won't
	// be able to get a version number, but we can at least detect if they
	// have a WMP at all.
	if (navigator.plugins && navigator.plugins.length && retval=='None')
	{
		for (var i=0; i<navigator.plugins.length; i++)
		{
			if (navigator.plugins[i].name.match(/Windows Media Player/i))
			{
				if (parseInt(navigator.plugins[i].versionInfo))
				{
					retval = navigator.plugins[i].versionInfo;
				}
				else
				{
					retval='Unknown';
				}
			}
		}
	}
	return retval;
}

//
// Get the version of the ActiveX plugin the user has installed.
//
function getver_activex()
{
	var retval='Not Installed';

	if (activex==true)
	{
		retval='1.0'; // If a version check becomes available it will need to go here.//
	}	
	return retval;
}

//
// Set the text of the DOM item with the given ID to the given text.
//
function set_text(id, text)
{
	var victim = document.getElementById(id);
	if (victim)
	{
		if (typeof(victim.innerText)!='undefined')
		{
			victim.innerText=text;
		}
		else
		{
			if (!victim.childNodes[0])
			{
				victim.appendChild(document.createTextNode(text));
			}
			else
			{
				victim.childNodes[0].nodeValue=text;
			}
		}
	}
}

//
// Returns true if 'current' is greater than or equal to 'required'.
// This supports (i.e., throws away) other characters in the version string,
// which has the side effect that v_i_a('1.0a', '1.0b') will come back true.
// We'll deal with that if we ever require a version where the alpha is 
// significant.
//
function version_is_adequate(current, required)
{
	// Clean up the versions (we only want numbers and decimals), then
	// explode on those decimals
	var ver_regex = /[^0-9.]/gi;
	var ld_regex  = /^\./gi;
	var cur_split = current.replace(ver_regex, '').replace(ld_regex, '').split('.');
	var req_split = required.replace(ver_regex, '').replace(ld_regex, '').split('.');
	var retval=true;
	
	// Loop until we run out of version number places or we find a version 
	// number less than what we want
	for (var place=0; place<Math.max(cur_split.length,req_split.length) && retval; place++)
	{
		cur = parseInt(cur_split[place]);
		req = parseInt(req_split[place]);
		// Make sure that we're not comparing a decimal place against a null
		req = req ? req : 0;
		cur = cur ? cur : 0;
		
		// Retval gets set to false if'n it finds a current-place that's
		// less than the corresponding required-place
		retval = cur >= req;
		
		// Of course, if we hit a spot in the current version number 
		// that's//larger* than the corresponding
		// spot in the required version number, we can stop looking.
		if (cur > req)
		{
			break;
		}
	}
	
	return retval;
}

//
// Set the status block with the given ID to the appropriate value, and hides
// the download link if it's not needed.
//
function set_status(id, current, required)
{
	var victim = document.getElementById('stat_' + id);
	
	if (victim)
	{
		if (version_is_adequate(current, required))
		{
			set_text('stat_' + id, 'OK');
			document.getElementById('dl_' + id).style.display='none';
		}
		else
		{
			set_text('stat_' + id, 'NEED');
			victim.style['background']='#FFFF00';
		}
	}
}

//
// Initialize the screen so that it shows only the rows we want shown.
//
function init()
{
	var cur_versions = {
		real: getver_real(),
		flash: getver_flash(),
		shock: getver_shock(),
		quick: getver_quick(),
		wmp: getver_wmp(),
		activex: getver_activex()
	};
	
	var expiration = new Date();

	// This section checks for OS version. If version is not XP or newer
	// it will change the wmp array to version 9.0.

	// check OS
	// for OS other than XP set min WMP version to 9
	if (navigator.userAgent.indexOf("Windows NT 5") < 0)
	{
		versions['wmp'] = "9.0";	
	}
	// If it's a Linux system, we're going to be looking for the Helix DNA
	// player instead of the real RealPlayer player.
	if (getver_helix() != 'Unknown' && getver_helix() != 'None')
	{
		cur_versions['real'] = getver_helix();
		versions['real']=versions['helix'];
	}
	
	
	
	// Cycle through the list of plugins to check for
	for (var i in checkfor)
	{
		var check=checkfor[i];
		// Set the 'Required Version' cells
		set_text('req_' + check, versions[check]);
		// Set the 'Current Version' cells
		set_text('ver_' + check, cur_versions[check]);
		// Set the 'Status' and 'Download link' cells
		set_status(check, cur_versions[check], versions[check]);
	}
}

var checkfor = new Array("real","flash","shock","quick","wmp","activex");
addLoadListener(init);

