3
* @author Warren Krewenki
5
* This package is distributed under the BSD license.
6
* For full license information, see LICENSE.TXT
8
* Based on Lightbox 2 by Lokesh Dhakar (http://www.huddletogether.com/projects/lightbox2/)
14
$.fn.lightbox = function(options) {
16
var opts = $.extend({}, $.fn.lightbox.defaults, options);
18
$(window).resize(resizeOverlayToFitWindow);
23
# Initialize the lightbox by creating our html and reading some image data
24
# This method is called by the constructor after any click events trigger it
25
# You will never call it by itself, to my knowledge.
27
function initialize() {
28
$('#overlay, #lightbox').remove();
29
opts.inprogress = false;
31
// if jsonData, build the imageArray from data provided in JSON format
32
if (opts.jsonData && opts.jsonData.length > 0) {
33
var parser = opts.jsonDataParser ? opts.jsonDataParser : $.fn.lightbox.parseJsonData;
35
opts.imageArray = parser(opts.jsonData);
38
var outerImage = '<div id="outerImageContainer"><div id="imageContainer" style="height: 520px; width: 720px;"><iframe id="lightboxIframe"></iframe><img id="lightboxImage" /><div id="hoverNav"><a href="javascript://" title="' + opts.strings.prevLinkTitle + '" id="prevLink"></a><a href="javascript://" id="nextLink" title="' + opts.strings.nextLinkTitle + '"></a></div><div id="loading"><a href="javascript://" id="loadingLink"><img src="'+opts.fileLoadingImage+'"></a></div></div></div>';
39
var imageData = '<div id="imageDataContainer" class="clearfix"><div id="imageData"><div id="imageDetails"><span id="caption"></span><span id="numberDisplay"></span></div><div id="bottomNav">';
41
// var outerImage = '<div id="outerImageContainer"><div id="imageContainer" style="height: 520px; width: 720px;"><iframe id="lightboxIframe"></iframe><img id="lightboxImage" syle="vertical-align: middle;" /><div id="hoverNav"><a href="javascript://" title="' + opts.strings.prevLinkTitle + '" id="prevLink"></a><a href="javascript://" id="nextLink" title="' + opts.strings.nextLinkTitle + '"></a></div><div id="loading"><a href="javascript://" id="loadingLink"><img src="'+opts.fileLoadingImage+'"></a></div></div></div>';
42
// var imageData = '<div id="imageDataContainer" class="clearfix"><div id="imageData"><div id="imageDetails"><span id="caption"></span><span id="numberDisplay"></span></div><div id="bottomNav">';
44
if (opts.displayHelp) {
45
imageData += '<span id="helpDisplay">' + opts.strings.help + '</span>';
48
imageData += '<a href="javascript://" id="bottomNavClose" title="' + opts.strings.closeTitle + '"><img src="'+opts.fileBottomNavCloseImage+'"></a></div></div></div>';
52
if (opts.navbarOnTop) {
53
string = '<div id="overlay"></div><div id="lightbox">' + imageData + outerImage + '</div>';
54
$("body").append(string);
55
$("#imageDataContainer").addClass('ontop');
57
string = '<div id="overlay"></div><div id="lightbox">' + outerImage + imageData + '</div>';
58
$("body").append(string);
61
//$("#overlay, #lightbox").click(function(){ end(); }).hide();
62
$("#loadingLink, #bottomNavClose").click(function(){ end(); return false;});
63
$('#outerImageContainer').width(opts.widthCurrent).height(opts.heightCurrent);
64
$('#imageDataContainer').width(opts.widthCurrent);
66
if (!opts.imageClickClose) {
67
$("#lightboxImage").click(function(){ return false; });
68
$("#hoverNav").click(function(){ return false; });
75
# Get the document and window width/heigh
80
# # => [1024,768,1024,768]
82
# Returns a numerically indexed array of document width/height and window width/height
84
function getPageSize() {
85
var jqueryPageSize = new Array($(document).width(),$(document).height(), $(window).width(), $(window).height());
86
return jqueryPageSize;
89
function getPageScroll() {
92
if (self.pageYOffset) {
93
yScroll = self.pageYOffset;
94
xScroll = self.pageXOffset;
95
} else if (document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict
96
yScroll = document.documentElement.scrollTop;
97
xScroll = document.documentElement.scrollLeft;
98
} else if (document.body) {// all other Explorers
99
yScroll = document.body.scrollTop;
100
xScroll = document.body.scrollLeft;
103
var arrayPageScroll = new Array(xScroll,yScroll);
104
return arrayPageScroll;
108
# Deploy the sexy overlay and display the lightbox
110
# imageObject - the jQuery object passed via the click event in the constructor
114
# showLightbox($('#CheesusCrust'))
116
# Returns a boolean true, because it's got nothing else to return. It should give visual feedback when run
118
function showLightbox(imageObject) {
120
* select, embed and object tags render over the lightbox in some browsers
121
* Right now, the best way to fix it is to hide them, but that can trigger reloading of some flash content
122
* I don't have a better fix for this right now, but I want ot leave this comment here so you and I both
123
* know that i'm aware of it, and I would love to fix it, if you have any suggestions.
125
$("select, embed, object").hide();
127
// Resize and display the sexy, sexy overlay.
128
resizeOverlayToFitWindow();
129
$("#overlay").hide().css({ opacity : opts.overlayOpacity }).fadeIn();
132
// if data is not provided by jsonData parameter
133
if (!opts.jsonData) {
134
opts.imageArray = [];
135
// if image is NOT part of a set..
136
if ((!imageObject.rel || (imageObject.rel == '')) && !opts.allSet) {
137
// add single image to Lightbox.imageArray
138
opts.imageArray.push(new Array(imageObject.href, opts.displayTitle ? imageObject.title : ''));
140
// if image is part of a set..
141
$("a").each(function() {
142
if(this.href && (this.rel == imageObject.rel)) {
143
opts.imageArray.push(new Array(this.href, opts.displayTitle ? this.title : ''));
149
if (opts.imageArray.length > 1) {
150
for (i = 0; i < opts.imageArray.length; i++) {
151
for (j = opts.imageArray.length - 1; j > i; j--) {
152
if (opts.imageArray[i][0] == opts.imageArray[j][0]) {
153
opts.imageArray.splice(j, 1);
157
while (opts.imageArray[imageNum][0] != imageObject.href) {
159
if (imageNum < opts.imageArray.length) {
165
// calculate top and left offset for the lightbox
166
var arrayPageScroll = getPageScroll();
167
var lightboxTop = arrayPageScroll[1] + ($(window).height() / 10);
168
var lightboxLeft = arrayPageScroll[0];
169
$('#lightbox').css({top: lightboxTop+'px', left: lightboxLeft+'px'}).show();
171
if (!opts.slideNavBar) {
172
$('#imageData').hide();
175
changeImage(imageNum);
178
function changeImage(imageNum) {
179
if (opts.inprogress == false) {
180
opts.inprogress = true;
183
opts.activeImage = imageNum;
185
// hide elements during transition
186
$('#loading').show();
187
$('#lightboxImage, #hoverNav, #prevLink, #nextLink').hide();
189
// delay preloading image until navbar will slide up
190
if (opts.slideNavBar) {
191
$('#imageDataContainer').hide();
192
$('#imageData').hide();
198
function doChangeImage() {
199
imgPreloader = new Image();
201
// once image is preloaded, resize image container
202
imgPreloader.onload = function() {
203
var newWidth = imgPreloader.width;
204
var newHeight = imgPreloader.height;
206
if (opts.scaleImages) {
207
newWidth = parseInt(opts.xScale * newWidth);
208
newHeight = parseInt(opts.yScale * newHeight);
211
if (opts.fitToScreen) {
212
var arrayPageSize = getPageSize();
214
var initialPageWidth = arrayPageSize[2] - 2 * opts.borderSize;
215
var initialPageHeight = arrayPageSize[3] - 200;
217
var dI = initialPageWidth/initialPageHeight;
218
var dP = imgPreloader.width/imgPreloader.height;
220
if ((imgPreloader.height > initialPageHeight) || (imgPreloader.width > initialPageWidth)) {
222
newWidth = parseInt((initialPageHeight/imgPreloader.height) * imgPreloader.width);
223
newHeight = initialPageHeight;
225
newHeight = parseInt((initialPageWidth/imgPreloader.width) * imgPreloader.height);
226
newWidth = initialPageWidth;
230
if (newWidth > 700) {
233
if (newHeight > 500) {
236
var line_height = (newHeight / 100) * 7 + 24
237
$("#imageContainer").attr("style", "line-height:" + line_height)
238
$("#lightboxImage").attr("style", "line-height:" + line_height)
240
attr('src', opts.imageArray[opts.activeImage][0]).
244
resizeImageContainer(newWidth, newHeight);
247
imgPreloader.src = opts.imageArray[opts.activeImage][0];
251
$('#overlay, #lightbox, #outerImageContainer, #imageDataContainer').remove();
254
function preloadNeighborImages() {
255
if (opts.loopImages && opts.imageArray.length > 1) {
256
preloadNextImage = new Image();
257
preloadNextImage.src = opts.imageArray[(opts.activeImage == (opts.imageArray.length - 1)) ? 0 : opts.activeImage + 1][0];
259
preloadPrevImage = new Image();
260
preloadPrevImage.src = opts.imageArray[(opts.activeImage == 0) ? (opts.imageArray.length - 1) : opts.activeImage - 1][0];
262
if ((opts.imageArray.length - 1) > opts.activeImage) {
263
preloadNextImage = new Image();
264
preloadNextImage.src = opts.imageArray[opts.activeImage + 1][0];
266
if (opts.activeImage > 0) {
267
preloadPrevImage = new Image();
268
preloadPrevImage.src = opts.imageArray[opts.activeImage - 1][0];
273
function resizeImageContainer(imgWidth, imgHeight) {
274
// get current width and height
275
opts.widthCurrent = $("#outerImageContainer").outerWidth();
276
opts.heightCurrent = $("#outerImageContainer").outerHeight();
278
// get new width and height
282
// calculate size difference between new and old image, and resize if necessary
283
wDiff = opts.widthCurrent - widthNew;
284
hDiff = opts.heightCurrent - heightNew;
286
$('#imageDataContainer').animate({width: widthNew},opts.resizeSpeed,'linear');
287
$('#outerImageContainer').animate({width: widthNew},opts.resizeSpeed,'linear', function() {
288
$('#outerImageContainer').animate({height: heightNew},opts.resizeSpeed,'linear', function() {
293
afterTimeout = function () {
294
$('#prevLink').height(500);
295
$('#nextLink').height(500);
298
// if new and old image are same size and no scaling transition is necessary,
299
// do a quick pause to prevent image flicker.
300
if((hDiff == 0) && (wDiff == 0)) {
301
if (jQuery.browser.msie) {
302
setTimeout(afterTimeout, 250);
304
setTimeout(afterTimeout, 100);
307
// otherwise just trigger the height and width change
313
function showImage() {
314
$('#loading').hide();
315
$('#lightboxImage').fadeIn("fast");
317
preloadNeighborImages();
319
opts.inprogress = false;
322
function updateDetails() {
323
$('#numberDisplay').html('');
325
if (opts.imageArray[opts.activeImage][1]) {
326
$('#caption').html(opts.imageArray[opts.activeImage][1]).show();
329
// if image is part of set display 'Image x of x'
330
if (opts.imageArray.length > 1) {
333
nav_html = opts.strings.image + (opts.activeImage + 1) + opts.strings.of + opts.imageArray.length;
335
if (opts.displayDownloadLink) {
336
nav_html += "<a href='" + opts.imageArray[opts.activeImage][0] + "'>" + opts.strings.download + "</a>";
339
if (!opts.disableNavbarLinks) {
340
// display previous / next text links
341
if ((opts.activeImage) > 0 || opts.loopImages) {
342
nav_html = '<a title="' + opts.strings.prevLinkTitle + '" href="#" id="prevLinkText">' + opts.strings.prevLinkText + "</a>" + nav_html;
345
if (((opts.activeImage + 1) < opts.imageArray.length) || opts.loopImages) {
346
nav_html += '<a title="' + opts.strings.nextLinkTitle + '" href="#" id="nextLinkText">' + opts.strings.nextLinkText + "</a>";
350
$('#numberDisplay').html(nav_html).show();
353
if (opts.slideNavBar) {
354
$("#imageData").slideDown(opts.navBarSlideSpeed);
356
$("#imageData").show();
359
resizeOverlayToFitWindow();
364
# Resize the sexy overlay to fit the constraints of your current viewing environment
366
# This should now happen whenever a window is resized, so you should always see a full overlay
368
function resizeOverlayToFitWindow(){
369
$('#overlay').css({width: $(document).width(), height: $(document).height()});
373
function updateNav() {
374
if (opts.imageArray.length > 1) {
375
$('#hoverNav').show();
377
// if loopImages is true, always show next and prev image buttons
378
if(opts.loopImages) {
379
$('#prevLink,#prevLinkText').show().click(function() {
380
changeImage((opts.activeImage == 0) ? (opts.imageArray.length - 1) : opts.activeImage - 1);
384
$('#nextLink,#nextLinkText').show().click(function() {
385
changeImage((opts.activeImage == (opts.imageArray.length - 1)) ? 0 : opts.activeImage + 1);
389
// if not first image in set, display prev image button
390
if(opts.activeImage != 0) {
391
$('#prevLink,#prevLinkText').show().click(function() {
392
changeImage(opts.activeImage - 1);
397
// if not last image in set, display next image button
398
if(opts.activeImage != (opts.imageArray.length - 1)) {
399
$('#nextLink,#nextLinkText').show().click(function() {
400
changeImage(opts.activeImage +1);
410
function keyboardAction(e) {
412
var keycode = e.keyCode;
415
var key = String.fromCharCode(keycode).toLowerCase();
418
if ((key == 'x') || (key == 'o') || (key == 'c') || (keycode == escapeKey)) {
421
// display previous image
422
} else if ((key == 'p') || (keycode == 37)) {
424
disableKeyboardNav();
425
changeImage((o.activeImage == 0) ? (o.imageArray.length - 1) : o.activeImage - 1);
426
} else if (o.activeImage != 0) {
427
disableKeyboardNav();
428
changeImage(o.activeImage - 1);
431
// display next image
432
} else if ((key == 'n') || (keycode == 39)) {
433
if (opts.loopImages) {
434
disableKeyboardNav();
435
changeImage((o.activeImage == (o.imageArray.length - 1)) ? 0 : o.activeImage + 1);
436
} else if (o.activeImage != (o.imageArray.length - 1)) {
437
disableKeyboardNav();
438
changeImage(o.activeImage + 1);
443
function enableKeyboardNav() {
444
$(document).bind('keydown', {opts: opts}, keyboardAction);
447
function disableKeyboardNav() {
448
$(document).unbind('keydown');
452
$.fn.lightbox.parseJsonData = function(data) {
455
$.each(data, function() {
456
imageArray.push(new Array(this.url, this.title));
461
$.fn.lightbox.defaults = {
462
triggerEvent: "click",
464
fileLoadingImage: '/multi_image/static/lib/lightbox/images/loading.gif',
465
fileBottomNavCloseImage: '/multi_image/static/lib/lightbox/images/closelabel.gif',
468
imageArray: new Array,
479
displayDownloadLink: false,
481
navBarSlideSpeed: 350,
484
help: ' \u2190 / P - previous image\u00a0\u00a0\u00a0\u00a0\u2192 / N - next image\u00a0\u00a0\u00a0\u00a0ESC / X - close image gallery',
485
prevLinkTitle: 'previous image',
486
nextLinkTitle: 'next image',
487
prevLinkText: '« Previous',
488
nextLinkText: 'Next »',
489
closeTitle: 'close image gallery',
495
disableNavbarLinks: false,
497
imageClickClose: true,
b'\\ No newline at end of file'