// ************************************************************************************ //
//
// File name: flash_detect.js
// VVersion: 1.0
// Date: 03/08/2005
// Author: Christian Madden 
// 
// ************************************************************************************ //

// ************************************************************************************ //
// Function to provide basic browser and platform identification for use by 
// other Javascript functions.  
// Depends:
//	flash_init.js
//  flash_writer.js
// Example of proper page inclusion:
// 	<script src="/js/flash_init.js" type="text/javascript" language="javascript" type="text/javascript"></script>
// 	<script src="/js/flash_writer.js" type="text/javascript" language="javascript" type="text/javascript"></script>
//	<script src="/js/flash_detect.js" type="text/javascript" language="javascript" type="text/javascript"></script>

// ************************************************************************************ //

/* ************************************************************************** */
// VBScript-accessed globals
/* ************************************************************************** */
for(var i = _MIN_FLASH_VERSION; i <= _MAX_FLASH_VERSION; i++)
{
	eval("var vbflash" + i + "Installed = false;");
}
/* ************************************************************************** */

/* ************************************************************************** */
// FlashDetect object
/* ************************************************************************** */
function FlashDetect()
{
	this.minVersion = 2;
	this.maxVersion = 9;
	this.requiredVersion = 2;
	this.detectedVersion = 0;
	this.defaultRedirect = "";
	this.detectionComplete = false;
	
	// Create a FlashWriter object to create the tags
	this.flashWriter = new FlashWriter();
	
	// Load config parameters defined in flash_init.js
	this.readConfigGlobals();
	
	// Init all Flash versions to false
	for(var i = this.minVersion; i <= this.maxVersion; i++)
	{
		eval("this.flash" + i + "Installed = false;");
	}
	
	// Call the appropriate detection method
	if(this.isIEWin()){ this.VBScriptCheck(); }
	else{ this.JSCheck(); }
	
	// Loop through all versions we're checking, and set detectedVersion to highest detected version.
	for(var i = this.minVersion; i <= this.maxVersion; i++)
	{  
		if(eval("this.flash" + i + "Installed") == true){ this.detectedVersion = i; }
	}
	 
	// All WebTV versions use Flash 4
	if(navigator.userAgent.indexOf("WebTV") != -1){ this.detectedVersion = 4; }
}
/* ************************************************************************** */


/* ************************************************************************** */
// Public interface
FlashDetect.prototype.useFlash = function(){ return (this.detectedVersion >= this.requiredVersion); };
FlashDetect.prototype.hasAnyFlashVersion = function(){ return this.detectedVersion > 0; };
FlashDetect.prototype.needsUpgrade = function(){ return (this.hasAnyFlashVersion() && (this.detectedVersion < this.requiredVersion)); };
FlashDetect.prototype.getVersion = function(){ return this.detectedVersion; };
FlashDetect.prototype.insertFlashOrAlternateImage = FlashDetect_insertFlashOrAlternateImage;
FlashDetect.prototype.insertFlashOrAlternateHTML = FlashDetect_insertFlashOrAlternateHTML;
FlashDetect.prototype.insertFlashOrRedirect = FlashDetect_insertFlashOrRedirect;

// Internal
FlashDetect.prototype.readConfigGlobals = FlashDetect_readConfigGlobals;
FlashDetect.prototype.isIEWin = FlashDetect_isIEWin;
FlashDetect.prototype.JSCheck = FlashDetect_JSCheck;
FlashDetect.prototype.VBScriptCheck = FlashDetect_VBScriptCheck;
/* ************************************************************************** */


/* ************************************************************************** */
// Function to generate a flash tag or alternate image
// Args:
// 	swf - the path to the flash file
// 	width - width of the flash or alternate image
// 	height - height of the flash file or alternate image
// 	img - path to the alternate image
// 	OPTIONAL
// 	imgAltText - alt text for the alternate image tag
// 	imgHref - HREF to use if the alternate iimage is to be linked
// 	imgHrefTarget- target for the link around the alternate image
// 	imgMapName - name of the client side image map for the alternate iimage
// 	params - parameters passed to create the flash tag
//	forceAlt - force alternate
//
/* ************************************************************************** */
function FlashDetect_insertFlashOrAlternateImage(swf, width, height, img, imgAltText, imgHref, imgHrefTarget, imgMapName, params, forceAlt, elseMessage)
{
	var html;
	flashUsed = true;
	if(imgHref)
	{
		if(imgHrefTarget)
		{
			html = '<a href="'+ imgHref +'" target="' + imgHrefTarget + '"><img src="' + img + '" height="' + height + '" width="' + width + '" border="0"  alt="' + imgAltText + '" /></a>';
		}
		else
		{
			html = '<a href="'+ imgHref +'"><img src="' + img + '" height="' + height + '" width="' + width + '" border="0"  alt="' + imgAltText + '" /></a>';
		}
	}
	else if(imgMapName)
	{
		html = '<a href="'+ imgHref +'"><img src="' + img + '" height="' + height + '" width="' + width + '" border="0"  alt="' + imgAltText + '" usemap="#' + imgMapName + '" /></a>';
	}	
	else
	{   
	    if(img)
		{
		  html = '<img src="' + img + '" height="' + height + '" width="' + width + '" alt="' + imgAltText + '" />';
		}
		else
		{
		  html = '<p>' + elseMessage + '</p>';	
		}
	}
	this.insertFlashOrAlternateHTML(swf, width, height, html, params, forceAlt)
}
/* ************************************************************************** */


