/* Real Player detection on a Web page.
* Version 1.0
* March 09, 2004
* Paul Davis <pdavis@real.com>, Real Networks Inc.
* This example illustrates how to detect an installed copy of Real Player to ensure proper playback of Real Media files embedded in a web page.
* If the user has IE5+, the code will attempt to instantiate the Real Player active X control and query it. If the user doesn't have IE5+, the code will 1st attempt to look for Real Player in the plug-ins array and then the mime-Type array.
* The code won't detect the presence Real Player if Netscape Browser 6+ is installed after installing Real Player. This is a known issue in Netscape browser.
* The code will return a "true" value if Real Player version G2 or above is installed, but it may not be able to detect the correct Real Player version number in all cases.
* The recommended next step, when the detection fails to find Real Player, is to redirect the user to a landing page, and allow the user to choose to either download Real Player or to play the embedded contents anyway. In the example, http://guide.real.com is used as the landing page.
* The URL to download the latest release of RealPlayer is http://www.real.com/player.
* Finally, the Real Player detection logic should be intended to enhance web user experience, and not to be used as a mean to restrict access to contents.
*/

/*
* The version variable will be set when we are able
* to detect the version. Note, we do not do comprehensive
* version detection, because of the time overhead in
* instantiating a plugin. This invloves creating an instance of
* the plugin (on non Windows IE5+ platforms), setting up a
* background thread to check when it is ready and querying
* the plugin for its version.
*/

var version = 0;

function isObject(type)
{
 return ( "undefined" != typeof(type) );
}

function winIE5upPlyrDetect()
{
 var player;
 var iectl;
 try
 {
  // attempt to instantiate the IE control to see if activeX is enabled.
  iectl = new ActiveXObject("Shell.Explorer");
 }
 catch(e) {}
 try
 {
  player = new ActiveXObject("rmocx.RealPlayer G2 Control.1");
  version = (player.GetVersionInfo());
 }
 catch(e) {}
 if(!isObject(iectl))
 {
  return "unknown"; //ActiveX disabled
 }
 return new String(isObject(player));
}

function checkPlugin(name)
{
 plugin = navigator.plugins[name];
 if(isObject(plugin))
 {
  version = plugin.description;
  return true;
 }
 return false;
}

function pluginDetect()
{
 return (
 (checkPlugin("RealPlayer Version Plugin"))
 ||
 (checkPlugin("RealOne Player Version Plugin"))
 );
}

function detectPlugin() {
    /* allow for multiple checks in a single pass */
    var daPlugins = detectPlugin.arguments;
   	/* consider pluginFound to be false until proven true */
    var pluginFound = false;
  	 /* if plugins array is there and not fake */
    if (navigator.plugins && navigator.plugins.length > 0) {
	var pluginsArrayLength = navigator.plugins.length;
	/* for each plugin... */
	for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
	  	/* loop through all desired names and check each against the current plugin name */
	    var numFound = 0;
	    for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
	    /* if desired plugin name is found in either plugin name or description */
		if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || 
		    (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
		   
		    numFound++;
		}   
	    }
	    /* now that we have checked all the required names against this one plugin, */
	    /* if the number we found matches the total number provided then we were successful */
	    if(numFound == daPlugins.length) {
		pluginFound = true;
		/* if we've found the plugin, we can stop looking through at the rest of the plugins */
		break;
	    }
	}
    }
    return pluginFound;
}/* detectPlugin */

function mimeTypeDetect()
{
 return (
 isObject(navigator.mimeTypes)
 &&
 isObject(navigator.mimeTypes["audio/x-pn-realaudio-plugin"])
 );
}

function isWinIE5plus()
{
 var result = false;
 var uaLower = navigator.userAgent.toLowerCase();
 if(uaLower.indexOf("windows") >=0 && uaLower.indexOf("msie")>=0)
 {
  var versRX = /msie\s+[5-9]/;
  result = versRX.test(uaLower);
 }
 return result;
}

/*
* Returns a string, "true", "false", "unknown" based on the results
* of player detection.
* "false" will only be returned if the user has IE5+, activeX is 
* enabled, and we can not instantiate the player control.
* That is the only case where we are sure the user does not have
* the player installed. All other cases where the player can not
* be detected will return "unknown".
*/

function hasRealPlayer()
{
 if( isWinIE5plus() )
 {
  return winIE5upPlyrDetect();
 }
 else
 {
  return ((pluginDetect() || mimeTypeDetect())?"true":"unknown");
 }
}

function playStream(url, failpage)
{
 switch( hasRealPlayer()+"" )
 {
  case "true":
  /* Successful player detection */
  document.location.href = url;
  break;
 
  case "unknown":
  /* Unable to determine if the user has a player
  * If using for media ensure resulting page offers link to the media. */
  document.location.href = url;
  break;
 
  case "false":
  /* Player is definitely not installed */
  noplayer=window.open(failpage,"noplayer","width=550,height=400,left=20,top=100,resizable=yes")
  noplayer.focus();
  break;
 }
}
