/*
	FlashReplace is developed by Robert Nyman, http://www.robertnyman.com, and it is released according to the
	Creative Commons Deed license (http://creativecommons.org/licenses/GPL/2.0/)
	Modified 20 April 07 - by Aaron Robson, http://intrepidnoodle.com
*/
// ---
var FlashReplace = {
	flashIsInstalled : null,
	defaultFlashVersion : 7,
	replace : function (elmToReplace, src, width, height, version, params){		
		this.flashIsInstalled = this.checkForFlash(version || this.defaultFlashVersion);
		if(elmToReplace && this.flashIsInstalled){
			var markup = this.constructMarkup(src, width, height, params);
			this.replaceElementWithMarkup(elmToReplace, markup);			
		}
	},

	constructMarkup : function(src, width, height, params){
		var obj = '<object' + ((window.ActiveXObject)? ' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" data="' + src + '"' : '');
		obj += ' width="' + width + '"';
		obj += ' height="' + height + '"';
		obj += '>';
		param = '<param';
		param += ' name="movie"';
		param += ' value="' + src + '"';
		param += ' />';
		param += '';
		var extraParams = '';
		var extraAttributes = '';
		for(var i in params){
			extraParams += '<param name="' + i + '" value="' + params[i] + '" />';
			extraAttributes += ' ' + i + '="' + params[i] + '"';
		}
		var embed = '<embed src="' + src + '" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '"';
		var embedEnd = extraAttributes + '></embed>';
		var objEnd = '</object>';
		
		return obj + param + extraParams + embed + embedEnd + objEnd;	
	},
	
	replaceElementWithMarkup : function(elmToReplace, markup){
		elmToReplace.replace(markup); // using prototype here.. change if you wish
	},
	
	checkForFlash : function (version){
		this.flashIsInstalled = false;
		if(window.ActiveXObject){
			try{
				var flash = new ActiveXObject(("ShockwaveFlash.ShockwaveFlash." + version));
				this.flashIsInstalled = true;
			}
			catch(e){
				// Throws an error if the version isn't available			
			}
		}
		else if(navigator.plugins && navigator.mimeTypes.length > 0){
			var flash = navigator.plugins["Shockwave Flash"];
			if(flash){
				var flashVersion = navigator.plugins["Shockwave Flash"].description.replace(/.*(\d+\.\d+).*/, "$1");
				if(flashVersion >= version){
					this.flashIsInstalled = true;
				}
			}
		}
		return this.flashIsInstalled;
	}
};
// ---
