4 by Lokesh Dhakar - http://www.lokeshdhakar.com
6 For more information, visit:
7 http://lokeshdhakar.com/projects/lightbox2/
9 Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
10 - free for use in both personal and commercial projects
11 - attribution requires leaving author name, author link, and the license info intact
14 - Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.com), and Thomas Fuchs(mir.aculo.us) for ideas, libs, and snippets.
15 - Artemy Tregubenko (arty.name) for cleanup and help in updating to latest proto-aculous in v2.05.
33 - preloadNeigbhoringImages
39 options = new LightboxOptions
40 lightbox = new Lightbox options
44 var $, Lightbox, LightboxOptions;
48 LightboxOptions = (function() {
50 function LightboxOptions() {
51 this.fileLoadingImage = 'images/loading.gif';
52 this.fileCloseImage = 'images/close.png';
53 this.resizeDuration = 700;
54 this.fadeDuration = 500;
55 this.labelImage = "Image";
59 return LightboxOptions;
63 Lightbox = (function() {
65 function Lightbox(options) {
66 this.options = options;
68 this.currentImageIndex = void 0;
72 Lightbox.prototype.init = function() {
77 Lightbox.prototype.enable = function() {
79 return $('body').on('click', 'a[rel^=lightbox], area[rel^=lightbox]', function(e) {
80 _this.start($(e.currentTarget));
85 Lightbox.prototype.build = function() {
90 }).after($('<div/>', {
92 }).append($('<div/>', {
93 "class": 'lb-outerContainer'
94 }).append($('<div/>', {
95 "class": 'lb-container'
96 }).append($('<img/>', {
100 }).append($('<a/>', {
106 }).append($('<a/>', {
108 }).append($('<img/>', {
109 src: this.options.fileLoadingImage
110 }))))), $('<div/>', {
111 "class": 'lb-dataContainer'
112 }).append($('<div/>', {
114 }).append($('<div/>', {
115 "class": 'lb-details'
116 }).append($('<span/>', {
117 "class": 'lb-caption'
121 "class": 'lb-closeContainer'
122 }).append($('<a/>', {
124 }).append($('<img/>', {
125 src: this.options.fileCloseImage
126 }))))))).appendTo($('body'));
127 $('#lightboxOverlay').hide().on('click', function(e) {
131 $lightbox = $('#lightbox');
132 $lightbox.hide().on('click', function(e) {
133 if ($(e.target).attr('id') === 'lightbox') _this.end();
136 $lightbox.find('.lb-outerContainer').on('click', function(e) {
137 if ($(e.target).attr('id') === 'lightbox') _this.end();
140 $lightbox.find('.lb-prev').on('click', function(e) {
141 _this.changeImage(_this.currentImageIndex - 1);
144 $lightbox.find('.lb-next').on('click', function(e) {
145 _this.changeImage(_this.currentImageIndex + 1);
148 $lightbox.find('.lb-loader, .lb-close').on('click', function(e) {
154 Lightbox.prototype.start = function($link) {
155 var $lightbox, $window, a, i, imageNumber, left, top, _len, _ref;
156 $(window).on("resize", this.sizeOverlay);
157 $('select, object, embed').css({
160 $('#lightboxOverlay').width($(document).width()).height($(document).height()).fadeIn(this.options.fadeDuration);
163 if ($link.attr('rel') === 'lightbox') {
165 link: $link.attr('href'),
166 title: $link.attr('title')
169 _ref = $($link.prop("tagName") + '[rel="' + $link.attr('rel') + '"]');
170 for (i = 0, _len = _ref.length; i < _len; i++) {
173 link: $(a).attr('href'),
174 title: $(a).attr('title')
176 if ($(a).attr('href') === $link.attr('href')) imageNumber = i;
180 top = $window.scrollTop() + $window.height() / 10;
181 left = $window.scrollLeft();
182 $lightbox = $('#lightbox');
186 }).fadeIn(this.options.fadeDuration);
187 this.changeImage(imageNumber);
190 Lightbox.prototype.changeImage = function(imageNumber) {
191 var $image, $lightbox, preloader,
193 this.disableKeyboardNav();
194 $lightbox = $('#lightbox');
195 $image = $lightbox.find('.lb-image');
197 $('#lightboxOverlay').fadeIn(this.options.fadeDuration);
198 $('.loader').fadeIn('slow');
199 $lightbox.find('.lb-image, .lb-nav, .lb-prev, .lb-next, .lb-dataContainer, .lb-numbers, .lb-caption').hide();
200 $lightbox.find('.lb-outerContainer').addClass('animating');
201 preloader = new Image;
202 preloader.onload = function() {
203 $image.attr('src', _this.album[imageNumber].link);
204 $image.width = preloader.width;
205 $image.height = preloader.height;
206 return _this.sizeContainer(preloader.width, preloader.height);
208 preloader.src = this.album[imageNumber].link;
209 this.currentImageIndex = imageNumber;
212 Lightbox.prototype.sizeOverlay = function() {
213 return $('#lightboxOverlay').width($(document).width()).height($(document).height());
216 Lightbox.prototype.sizeContainer = function(imageWidth, imageHeight) {
217 var $container, $lightbox, $outerContainer, containerBottomPadding, containerLeftPadding, containerRightPadding, containerTopPadding, newHeight, newWidth, oldHeight, oldWidth,
219 $lightbox = $('#lightbox');
220 $outerContainer = $lightbox.find('.lb-outerContainer');
221 oldWidth = $outerContainer.outerWidth();
222 oldHeight = $outerContainer.outerHeight();
223 $container = $lightbox.find('.lb-container');
224 containerTopPadding = parseInt($container.css('padding-top'), 10);
225 containerRightPadding = parseInt($container.css('padding-right'), 10);
226 containerBottomPadding = parseInt($container.css('padding-bottom'), 10);
227 containerLeftPadding = parseInt($container.css('padding-left'), 10);
228 newWidth = imageWidth + containerLeftPadding + containerRightPadding;
229 newHeight = imageHeight + containerTopPadding + containerBottomPadding;
230 if (newWidth !== oldWidth && newHeight !== oldHeight) {
231 $outerContainer.animate({
234 }, this.options.resizeDuration, 'swing');
235 } else if (newWidth !== oldWidth) {
236 $outerContainer.animate({
238 }, this.options.resizeDuration, 'swing');
239 } else if (newHeight !== oldHeight) {
240 $outerContainer.animate({
242 }, this.options.resizeDuration, 'swing');
244 setTimeout(function() {
245 $lightbox.find('.lb-dataContainer').width(newWidth);
246 $lightbox.find('.lb-prevLink').height(newHeight);
247 $lightbox.find('.lb-nextLink').height(newHeight);
249 }, this.options.resizeDuration);
252 Lightbox.prototype.showImage = function() {
254 $lightbox = $('#lightbox');
255 $lightbox.find('.lb-loader').hide();
256 $lightbox.find('.lb-image').fadeIn('slow');
258 this.updateDetails();
259 this.preloadNeighboringImages();
260 this.enableKeyboardNav();
263 Lightbox.prototype.updateNav = function() {
265 $lightbox = $('#lightbox');
266 $lightbox.find('.lb-nav').show();
267 if (this.currentImageIndex > 0) $lightbox.find('.lb-prev').show();
268 if (this.currentImageIndex < this.album.length - 1) {
269 $lightbox.find('.lb-next').show();
273 Lightbox.prototype.updateDetails = function() {
276 $lightbox = $('#lightbox');
277 if (typeof this.album[this.currentImageIndex].title !== 'undefined' && this.album[this.currentImageIndex].title !== "") {
278 $lightbox.find('.lb-caption').html(this.album[this.currentImageIndex].title).fadeIn('fast');
280 if (this.album.length > 1) {
281 $lightbox.find('.lb-number').html(this.options.labelImage + ' ' + (this.currentImageIndex + 1) + ' ' + this.options.labelOf + ' ' + this.album.length).fadeIn('fast');
283 $lightbox.find('.lb-number').hide();
285 $lightbox.find('.lb-outerContainer').removeClass('animating');
286 $lightbox.find('.lb-dataContainer').fadeIn(this.resizeDuration, function() {
287 return _this.sizeOverlay();
291 Lightbox.prototype.preloadNeighboringImages = function() {
292 var preloadNext, preloadPrev;
293 if (this.album.length > this.currentImageIndex + 1) {
294 preloadNext = new Image;
295 preloadNext.src = this.album[this.currentImageIndex + 1].link;
297 if (this.currentImageIndex > 0) {
298 preloadPrev = new Image;
299 preloadPrev.src = this.album[this.currentImageIndex - 1].link;
303 Lightbox.prototype.enableKeyboardNav = function() {
304 $(document).on('keyup.keyboard', $.proxy(this.keyboardAction, this));
307 Lightbox.prototype.disableKeyboardNav = function() {
308 $(document).off('.keyboard');
311 Lightbox.prototype.keyboardAction = function(event) {
312 var KEYCODE_ESC, KEYCODE_LEFTARROW, KEYCODE_RIGHTARROW, key, keycode;
314 KEYCODE_LEFTARROW = 37;
315 KEYCODE_RIGHTARROW = 39;
316 keycode = event.keyCode;
317 key = String.fromCharCode(keycode).toLowerCase();
318 if (keycode === KEYCODE_ESC || key.match(/x|o|c/)) {
320 } else if (key === 'p' || keycode === KEYCODE_LEFTARROW) {
321 if (this.currentImageIndex !== 0) {
322 this.changeImage(this.currentImageIndex - 1);
324 } else if (key === 'n' || keycode === KEYCODE_RIGHTARROW) {
325 if (this.currentImageIndex !== this.album.length - 1) {
326 this.changeImage(this.currentImageIndex + 1);
331 Lightbox.prototype.end = function() {
332 this.disableKeyboardNav();
333 $(window).off("resize", this.sizeOverlay);
334 $('#lightbox').fadeOut(this.options.fadeDuration);
335 $('#lightboxOverlay').fadeOut(this.options.fadeDuration);
336 return $('select, object, embed').css({
337 visibility: "visible"
346 var lightbox, options;
347 options = new LightboxOptions;
348 return lightbox = new Lightbox(options);