/***********************************************************************************
 * @name: FlashObject
 * @description: Detects for Flash Player and writes the flash movie to the page
 *
 * @author:	christopher a. guy
 * @company: Tribal DDB Chicago
 * @copyright: copyright (c) 2005 Tribal DDB Chicago.
 *				Free to redistribute and modify as long as copyright 
 *				notice remains intact.
 *
 * @date: 2005/03/25
 * @version: 1.3
 *
 * @revisons: 2005-06-21 - Added render function 
 *			  2005-06-23 - The parameters wmode, FlashVars and allowScriptAccess 
 *				can now be set using the addParameter method
 * 			  2005-12-20 - Added default swLiveConnect parameter
 *			  2006-01-06 - Set bgcolor and quality using addParameter 
 *				instead of directly setting it when creating html
 *			  2006-01-06 - Added workaround to fix display problem with Safari and 
 *				other KHTML based browsers. 
 ***********************************************************************************/

/**
 * FlashObject : Contructor Object
 *
 * @param fn	File name & path of the swf to display (required)
 * @param id	Name of the flash movie. Default is 'flashMovie' (optional)
 * @param w		Width of the flash movie (optional)
 * @param h		Height of the flash movie (optional)
 * @param c		Hex String of the background color. Default is '#CCCCCC' (optional)
 * @param v		Flash Player version to check. Default is 6 (optional)
 * @param fv	List of variable to pass to flash movie (optional)
 * @param wn	Window mode. Default is 'window' (optional) 
 *				Options: window | opaque | transparent 
 *				
 * @param sa	Sets the script access. Default is 'sameDomain' (optional)
 *
 * @return nothing
 */
function FlashObject(fn,id,w,h,c,v,fv,wm,sa) {
	// pre-defined parameters
	this._fileName	= (fn != null)	? fn : ''; // File Name
	this._id		= (id != null)	? id : "flashMovie"; // name
	this._width		= (w != null)	? w : 100;	// movie width
	this._height	= (h != null)	? h : 100; // movie height
	this._color		= (c != null)	? c : "#FFFFFF";			// movie bgcolor
	this._version	= (v != null)	? v : 6;		// flash version
	
	// set optional addParameter variables to default state
	this._variables		= (fv != null)? fv : ''; // flash variables
	this._windowMode	= (wm != null)? wm : "window";	// Window Mode window | opaque | transparent
	this._scriptAccess	= (sa != null)? sa : "sameDomain"; // allow script access
	
	/* Add add the parameters
	 * we do it this way to enable backwards compatability with 1.0
	 * these can be overwritten using addParameter function
	 */
	this.addParameter("FlashVars",this._variables);
	this.addParameter("wmode",this._windowMode);
	this.addParameter("allowScriptAccess",this._scriptAccess);
	
	/* Added 2005-12-20 */
	this.addParameter("swLiveConnect","true");

	/* Added 2006-01-06 */
	this.addParameter("bgcolor",this._color);
	this.addParameter("quality","high");
	
	// check for the required file name
	if (this._fileName.length < 5 || this._fileName.indexOf(".swf") == -1) {
		document.writeln("Please provide the name and path of a Flash file to display<br />");
	} else {
		// detect flash player
		this.FlashCanPlay = this.detectFlash(this._version);
	}
}

/**
 * addParameter : Adds additional parameters to the script
 *
 * @param name	The name of the parameter (required)
 * @param value	The parameter's value (required)
 *
 * @return nothing
 */
FlashObject.prototype.addParameter = function(name, value) {
	if(this.param == null){
		this.param = new Object();
	}
	this.param[name] = value;
}

/**
 * detectFlash : Detects for flash player version defined in constructor
 *
 * @param version	The Flash Player version to check for (required)
 *
 * @return boolean true | false
 */
