]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/thickbox/thickbox.js
Wordpress 3.2
[autoinstalls/wordpress.git] / wp-includes / js / thickbox / thickbox.js
1 /*
2  * Thickbox 3.1 - One Box To Rule Them All.
3  * By Cody Lindley (http://www.codylindley.com)
4  * Copyright (c) 2007 cody lindley
5  * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
6 */
7
8 if ( typeof tb_pathToImage != 'string' ) {
9         var tb_pathToImage = thickboxL10n.loadingAnimation;
10 }
11 if ( typeof tb_closeImage != 'string' ) {
12         var tb_closeImage = thickboxL10n.closeImage;
13 }
14
15 /*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/
16
17 //on page load call tb_init
18 jQuery(document).ready(function(){
19         tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
20         imgLoader = new Image();// preload image
21         imgLoader.src = tb_pathToImage;
22 });
23
24 //add thickbox to href & area elements that have a class of .thickbox
25 function tb_init(domChunk){
26         jQuery(domChunk).live('click', tb_click);
27 }
28
29 function tb_click(){
30         var t = this.title || this.name || null;
31         var a = this.href || this.alt;
32         var g = this.rel || false;
33         tb_show(t,a,g);
34         this.blur();
35         return false;
36 }
37
38 function tb_show(caption, url, imageGroup) {//function called when the user clicks on a thickbox link
39
40         try {
41                 if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
42                         jQuery("body","html").css({height: "100%", width: "100%"});
43                         jQuery("html").css("overflow","hidden");
44                         if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
45                                 jQuery("body").append("<iframe id='TB_HideSelect'>"+thickboxL10n.noiframes+"</iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
46                                 jQuery("#TB_overlay").click(tb_remove);
47                         }
48                 }else{//all others
49                         if(document.getElementById("TB_overlay") === null){
50                                 jQuery("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
51                                 jQuery("#TB_overlay").click(tb_remove);
52                         }
53                 }
54
55                 if(tb_detectMacXFF()){
56                         jQuery("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
57                 }else{
58                         jQuery("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
59                 }
60
61                 if(caption===null){caption="";}
62                 jQuery("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
63                 jQuery('#TB_load').show();//show loader
64
65                 var baseURL;
66            if(url.indexOf("?")!==-1){ //ff there is a query string involved
67                         baseURL = url.substr(0, url.indexOf("?"));
68            }else{
69                         baseURL = url;
70            }
71
72            var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
73            var urlType = baseURL.toLowerCase().match(urlString);
74
75                 if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){//code to show images
76
77                         TB_PrevCaption = "";
78                         TB_PrevURL = "";
79                         TB_PrevHTML = "";
80                         TB_NextCaption = "";
81                         TB_NextURL = "";
82                         TB_NextHTML = "";
83                         TB_imageCount = "";
84                         TB_FoundURL = false;
85                         if(imageGroup){
86                                 TB_TempArray = jQuery("a[rel="+imageGroup+"]").get();
87                                 for (TB_Counter = 0; ((TB_Counter < TB_TempArray.length) && (TB_NextHTML === "")); TB_Counter++) {
88                                         var urlTypeTemp = TB_TempArray[TB_Counter].href.toLowerCase().match(urlString);
89                                                 if (!(TB_TempArray[TB_Counter].href == url)) {
90                                                         if (TB_FoundURL) {
91                                                                 TB_NextCaption = TB_TempArray[TB_Counter].title;
92                                                                 TB_NextURL = TB_TempArray[TB_Counter].href;
93                                                                 TB_NextHTML = "<span id='TB_next'>&nbsp;&nbsp;<a href='#'>"+thickboxL10n.next+"</a></span>";
94                                                         } else {
95                                                                 TB_PrevCaption = TB_TempArray[TB_Counter].title;
96                                                                 TB_PrevURL = TB_TempArray[TB_Counter].href;
97                                                                 TB_PrevHTML = "<span id='TB_prev'>&nbsp;&nbsp;<a href='#'>"+thickboxL10n.prev+"</a></span>";
98                                                         }
99                                                 } else {
100                                                         TB_FoundURL = true;
101                                                         TB_imageCount = thickboxL10n.image + ' ' + (TB_Counter + 1) + ' ' + thickboxL10n.of + ' ' + (TB_TempArray.length);
102                                                 }
103                                 }
104                         }
105
106                         imgPreloader = new Image();
107                         imgPreloader.onload = function(){
108                         imgPreloader.onload = null;
109
110                         // Resizing large images - orginal by Christian Montoya edited by me.
111                         var pagesize = tb_getPageSize();
112                         var x = pagesize[0] - 150;
113                         var y = pagesize[1] - 150;
114                         var imageWidth = imgPreloader.width;
115                         var imageHeight = imgPreloader.height;
116                         if (imageWidth > x) {
117                                 imageHeight = imageHeight * (x / imageWidth);
118                                 imageWidth = x;
119                                 if (imageHeight > y) {
120                                         imageWidth = imageWidth * (y / imageHeight);
121                                         imageHeight = y;
122                                 }
123                         } else if (imageHeight > y) {
124                                 imageWidth = imageWidth * (y / imageHeight);
125                                 imageHeight = y;
126                                 if (imageWidth > x) {
127                                         imageHeight = imageHeight * (x / imageWidth);
128                                         imageWidth = x;
129                                 }
130                         }
131                         // End Resizing
132
133                         TB_WIDTH = imageWidth + 30;
134                         TB_HEIGHT = imageHeight + 60;
135                         jQuery("#TB_window").append("<a href='' id='TB_ImageOff' title='"+thickboxL10n.close+"'><img id='TB_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+"'/></a>" + "<div id='TB_caption'>"+caption+"<div id='TB_secondLine'>" + TB_imageCount + TB_PrevHTML + TB_NextHTML + "</div></div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton' title='"+thickboxL10n.close+"'><img src='" + tb_closeImage + "' /></a></div>");
136
137                         jQuery("#TB_closeWindowButton").click(tb_remove);
138
139                         if (!(TB_PrevHTML === "")) {
140                                 function goPrev(){
141                                         if(jQuery(document).unbind("click",goPrev)){jQuery(document).unbind("click",goPrev);}
142                                         jQuery("#TB_window").remove();
143                                         jQuery("body").append("<div id='TB_window'></div>");
144                                         tb_show(TB_PrevCaption, TB_PrevURL, imageGroup);
145                                         return false;
146                                 }
147                                 jQuery("#TB_prev").click(goPrev);
148                         }
149
150                         if (!(TB_NextHTML === "")) {
151                                 function goNext(){
152                                         jQuery("#TB_window").remove();
153                                         jQuery("body").append("<div id='TB_window'></div>");
154                                         tb_show(TB_NextCaption, TB_NextURL, imageGroup);
155                                         return false;
156                                 }
157                                 jQuery("#TB_next").click(goNext);
158
159                         }
160
161                         jQuery(document).bind('keydown.thickbox', function(e){
162                                 e.stopImmediatePropagation();
163
164                                 if ( e.which == 27 ){ // close
165                                         if ( ! jQuery(document).triggerHandler( 'wp_CloseOnEscape', [{ event: e, what: 'thickbox', cb: tb_remove }] ) )
166                                                 tb_remove();
167
168                                 } else if ( e.which == 190 ){ // display previous image
169                                         if(!(TB_NextHTML == "")){
170                                                 jQuery(document).unbind('thickbox');
171                                                 goNext();
172                                         }
173                                 } else if ( e.which == 188 ){ // display next image
174                                         if(!(TB_PrevHTML == "")){
175                                                 jQuery(document).unbind('thickbox');
176                                                 goPrev();
177                                         }
178                                 }
179                                 return false;
180                         });
181
182                         tb_position();
183                         jQuery("#TB_load").remove();
184                         jQuery("#TB_ImageOff").click(tb_remove);
185                         jQuery("#TB_window").css({display:"block"}); //for safari using css instead of show
186                         };
187
188                         imgPreloader.src = url;
189                 }else{//code to show html
190
191                         var queryString = url.replace(/^[^\?]+\??/,'');
192                         var params = tb_parseQuery( queryString );
193
194                         TB_WIDTH = (params['width']*1) + 30 || 630; //defaults to 630 if no paramaters were added to URL
195                         TB_HEIGHT = (params['height']*1) + 40 || 440; //defaults to 440 if no paramaters were added to URL
196                         ajaxContentW = TB_WIDTH - 30;
197                         ajaxContentH = TB_HEIGHT - 45;
198
199                         if(url.indexOf('TB_iframe') != -1){// either iframe or ajax window
200                                         urlNoQuery = url.split('TB_');
201                                         jQuery("#TB_iframeContent").remove();
202                                         if(params['modal'] != "true"){//iframe no modal
203                                                 jQuery("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='"+thickboxL10n.close+"'><img src='" + tb_closeImage + "' /></a></div></div><iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' >"+thickboxL10n.noiframes+"</iframe>");
204                                         }else{//iframe modal
205                                         jQuery("#TB_overlay").unbind();
206                                                 jQuery("#TB_window").append("<iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;'>"+thickboxL10n.noiframes+"</iframe>");
207                                         }
208                         }else{// not an iframe, ajax
209                                         if(jQuery("#TB_window").css("display") != "block"){
210                                                 if(params['modal'] != "true"){//ajax no modal
211                                                 jQuery("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'><img src='" + tb_closeImage + "' /></a></div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
212                                                 }else{//ajax modal
213                                                 jQuery("#TB_overlay").unbind();
214                                                 jQuery("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
215                                                 }
216                                         }else{//this means the window is already up, we are just loading new content via ajax
217                                                 jQuery("#TB_ajaxContent")[0].style.width = ajaxContentW +"px";
218                                                 jQuery("#TB_ajaxContent")[0].style.height = ajaxContentH +"px";
219                                                 jQuery("#TB_ajaxContent")[0].scrollTop = 0;
220                                                 jQuery("#TB_ajaxWindowTitle").html(caption);
221                                         }
222                         }
223
224                         jQuery("#TB_closeWindowButton").click(tb_remove);
225
226                                 if(url.indexOf('TB_inline') != -1){
227                                         jQuery("#TB_ajaxContent").append(jQuery('#' + params['inlineId']).children());
228                                         jQuery("#TB_window").unload(function () {
229                                                 jQuery('#' + params['inlineId']).append( jQuery("#TB_ajaxContent").children() ); // move elements back when you're finished
230                                         });
231                                         tb_position();
232                                         jQuery("#TB_load").remove();
233                                         jQuery("#TB_window").css({display:"block"});
234                                 }else if(url.indexOf('TB_iframe') != -1){
235                                         tb_position();
236                                         if(jQuery.browser.safari){//safari needs help because it will not fire iframe onload
237                                                 jQuery("#TB_load").remove();
238                                                 jQuery("#TB_window").css({display:"block"});
239                                         }
240                                 }else{
241                                         jQuery("#TB_ajaxContent").load(url += "&random=" + (new Date().getTime()),function(){//to do a post change this load method
242                                                 tb_position();
243                                                 jQuery("#TB_load").remove();
244                                                 tb_init("#TB_ajaxContent a.thickbox");
245                                                 jQuery("#TB_window").css({display:"block"});
246                                         });
247                                 }
248
249                 }
250
251                 if(!params['modal']){
252                         jQuery(document).bind('keyup.thickbox', function(e){
253
254                                 if ( e.which == 27 ){ // close
255                                         e.stopImmediatePropagation();
256                                         if ( ! jQuery(document).triggerHandler( 'wp_CloseOnEscape', [{ event: e, what: 'thickbox', cb: tb_remove }] ) )
257                                                 tb_remove();
258
259                                         return false;
260                                 }
261                         });
262                 }
263
264         } catch(e) {
265                 //nothing here
266         }
267 }
268
269 //helper functions below
270 function tb_showIframe(){
271         jQuery("#TB_load").remove();
272         jQuery("#TB_window").css({display:"block"});
273 }
274
275 function tb_remove() {
276         jQuery("#TB_imageOff").unbind("click");
277         jQuery("#TB_closeWindowButton").unbind("click");
278         jQuery("#TB_window").fadeOut("fast",function(){jQuery('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
279         jQuery("#TB_load").remove();
280         if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
281                 jQuery("body","html").css({height: "auto", width: "auto"});
282                 jQuery("html").css("overflow","");
283         }
284         jQuery(document).unbind('.thickbox');
285         return false;
286 }
287
288 function tb_position() {
289 var isIE6 = typeof document.body.style.maxHeight === "undefined";
290 jQuery("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
291         if ( ! isIE6 ) { // take away IE6
292                 jQuery("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
293         }
294 }
295
296 function tb_parseQuery ( query ) {
297    var Params = {};
298    if ( ! query ) {return Params;}// return empty object
299    var Pairs = query.split(/[;&]/);
300    for ( var i = 0; i < Pairs.length; i++ ) {
301       var KeyVal = Pairs[i].split('=');
302       if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
303       var key = unescape( KeyVal[0] );
304       var val = unescape( KeyVal[1] );
305       val = val.replace(/\+/g, ' ');
306       Params[key] = val;
307    }
308    return Params;
309 }
310
311 function tb_getPageSize(){
312         var de = document.documentElement;
313         var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
314         var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
315         arrayPageSize = [w,h];
316         return arrayPageSize;
317 }
318
319 function tb_detectMacXFF() {
320   var userAgent = navigator.userAgent.toLowerCase();
321   if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
322     return true;
323   }
324 }