// initialize global variables
var detectableWithVB = false;
var pluginFound = false;

function redirectCheck(pluginFound, redirectURL, redirectIfFound) {
    // check for redirection
    if( redirectURL && ((pluginFound && redirectIfFound) || (!pluginFound && !redirectIfFound)) ) {
	// go away
	window.location = redirectURL;
	return pluginFound;
    } else {
	// stay here and return result of plugin detection
	return pluginFound;
    }	
}

function canDetectPlugins() {
    if( detectableWithVB || (navigator.plugins && navigator.plugins.length > 0) ) {
	return true;
    } else {
	return false;
    }
}

function detectQuickTime(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('QuickTime');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectQuickTimeActiveXControl();
    }
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

function detectReal(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('RealPlayer');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
		pluginFound = (detectActiveXControl('rmocx.RealPlayer G2 Control') ||
		       detectActiveXControl('RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') ||
		       detectActiveXControl('RealVideo.RealVideo(tm) ActiveX Control (32-bit)'));
    }
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

function detectWindowsMedia(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('Windows Media Player');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB)
		pluginFound = detectActiveXControl('MediaPlayer.MediaPlayer.1');

    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

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) ) {
		    // this name was found
		    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

// Here we write out the VBScript block for MSIE Windows
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
    document.writeln('<script language="VBscript">');

    document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
    document.writeln('detectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  detectableWithVB = True');
    document.writeln('End If');

    document.writeln('\'this next function will detect most plugins');
    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('\'and the following function handles QuickTime');
    document.writeln('Function detectQuickTimeActiveXControl()');
    document.writeln('  on error resume next');
    document.writeln('  detectQuickTimeActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('    detectQuickTimeActiveXControl = False');
    document.writeln('    hasQuickTimeChecker = false');
    document.writeln('    Set hasQuickTimeChecker = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1")');
    document.writeln('    If IsObject(hasQuickTimeChecker) Then');
    document.writeln('      If hasQuickTimeChecker.IsQuickTimeAvailable(0) Then ');
    document.writeln('        detectQuickTimeActiveXControl = True');
    document.writeln('      End If');
    document.writeln('    End If');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('</scr' + 'ipt>');
}





// DETECTION FLASH PLAYER

var flash2Installed = false;	// boolean. true if flash 2 is installed
var flash3Installed = false;	// boolean. true if flash 3 is installed
var flash4Installed = false;	// boolean. true if flash 4 is installed
var flash5Installed = false;	// boolean. true if flash 5 is installed
var flash6Installed = false;	// boolean. true if flash 6 is installed
var flash7Installed = false;	// boolean. true if flash 7 is installed
var flash8Installed = false;	// boolean. true if flash 8 is installed
var flash9Installed = false;	// boolean. true if flash 9 is installed
var maxVersion		= 9;		// highest version we can actually detect
var actualVersion	= 0;		// version the user really has
var hasRightVersion = false;	// boolean. true if it's safe to embed the flash movie in the page

var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;				// true if we're on ie
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;	// true if we're on windows


/*
function testAndPrintFlashObject() {

}
*/

function detectFlash() {

	if(isIE && isWin) {
		// Write vbscript detection on ie win. IE on Windows doesn't support regular JavaScript plugins array detection.
		document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n');
		document.write('on error resume next \n');
		document.write('flash2Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.2"))) \n');
		document.write('flash3Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3"))) \n');
		document.write('flash4Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.4"))) \n');
		document.write('flash5Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5"))) \n');  
		document.write('flash6Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6"))) \n');  
		document.write('flash7Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.7"))) \n');
		document.write('flash8Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.8"))) \n');
		document.write('flash9Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.9"))) \n');
		document.write('<\/SCR' + 'IPT\> \n'); // break up end tag so it doesn't end our script
	}

	if(navigator.plugins && (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) ) {
		var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
		var flashDescription = navigator.plugins["Shockwave Flash" + isVersion2].description;

		var flashVersion = parseInt(flashDescription.substring(16));
		flash2Installed = flashVersion == 2;
		flash3Installed = flashVersion == 3;
		flash4Installed = flashVersion == 4;
		flash5Installed = flashVersion == 5;
		flash6Installed = flashVersion == 6;
		flash7Installed = flashVersion == 7;
		flash8Installed = flashVersion == 8;
		flash9Installed = flashVersion >= 9;
	}
  
	// Loop through all versions we're checking, and set actualVersion to highest detected version.
	for(var i = 2; i <= maxVersion; i++)
		if(eval("flash" + i + "Installed") == true)
			actualVersion = i;
  
	// If we're on msntv (formerly webtv), the version supported is 4 (as of January 1, 2004).
	// Note that we don't bother sniffing varieties of msntv. You could if you were sadistic...
	if(navigator.userAgent.indexOf("WebTV") != -1)
		actualVersion = 4;
  
	return actualVersion;

//	if(actualVersion < requiredVersion)
//		window.location = (actualVersion >= 2) ? upgradePage : noFlashPage;
}

function printFlashError(actualVersion, neededVersion) {
	if(!neededVersion)
		neededVersion = 7;

	//document.writeln("<br><br><br>");
	if(actualVersion >= 2)
		document.writeln("<h2>You are still using<br>an older version of Flash player</h2><br><br>");
	else
		document.writeln("<h2>You do not have the flash player installed</h2><br><br>");
	document.writeln("<h3>You must have Flash Player "+ neededVersion +"<br>installed on your computer</h3>");
	document.writeln("<br><a style='color:#C40000' href=\"http://www.macromedia.com/go/getflashplayer\" target=_blank title=\"Click to install the latest version of Flash player\" onmouseover=\"window.status='Click to install the latest version of Flash player'; return true\" onmouseout=\"window.status=''; return true\"><img src=\"/img/get_flashplayer.gif\" border=0 width=88 height=31 alt=\"Get Flash player\"><br>");
	document.writeln("<b>Click here to install the latest version of Flash player</b></a>");
//	document.writeln("<br><p style='font-size:9px'>Macromedia and Flash are trademarks of Macromedia, Inc.</p>");
	//window.location = (actualVersion >= 2) ? upgradePage : noFlashPage;
}

/*
function printFlashObject(ID, objectAttributes, FlashSRC, MoreParams) {
	document.writeln('<object '+ (ID ? "id='"+ID+"'" : "") +' '+ objectAttributes +' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0">');
	document.writeln('<param name=movie value="'+ FlashSRC +'">');
	if(MoreParams)
		document.writeln(MoreParams);
	document.writeln("<param name=quality value=high>");
	document.writeln("<param name=salign value=t>");
	document.writeln("<param name=wmode value=transparent>");
	if(MoreParams) {
		MoreParamsEmbed = MoreParams.replace("<param name=\"", "");
		MoreParamsEmbed = MoreParamsEmbed.replace("\" value=", "=");
		MoreParamsEmbed = MoreParamsEmbed.replace(">", "");
	}
	else
		MoreParamsEmbed = "";

	document.writeln('<embed '+ (ID ? "id='"+ID+"'" : "") +' '+ objectAttributes +' '+ MoreParamsEmbed +' src="'+ FlashSRC +'" quality=high wmode=transparent ');
	document.writeln("pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\">");
	document.writeln("</embed>");
	document.writeln("</object>");
}
*/