Project

General

Profile

Download (12.2 KB) Statistics
| Branch: | Tag: | Revision:
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 = "images/loadingAnimation.gif";
9

    
10

    
11
/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/
12

    
13
//on page load call tb_init
14
$(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
	$(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
			$("body","html").css({height: "100%", width: "100%"});
37
			$("html").css("overflow","hidden");
38
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
39
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
40
				$("#TB_overlay").click(tb_remove);
41
			}
42
		}else{//all others
43
			if(document.getElementById("TB_overlay") === null){
44
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
45
				$("#TB_overlay").click(tb_remove);
46
			}
47
		}
48
		
49
		if(tb_detectMacXFF()){
50
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
51
		}else{
52
			$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
53
		}
54
		
55
		if(caption===null){caption="";}
56
		$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
57
		$('#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

    
70
		if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){//code to show images
71
				
72
			var zoom = true;
73
				
74
			TB_PrevCaption = "";
75
			TB_PrevURL = "";
76
			TB_PrevHTML = "";
77
			TB_NextCaption = "";
78
			TB_NextURL = "";
79
			TB_NextHTML = "";
80
			TB_imageCount = "";
81
			TB_FoundURL = false;
82
			if(imageGroup){
83
				TB_TempArray = $("a[@rel="+imageGroup+"]").get();
84
				for (TB_Counter = 0; ((TB_Counter < TB_TempArray.length) && (TB_NextHTML === "")); TB_Counter++) {
85
					var urlTypeTemp = TB_TempArray[TB_Counter].href.toLowerCase().match(urlString);
86
						if (!(TB_TempArray[TB_Counter].href == url)) {						
87
							if (TB_FoundURL) {
88
								TB_NextCaption = TB_TempArray[TB_Counter].title;
89
								TB_NextURL = TB_TempArray[TB_Counter].href;
90
								TB_NextHTML = "<span id='TB_next'>&nbsp;&nbsp;<a href='#'>Next &gt;</a></span>";
91
							} else {
92
								TB_PrevCaption = TB_TempArray[TB_Counter].title;
93
								TB_PrevURL = TB_TempArray[TB_Counter].href;
94
								TB_PrevHTML = "<span id='TB_prev'><a href='#'>&lt; Prev</a>&nbsp;&nbsp;</span>";
95
							}
96
						} else {
97
							TB_FoundURL = true;
98
							TB_imageCount = (TB_Counter + 1) +"/"+ (TB_TempArray.length);											
99
						}
100
				}
101
			}
102

    
103
			imgPreloader = new Image();
104
			imgPreloader.onload = function(){		
105
			imgPreloader.onload = null;
106
				
107
			// Resizing large images - orginal by Christian Montoya edited by me.
108
			var pagesize = tb_getPageSize();
109
			var x = pagesize[0] - 150;
110
			var y = pagesize[1] - 150;
111
			var imageWidth = imgPreloader.width;
112
			var imageHeight = imgPreloader.height;
113
			if (imageWidth > x) {
114
				imageHeight = imageHeight * (x / imageWidth); 
115
				imageWidth = x; 
116
				if (imageHeight > y) { 
117
					imageWidth = imageWidth * (y / imageHeight); 
118
					imageHeight = y; 
119
				}
120
			} else if (imageHeight > y) { 
121
				imageWidth = imageWidth * (y / imageHeight); 
122
				imageHeight = y; 
123
				if (imageWidth > x) { 
124
					imageHeight = imageHeight * (x / imageWidth); 
125
					imageWidth = x;
126
				}
127
			}
128
			// End Resizing
129
			
130
			if(zoom){
131
				// viewport should be wider if zooming is enabled
132
				dx = (x - imageWidth)/1.5 ;
133
				
134
				imageWidth += dx;
135
			}
136
			
137
			TB_WIDTH = imageWidth + 30;
138
			TB_HEIGHT = imageHeight + 40;
139
			//$("#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'>close</a> or Esc Key</div>"); 		
140
			
141
			$("#TB_window").append(	"<img id='TB_Image' src='"+url+"' alt='"+caption+"'/>" 
142
									+ "<div id='TB_caption'>"+caption
143
										+"<div id='TB_secondLine'>" 
144
											+ TB_PrevHTML + TB_imageCount + TB_NextHTML 
145
										+ "</div>"
146
									+ "</div>"
147
								
148
									+ "<div id='TB_closeWindow'>"
149
										+ "<a href='#' id='TB_closeWindowButton' title='Close'>close</a> or Esc Key"
150
									+ "</div>"); 		
151
			
152
			
153
			$("#TB_closeWindowButton").click(tb_remove);
154
			
155
			if(zoom){
156
				var imageToolOptions = {
157
					'viewportWidth' :  imageWidth,
158
					'viewportHeight' : imageHeight
159
				}				
160
			
161
				$("#TB_Image").imagetool(imageToolOptions);
162
				$("#TB_caption").after( "<div id='TB_zoom_instructions'>Click and drag to navigate. Ctrl-Click and drag to zoom.</div>" );
163
			}
164
			
165
			
166
			if (!(TB_PrevHTML === "")) {
167
				function goPrev(){
168
					if($(document).unbind("click",goPrev)){$(document).unbind("click",goPrev);}
169
					$("#TB_window").remove();
170
					$("body").append("<div id='TB_window'></div>");
171
					tb_show(TB_PrevCaption, TB_PrevURL, imageGroup);
172
					return false;	
173
				}
174
				$("#TB_prev").click(goPrev);
175
			}
176
			
177
			if (!(TB_NextHTML === "")) {		
178
				function goNext(){
179
					$("#TB_window").remove();
180
					$("body").append("<div id='TB_window'></div>");
181
					tb_show(TB_NextCaption, TB_NextURL, imageGroup);				
182
					return false;	
183
				}
184
				$("#TB_next").click(goNext);
185
				
186
			}
187

    
188
			document.onkeydown = function(e){ 	
189
				if (e == null) { // ie
190
					keycode = event.keyCode;
191
				} else { // mozilla
192
					keycode = e.which;
193
				}
194
				if(keycode == 27){ // close
195
					tb_remove();
196
				} else if(keycode == 190){ // display previous image
197
					if(!(TB_NextHTML == "")){
198
						document.onkeydown = "";
199
						goNext();
200
					}
201
				} else if(keycode == 188){ // display next image
202
					if(!(TB_PrevHTML == "")){
203
						document.onkeydown = "";
204
						goPrev();
205
					}
206
				}	
207
			};
208
			
209
			tb_position();
210
			$("#TB_load").remove();
211
			$("#TB_ImageOff").click(tb_remove);
212
			$("#TB_window").css({display:"block"}); //for safari using css instead of show
213
			};
214
			
215
			imgPreloader.src = url;
216
		}else{//code to show html
217
			
218
			var queryString = url.replace(/^[^\?]+\??/,'');
219
			var params = tb_parseQuery( queryString );
220

    
221
			TB_WIDTH = (params['width']*1) + 30 || 630; //defaults to 630 if no paramaters were added to URL
222
			TB_HEIGHT = (params['height']*1) + 40 || 440; //defaults to 440 if no paramaters were added to URL
223
			ajaxContentW = TB_WIDTH - 30;
224
			ajaxContentH = TB_HEIGHT - 45;
225
			
226
			if(url.indexOf('TB_iframe') != -1){// either iframe or ajax window		
227
					urlNoQuery = url.split('TB_');
228
					$("#TB_iframeContent").remove();
229
					if(params['modal'] != "true"){//iframe no modal
230
						$("#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> or Esc Key</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>");
231
					}else{//iframe modal
232
					$("#TB_overlay").unbind();
233
						$("#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>");
234
					}
235
			}else{// not an iframe, ajax
236
					if($("#TB_window").css("display") != "block"){
237
						if(params['modal'] != "true"){//ajax no modal
238
						$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'>close</a> or Esc Key</div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
239
						}else{//ajax modal
240
						$("#TB_overlay").unbind();
241
						$("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");	
242
						}
243
					}else{//this means the window is already up, we are just loading new content via ajax
244
						$("#TB_ajaxContent")[0].style.width = ajaxContentW +"px";
245
						$("#TB_ajaxContent")[0].style.height = ajaxContentH +"px";
246
						$("#TB_ajaxContent")[0].scrollTop = 0;
247
						$("#TB_ajaxWindowTitle").html(caption);
248
					}
249
			}
250
					
251
			$("#TB_closeWindowButton").click(tb_remove);
252
			
253
			if(url.indexOf('TB_inline') != -1){	
254
				$("#TB_ajaxContent").append($('#' + params['inlineId']).children());
255
				$("#TB_window").unload(function () {
256
					$('#' + params['inlineId']).append( $("#TB_ajaxContent").children() ); // move elements back when you're finished
257
				});
258
				tb_position();
259
				$("#TB_load").remove();
260
				$("#TB_window").css({display:"block"}); 
261
			}else if(url.indexOf('TB_iframe') != -1){
262
				tb_position();
263
				if($.browser.safari){//safari needs help because it will not fire iframe onload
264
					$("#TB_load").remove();
265
					$("#TB_window").css({display:"block"});
266
				}
267
			}else{
268
				$("#TB_ajaxContent").load(url += "&random=" + (new Date().getTime()),function(){//to do a post change this load method
269
					tb_position();
270
					$("#TB_load").remove();
271
					tb_init("#TB_ajaxContent a.thickbox");
272
					$("#TB_window").css({display:"block"});
273
				});
274
			}
275
			
276
		}
