/*
------------------------------------------------------------------------------

Coded by Travis Beckham
http://www.squidfingers.com/
Last modified on October 3, 2006

------------------------------------------------------------------------------
Description
------------------------------------------------------------------------------

This JavaScript file contains scripts that determine the version of Flash 
installed in the browser, and aid in the rendering of html tags needed to 
display a Flash movie.

------------------------------------------------------------------------------
Usage
------------------------------------------------------------------------------

# Example 1
# Redirect to a URL if the correct version of Flash plug-in is not installed, 
# otherwise, write the HTML tags needed the display the Flash movie.

[script]
	if (FlashRedirect.hasVersion(8, 'noflash.html')) {
		var tag = new FlashTag('flash', 'main.swf', '8', '400', '300');
		tag.addParameter('bgcolor', '#ffffff');
		tag.addParameter('allowScriptAccess', 'sameDomain');
		tag.writeHtml();
	}
[/script]

# Example 2
# If the browser has the correct version of the Flash plug-in, write the HTML
# tags needed the display the Flash movie, otherwise, write a message to the
# page telling the user that they need to upgrade their Flash plug-in.

[script]
	if (Flash.hasVersion(8)) {
		var tag = new FlashTag('flash', 'main.swf', '8', '400', '300');
		tag.addVariable('foo', 'bar');
		tag.writeHtml();
	} else {
		document.write('Flash 8 is not installed.');
	}
[/script]

# Example 3
# Insert a Flash movie into an element node after the page has loaded.

[html]
	<div id="myId"></div>
	[script]
		var tag = new FlashTag('flash', 'main.swf', '8', '400', '300');
		tag.appendElement('myId');
	[/script]
[/html]

# Example 4
# Debug the HTML output. There's no need to use this on a live site. It should
# only be use for testing purposes.

[script]
	var tag = new FlashTag('flash', 'main.swf', '8', '400', '300');
	document.write(tag.debug());
[/script]

*/
// ---------------------------------------------------------------------------
// Flash Object

var Flash = new Object();

/**
 * This method returns true is the browser has a Flash plug-in version that is 
 * equal to or greater than the number specified by the parameter
 * 'versionRequired', otherwise, false is returned.
 * 
 * @param versionRequired Number
 * @return Boolean
 */
Flash.hasVersion = function (versionRequired) {
	if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes['application/x-shockwave-flash'] && navigator.mimeTypes['application/x-shockwave-flash'].enabledPlugin) {
		var description = navigator.plugins['Shockwave Flash'].description;
		var version = parseInt(description.charAt(description.indexOf('.') - 1));
		return version >= versionRequired;
	}
	if (navigator.appVersion.indexOf('Windows') != -1 && window.execScript) {
		this.hasVersionResult = null;
		execScript('on error resume next: Flash.hasVersionResult=IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.' + versionRequired + '"))','VBScript');
		return this.hasVersionResult;
	}
	return false;
}

// ---------------------------------------------------------------------------
// FlashRedirect Object

var FlashRedirect = new Object();

/**
 * This method returns true is the browser has the Flash version required. If 
 * it does not, false is returned, and the browser is redirected to the 
 * location specified by the parameter 'redirectLocation'.
 * 
 * @param versionRequired Number
 * @param redirectLocation String
 * @return Boolean
 */
FlashRedirect.hasVersion = function (versionRequired, redirectLocation) {
	if (Flash.hasVersion(versionRequired)) {
		return true;
	} else {
		window.location.href = redirectLocation;
		return false;
	}
}

// ---------------------------------------------------------------------------
// FlashTag Class

/**
 * This class contains the methods needed to generate an HTML string or 
 * HTMLElement needed to display a Flash movie.
 * 
 * @param id String
 * @param movie String
 * @param version Number
 * @param width String
 * @param height String
 */
FlashTag = function (id, movie, version, width, height) {
	this.movie = movie;
	this.version = version;
	this.attrs = new Array();
	this.params = new Array();
	this.vars = new Array();
	this.addAttribute('id', id);
	this.addAttribute('width', width);
	this.addAttribute('height', height);
}
/**
 * Add an attribute to the Flash tag. The use of this method will be rare
 * because the most common attributes are set by the constructor.
 * 
 * @param name String
 * @param value String
 * @return Void
 */
