foist
[fingerprinter.git] / javascript / jquery.flash.js
1 /**
2  * Flash (http://jquery.lukelutman.com/plugins/flash)
3  * A jQuery plugin for embedding Flash movies.
4  * 
5  * Version 1.0
6  * November 9th, 2006
7  *
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
12  * 
13  * Inspired by:
14  * SWFObject (http://blog.deconcept.com/swfobject/)
15  * UFO (http://www.bobbyvandersluis.com/ufo/)
16  * sIFR (http://www.mikeindustries.com/sifr/)
17  * 
18  * IMPORTANT: 
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).
22  *
23  **/ 
24 ;(function(){
25         
26 var $$;
27
28 /**
29  * 
30  * @desc Replace matching elements with a flash movie.
31  * @author Luke Lutman
32  * @version 1.0.1
33  *
34  * @name flash
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).
39  * @type jQuery
40  *
41  * @cat plugins/flash
42  * 
43  * @example $('#hello').flash({ src: 'hello.swf' });
44  * @desc Embed a Flash movie.
45  *
46  * @example $('#hello').flash({ src: 'hello.swf' }, { version: 8 });
47  * @desc Embed a Flash 8 movie.
48  *
49  * @example $('#hello').flash({ src: 'hello.swf' }, { expressInstall: true });
50  * @desc Embed a Flash movie using Express Install if flash isn't installed.
51  *
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.
54  *
55 **/
56 $$ = jQuery.fn.flash = function(htmlOptions, pluginOptions, replace, update) {
57         
58         // Set the default block.
59         var block = replace || $$.replace;
60         
61         // Merge the default and passed plugin options.
62         pluginOptions = $$.copy($$.pluginOptions, pluginOptions);
63         
64         // Detect Flash.
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 = {
70                                 flashvars: {    
71                                         MMredirectURL: location,
72                                         MMplayerType: 'PlugIn',
73                                         MMdoctitle: jQuery('title').text() 
74                                 }                                       
75                         };
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;
80                 // Fail
81                 } else {
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.
86                         return this;
87                 }
88         }
89         
90         // Merge the default, express install and passed html options.
91         htmlOptions = $$.copy($$.htmlOptions, expressInstallOptions, htmlOptions);
92         
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));
96         });
97         
98 };
99 /**
100  *
101  * @name flash.copy
102  * @desc Copy an arbitrary number of objects into a new object.
103  * @type Object
104  * 
105  * @example $$.copy({ foo: 1 }, { bar: 2 });
106  * @result { foo: 1, bar: 2 };
107  *
108 **/
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);
119         }
120         options.flashvars = flashvars;
121         return options;
122 };
123 /*
124  * @name flash.hasFlash
125  * @desc Check if a specific version of the Flash plugin is installed
126  * @type Boolean
127  *
128 **/
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;
142         }
143         // major version, minor version and revision match exactly
144         return true;
145 };
146 /**
147  *
148  * @name flash.hasFlash.playerVersion
149  * @desc Get the version of the installed Flash plugin.
150  * @type String
151  *
152 **/
153 $$.hasFlash.playerVersion = function() {
154         // ie
155         try {
156                 try {
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'; }                            
162                 } catch(e) {}
163                 return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
164         // other browsers
165         } catch(e) {
166                 try {
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];
169                         }
170                 } catch(e) {}           
171         }
172         return '0,0,0';
173 };
174 /**
175  *
176  * @name flash.htmlOptions
177  * @desc The default set of options for the object or embed tag.
178  *
179 **/
180 $$.htmlOptions = {
181         height: 240,
182         flashvars: {},
183         pluginspage: 'http://www.adobe.com/go/getflashplayer',
184         src: '#',
185         type: 'application/x-shockwave-flash',
186         width: 320              
187 };
188 /**
189  *
190  * @name flash.pluginOptions
191  * @desc The default set of options for checking/updating the flash Plugin.
192  *
193 **/
194 $$.pluginOptions = {
195         expressInstall: false,
196         update: true,
197         version: '6.0.65'
198 };
199 /**
200  *
201  * @name flash.replace
202  * @desc The default method for replacing an element with a Flash movie.
203  *
204 **/
205 $$.replace = function(htmlOptions) {
206         this.innerHTML = '<div class="alt">'+this.innerHTML+'</div>';
207         jQuery(this)
208                 .addClass('flash-replaced')
209                 .prepend($$.transform(htmlOptions));
210 };
211 /**
212  *
213  * @name flash.update
214  * @desc The default method for replacing an element with an update message.
215  *
216 **/
217 $$.update = function(htmlOptions) {
218         var url = String(location).split('?');
219         url.splice(1,0,'?hasFlash=true&');
220         url = url.join('');
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>';
223         jQuery(this)
224                 .addClass('flash-update')
225                 .prepend(msg);
226 };
227 /**
228  *
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"
232  *
233 **/
234 function toAttributeString() {
235         var s = '';
236         for(var key in this)
237                 if(typeof this[key] != 'function')
238                         s += key+'="'+this[key]+'" ';
239         return s;               
240 };
241 /**
242  *
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
246  *
247 **/
248 function toFlashvarsString() {
249         var s = '';
250         for(var key in this)
251                 if(typeof this[key] != 'function')
252                         s += key+'='+encodeURIComponent(this[key])+'&';
253         return s.replace(/&$/, '');             
254 };
255 /**
256  *
257  * @name flash.transform
258  * @desc Transform a set of html options into an embed tag.
259  * @type String 
260  *
261  * @example $$.transform(htmlOptions)
262  * @result <embed src="foo.swf" ... />
263  *
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.
268  *
269 **/
270 $$.transform = function(htmlOptions) {
271         htmlOptions.toString = toAttributeString;
272         if(htmlOptions.flashvars) htmlOptions.flashvars.toString = toFlashvarsString;
273         return '<embed ' + String(htmlOptions) + '/>';          
274 };
275
276 /**
277  *
278  * Flash Player 9 Fix (http://blog.deconcept.com/2006/07/28/swfobject-143-released/)
279  *
280 **/
281 if (window.attachEvent) {
282         window.attachEvent("onbeforeunload", function(){
283                 __flash_unloadHandler = function() {};
284                 __flash_savedUnloadHandler = function() {};
285         });
286 }
287         
288 })();