/**
 * Javascript for embedding Flash objects
 * @version 2.1
 * @author pmander
 */

var Q = "\"";

/**
 * A Object representing a Flash Object
 * @param data	The flash source file
 * @param width The width of the flash object
 * @param height	The height of the flash object
 * @param bgcolor	The hex code of the background color of the flash object
 * @param altImage	An alternative image to use if Flash is not installed
 * @param altName	A user friendly name for the Flash object
 */
function FlashObject(data, width, height, bgcolor, altImage, altName)
{
	//fields
	this.data = data;
	this.width = width;
	this.height = height;
	this.bgcolor = bgcolor;
	this.altImage = altImage;	
	this.altName = altName;
	this.params = new Array();
	this.minVersion = 0;
	this.link = "";
	
	//methods
	this.addParam = FlashObject_addParam;
	this.setLink = FlashObject_setLink;
	this.setMinVersion = FlashObject_setMinVersion;
	this.print = FlashObject_print;
}
/**
 * Adds a <param> tag to the flash object
 * @param name	the paramater's name
 * @parma value	the parameter's value
 */
function FlashObject_addParam(name, value)
{
	this.params.push(new Param(name, value));
}

/**
 * Adds a link by appending the query string '?link=url to the data field of this FlashObject
 * @param the url of the link
 */
function FlashObject_setLink(url)
{
	this.link = "?link=" + url;
}
/**
 * Sets the minimum version of Flash required to play the Flash Object
 * @param minVersion	an integer
 */
function FlashObject_setMinVersion(minVersion)
{
	this.minVersion = minVersion;
}
/**
 * Prints the FlashObject within the specified element 
 * @param flashContainerID	the ID of the element to print the flash within
 */
function FlashObject_print(flashContainerID)
{
	var flashContainer = document.getElementById(flashContainerID);
	
	var data = this.data;
	var width = this.width;
	var height = this.height;
	var bgcolor = this.bgcolor;
	var altImage =  this.altImage;
	var altName = this.altImage;
	
	var flashVersion = getFlashVersion();
	var link = this.link;
	
	var markup = "";
	
	if(isFlashInstalled() && flashVersion >= flashObject.minVersion)
	{
		if(bgcolor == "")
			bgcolor = "#ffffff";
		
		//get params
		var params = flashObject.params;
		var paramsString = "";
		
		for(var i = 0;  i < params.length; i++)
		{
			var paramName = params[i].name;
			var paramValue = params[i].value;
			paramsString += "<param name=" + Q + paramName + Q + " value=" + Q + paramValue + Q + " />";
		}
		
		//get markup
		markup = "<object type=\"application/x-shockwave-flash\" "
					+ "data=" +  Q + data + link + Q
					+ "width=" + Q + width + Q
					+ "height=" + Q + height + Q + ">"
					+ "<param name=\"movie\" value=" + Q + data + link + Q + "/>"
					+ "<param name=\"quality\" value=\"high\" />"
					+ "<param name=\"bgcolor\" value=" + Q + bgcolor + Q + " />"	
					+ paramsString
					+ "<embed src=" + Q + data + link + Q
						+ "type=\"application/x-shockwave-flash\" "
						+ "width=" + Q + width + Q
						+ "height=" + Q + height + Q
						+ "quality=\"high\" "
						+ "bgcolor=" + Q + bgcolor + Q + " >"
					+ "</embed>"
				  + "</object>";
		
		flashContainer.style.background = "none";
	}
	else if(altImage != "")
	{
		markup = "<img src=" + Q + altImage + Q	
					+ "width=" + Q + width + Q
					+ "height=" + Q + height + Q
					+ "alt=" + Q + altName + Q
					+ "style=\"border-width: 0\" />";
					
		flashContainer.style.background = "none";
	}
	else
	{
		if(altName == "")
			altName = "This feature";		
		
		var url = "http://www.macromedia.com/software/flashplayer/";
		
		//modify this text for different languages
		if(flashVersion >= flashObject.minVersion)
		{
			markup =	"<p><strong>" + altName + " cannot be displayed.</strong></p>" 
					  +	"<p>To benefit from this feature the Macromedia Flash Player plugin must be installed.</p>"
					  + "<p><a target=\"_blank\" href=" + Q + url + Q + ">Download Macromedia Flash Player plugin.</a></p>";
		}
		else
		{
			markup =	"<p><strong>" + altName + " cannot be displayed.</strong></p>" 
					  +	"<p>To benefit from this feature a newer version of the Macromedia Flash Player plugin must be installed.</p>"
					  + "<p><a target=\"_blank\" href=" + Q + url + Q + ">Download latest version of Macromedia Flash Player plugin.</a></p>";
		}

		flashContainer.style.overflow = "auto";
	}
	
	flashContainer.style.height = height + "px";
	flashContainer.style.width = width + "px";  
	
	flashContainer.innerHTML = markup;	
}

/** 
 * An object representing a parameter
 */
function Param(name, value)
{
	this.name = name;
	this.value = value;
}

/**
 * Determines if flash is installed 
 */
function isFlashInstalled()
{
 	var flashInstalled = false;

	if(navigator.mimeTypes['application/x-shockwave-flash'] && navigator.mimeTypes['application/x-shockwave-flash'].enabledPlugin)
	{
		flashInstalled = true;
	}
	else if(navigator.plugins["Shockwave Flash"])
	{
		flashInstalled = true;
	}
	else if(navigator.appName.indexOf("Internet Explorer") >= 0)
	{
		try
		{
			var test = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			flashInstalled = true;
		}
		catch(e) { }
	}		

	return flashInstalled;
}

/**
 * Gets the current flash version 
 * @return Returns the latest version of flash if the version could not be detected or if flash is not installed
 */
function getFlashVersion()
{
	var MAX_FLASH_VERSION = 10;
	var version = MAX_FLASH_VERSION;
	
	if(navigator.plugins["Shockwave Flash"])
	{
		var description = navigator.plugins["Shockwave Flash"].description;
		version = parseInt(description.charAt(description.indexOf('.')-1));
		
	}
	else if(navigator.appName.indexOf("Internet Explorer"))
	{
		for(var i=MAX_FLASH_VERSION; i > 0; i--)
		{
			try
			{
				var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
				version = parseInt(i);
				break;
			}
			catch(e) { }
		}
	}

	return version;
}
	
	
