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