Updated the 19 build version to 20100611
[moodle.git] / lib / ufo.js
blob5f2608ae9eaf1db2a60d1a0e30119045bc2e296c
1 /*      Unobtrusive Flash Objects (UFO) v3.22 <http://www.bobbyvandersluis.com/ufo/>
2         Copyright 2005-2007 Bobby van der Sluis
3         This software is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
5     CONTAINS MINOR CHANGE FOR MOODLE (bottom code for MDL-9825)
6 */
8 var UFO = {
9         req: ["movie", "width", "height", "majorversion", "build"],
10         opt: ["play", "loop", "menu", "quality", "scale", "salign", "wmode", "bgcolor", "base", "flashvars", "devicefont", "allowscriptaccess", "seamlesstabbing", "allowfullscreen", "allownetworking"],
11         optAtt: ["id", "name", "align"],
12         optExc: ["swliveconnect"],
13         ximovie: "ufo.swf",
14         xiwidth: "215",
15         xiheight: "138",
16         ua: navigator.userAgent.toLowerCase(),
17         pluginType: "",
18         fv: [0,0],
19         foList: [],
20                 
21         create: function(FO, id) {
22                 if (!UFO.uaHas("w3cdom") || UFO.uaHas("ieMac")) return;
23                 UFO.getFlashVersion();
24                 UFO.foList[id] = UFO.updateFO(FO);
25                 UFO.createCSS("#" + id, "visibility:hidden;");
26                 UFO.domLoad(id);
27         },
29         updateFO: function(FO) {
30                 if (typeof FO.xi != "undefined" && FO.xi == "true") {
31                         if (typeof FO.ximovie == "undefined") FO.ximovie = UFO.ximovie;
32                         if (typeof FO.xiwidth == "undefined") FO.xiwidth = UFO.xiwidth;
33                         if (typeof FO.xiheight == "undefined") FO.xiheight = UFO.xiheight;
34                 }
35                 FO.mainCalled = false;
36                 return FO;
37         },
39         domLoad: function(id) {
40                 var _t = setInterval(function() {
41                         if ((document.getElementsByTagName("body")[0] != null || document.body != null) && document.getElementById(id) != null) {
42                                 UFO.main(id);
43                                 clearInterval(_t);
44                         }
45                 }, 250);
46                 if (typeof document.addEventListener != "undefined") {
47                         document.addEventListener("DOMContentLoaded", function() { UFO.main(id); clearInterval(_t); } , null); // Gecko, Opera 9+
48                 }
49         },
51         main: function(id) {
52                 var _fo = UFO.foList[id];
53                 if (_fo.mainCalled) return;
54                 UFO.foList[id].mainCalled = true;
55                 document.getElementById(id).style.visibility = "hidden";
56                 if (UFO.hasRequired(id)) {
57                         if (UFO.hasFlashVersion(parseInt(_fo.majorversion, 10), parseInt(_fo.build, 10))) {
58                                 if (typeof _fo.setcontainercss != "undefined" && _fo.setcontainercss == "true") UFO.setContainerCSS(id);
59                                 UFO.writeSWF(id);
60                         }
61                         else if (_fo.xi == "true" && UFO.hasFlashVersion(6, 65)) {
62                                 UFO.createDialog(id);
63                         }
64                 }
65                 document.getElementById(id).style.visibility = "visible";
66         },
67         
68         createCSS: function(selector, declaration) {
69                 var _h = document.getElementsByTagName("head")[0]; 
70                 var _s = UFO.createElement("style");
71                 if (!UFO.uaHas("ieWin")) _s.appendChild(document.createTextNode(selector + " {" + declaration + "}")); // bugs in IE/Win
72                 _s.setAttribute("type", "text/css");
73                 _s.setAttribute("media", "screen"); 
74                 _h.appendChild(_s);
75                 if (UFO.uaHas("ieWin") && document.styleSheets && document.styleSheets.length > 0) {
76                         var _ls = document.styleSheets[document.styleSheets.length - 1];
77                         if (typeof _ls.addRule == "object") _ls.addRule(selector, declaration);
78                 }
79         },
80         
81         setContainerCSS: function(id) {
82                 var _fo = UFO.foList[id];
83                 var _w = /%/.test(_fo.width) ? "" : "px";
84                 var _h = /%/.test(_fo.height) ? "" : "px";
85                 UFO.createCSS("#" + id, "width:" + _fo.width + _w +"; height:" + _fo.height + _h +";");
86                 if (_fo.width == "100%") {
87                         UFO.createCSS("body", "margin-left:0; margin-right:0; padding-left:0; padding-right:0;");
88                 }
89                 if (_fo.height == "100%") {
90                         UFO.createCSS("html", "height:100%; overflow:hidden;");
91                         UFO.createCSS("body", "margin-top:0; margin-bottom:0; padding-top:0; padding-bottom:0; height:100%;");
92                 }
93         },
95         createElement: function(el) {
96                 return (UFO.uaHas("xml") && typeof document.createElementNS != "undefined") ?  document.createElementNS("http://www.w3.org/1999/xhtml", el) : document.createElement(el);
97         },
99         createObjParam: function(el, aName, aValue) {
100                 var _p = UFO.createElement("param");
101                 _p.setAttribute("name", aName); 
102                 _p.setAttribute("value", aValue);
103                 el.appendChild(_p);
104         },
106         uaHas: function(ft) {
107                 var _u = UFO.ua;
108                 switch(ft) {
109                         case "w3cdom":
110                                 return (typeof document.getElementById != "undefined" && typeof document.getElementsByTagName != "undefined" && (typeof document.createElement != "undefined" || typeof document.createElementNS != "undefined"));
111                         case "xml":
112                                 var _m = document.getElementsByTagName("meta");
113                                 var _l = _m.length;
114                                 for (var i = 0; i < _l; i++) {
115                                         if (/content-type/i.test(_m[i].getAttribute("http-equiv")) && /xml/i.test(_m[i].getAttribute("content"))) return true;
116                                 }
117                                 return false;
118                         case "ieMac":
119                                 return /msie/.test(_u) && !/opera/.test(_u) && /mac/.test(_u);
120                         case "ieWin":
121                                 return /msie/.test(_u) && !/opera/.test(_u) && /win/.test(_u);
122                         case "gecko":
123                                 return /gecko/.test(_u) && !/applewebkit/.test(_u);
124                         case "opera":
125                                 return /opera/.test(_u);
126                         case "safari":
127                                 return /applewebkit/.test(_u);
128                         default:
129                                 return false;
130                 }
131         },
132         
133         getFlashVersion: function() {
134                 if (UFO.fv[0] != 0) return;  
135                 if (navigator.plugins && typeof navigator.plugins["Shockwave Flash"] == "object") {
136                         UFO.pluginType = "npapi";
137                         var _d = navigator.plugins["Shockwave Flash"].description;
138                         if (typeof _d != "undefined") {
139                                 _d = _d.replace(/^.*\s+(\S+\s+\S+$)/, "$1");
140                                 var _m = parseInt(_d.replace(/^(.*)\..*$/, "$1"), 10);
141                                 var _r = /r/.test(_d) ? parseInt(_d.replace(/^.*r(.*)$/, "$1"), 10) : 0;
142                                 UFO.fv = [_m, _r];
143                         }
144                 }
145                 else if (window.ActiveXObject) {
146                         UFO.pluginType = "ax";
147                         try { // avoid fp 6 crashes
148                                 var _a = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
149                         }
150                         catch(e) {
151                                 try { 
152                                         var _a = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
153                                         UFO.fv = [6, 0];
154                                         _a.AllowScriptAccess = "always"; // throws if fp < 6.47 
155                                 }
156                                 catch(e) {
157                                         if (UFO.fv[0] == 6) return;
158                                 }
159                                 try {
160                                         var _a = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
161                                 }
162                                 catch(e) {}
163                         }
164                         if (typeof _a == "object") {
165                                 var _d = _a.GetVariable("$version"); // bugs in fp 6.21/6.23
166                                 if (typeof _d != "undefined") {
167                                         _d = _d.replace(/^\S+\s+(.*)$/, "$1").split(",");
168                                         UFO.fv = [parseInt(_d[0], 10), parseInt(_d[2], 10)];
169                                 }
170                         }
171                 }
172         },
174         hasRequired: function(id) {
175                 var _l = UFO.req.length;
176                 for (var i = 0; i < _l; i++) {
177                         if (typeof UFO.foList[id][UFO.req[i]] == "undefined") return false;
178                 }
179                 return true;
180         },
181         
182         hasFlashVersion: function(major, release) {
183                 return (UFO.fv[0] > major || (UFO.fv[0] == major && UFO.fv[1] >= release)) ? true : false;
184         },
186         writeSWF: function(id) {
187                 var _fo = UFO.foList[id];
188                 var _e = document.getElementById(id);
189                 if (UFO.pluginType == "npapi") {
190                         if (UFO.uaHas("gecko") || UFO.uaHas("xml")) {
191                                 while(_e.hasChildNodes()) {
192                                         _e.removeChild(_e.firstChild);
193                                 }
194                                 var _obj = UFO.createElement("object");
195                                 _obj.setAttribute("type", "application/x-shockwave-flash");
196                                 _obj.setAttribute("data", _fo.movie);
197                                 _obj.setAttribute("width", _fo.width);
198                                 _obj.setAttribute("height", _fo.height);
199                                 var _l = UFO.optAtt.length;
200                                 for (var i = 0; i < _l; i++) {
201                                         if (typeof _fo[UFO.optAtt[i]] != "undefined") _obj.setAttribute(UFO.optAtt[i], _fo[UFO.optAtt[i]]);
202                                 }
203                                 var _o = UFO.opt.concat(UFO.optExc);
204                                 var _l = _o.length;
205                                 for (var i = 0; i < _l; i++) {
206                                         if (typeof _fo[_o[i]] != "undefined") UFO.createObjParam(_obj, _o[i], _fo[_o[i]]);
207                                 }
208                                 _e.appendChild(_obj);
209                         }
210                         else {
211                                 var _emb = "";
212                                 var _o = UFO.opt.concat(UFO.optAtt).concat(UFO.optExc);
213                                 var _l = _o.length;
214                                 for (var i = 0; i < _l; i++) {
215                                         if (typeof _fo[_o[i]] != "undefined") _emb += ' ' + _o[i] + '="' + _fo[_o[i]] + '"';
216                                 }
217                                 _e.innerHTML = '<embed type="application/x-shockwave-flash" src="' + _fo.movie + '" width="' + _fo.width + '" height="' + _fo.height + '" pluginspage="http://www.macromedia.com/go/getflashplayer"' + _emb + '></embed>';
218                         }
219                 }
220                 else if (UFO.pluginType == "ax") {
221                         var _objAtt = "";
222                         var _l = UFO.optAtt.length;
223                         for (var i = 0; i < _l; i++) {
224                                 if (typeof _fo[UFO.optAtt[i]] != "undefined") _objAtt += ' ' + UFO.optAtt[i] + '="' + _fo[UFO.optAtt[i]] + '"';
225                         }
226                         var _objPar = "";
227                         var _l = UFO.opt.length;
228                         for (var i = 0; i < _l; i++) {
229                                 if (typeof _fo[UFO.opt[i]] != "undefined") _objPar += '<param name="' + UFO.opt[i] + '" value="' + _fo[UFO.opt[i]] + '" />';
230                         }
231                         var _p = window.location.protocol == "https:" ? "https:" : "http:";
232                         _e.innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' + _objAtt + ' width="' + _fo.width + '" height="' + _fo.height + '" codebase="' + _p + '//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + _fo.majorversion + ',0,' + _fo.build + ',0"><param name="movie" value="' + _fo.movie + '" />' + _objPar + '</object>';
233                 }
234         },
235                 
236         createDialog: function(id) {
237                 var _fo = UFO.foList[id];
238                 UFO.createCSS("html", "height:100%; overflow:hidden;");
239                 UFO.createCSS("body", "height:100%; overflow:hidden;");
240                 UFO.createCSS("#xi-con", "position:absolute; left:0; top:0; z-index:1000; width:100%; height:100%; background-color:#fff; filter:alpha(opacity:75); opacity:0.75;");
241                 UFO.createCSS("#xi-dia", "position:absolute; left:50%; top:50%; margin-left: -" + Math.round(parseInt(_fo.xiwidth, 10) / 2) + "px; margin-top: -" + Math.round(parseInt(_fo.xiheight, 10) / 2) + "px; width:" + _fo.xiwidth + "px; height:" + _fo.xiheight + "px;");
242                 var _b = document.getElementsByTagName("body")[0];
243                 var _c = UFO.createElement("div");
244                 _c.setAttribute("id", "xi-con");
245                 var _d = UFO.createElement("div");
246                 _d.setAttribute("id", "xi-dia");
247                 _c.appendChild(_d);
248                 _b.appendChild(_c);
249                 var _mmu = window.location;
250                 if (UFO.uaHas("xml") && UFO.uaHas("safari")) {
251                         var _mmd = document.getElementsByTagName("title")[0].firstChild.nodeValue = document.getElementsByTagName("title")[0].firstChild.nodeValue.slice(0, 47) + " - Flash Player Installation";
252                 }
253                 else {
254                         var _mmd = document.title = document.title.slice(0, 47) + " - Flash Player Installation";
255                 }
256                 var _mmp = UFO.pluginType == "ax" ? "ActiveX" : "PlugIn";
257                 var _uc = typeof _fo.xiurlcancel != "undefined" ? "&xiUrlCancel=" + _fo.xiurlcancel : "";
258                 var _uf = typeof _fo.xiurlfailed != "undefined" ? "&xiUrlFailed=" + _fo.xiurlfailed : "";
259                 UFO.foList["xi-dia"] = { movie:_fo.ximovie, width:_fo.xiwidth, height:_fo.xiheight, majorversion:"6", build:"65", flashvars:"MMredirectURL=" + _mmu + "&MMplayerType=" + _mmp + "&MMdoctitle=" + _mmd + _uc + _uf };
260                 UFO.writeSWF("xi-dia");
261         },
263         expressInstallCallback: function() {
264                 var _b = document.getElementsByTagName("body")[0];
265                 var _c = document.getElementById("xi-con");
266                 _b.removeChild(_c);
267                 UFO.createCSS("body", "height:auto; overflow:auto;");
268                 UFO.createCSS("html", "height:auto; overflow:auto;");
269         },
271         cleanupIELeaks: function() {
272                 var _o = document.getElementsByTagName("object");
273                 var _l = _o.length
274                 for (var i = 0; i < _l; i++) {
275                         _o[i].style.display = "none";
276             var j = 0;
277                         for (var x in _o[i]) {
278                 j++;
279                                 if (typeof _o[i][x] == "function") {
280                                         _o[i][x] = null;
281                                 }
282                 if (j > 1000) {
283                     // something is wrong, probably infinite loop caused by embedded html file
284                     // see MDL-9825
285                     break;
286                                 }
287                         }
288                 }
289         }
293 if (typeof window.attachEvent != "undefined" && UFO.uaHas("ieWin")) {
294         window.attachEvent("onunload", UFO.cleanupIELeaks);