[SPIP] +2.1.12
[velocampus/web/www.git] / www / plugins / auto / thickbox2 / javascript / thickbox.js
1 /*
2 * Thickbox 2.1 - One Box To Rule Them All.
3 * By Cody Lindley (http://www.codylindley.com)
4 * Copyright (c) 2006 cody lindley
5 * Licensed under the MIT License:
6 *http://www.opensource.org/licenses/mit-license.php
7 * Thickbox is built on top of the very light weight jQuery library.
8 * Modified for SPIP <www.spip.net> by Fil <fil@rezo.net>:
9 * Modified by BoOz@rezo.net
10 * - added recognition of images based on a.type
11 * - added an image gallery
12 * - added keyboard navigation ('n'ext (->), 'p'revious (<-), 'q'uit (esc))
13 * - customize path to the css and wheel image
14 * - default size for thickbox links that have no width x height indicated ??
15 * - don't load css when not needed ??
16 * - TODO: don't load js when not needed!!
17
18 */
19
20
21 //on page load call TB_init
22 //
23 // init
24 //
25 var DELAI = 7000 ; //nombre de secondes entre deux images
26 var imageArray = [];
27 var FULL_S = false;
28 var DIAPO = false;
29
30 if(typeof TB_chemin_css == 'undefined') { TB_chemin_css = 'thickbox.css'; }
31 if(typeof TB_chemin_animation == 'undefined') { TB_chemin_animation = 'circle_animation.gif'; }
32 //add thickbox to href elements that have a class of .thickbox
33
34 function TB_image() {
35 var t = this.title || this.name ;
36 var longdesc = jQuery('img[longdesc]', this).attr('longdesc');
37
38 if (longdesc)
39 TB_show(t,longdesc);
40 else
41 TB_show(t,this.href,'image');
42
43 return false;
44 }
45
46 //add thickbox to href elements that have a class of .thickbox
47 function TB_init(root) {
48 jQuery("a.thickbox",root).each(
49 function(i) {
50 this.onclick = TB_image;
51 var t = this.alt || this.name || null;
52 this.alt = t + " - view bigger version";
53 if (
54 (this.type && this.type.match(/^image[\/](jpeg|gif|png)$/i))
55 || (this.href && this.href.match(/\.(jpeg|jpg|png|gif)$/i))
56 ) {
57 // we store image links in an array (for a gallery)
58 imageArray.push ([
59 this.href,
60 this.title || this.name
61 ]);
62
63 }
64 }
65 );
66 //console.log(imageArray);
67 }
68
69 function TB_show(caption, url) {//function called when the user clicks on a thickbox link
70 try {
71 if (document.getElementById("TB_HideSelect") == null) {
72 jQuery("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay' title='Fermer'></div><div id='TB_window'></div>");
73 jQuery("#TB_overlay").click(TB_remove);
74 }
75 jQuery(".TB_hide").hide();
76
77 if(caption==null){caption=""};
78
79 if(!FULL_S){
80 jQuery(window).scroll(TB_position);
81 }
82
83 TB_overlaySize();
84
85 jQuery("body").append("<div id='TB_load'><img src='"+TB_chemin_animation+"' alt='loading' /></div>");
86 TB_load_position();
87
88 if(url.indexOf("?")!==-1){ //If there is a query string involved
89 var baseURL = url.substr(0, url.indexOf("?"));
90 }else{
91 var baseURL = url;
92 }
93 var urlString = /\.jpg|\.jpeg|\.png|\.gif|\.bmp/g;
94 var urlType = baseURL.toLowerCase().match(urlString);
95
96 if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){//code to show images
97
98 TB_PrevCaption = "";
99 TB_PrevURL = "";
100 TB_PrevHTML = "";
101 TB_NextCaption = "";
102 TB_NextURL = "";
103 TB_NextHTML = "";
104 TB_imageCount = "";
105 TB_Full_Size = "";
106 TB_FoundURL = false;
107
108 if(imageArray.length > 0){
109 TB_TempArray = imageArray ;
110 for (TB_Counter = 0; ((TB_Counter < TB_TempArray.length) && (TB_NextHTML == "")); TB_Counter++) {
111 var urlTypeTemp = TB_TempArray[TB_Counter][0].toLowerCase().match(urlString);
112 if (!(TB_TempArray[TB_Counter][0] == url)) {
113 if (TB_FoundURL) {
114 TB_NextCaption = TB_TempArray[TB_Counter][1];
115 TB_NextURL = TB_TempArray[TB_Counter][0];
116 TB_NextHTML = "<span id='TB_next'>&nbsp;&nbsp;<a href='#' title='Image suivante'><strong> &gt;</strong></a></span>";
117 } else {
118 TB_PrevCaption = TB_TempArray[TB_Counter][1];
119 TB_PrevURL = TB_TempArray[TB_Counter][0];
120 TB_PrevHTML = "<span id='TB_prev'>&nbsp;&nbsp;<a href='#' title='Image precedente'><strong>&lt; </strong></a></span>";
121 }
122 } else {
123 TB_FoundURL = true;
124 TB_imageCount =(TB_Counter + 1) +" / "+ (TB_TempArray.length);
125 }
126 }
127 }
128 if (!(TB_NextHTML == "")) { //preload de la prochaine image
129 imageSuivante = new Image();
130 imageSuivante.src = TB_NextURL ;
131 }
132
133 imgPreloader = new Image();
134 imgPreloader.onload = function(){
135 imgPreloader.onload = null;
136 //console.log("loaded" + url);
137 // Resizing large images - orginal by Christian Montoya edited by me.
138 TB_Big_Image = false ;
139 var pagesize = TB_getPageSize();
140 var x = pagesize[0] - 150;
141 var y = pagesize[1] - 150;
142 var imageWidth = imgPreloader.width;
143 var imageHeight = imgPreloader.height;
144 IMAGE_WIDTH = imageWidth ;
145 IMAGE_HEIGHT = imageHeight ;
146
147 if (imageWidth > x) {
148 TB_Big_Image = true ;
149 imageHeight = imageHeight * (x / imageWidth);
150 imageWidth = x;
151
152 if (imageHeight > y) {
153 TB_Big_Image = true ;
154 imageWidth = imageWidth * (y / imageHeight);
155 imageHeight = y;
156 }
157 } else if (imageHeight > y) {
158 TB_Big_Image = true ;
159 imageWidth = imageWidth * (y / imageHeight);
160 imageHeight = y;
161
162 if (imageWidth > x) {
163 TB_Big_Image = true ;
164 imageHeight = imageHeight * (x / imageWidth);
165 imageWidth = x;
166 }
167 }
168 // End Resizing
169 if(!DIAPO){
170 TB_Diapo = "<span id='TB_Diapo'>&nbsp;&nbsp;<a href='#'><strong>[Diaporama]</strong></a></span>";
171 }else{
172 TB_Diapo = "<span id='TB_Diapo'>&nbsp;&nbsp;<a href='#' title='Stop'><strong>[Stop]</strong></a></span>";
173 }
174 if (TB_Big_Image)
175 TB_Full_Size = "<span id='TB_Full'>&nbsp;&nbsp;<a href='#'><strong>[Zoom]</strong></a></span>";
176 TB_WIDTH = imageWidth + 20;
177 TB_HEIGHT = imageHeight + 20;
178
179 jQuery("#TB_window").append("<a href='#' id='TB_ImageOff'><img id='TB_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+" - next picture'/></a>" + "<div id='TB_legend' style='background-color:#fff'><div id='TB_caption'>"+caption+"</div><div id='TB_secondLine'>" + TB_imageCount + TB_Full_Size + TB_PrevHTML + TB_NextHTML + TB_Diapo +"</div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton'><img src='"+TB_chemin_close+"' alt='Fermer' /></a></div></div>");
180
181 jQuery("#TB_closeWindowButton").click(TB_remove);
182 jQuery("#TB_load").remove();
183 jQuery("#TB_window").fadeIn("slow");
184 //setTimeout('jQuery("#TB_legend").slideDown(800);',1600);
185
186 if (!(TB_NextHTML == "")) {
187 function goNext(){
188 FULL_S = false ;
189 jQuery("#TB_window").remove();
190 jQuery("body").append("<div id='TB_window'></div>");
191 TB_show(TB_NextCaption, TB_NextURL);
192 return false;
193 }
194 jQuery("#TB_next").click(goNext);
195 }
196
197 if (!(TB_PrevHTML == "")) {
198 function goPrev(){
199 FULL_S = false ;
200 if(jQuery(document).unbind('click',goPrev)){jQuery(document).unbind('click',goPrev)};
201 jQuery("#TB_window").remove();
202 jQuery("body").append("<div id='TB_window'></div>");
203 TB_show(TB_PrevCaption, TB_PrevURL);
204 return false;
205 }
206 jQuery("#TB_prev").click(goPrev);
207 }
208
209 if (!(TB_Full_Size == "")) {
210 function fullSize(){
211 var arrayPageScroll = TB_getPageScrollTop();
212 var pagesize = TB_getPageSize();
213
214 if(!FULL_S){
215 FULL_S = true ;
216 TB_TOP = arrayPageScroll[1];
217
218 if( (arrayPageScroll[0] + (pagesize[0] - IMAGE_WIDTH)/2) > 0 ){
219 TB_LEFT = arrayPageScroll[0] + (pagesize[0] - IMAGE_WIDTH)/2 ;
220 }else{
221 TB_LEFT = 50 ;
222 }
223
224 jQuery("#TB_window").animate({top:TB_TOP,left:TB_LEFT,width:(IMAGE_WIDTH+20),height:(IMAGE_HEIGHT+20)},1500);
225 jQuery("#TB_Image").animate({top:20,left:20,width:IMAGE_WIDTH,height:IMAGE_HEIGHT},1500, TB_recadre);
226 }
227 else{
228 FULL_S = false ;
229 jQuery("#TB_window").animate({top: (arrayPageScroll[1] + (pagesize[1]-TB_HEIGHT)/2),left:(arrayPageScroll[0] + (pagesize[0] - TB_WIDTH)/2), width:TB_WIDTH,height:TB_HEIGHT},1500);
230 jQuery("#TB_Image").animate({top:20,left:20,width:(TB_WIDTH - 20),height:(TB_HEIGHT - 20)},1500,TB_recadre);
231 }
232 return false;
233 }
234 jQuery("#TB_Full").click(fullSize);
235 }
236
237 if(!(TB_NextHTML == "")){
238 jQuery("#TB_ImageOff").click(goNext);
239 }else{
240 jQuery("#TB_ImageOff").click(TB_remove);
241 }
242
243 jQuery("#TB_Diapo").click(diaporama);
244
245 document.onkeydown = function(e){
246 if (e == null) { // ie
247 keycode = event.keyCode;
248 } else { // mozilla
249 keycode = e.which;
250 }
251 if(keycode == 27 | keycode == 67 | keycode == 70){ // close
252 TB_remove();
253 } else if(keycode == 190 | keycode == 39){ // display previous image <-
254 if(!(TB_NextHTML == "")){
255 document.onkeydown = "";
256 goNext();
257 }
258 } else if(keycode == 188| keycode == 37){ // display next image ->
259 if(!(TB_PrevHTML == "")){
260 document.onkeydown = "";
261 goPrev();
262 }
263 }
264 }
265
266 TB_position() ;
267
268 jQuery("#TB_load").remove();
269 jQuery("#TB_window").css({display:"block"}); //for safari using css instead of show
270
271
272 //diapo
273 //console.log("deb " + DELAI);
274 if(DIAPO)
275 setTimeout('diapo();',DELAI);
276 jQuery("#TB_ImageOff")[0].focus();
277 }
278
279 imgPreloader.src = url;
280 //console.log("hop" + url);
281
282 }else{//code to show html pages
283 //console.log(url);
284 var queryString = url.replace(/^[^\?]+\?+/,'');
285 //rhooooo, c'est pas du boulot ce thickbox
286 queryString = queryString.replace(/^[^\?]+\?+/,'');
287 //console.log(queryString);
288 var params = TB_parseQuery( queryString );
289 //console.log(params);
290
291 TB_WIDTH = (params['width']*1) + 30;
292 TB_HEIGHT = (params['height']*1) + 40;
293
294 ajaxContentW = TB_WIDTH - 30;
295 ajaxContentH = TB_HEIGHT - 45;
296
297 if(url.indexOf('TB_iframe') != -1){
298 urlNoQuery = url.split('TB_');
299 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'>close</a></div></div><iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' onload='TB_showIframe()'> </iframe>");
300 }else{
301 jQuery("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'>close</a></div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
302 }
303
304 jQuery("#TB_closeWindowButton").click(TB_remove);
305
306 if(url.indexOf('TB_inline') != -1){
307 jQuery("#TB_ajaxContent").html(jQuery('#' + params['inlineId']).html());
308 TB_position();
309 jQuery("#TB_load").remove();
310 jQuery("#TB_window").css({display:"block"});
311 }else if(url.indexOf('TB_iframe') != -1){
312 TB_position();
313 if(frames['TB_iframeContent'] == undefined){//be nice to safari
314 jQuery("#TB_load").remove();
315 jQuery("#TB_window").css({display:"block"});
316 jQuery(document).keyup( function(e){ var key = e.keyCode; if(key == 27){TB_remove()} });
317 }
318 }else{
319 jQuery("#TB_ajaxContent").load(url, function(){
320 TB_position();
321 jQuery("#TB_load").remove();
322 jQuery("#TB_window").css({display:"block"});
323 });
324 }
325
326 }
327
328 jQuery(window).resize(TB_position);
329
330 document.onkeyup = function(e){
331 if (e == null) { // ie
332 keycode = event.keyCode;
333 } else { // mozilla
334 keycode = e.which;
335 }
336
337 if(keycode == 27){ // close
338 TB_remove();
339 }
340 }
341
342 } catch(e) {
343 alert( e );
344 }
345 }
346
347 //helper functions below
348 function diaporama(){
349 //alert("diapo");
350 if(!DIAPO){
351 DIAPO = true ;
352 //console.log("deb");
353 diapo();
354 //jQuery("TB_secondLine").html(TB_imageCount + TB_Full_Size + TB_PrevHTML + TB_NextHTML + "[Stop]");
355 } else {
356 DIAPO = false ;
357 }
358 return false;
359 }
360
361 function diapo(){
362 //console.log(DIAPO);
363 if(DIAPO){
364 if(TB_NextURL !=""){
365 jQuery("#TB_window").remove();
366 jQuery("body").append("<div id='TB_window'></div>");
367 //console.log("hop");
368 TB_show(TB_NextCaption, TB_NextURL);
369 }else DIAPO = false ;
370 }
371 return false;
372 }
373
374 function TB_showIframe(){
375 jQuery("#TB_load").remove();
376 jQuery("#TB_window").css({display:"block"});
377 }
378
379 function TB_remove() {
380 DIAPO = false ;
381 FULL_S = false ;
382 jQuery("#TB_imageOff").unbind('click');
383 jQuery("#TB_overlay").unbind('click');
384 jQuery("#TB_closeWindowButton").unbind('click');
385 jQuery("#TB_window").fadeOut("fast",function(){jQuery('#TB_window,#TB_overlay,#TB_HideSelect').remove();});
386 jQuery("#TB_load").remove();
387 jQuery(".TB_hide").show();
388
389 return false;
390 }
391
392 function TB_position() {
393 var pagesize = TB_getPageSize();
394 var arrayPageScroll = TB_getPageScrollTop();
395 var legendHeight = jQuery("#TB_legend").height() ;
396
397 if(FULL_S && DIAPO){
398 FULL_S = false ;
399 jQuery("#TB_window").animate({top: (arrayPageScroll[1] + (pagesize[1]-TB_HEIGHT)/2),left:(arrayPageScroll[0] + (pagesize[0] - TB_WIDTH)/2), width:TB_WIDTH,height:TB_HEIGHT},1500);
400 jQuery("#TB_Image").animate({top:20,left:20,width:(TB_WIDTH - 20),height:(TB_HEIGHT - 20)},1500,TB_recadre);
401 }
402
403 if(!FULL_S || DIAPO){
404 jQuery("#TB_window").css({width:TB_WIDTH+"px",left: (arrayPageScroll[0] + (pagesize[0] - TB_WIDTH)/2)+"px", top: (arrayPageScroll[1] + (pagesize[1]-TB_HEIGHT)/2 - legendHeight/2)+"px" });
405 }
406 }
407
408 function TB_overlaySize(){
409 if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {
410 yScroll = window.innerHeight + window.scrollMaxY;
411 xScroll = window.innerWidth + window.scrollMaxX;
412 var deff = document.documentElement;
413 var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
414 var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
415 xScroll -= (window.innerWidth - wff);
416 yScroll -= (window.innerHeight - hff);
417 } else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
418 yScroll = document.body.scrollHeight;
419 xScroll = document.body.scrollWidth;
420 } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
421 yScroll = document.body.offsetHeight;
422 xScroll = document.body.offsetWidth;
423 }
424 jQuery("#TB_overlay").css({"height":yScroll +"px", "width":xScroll +"px"});
425 jQuery("#TB_HideSelect").css({"height":yScroll +"px","width":xScroll +"px"});
426 }
427
428 function TB_load_position() {
429 var pagesize = TB_getPageSize();
430 var arrayPageScroll = TB_getPageScrollTop();
431 jQuery("#TB_load")
432 .css({left: (arrayPageScroll[0] + (pagesize[0] - 100)/2)+"px", top: (arrayPageScroll[1] + ((pagesize[1]-100)/2))+"px" })
433 .css({display:"block"});
434 }
435
436 function TB_parseQuery ( query ) {
437 var Params = new Object ();
438 if ( ! query ) return Params; // return empty object
439 var Pairs = query.split(/[;&]/);
440
441 for ( var i = 0; i < Pairs.length; i++ ) {
442 var KeyVal = Pairs[i].split('=');
443
444 if ( ! KeyVal || KeyVal.length != 2 ) continue;
445 var key = unescape( KeyVal[0] );
446 var val = unescape( KeyVal[1] );
447 val = val.replace(/\+/g, ' ');
448 Params[key] = val;
449 }
450 return Params;
451 }
452
453 function TB_getPageScrollTop(){
454 var yScrolltop;
455 var xScrollleft;
456
457 if (self.pageYOffset || self.pageXOffset) {
458 yScrolltop = self.pageYOffset;
459 xScrollleft = self.pageXOffset;
460
461 } else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){// Explorer 6 Strict
462 yScrolltop = document.documentElement.scrollTop;
463 xScrollleft = document.documentElement.scrollLeft;
464
465 } else if (document.body) {// all other Explorers
466 yScrolltop = document.body.scrollTop;
467 xScrollleft = document.body.scrollLeft;
468 }
469 arrayPageScroll = new Array(xScrollleft,yScrolltop)
470 return arrayPageScroll;
471 }
472
473 function TB_getPageSize(){
474 var de = document.documentElement;
475 var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
476 var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
477 arrayPageSize = new Array(w,h)
478 return arrayPageSize;
479 }
480
481 function TB_recadre(){
482 TB_overlaySize();
483 TB_position();
484 }