/* ************************************************************************** */
// Function to generate a flash tag or alternate block of HTML code
// Args:
// 	swf - the path to the flash file
// 	width - width of the flash 
// 	height - height of the flash 
// 	html - text string of HTML code
// 	OPTIONAL
// 	params - parameters passed to create the flash tag
//  forceAlt - force alternate
// 
/* ************************************************************************** */
function FlashDetect_insertFlashOrAlternateHTML(swf, width, height, html, params, forceAlt)
{
	flashUsed = true;
	if(this.useFlash() && forceAlt != "html"){ document.write(this.flashWriter.getTag(swf, width, height, params)); }
	else{ document.write(html + "<br />"); }
}
/* ************************************************************************** */


/* ************************************************************************** */
// Function to generate a flash tag or redirect the browser to an alternate page
// Args:
// 	swf - the path to the flash file
// 	width - width of the flash 
// 	height - height of the flash 
// 	redirect_url - URL of the alternate page
// 	OPTIONAL
// 	upgrade_url - URL of the Flash upgrade page
// 	params - parameters passed to create the flash tag
//  forceAlt - force alternate
// 
/* ************************************************************************** */
function FlashDetect_insertFlashOrRedirect(swf, width, height, redirect_url, upgrade_url, params, forceAlt)
{
	flashUsed = true;
	if(this.useFlash() && forceAlt != "html")
	{
		document.write(this.flashWriter.getTag(swf, width, height, params));
	}
	else if(upgrade_url != null && upgrade_url.length > 0 && this.needsUpgrade())
	{
		window.location = upgrade_url;
	}
	else
	{
		window.location = redirect_url;
	}
}
/* ************************************************************************** */


/* ************************************************************************** */
// Function to initialize flash player requirements
/* ************************************************************************** */
function FlashDetect_readConfigGlobals()
{
	this.minVersion = _MIN_FLASH_VERSION;
	this.maxVersion = _MAX_FLASH_VERSION;
	this.requiredVersion = _REQUIRED_FLASH_VERSION;
	this.defaultRedirect = _DEFAULT_FLASH_REDIRECT_URL;
	
}
/* ************************************************************************** */


/* ************************************************************************** */
// Function to determine if the client browser is IE on windows
/* ************************************************************************** */
function FlashDetect_isIEWin()
{
	return ((navigator.appVersion.indexOf("MSIE") != -1) && (navigator.appVersion.toLowerCase().indexOf("win") != -1));
}
/* ************************************************************************** */


/* ************************************************************************** */
// Function to set object variables that indicate installed Flash version.  Uses 
// JavaScript to do the detection
/* ************************************************************************** */
function FlashDetect_JSCheck()
{  	
	if(navigator.plugins)
	{
		// Check for flash 2 or flash 3+.
		if(navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"])
		{
			// Set convenient references to flash 2 and the plugin description.
			var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + isVersion2].description;
			
			// A flash plugin-description looks like this: Shockwave Flash 4.0 r5
			var flashVersion = parseInt(flashDescription.substring(16));

			// We found the version, now set appropriate version flags. Make sure
			// to use >= on the highest version so we don't prevent future version
			// users from entering the site.
			for(var i = this.minVersion; i <= this.maxVersion; i++)
			{
				if(i != this.maxVersion)
				{ 
					eval("this.flash" + i + "Installed = flashVersion == " + i + ";");
				}
				else
				{
					eval("this.flash" + i + "Installed = flashVersion >= " + i + ";");
				}
			}
		}
	}
}
/* ************************************************************************** */

/* ************************************************************************** */
// Function to set object variables that indicate installed Flash version.  Uses 
// VBScript to do the detection, only used on IE Windows machines
/* ************************************************************************** */
function FlashDetect_VBScriptCheck()
{
	// Write VBScript detection on IE Win. IE on Windows doesn't support regular JS plugins array detection.
	if(this.isIEWin())
	{
		document.write('<scr' + 'ipt language="VBScript" type="text\/vbscript"\> \n');
		document.write('on error resume next \n');
		
		// Loop from min to max Flash version and write out VBScript
		for(var i = this.minVersion; i <= this.maxVersion; i++)
		{
			document.write('vbflash' + i + 'Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.' + i + '"))) \n');
		}
		document.write('<\/scr' + 'ipt\> \n'); // Break up end tag so it doesn't end our script

		// Read the VBScript-set global values
		for(var i = this.minVersion; i <= this.maxVersion; i++)
		{
			eval("this.flash" + i + "Installed = vbflash" + i + "Installed");
		}
	}
}
/* ************************************************************************** */


//* ************************************************************************** *//
// Instantiate a FlashDetect object named flash
//* ************************************************************************** *//
var flash = new FlashDetect();
var flashUsed = false;
//* ************************************************************************** *//