FlashObject.prototype.detectFlash = function(version) {
	flashCanPlay = false; // initialize variable without var so vbscript can access it
	var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
	if ( plugin ) {
		var words = navigator.plugins["Shockwave Flash"].description.split(" ");
		   for (var i = 0; i < words.length; ++i) {
			if (isNaN(parseInt(words[i])))
				continue;
			var pluginVersion = words[i]; 
		   }
		flashCanPlay = pluginVersion >= version;
	}
	else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 && (navigator.appVersion.indexOf("Win") != -1)) {
		execScript('on error resume next: flashCanPlay = IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+version+'"))','VBScript');
	}
	return (flashCanPlay);
}

/**
 * createHTML : Creates the html to display flash movie
 *
 * @return	If Flash Player is detected it returns the html to write the flash movie.
 *			If Flash Player is not detected it returns false.
 */
FlashObject.prototype.createHTML = function(){
	var html = '';
	if (this.FlashCanPlay){
		html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ';
		html += '  codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+this._version+',0,0,0" ';
		html += '	width="'+ this._width +'" height="'+ this._height +'" id="'+ this._id +'" align="middle">';
		html += '	<param name="movie" value="'+ this._fileName +'" />';

		// add additional parameters for WIN IE
		if(this.param != null){
			for (var name in this.param){
				if(name != "swLiveConnect") {
					html += '<param name="' + name + '" value="' + this.param[name] + '" />';
				}
			}
		}
		html += '<embed src="'+ this._fileName +'" ';
		html += '	align="middle" ';
		html += '	width="'+ this._width +'" ';
		html += '	height="'+ this._height +'" ';
		html += '	name="'+ this._id +'" ';
        html += '	id="'+ this._id +'" ';

		// add additional parameters for everyone else
		if(this.param != null){
			for (var name in this.param){
				html += '	' + name + '="' + this.param[name] + '" ';
			}
		}
		html += '	type="application/x-shockwave-flash"';
		html += '	pluginspage="http://www.macromedia.com/go/getflashplayer" />';
		html += '</object>';
	}else{
		return false;
	}
	return html;
}
/**
 * render : writes the flash hmtl to the page
 *
 * @param html		The html string to display (required)
 * @param elementId	The id of the element to display the hmtl (optional)
 * @param debug		Prints the flash variables (optional)
 *
 * @return nothing
 */
FlashObject.prototype.render =	function(html,elementId,debug){
	var elementId = (elementId != null) ? elementId : false;
	var html = (html != null) ? html : "<h2>Missing html</h2>";

	html = (debug) ? this.showDebugInfo() : html;

	if (elementId) {
		document.getElementById(elementId).innerHTML = html;

		/* Added 2006-01-06	: Workaround to force Safari and KHTML based browsers to repaint document */
		var ua = navigator.userAgent.toLowerCase();
		if(ua.indexOf("safari") != -1 || ua.indexOf("applewebkit") != -1 || ua.indexOf("konqueror") != -1 ) {
			// create a new tag and fill it with the contents of the elementId innerText
			var box = document.createElement("div");
			box.innerText = document.getElementById(elementId).innerText;
			box.setAttribute("style","display:none;");
			document.body.appendChild(box);
		}
	} else {
		document.write(html);
	}
}
/**
 * redirect : Redirects user to a supplied url
 *
 * @param url	The url to send to
 * 
 *				url can be a fully qualified url and path: 'http://www.somesite.com/no-flash/'
 *				or it can be a relative link within the site: '../home/no-flash.hmtl'
 *
 * @return nothing
 */
FlashObject.prototype.redirect = function(url){
	if(url != null){
		document.location.replace(url);
	}else{
		document.writeln("Please supply a redirect url.<br />");
	}
}
/**
 * showDebugInfo : Concats a string of variables and returns them.
 *
 * @return string of variables
 */
FlashObject.prototype.showDebugInfo = function(){

	var vars='';
	vars += "movie" + " = '" + this._fileName + "'<br />";
	vars += "id" + " = '" + this._id + "'<br />";
	vars += "width" + " = '" + this._width + "'<br />";
	vars += "height" + " = '" + this._height + "'<br />";
	vars += "version" + " = '" + this._version + "'<br />";
	
	for (var name in this.param){
		vars += name + " = '" + this.param[name] + "'<br />";
	}

	return vars;
}