/*
jquery.flash v1.3 -  19/12/09
(c)2009 Stephen Belanger - MIT/GPL.
http://docs.jquery.com/License
*/
$.fn.extend({
	// Create flash object
	flash:function(o) {
		var has, cv, ie;
		
		// Create attr/param strings - these functions are the secret to awesome minification levels.
		function attr(a,b) { return ' '+a+'="'+b+'"'; }
		function param(a,b) { return '<param name="'+a+'" value="'+b+'" />'; }
		
		// get flash version
		// Browsers that don't suck like IE.
		var p = navigator.plugins;
		if (p && p.length) {
			var f = p['Shockwave Flash'];
			if (f) {
				has = true;
				if (f.description)
					cv = f.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split(".");
			}
			if (p['Shockwave Flash 2.0']) {
				has = true;
				cv = '2.0.0.11';
			}
		// IE. I pray that one day I can remove this else block.
		} else {
			try{
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
			}catch(e){
				try {
					// Flash 6 was lame and didn't support GetVariable properly. >.>
					var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
					cv = [6,0,21]; has = true;
				} catch(e) {};
				try {
					axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
				} catch(e) {};
			}
			// Did any of those roundabout attempts at checking for Flash actually work?
			if (axo != null) {
				cv = axo.GetVariable("$version").split(" ")[1].split(",");
				has = true;
				ie = true;
			}
		}
		
		// Loop through all supplied elements (multi-object support--yay!)
		$(this).each(function(){
			// Store current element in variable for easy access.
			var e = $(this);
			
			// Merge supplied settings with defaults
			s = $.extend({
				id					: e.attr('id'), // ID
				class				: e.attr('class'), // class
				width				: e.attr('width'), // Width
				height				: e.attr('height'), // Height
				src					: e.attr('href'), // Path to swf
				classid				: 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000', // Ditto.
				pluginspace			: 'http://get.adobe.com/flashplayer', // Download Firefox plugin if missing.
				availattrs			: [],
				availparams			: [],
				//mime				: 'application/x-shockwave-flash', // Tells the browser what kind of object it is.
				version				: '9.0.24' // Minimum Flash version
			}, o);
			
			// Only swap if user has flash installed.
			if(has) {
				
				// Collect available attributes and parameters.
				var a = s.availattrs.concat(['id','class','width','height','src']),
					b = s.availparams.concat(['src','bgcolor','quality','allowscriptaccess','allowfullscreen','vars','wmode']),
					
					// Parse required version
					rv = s.version.split('.'),
					
					// Reassign 'o' to Open outout buffer.
					o = '<object';
				
				// If codebase hasn't been set we should create it using the required version value.
				if( ! s.codebase) {
					s.codebase = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + rv.join(',');
				}
				
				// If less than minimum version and express install swf is set; replace src swf
				if (s.express) {
					for (var i in cv) {
						// Break as soon as we find a larger portion.
						// 2.1 is newer than 1.2 but of the second portions 2 > 1.
						if ( parseInt(cv[i]) > parseInt(rv[i]) ) {
							break;
						};
						// Only set express install if version is less.
						if ( parseInt(cv[i]) < parseInt(rv[i]) ) {
							s.src = s.express;
						};
					};
				};
				
				// Convert Flash variables to parameter string
				if(s.flashvars) { s.flashvars = unescape($.param(s.flashvars)); }
				
				// IE needs classid and codebasem but breaks on pluginspage
				// Other browsers are the other way around.
				a = ie ? a.concat(['classid','codebase']) : a.concat(['pluginspage']);
				
				// Loop through acceptable attributes.
				for (k in a) {
					// if the current key is 'src' we should use the name 'data'.
					n = (k == a.indexOf('src'))
						? 'data'
						: a[k];
					
					// Add attribute to output buffer.
					o += s[a[k]]
						? attr(n, s[a[k]])
						: '';
				};
				o += '>';
				
				// Loop through acceptable parameters.
				for (k in b) {
					// if the current key is 'src' we should use the name 'movie'.
					n = (k == b.indexOf('src'))
						? 'movie'
						: b[k];
					
					// Add parameter to output buffer.
					o += s[b[k]]
						? param(n, s[b[k]])
						: '';
				};
				
				// Close Object
				o += '</object>';
				
				// Replace
				e.replaceWith(o);
			}
			
			return this;
		});
	}
});