// 2003/11/18
/*	
	FlashWriter: squidfingers.com / fivesevensix.com
	

	To redirect the page if a specific version is not found, put this in the <head>:
	
	<script type="text/javascript">
		// Redirect if Flash version isn't found. 
		// arguments: require version 6, go to "noflash.html"
		// Note: will not redirect if the referring page is the no flash page
		FlashWriter.redirect(6, "noflash.html");
	</script>
	
	
	To write Flash Content, put something like this where you want it:
	
	<noscript id="noflash">	
		<!-- alternate content if required version of Flash is not found -->
		<p style="color:red;">Flash Required. (v5+ browsers)</p>
	</noscript>
	<script type="text/javascript">
	
		// create a new FlashWriter. 
		// arguments: required version, swf file, width, height, hideMenu
		fWriter = new FlashWriter(6, 'test.swf', 400, 300, true);
		
		// add a property to object/embed tags besides those available in the constructor
		fWriter.addProperty('bgcolor', '#dddddd');
		
		// add a variable to send to .swf
		fWriter.addVar("a", "aValue");		
		
		// write object/embed tags or alternate content to the page
		// argument 1: id of the noscript tag above
		// argument 2: alternate content for old browsers
		fWriter.write('noflash', 'Flash Required. (v4+ browsers)');
		
	</script>
*/
FlashWriter = function(version, swf, width, height, hideMenu) {
	this.version = version;
	this.swf = swf;
	this.width = width;
	this.height = height;
	this.hideMenu = hideMenu;
	this.vars = {};
	this.props = {};
}
FlashWriter.prototype.addProperty = function(name, value) {
	this.props[name] = value;
}
FlashWriter.prototype.addVar = function(name, value) {
	this.vars[name] = value;
}
FlashWriter.prototype.write = function(altId, altContent) {
	if (!FlashWriter.isRedirecting) document.write(this.getContent(altId, altContent));
}
FlashWriter.prototype.getContent = function(altId, altContent) {
	if (FlashWriter.hasFlashVersion(this.version)) {
		return this.getFlashTags();
	} else {
		var altIdContent = null;
		if (altId != null && document.getElementById && document.getElementById(altId)) {
			altIdContent = document.getElementById(altId).innerHTML.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&");
		}
		if (altIdContent) {
			return altIdContent;
		} else if (altContent != null) {
			return altContent;
		} else {
			return this.getFlashTags();
		}
	}	
}
FlashWriter.prototype.getFlashTags = function() {
	var flashVars = "";
 	for (var i in this.vars) {
 		flashVars += escape(i) + "=" + escape(this.vars[i]) + "&";
 	}
	var fVars = flashVars.length ? "?"+flashVars : "";
	var tags = '<object';
	tags += ' width="'+this.width+'"';
	tags += ' height="'+this.height+'"';
	tags += ' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
	tags += ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+this.version+',0,0,0">';
	tags += '<param name="movie" value="'+this.swf+fVars+'">';
	if (flashVars.length) tags += '<param name="FlashVars" value="'+flashVars+'">'
	if (this.hideMenu) tags += '<param name="menu" value="false">';
	for (var i in this.props) {
		tags += '<param name="'+i+'" value="'+this.props[i]+'">';
	}	
	tags += '<embed src="'+this.swf+fVars+'"';
	tags += ' width="'+this.width+'"';
	tags += ' height="'+this.height+'"';
	if (flashVars.length) tags += ' FlashVars="'+flashVars+'"';
	if (this.hideMenu) tags += ' menu="false"';
	for (var i in this.props) {
		tags += ' '+i+'="'+this.props[i]+'"';
	}
	tags += ' type="application/x-shockwave-flash"';
	tags += ' pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">';
	tags += '<\/embed>';	
	tags += '<\/object>';
	return tags;
}


// Static Class Methods	------------------------------------------------------

FlashWriter.redirect = function(version, noFlashPage, assumeFlash) {
	// do not redirect if previous page is the noFlashPage
	if (document.referrer.indexOf(noFlashPage) == -1 && !this.hasVersionAssumption(version, assumeFlash)) {
		this.isRedirecting = true;
		window.location.replace(noFlashPage);
	}
}
FlashWriter.hasVersionAssumption = function(version, assumeFlash) {
	var hasVersion = this.hasFlashVersion(version);
	// if the version cannot be determined and assumeFlash is not false, then assume they have it
	return (hasVersion == null && (assumeFlash != false)) ? true : hasVersion;
}
FlashWriter.hasFlashVersion = function(requiredVersion) {
	// hasFlashVersion returns:
	// true  - has the correct Flash version or greater
	// false - does not have the correct Flash version
	// null  - cannot be checked for the Flash plug-in
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		var version = 0;
		var plugin = navigator.plugins["Shockwave Flash"];
		if (typeof plugin == "object") {
			var description = plugin.description;
			version = parseInt(description.charAt(description.indexOf(".")-1));
		}
		return (version >= requiredVersion);
	} else if (navigator.appVersion.indexOf("Mac") == -1 && window.execScript) {
		FlashWriter.tmpFlash = false;
		for (var i = requiredVersion; i <= requiredVersion+10 && FlashWriter.tmpFlash != true; i++) {
			execScript('on error resume next: FlashWriter.tmpFlash=IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+i+'"))','VBScript');
		}
		var result = FlashWriter.tmpFlash; delete(FlashWriter.tmpFlash);
		return result;
	}
	return null;
}
FlashWriter.isRedirecting = false;