FlashTag.prototype.addAttribute = function (name, value) {
	this.attrs[name] = value;
}
/**
 * Add a parameter to the Flash tag.
 * 
 * @param name String
 * @param value String
 * @return Void
 */
FlashTag.prototype.addParameter = function (name, value) {
	this.params[name] = value;
}
/**
 * Add a variable to be inserted into the root timeline of the Flash movie.
 *
 * @param name String
 * @param value String
 * @return Void
 */
FlashTag.prototype.addVariable = function (name, value) {
	this.vars[name] = value;
}
/**
 * Get a string of HTML that's needed to display the Flash movie.
 * 
 * @return String
 */
FlashTag.prototype.getHtml = function(){
	// build variables
	var fv = '';
	for (var i in this.vars) {
		var v = this.vars[i];
		// build a variable
		if (v) fv += i + '=' + escape(v) + '&';
	}
	this.addParameter('FlashVars', fv);
	// append variables to the movie name
	var movie = this.movie + '?' + fv;
	// build the appropriate tag to display the Flash movie
	if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
		// build an <embed> tag...
		// set specific attributes for the <embed> tag
		this.addAttribute('src', movie);
		this.addAttribute('type', 'application/x-shockwave-flash');
		this.addAttribute('pluginspage', 'http://www.macromedia.com/go/getflashplayer');
		if (this.attrs['id']) this.addAttribute('name', this.attrs['id']);
		// merge the attributes and parameters into a single array
		var attrs = new Array();
		for (var i in this.attrs) attrs[i] = this.attrs[i];
		for (var i in this.params) attrs[i] = this.params[i];
		// build the <embed> tag
		var tag = '<embed';
		for (var i in attrs) {
			var a = attrs[i];
			// build an attribute
			if (a) tag += ' ' + i + '="' + a + '"';
		}
		tag += '></embed>';
	} else {
		// build an <object> tag...
		// set specific attributes for the <object> tag
		this.addAttribute('classid', 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000');
		this.addAttribute('codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + this.version + ',0,0,0');
		// set specific parameters for the <object> tag
		this.addParameter('movie', movie);
		// build the <object> tag
		var tag = '<object';
		for (var i in this.attrs) {
			var a = this.attrs[i];
			// build an attribute
			if (a) tag += ' ' + i + '="' + a + '"';
		}
		tag += '>';
		for (var i in this.params) {
			var p = this.params[i];
			// build a parameter
			if (p) tag += '<param name="' + i + '" value="' + p + '" />';
		}
		tag += '</object>';
	}
	return tag;
}
/**
 * Get an element node that's needed to display the Flash movie.
 *
 * @return HTMLElement
 */
FlashTag.prototype.getElement = function(){
	var div = document.createElement('div');
	div.innerHTML = this.getHtml();
	return div.firstChild;
}
/**
 * Write the Flash movie to the page. This method can only be used as the HTML
 * page is loading.
 * 
 * @return Void
 */
FlashTag.prototype.writeHtml = function(){
	document.write(this.getHtml());
}
/**
 * Insert an element node into the page to display the Flash movie. This 
 * method may be called before or after the page loads, however, the element 
 * specified by the parameter 'elem' must be available in the DOM before this  
 * method is called. If the parameter 'append' is true, the Flash movie will
 * be inserted after the last child node of 'elem', If 'append' is false, the
 * Flash movie replace all the child nodes of 'elem'.
 * 
 * @param elem This parameter accepts either an HTMLElement or an id name.
 * @param append Boolean
 * @return Void
 */
FlashTag.prototype.insertElement = function (elem, append) {
	if (typeof(elem) == 'string') elem = document.getElementById(elem);
	if (append) elem.appendChild(this.getElement());
	else elem.innerHTML = this.getHtml();
}
/**
 * Debug the HTML generated by this class.
 *  
 * @return String
 */
FlashTag.prototype.debug = function(){
	return this.getHtml().replace(/</g, '&lt;').replace(/>/g, '&gt;\n');
}

// ---------------------------------------------------------------------------