277

    
278
		if(!params['modal']){
279
			document.onkeyup = function(e){ 	
280
				if (e == null) { // ie
281
					keycode = event.keyCode;
282
				} else { // mozilla
283
					keycode = e.which;
284
				}
285
				if(keycode == 27){ // close
286
					tb_remove();
287
				}	
288
			};
289
		}
290
		
291
	} catch(e) {
292
		//nothing here
293
	}
294
}
295

    
296
//helper functions below
297
function tb_showIframe(){
298
	$("#TB_load").remove();
299
	$("#TB_window").css({display:"block"});
300
}
301

    
302
function tb_remove() {
303
 	$("#TB_imageOff").unbind("click");
304
	$("#TB_closeWindowButton").unbind("click");
305
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
306
	$("#TB_load").remove();
307
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
308
		$("body","html").css({height: "auto", width: "auto"});
309
		$("html").css("overflow","");
310
	}
311
	document.onkeydown = "";
312
	document.onkeyup = "";
313
	return false;
314
}
315

    
316
function tb_position() {
317
$("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
318
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
319
		$("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
320
	}
321
}
322

    
323
function tb_parseQuery ( query ) {
324
   var Params = {};
325
   if ( ! query ) {return Params;}// return empty object
326
   var Pairs = query.split(/[;&]/);
327
   for ( var i = 0; i < Pairs.length; i++ ) {
328
      var KeyVal = Pairs[i].split('=');
329
      if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
330
      var key = unescape( KeyVal[0] );
331
      var val = unescape( KeyVal[1] );
332
      val = val.replace(/\+/g, ' ');
333
      Params[key] = val;
334
   }
335
   return Params;
336
}
337

    
338
function tb_getPageSize(){
339
	var de = document.documentElement;
340
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
341
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
342
	arrayPageSize = [w,h];
343
	return arrayPageSize;
344
}
345

    
346
function tb_detectMacXFF() {
347
  var userAgent = navigator.userAgent.toLowerCase();
348
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
349
    return true;
350
  }
351
}
352

    
353

    
(2-2/2)