PageSourceSearch

https://diterlizzi.com/wp-content/themes/diterlizzi/colorbox/jquery.colorbox.js

js diterlizzi.com collected 2026-10-02 06:21:41 UTC 18,688 bytes, 549 lines download raw bytes

1/*
2	ColorBox v1.2.5 - a full featured, light-weight, customizable lightbox based on jQuery 1.3
3	(c) 2009 Jack Moore - www.colorpowered.com - [email protected]
4	Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
5*/
6(function($){
7	
8	var open, element, settings, callback, maxWidth, maxHeight, loadedWidth, loadedHeight, interfaceHeight, interfaceWidth, index, $related, ssTimeout, $slideshow, $window, $close, $next, $prev, $current, $title, $modal, $wrap, $loadingOverlay, $loadingGraphic, $overlay, $modalContent, $loaded, $borderTopCenter, $borderMiddleLeft, $borderMiddleRight, $borderBottomCenter;
9	
10	/* Helper Functions */
11	//function for IE6 to set the background overlay
12	function IE6Overlay(){
13		$overlay.css({"position":"absolute", width:$window.width(), height:$window.height(), top:"10%", left:$window.scrollLeft()});
14	}
15
16	function slideshow(){
17		var stop;
18		function start(){
19			$slideshow
20			.text(settings.slideshowStop)
21			.bind("cbox_complete", function(){
22				ssTimeout = setTimeout($.fn.colorbox.next, settings.slideshowSpeed);
23			})
24			.bind("cbox_load", function(){
25				clearTimeout(ssTimeout);	
26			}).one("click", function(){
27				stop();
28				$(this).removeClass('hover');
29			});
30			$modal.removeClass("cboxSlideshow_off").addClass("cboxSlideshow_on");
31		}
32		
33		stop = function(){
34			clearTimeout(ssTimeout);
35			$slideshow
36			.text(settings.slideshowStart)
37			.unbind('cbox_complete cbox_load')
38			.one("click", function(){
39				start();
40				ssTimeout = setTimeout($.fn.colorbox.next, settings.slideshowSpeed);
41				$(this).removeClass('hover');
42			});
43			$modal.removeClass("cboxSlideshow_on").addClass("cboxSlideshow_off");
44		};
45		
46		if(settings.slideshow && $related.length>1){
47			if(settings.slideshowAuto){
48				start();
49			} else {
50				stop();
51			}
52		}
53	}
54
55	function clearInline(){
56		if($("#cboxInlineTemp").length > 0){
57			$loaded.children().insertBefore("#cboxInlineTemp");
58			$("#cboxInlineTemp").remove();
59		}
60	}
61
62	function cbox_key(e) {
63		if(e.keyCode == 37){
64			e.preventDefault();
65			$prev.click();
66		} else if(e.keyCode == 39){
67			e.preventDefault();
68			$next.click();
69		}
70	}
71
72	// Convert % values to pixels
73	function setSize(size, dimension){
74		dimension = dimension=='x' ? document.documentElement.clientWidth : document.documentElement.clientHeight;
75		return (typeof size == 'string') ? (size.match(/%/) ? (dimension/100)*parseInt(size, 10) : parseInt(size, 10)) : size;
76	}
77
78	function isImage(url){
79		return settings.photo ? true : url.match(/\.(gif|png|jpg|jpeg|bmp)(?:\?([^#]*))?(?:#(.*))?$/i);
80	}
81
82	/* Initializes ColorBox when the DOM has loaded */
83	$(function(){
84		$.fn.colorbox.init();
85	});
86
87	$.fn.colorbox = function(options, custom_callback) {
88		
89		if(this.length){
90			this.each(function(){
91				var data = $(this).data("colorbox") ? $.extend({},
92					$(this).data("colorbox"), options) : $.extend({}, $.fn.colorbox.settings, options);
93				
94				data.title = data.title ? data.title : this.title;
95				data.href = data.href ? data.href : this.href;
96				data.rel = data.rel ? data.rel : this.rel;
97				$(this).data("colorbox", data).addClass("cboxelement");
98			});
99		} else {
100			$(this).data("colorbox", $.extend({}, $.fn.colorbox.settings, options));
101		}
102		
103		$(this).unbind("click.colorbox").bind("click.colorbox", function (event) {
104			
105			element = this;
106			
107			settings = $(this).data('colorbox');
108			
109			//remove the focus from the anchor to prevent accidentally calling
110			//colorbox multiple times (by pressing the 'Enter' key
111			//after colorbox has opened, but before the user has clicked on anything else)
112			this.blur();
113			
114			callback = custom_callback ? custom_callback : false;
115			
116			if (settings.rel && settings.rel != 'nofollow') {
117				$related = $('.cboxelement').filter(function(){
118					return ($(this).data("colorbox").rel == settings.rel);
119				});
120				index = $related.index(this);
121			} else {
122				$related = $(this);
123				index = 0;
124			}
125			if (!open) {
126				$.event.trigger('cbox_open');
127				$close.html(settings.close);
128				$overlay.css({"opacity": settings.opacity}).show();
129				open = true;
130				$.fn.colorbox.position(setSize(settings.initialWidth, 'x'), setSize(settings.initialHeight, 'y'), 0);
131				if ($.browser.msie && $.browser.version < 7) {
132					$window.bind("resize scroll", IE6Overlay);
133				}
134			}
135			slideshow();
136			$.fn.colorbox.load();
137			
138			if(settings.overlayClose===true){
139				$overlay.css({"cursor":"pointer"}).click($.fn.colorbox.close);
140			}
141			event.preventDefault();
142		});
143		
144		if(options && options.open){
145			$(this).triggerHandler('click.colorbox');
146		}
147		
148		return this;
149	};
150
151	$.fn.colorbox.element = function(){
152		return element;
153	};
154
155	/*
156	  Initialize the modal: store common calculations, preload the interface graphics, append the html.
157	  This preps colorbox for a speedy open when clicked, and lightens the burdon on the browser by only
158	  having to run once, instead of each time colorbox is opened.
159	*/
160	$.fn.colorbox.init = function(){
161		
162		$window = $(window);
163		
164		$('body').prepend(
165			$overlay = $('<div id="cboxOverlay" />').hide(), 
166			$modal = $('<div id="colorbox" />')
167		);
168		
169		$wrap = $('<div id="cboxWrapper" />').appendTo($modal).append(
170			$('<div/>').append(
171				$('<div id="cboxTopLeft"/>'),
172				$borderTopCenter = $('<div id="cboxTopCenter"/>'),
173				$('<div id="cboxTopRight"/>')
174			),
175			$borderMiddleLeft = $('<div id="cboxMiddleLeft" />'),
176			$modalContent = $('<div id="cboxContent" />'),
177			$borderMiddleRight = $('<div id="cboxMiddleRight" />'),
178			$('<div/>').append(
179				$('<div id="cboxBottomLeft"/>'),
180				$borderBottomCenter = $('<div id="cboxBottomCenter"/>'),
181				$('<div id="cboxBottomRight"/>')
182			)
183		);
184		
185		$wrap.find("[id]").css({'float':'left'});
186		
187		$modalContent.append(
188			//loaded is filled with temporary HTML to allow the CSS backgrounds for those elements to load before ColorBox is actually called.
189			$loaded = $('<div id="cboxLoadedContent" style="width:0; height:0;" />'),
190			$loadingOverlay = $('<div id="cboxLoadingOverlay" />'),
191			$loadingGraphic = $('<div id="cboxLoadingGraphic" />'),
192			$title = $('<div id="cboxTitle" />'),
193			$current = $('<div id="cboxCurrent" />'),
194			$slideshow = $('<div id="cboxSlideshow" />'),
195			$next = $('<div id="cboxNext" />').click($.fn.colorbox.next),
196			$prev = $('<div id="cboxPrevious" />').click($.fn.colorbox.prev),
197			$close = $('<div id="cboxClose" />').click($.fn.colorbox.close)
198		);
199		
200		$modalContent.children()
201			.addClass("hover")
202			.mouseover(function(){$(this).addClass("hover");})
203			.mouseout(function(){$(this).removeClass("hover");})
204			.hide();
205		
206		//precalculate sizes that will be needed multiple times.
207		interfaceHeight = $borderTopCenter.height()+$borderBottomCenter.height()+$modalContent.outerHeight(true) - $modalContent.height();//Subtraction needed for IE6
208		interfaceWidth = $borderMiddleLeft.width()+$borderMiddleRight.width()+$modalContent.outerWidth(true) - $modalContent.width();
209		loadedHeight = $loaded.outerHeight(true);
210		loadedWidth = $loaded.outerWidth(true);
211		
212		$modal.css({"padding-bottom":interfaceHeight,"padding-right":interfaceWidth}).hide();//the padding removes the need to do size conversions during the animation step.
213		
214		//Setup button & key events.
215		$().bind("keydown.cbox_close", function(e){
216			if (e.keyCode == 27) {
217				e.preventDefault();
218				$close.click();
219			}
220		});
221		
222		$modalContent.children().removeClass("hover");
223	};
224	
225	//navigates to the next page/image in a set.
226	$.fn.colorbox.next = function(){
227		index = index < $related.length-1 ? index+1 : 0;
228		$.fn.colorbox.load();
229	};
230	
231	$.fn.colorbox.prev = function(){
232		index = index > 0 ? index-1 : $related.length-1;
233		$.fn.colorbox.load();
234	};
235	
236	$.fn.colorbox.position = function(mWidth, mHeight, speed, loadedCallback){
237		var winHeight = document.documentElement.clientHeight;
238		var posTop = winHeight/2 - mHeight/2;
239		var posLeft = document.documentElement.clientWidth/2 - mWidth/2;
240		//keeps the box from expanding to an inaccessible area offscreen.
241		if(mHeight > winHeight){posTop -=(mHeight - winHeight);}
242		if(posTop < 0){posTop = 0;} 
243		if(posLeft < 0){posLeft = 0;}
244		
245		posTop+=$window.scrollTop();
246		posLeft+=$window.scrollLeft();
247		
248		mWidth = mWidth - interfaceWidth;
249		mHeight = mHeight - interfaceHeight;
250		
251		//this gives the wrapper plenty of breathing room so it's floated contents can move around smoothly,
252		//but it has to be shrank down around the size of div#colorbox when it's done.  If not,
253		//it can invoke an obscure IE bug when using iframes.
254		$wrap[0].style.width = $wrap[0].style.height = "9999px";
255
256		function modalDimensions(that){
257			//loading overlay size has to be sure that IE6 uses the correct height.
258			$borderTopCenter[0].style.width = $borderBottomCenter[0].style.width = $modalContent[0].style.width = that.style.width;
259			$loadingGraphic[0].style.height = $loadingOverlay[0].style.height = $modalContent[0].style.height = $borderMiddleLeft[0].style.height = $borderMiddleRight[0].style.height = that.style.height;
260		}
261		
262		//setting the speed to 0 to reduce the delay between same-sized content.
263		var animate_speed = ($modal.width()===mWidth && $modal.height() === mHeight) ? 0 : speed;
264		$modal.dequeue().animate({height:mHeight, width:mWidth, top:"10%", left:posLeft}, {duration: animate_speed,
265			complete: function(){
266				modalDimensions(this);
267				
268				//shrink the wrapper down to exactly the size of colorbox to avoid a bug in IE's iframe implementation.
269				$wrap[0].style.width = (mWidth+interfaceWidth) + "px";
270				$wrap[0].style.height = (mHeight+interfaceHeight) + "px";
271				
272				if (loadedCallback) {loadedCallback();}
273				if ($.browser.msie && $.browser.version < 7) {IE6Overlay();}
274			},
275			step: function(){
276				modalDimensions(this);
277			}
278		});
279	};
280	
281	$.fn.colorbox.dimensions = function(object){
282		if(!open){ return; }
283		
284		$window.unbind('resize.cbox_resize');
285		
286		var speed = settings.transition=="none" ? 0 : settings.speed;
287		$loaded.remove();
288		$loaded = $(object);
289		
290		function getWidth(){
291			if(settings.width){
292				return maxWidth;
293			} else {
294				return maxWidth && maxWidth < $loaded.width() ? maxWidth : $loaded.width();
295			}
296		}
297		function getHeight(){
298			if(settings.height){
299				return maxHeight;
300			} else {
301				return maxHeight && maxHeight < $loaded.height() ? maxHeight : $loaded.height();
302			}
303		}
304		
305		$loaded.hide().appendTo('body')
306		.attr({id:'cboxLoadedContent'})
307		.css({width:getWidth()})
308		.css({height:getHeight()})//sets the height independently from the width in case the new width influences the value of height.
309		.prependTo($modalContent);
310		
311		if ($.browser.msie && $.browser.version < 7) {
312			$('select').not($('#colorbox select')).css({'visibility':'hidden'});
313		}
314				
315		if($('#cboxPhoto').length > 0 && settings.height){
316			var topMargin = ($loaded.height() - parseInt($('#cboxPhoto')[0].style.height, 10))/2;
317			$('#cboxPhoto').css({marginTop:(topMargin > 0?topMargin:0)});
318		}
319		
320		function setPosition(s){
321			var mWidth = $loaded.width()+loadedWidth+interfaceWidth;
322			var mHeight = $loaded.height()+loadedHeight+interfaceHeight;
323			$.fn.colorbox.position(mWidth, mHeight, s, function(){
324				if(!open){ return; }
325				
326				if($.browser.msie){
327					//This fadeIn helps the bicubic resampling to kick-in.
328					if($('#cboxPhoto').length > 0 ){$loaded.fadeIn(100);}
329					//IE adds a filter when ColorBox fades in and out that can cause problems if the loaded content contains transparent pngs.
330					$modal.css('filter','');
331				}
332				
333				$modalContent.children().show();
334				
335				//Waited until the iframe is added to the DOM & it is visible before setting the src.
336				//This increases compatability with pages using DOM dependent JavaScript.
337				$('#cboxIframe').attr('src', settings.href);
338				
339				$loadingOverlay.hide();
340				$loadingGraphic.hide();
341				$slideshow.hide();
342				
343				if($related.length>1){
344					$current.html(settings.current.replace(/\{current\}/, index+1).replace(/\{total\}/, $related.length));
345					$next.html(settings.next);
346					$prev.html(settings.previous);
347					
348					$().unbind('keydown', cbox_key).one('keydown', cbox_key);
349					
350					if(settings.slideshow){
351						$slideshow.show();
352					}
353				} else {
354					$current.add($next).add($prev).hide();
355				}
356				$title.html(settings.title);
357				
358				$.event.trigger('cbox_complete');
359				
360				if(callback){
361					callback.call(element);
362				}
363				
364				if (settings.transition === 'fade'){
365					$modal.fadeTo(speed, 1, function(){
366						if($.browser.msie){$modal.css('filter','');}
367					});
368				}
369				
370				$window.bind('resize.cbox_resize', function(){
371					$.fn.colorbox.position(mWidth, mHeight, 0);
372				});
373			});
374		}
375		if (settings.transition == 'fade') {
376			$modal.fadeTo(speed, 0, function(){setPosition(0);});
377		} else {
378			setPosition(speed);
379		}
380		
381		if(settings.preloading && $related.length>1){
382			var previous = index > 0 ? $related[index-1] : $related[$related.length-1];
383			var next = index < $related.length-1 ? $related[index+1] : $related[0];
384			if(isImage($(next).data('colorbox').href)){
385				$('<img />').attr('src', next);
386			}
387			if(isImage($(previous).data('colorbox').href)){
388				$('<img />').attr('src', previous);
389			}
390		}
391	};
392	
393	$.fn.colorbox.load = function(){
394		
395		element = $related[index];
396		
397		settings = $(element).data('colorbox');
398		
399		$.event.trigger('cbox_load');
400		
401		$loadingOverlay.show();
402		$loadingGraphic.show();
403		$close.show();
404		clearInline();//puts inline elements back if they are being used
405		
406		// Evaluate the height based on the optional height and width settings.
407		var height = settings.height ? setSize(settings.height, 'y') - loadedHeight - interfaceHeight : false;
408		var width = settings.width ? setSize(settings.width, 'x') - loadedWidth - interfaceWidth : false;
409		
410		//Re-evaluate the maximum dimensions based on the optional maxheight and maxwidth.
411		if(settings.maxHeight){
412			maxHeight = settings.maxHeight ? setSize(settings.maxHeight, 'y') - loadedHeight - interfaceHeight : false;
413			height = height && height < maxHeight ? height : maxHeight;
414		}
415		if(settings.maxWidth){
416			maxWidth = settings.maxWidth ? setSize(settings.maxWidth, 'x') - loadedWidth - interfaceWidth : false;
417			width = width && width < maxWidth ? width : maxWidth;
418		}
419		
420		maxHeight = height;
421		maxWidth = width;
422		
423		var href = settings.href;
424		
425		if (settings.inline) {
426			$('<div id="cboxInlineTemp" />').hide().insertBefore($(href)[0]);
427			$.fn.colorbox.dimensions($(href).wrapAll('<div/>').parent());
428		} else if (settings.iframe) {
429			$.fn.colorbox.dimensions(
430				$("<div><iframe id='cboxIframe' name='iframe_"+new Date().getTime()+"' frameborder=0 /></div>")
431			);//timestamp to prevent caching.
432		} else if (isImage(href)){
433			var loadingElement = new Image();
434			loadingElement.onload = function(){
435				loadingElement.onload = null;
436			
437				if((maxHeight || maxWidth) && settings.resize){
438					var width = this.width;
439					var height = this.height;
440					var percent = 0;
441					var that = this;
442					
443					var setResize = function(){
444						height += height * percent;
445						width += width * percent;
446						that.height = height;
447						that.width = width;	
448					};
449					if( maxWidth && width > maxWidth ){
450						percent = (maxWidth - width) / width;
451						setResize();
452					}
453					if( maxHeight && height > maxHeight ){
454						percent = (maxHeight - height) / height;
455						setResize();
456					}
457				}
458				
459				$.fn.colorbox.dimensions($("<div />").css({width:this.width, height:this.height}).append($(this).css({width:this.width, height:this.height, display:"block", margin:"auto", border:0}).attr('id', 'cboxPhoto')));
460				if($related.length > 1){
461					$(this).css({cursor:'pointer'}).click($.fn.colorbox.next);
462				}
463				if($.browser.msie && $.browser.version == 7){
464					this.style.msInterpolationMode='bicubic';
465				}
466			};
467			loadingElement.src = href;
468		} else {
469			$('<div />').load(href, function(data, textStatus){
470				if(textStatus == "success"){
471					$.fn.colorbox.dimensions($(this));
472				} else {
473					$.fn.colorbox.dimensions($("<p>Request unsuccessful.</p>"));
474				}
475			});
476		}	
477	};
478
479	//public function for closing colorbox.  To use this within an iframe use the following format: parent.$.fn.colorbox.close();
480	$.fn.colorbox.close = function(){
481		open = false;
482		clearTimeout(ssTimeout);
483		$window.unbind('resize.cbox_resize');
484		$slideshow.unbind('cbox_complete cbox_load click');
485		clearInline();
486		$overlay.css({cursor:'auto'}).fadeOut('fast').unbind('click', $.fn.colorbox.close);
487		$().unbind('keydown', cbox_key);
488		
489		if ($.browser.msie && $.browser.version < 7) {
490			$('select').css({'visibility':'inherit'});
491			$window.unbind('resize scroll', IE6Overlay);
492		}
493		
494		$modalContent.children().hide();
495		
496		$modal
497		.stop(true, false)
498		.removeClass()
499		.fadeOut('fast', function(){
500			element.focus();
501			$loaded.remove();
502			$modal.css({'opacity':1});
503			$.event.trigger('cbox_closed');
504		});
505	};
506
507	/*
508		ColorBox Default Settings.
509		
510		The colorbox() function takes one argument, an object of key/value pairs, that are used to initialize the modal.
511		
512		Please do not change these settings here, instead overwrite these settings when attaching the colorbox() event to your anchors.
513		Example (Global)	: $.fn.colorbox.settings.transition = "fade"; //changes the transition to fade for all colorBox() events proceeding it's declaration.
514		Example (Specific)	: $("a[href='http://www.google.com']").colorbox({width:"90%", height:"450px", iframe:true});
515		
516		See http://colorpowered.com/colorbox for details.
517	*/
518	$.fn.colorbox.settings = {
519		transition : "elastic",
520		speed : 350,
521		width : false,
522		height : false,
523		initialWidth : "400",
524		initialHeight : "400",
525		maxWidth : false,
526		maxHeight : false,
527		resize : true,
528		inline : false,
529		iframe : false,
530		photo : false,
531		href : false,
532		title : false,
533		rel : false,
534		opacity : 0.9,
535		preloading : true,
536		current : "image {current} of {total}",
537		previous : "previous",
538		next : "next",
539		close : "close",
540		open : false,
541		overlayClose : true,
542		slideshow:false,
543		slideshowAuto:true,
544		slideshowSpeed: 2500,
545		slideshowStart: "start slideshow",
546		slideshowStop: "stop slideshow"
547	};
548})(jQuery);
549

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.