2 * Flash (http://jquery.lukelutman.com/plugins/flash)
3 * A jQuery plugin for embedding Flash movies.
8 * Copyright (c) 2006 Luke Lutman (http://www.lukelutman.com)
9 * Dual licensed under the MIT and GPL licenses.
10 * http://www.opensource.org/licenses/mit-license.php
11 * http://www.opensource.org/licenses/gpl-license.php
14 * SWFObject (http://blog.deconcept.com/swfobject/)
15 * UFO (http://www.bobbyvandersluis.com/ufo/)
16 * sIFR (http://www.mikeindustries.com/sifr/)
19 * The packed version of jQuery breaks ActiveX control
20 * activation in Internet Explorer. Use JSMin to minifiy
21 * jQuery (see: http://jquery.lukelutman.com/plugins/flash#activex).
30 * @desc Replace matching elements with a flash movie.
35 * @param Hash htmlOptions Options for the embed/object tag.
36 * @param Hash pluginOptions Options for detecting/updating the Flash plugin (optional).
37 * @param Function replace Custom block called for each matched element if flash is installed (optional).
38 * @param Function update Custom block called for each matched if flash isn't installed (optional).
43 * @example $('#hello').flash({ src: 'hello.swf' });
44 * @desc Embed a Flash movie.
46 * @example $('#hello').flash({ src: 'hello.swf' }, { version: 8 });
47 * @desc Embed a Flash 8 movie.
49 * @example $('#hello').flash({ src: 'hello.swf' }, { expressInstall: true });
50 * @desc Embed a Flash movie using Express Install if flash isn't installed.
52 * @example $('#hello').flash({ src: 'hello.swf' }, { update: false });
53 * @desc Embed a Flash movie, don't show an update message if Flash isn't installed.
56 $$ = jQuery.fn.flash = function(htmlOptions, pluginOptions, replace, update) {
58 // Set the default block.
59 var block = replace || $$.replace;
61 // Merge the default and passed plugin options.
62 pluginOptions = $$.copy($$.pluginOptions, pluginOptions);
65 if(!$$.hasFlash(pluginOptions.version)) {
66 // Use Express Install (if specified and Flash plugin 6,0,65 or higher is installed).
67 if(pluginOptions.expressInstall && $$.hasFlash(6,0,65)) {
68 // Add the necessary flashvars (merged later).
69 var expressInstallOptions = {
71 MMredirectURL: location,
72 MMplayerType: 'PlugIn',
73 MMdoctitle: jQuery('title').text()
76 // Ask the user to update (if specified).
77 } else if (pluginOptions.update) {
78 // Change the block to insert the update message instead of the flash movie.
79 block = update || $$.update;
82 // The required version of flash isn't installed.
83 // Express Install is turned off, or flash 6,0,65 isn't installed.
84 // Update is turned off.
85 // Return without doing anything.
90 // Merge the default, express install and passed html options.
91 htmlOptions = $$.copy($$.htmlOptions, expressInstallOptions, htmlOptions);
93 // Invoke $block (with a copy of the merged html options) for each element.
94 return this.each(function(){
95 block.call(this, $$.copy(htmlOptions));
102 * @desc Copy an arbitrary number of objects into a new object.
105 * @example $$.copy({ foo: 1 }, { bar: 2 });
106 * @result { foo: 1, bar: 2 };
109 $$.copy = function() {
110 var options = {}, flashvars = {};
111 for(var i = 0; i < arguments.length; i++) {
112 var arg = arguments[i];
113 if(arg == undefined) continue;
114 jQuery.extend(options, arg);
115 // don't clobber one flash vars object with another
116 // merge them instead
117 if(arg.flashvars == undefined) continue;
118 jQuery.extend(flashvars, arg.flashvars);
120 options.flashvars = flashvars;
124 * @name flash.hasFlash
125 * @desc Check if a specific version of the Flash plugin is installed
129 $$.hasFlash = function() {
130 // look for a flag in the query string to bypass flash detection
131 if(/hasFlash\=true/.test(location)) return true;
132 if(/hasFlash\=false/.test(location)) return false;
133 var pv = $$.hasFlash.playerVersion().match(/\d+/g);
134 var rv = String([arguments[0], arguments[1], arguments[2]]).match(/\d+/g) || String($$.pluginOptions.version).match(/\d+/g);
135 for(var i = 0; i < 3; i++) {
136 pv[i] = parseInt(pv[i] || 0);
137 rv[i] = parseInt(rv[i] || 0);
138 // player is less than required
139 if(pv[i] < rv[i]) return false;
140 // player is greater than required
141 if(pv[i] > rv[i]) return true;
143 // major version, minor version and revision match exactly
148 * @name flash.hasFlash.playerVersion
149 * @desc Get the version of the installed Flash plugin.
153 $$.hasFlash.playerVersion = function() {
157 // avoid fp6 minor version lookup issues
158 // see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
159 var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
160 try { axo.AllowScriptAccess = 'always'; }
161 catch(e) { return '6,0,0'; }
163 return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
167 if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
168 return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
176 * @name flash.htmlOptions
177 * @desc The default set of options for the object or embed tag.
183 pluginspage: 'http://www.adobe.com/go/getflashplayer',
185 type: 'application/x-shockwave-flash',
190 * @name flash.pluginOptions
191 * @desc The default set of options for checking/updating the flash Plugin.
195 expressInstall: false,
201 * @name flash.replace
202 * @desc The default method for replacing an element with a Flash movie.
205 $$.replace = function(htmlOptions) {
206 this.innerHTML = '<div class="alt">'+this.innerHTML+'</div>';
208 .addClass('flash-replaced')
209 .prepend($$.transform(htmlOptions));
214 * @desc The default method for replacing an element with an update message.
217 $$.update = function(htmlOptions) {
218 var url = String(location).split('?');
219 url.splice(1,0,'?hasFlash=true&');
221 var msg = '<p>This content requires the Flash Player. <a href="http://www.adobe.com/go/getflashplayer">Download Flash Player</a>. Already have Flash Player? <a href="'+url+'">Click here.</a></p>';
222 this.innerHTML = '<span class="alt">'+this.innerHTML+'</span>';
224 .addClass('flash-update')
229 * @desc Convert a hash of html options to a string of attributes, using Function.apply().
230 * @example toAttributeString.apply(htmlOptions)
231 * @result foo="bar" foo="bar"
234 function toAttributeString() {
237 if(typeof this[key] != 'function')
238 s += key+'="'+this[key]+'" ';
243 * @desc Convert a hash of flashvars to a url-encoded string, using Function.apply().
244 * @example toFlashvarsString.apply(flashvarsObject)
245 * @result foo=bar&foo=bar
248 function toFlashvarsString() {
251 if(typeof this[key] != 'function')
252 s += key+'='+encodeURIComponent(this[key])+'&';
253 return s.replace(/&$/, '');
257 * @name flash.transform
258 * @desc Transform a set of html options into an embed tag.
261 * @example $$.transform(htmlOptions)
262 * @result <embed src="foo.swf" ... />
264 * Note: The embed tag is NOT standards-compliant, but it
265 * works in all current browsers. flash.transform can be
266 * overwritten with a custom function to generate more
267 * standards-compliant markup.
270 $$.transform = function(htmlOptions) {
271 htmlOptions.toString = toAttributeString;
272 if(htmlOptions.flashvars) htmlOptions.flashvars.toString = toFlashvarsString;
273 return '<embed ' + String(htmlOptions) + '/>';
278 * Flash Player 9 Fix (http://blog.deconcept.com/2006/07/28/swfobject-143-released/)
281 if (window.attachEvent) {
282 window.attachEvent("onbeforeunload", function(){
283 __flash_unloadHandler = function() {};
284 __flash_savedUnloadHandler = function() {};