-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathshadowbox.js
1340 lines (1082 loc) · 35.9 KB
/
shadowbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* Shadowbox, version 4.0.0 <http://shadowbox-js.com/>
* Copyright 2014 Michael Jackson
*/
(function (global) {
var documentElement = document.documentElement;
// Detect support for opacity.
var supportsOpacity = "opacity" in documentElement.style && typeof documentElement.style.opacity === "string";
// Detect support for fixed positioning.
var fixedDiv = document.createElement("div");
fixedDiv.style.position = "fixed";
fixedDiv.style.margin = 0;
fixedDiv.style.top = "20px";
documentElement.appendChild(fixedDiv, documentElement.firstChild);
var supportsFixed = (fixedDiv.offsetTop == 20);
documentElement.removeChild(fixedDiv);
// Detect touch-based devices.
var supportsTouch = ("createTouch" in document);
/**
* The current version of Shadowbox.
*/
shadowbox.version = "4.0.0";
shadowbox.guid = 1;
shadowbox.K = function () {
return this;
};
/**
* The default set of options.
*/
shadowbox.options = {
// Animate height/width transitions.
animate: true,
// Automatically close when done playing movies.
autoClose: false,
// Able to navigate from one end of a gallery to the other (i.e. from
// last item to first or vice versa) by choosing next/previous?
continuous: false,
// Easing function for animations. Based on a cubic polynomial.
ease: function (state) {
return 1 + Math.pow(state - 1, 3);
},
// Enable control of Shadowbox via the keyboard?
enableKeys: !supportsTouch,
// The space to maintain around the edge of Shadowbox at all times.
margin: 40,
// A hook function that is called when closing.
onClose: shadowbox.K,
// A hook function that is called when a player is finished loading and
// all display transitions are complete. Receives the player object as
// its only argument.
onDone: shadowbox.K,
// A hook function that is called when opening.
onOpen: shadowbox.K,
// A hook function that is called when a player is ready to be
// displayed. Receives the player object as its only argument.
onShow: shadowbox.K,
// Background color for the overlay.
overlayColor: "black",
// Opacity for the overlay.
overlayOpacity: 0.5,
// The index in the current gallery at which to start when first opening.
startIndex: 0
};
/**
* A map of file extensions to the player class that should be used to play
* files with that extension.
*/
shadowbox.players = {};
/**
* Registers the given player class to be used with the given file
* extensions.
*
* shadowbox.registerPlayer(shadowbox.VideoPlayer, "mov");
* shadowbox.registerPlayer(shadowbox.PhotoPlayer, [ "jpg", "jpeg" ]);
*/
shadowbox.registerPlayer = function (playerClass, extensions) {
extensions = extensions || [];
if (!isArray(extensions))
extensions = [ extensions ];
forEach(extensions, function (extension) {
shadowbox.players[extension] = playerClass;
});
};
// Cache references to oft-used DOM elements.
var containerElement, overlayElement, wrapperElement, bodyElement, contentElement, coverElement;
/**
* Appends Shadowbox to the DOM and initializes DOM references.
*/
function initialize() {
if (containerElement)
return; // Don't initialize twice!
// The Shadowbox markup:
//
// <div id="shadowbox">
// <div id="sb-overlay"></div>
// <div id="sb-wrapper">
// <div id="sb-body">
// <div id="sb-content"></div>
// <div id="sb-cover"></div>
// </div>
// <div id="sb-close"></div>
// <div id="sb-next"></div>
// <div id="sb-prev"></div>
// </div>
// </div>
containerElement = makeDom("div", { id: "shadowbox" });
overlayElement = makeDom("div", { id: "sb-overlay" });
wrapperElement = makeDom("div", { id: "sb-wrapper" });
bodyElement = makeDom("div", { id: "sb-body" });
contentElement = makeDom("div", { id: "sb-content" });
coverElement = makeDom("div", { id: "sb-cover" });
var closeElement = makeDom("div", { id: "sb-close" });
var nextElement = makeDom("div", { id: "sb-next" });
var previousElement = makeDom("div", { id: "sb-prev" });
// Append #shadowbox to the DOM.
makeDom(document.body, [
makeDom(containerElement, [
overlayElement,
makeDom(wrapperElement, [
makeDom(bodyElement, [ contentElement, coverElement ]),
closeElement,
nextElement,
previousElement
])
])
]);
// Use an absolutely positioned container in browsers that don't
// support fixed positioning.
if (!supportsFixed)
setStyle(containerElement, "position", "absolute");
// Setup a click listener on the overlay to close Shadowbox.
addEvent(overlayElement, "click", shadowbox.close);
// Setup callbacks on navigation elements.
addEvent(closeElement, "click", cancel(shadowbox.close));
addEvent(nextElement, "click", cancel(shadowbox.showNext));
addEvent(previousElement, "click", cancel(shadowbox.showPrevious));
}
var currentIndex = -1,
currentGallery,
currentPlayer,
currentOptions;
/**
* Opens an object (or an array of objects) in Shadowbox. Takes options as
* the second argument.
*
* shadowbox("myphoto.jpg");
* shadowbox([ "myphoto1.jpg", "myphoto2.jpg" ]);
* shadowbox([ "myphoto1.jpg", "myphoto2.jpg" ], {
* animate: false,
* overlayColor: "white",
* overlayOpacity: 0.8
* });
*
* Options may be any of shadowbox.options. Returns the number of objects
* that were able to be opened.
*/
function shadowbox(objects, options) {
if (typeof options === 'number')
options = { startIndex: options };
if (!isArray(objects))
objects = [ objects ];
currentOptions = mergeProperties({}, shadowbox.options);
if (options)
mergeProperties(currentOptions, options);
currentGallery = [];
// Normalize into player objects and append them to the gallery.
var startIndex = currentOptions.startIndex;
forEach(objects, function (object, index) {
var player = shadowbox.makePlayer(object);
if (player) {
currentGallery.push(player);
} else {
if (index < startIndex) {
startIndex -= 1;
} else if (index === startIndex) {
startIndex = 0;
}
}
});
// Display the first item in the gallery, if there's anything left.
if (currentGallery.length > 0) {
if (currentIndex == -1) {
initialize();
if (isFunction(currentOptions.onOpen))
currentOptions.onOpen();
setStyle(containerElement, "display", "block");
setContainerPosition();
setContainerSize();
toggleTroubleElements(0);
setStyle(overlayElement, "backgroundColor", currentOptions.overlayColor);
setStyle(overlayElement, "opacity", 0);
setStyle(containerElement, "visibility", "visible");
animateStyle(overlayElement, "opacity", currentOptions.overlayOpacity, 0.35, function () {
setWrapperSize({ width: 340, height: 200 });
setStyle(wrapperElement, "visibility", "visible");
shadowbox.show(startIndex);
});
} else {
shadowbox.show(startIndex);
}
}
return currentGallery.length;
}
// Alias.
shadowbox.open = shadowbox;
/**
* Displays the gallery item at the given index in Shadowbox. Assumes that
* Shadowbox is already initialized and open.
*/
shadowbox.show = function (index) {
// Guard against invalid indices and no-ops.
if (index < 0 || !currentGallery[index] || currentIndex === index)
return;
toggleControls(0);
toggleWindowHandlers(0);
toggleMouseMoveHandler(0);
toggleKeyDownHandler(0);
setStyle(coverElement, "display", "block");
setStyle(coverElement, "opacity", 1);
if (currentPlayer)
currentPlayer.remove();
currentIndex = index;
currentPlayer = currentGallery[currentIndex];
function playerIsReady() {
return !currentPlayer || currentPlayer.isReady !== false;
}
waitUntil(playerIsReady, function () {
if (!currentPlayer)
return; // Shadowbox was closed.
if (isFunction(currentOptions.onShow))
currentOptions.onShow(currentPlayer);
var size = getWrapperSize();
var fromWidth = parseInt(getStyle(wrapperElement, "width")) || 0,
fromHeight = parseInt(getStyle(wrapperElement, "height")) || 0,
toWidth = size.width,
toHeight = size.height,
changeWidth = toWidth - fromWidth,
changeHeight = toHeight - fromHeight;
function frameHandler(value) {
if (!currentPlayer)
return false; // Shadowbox was closed, cancel the animation.
setWrapperSize({
width: fromWidth + (changeWidth * value),
height: fromHeight + (changeHeight * value)
});
}
// Open to the correct dimensions. Use the low-level animation
// primitive to make this transition as smooth as possible.
animate(0, 1, 0.5, frameHandler, function () {
if (currentPlayer) {
currentPlayer.injectInto(contentElement);
if (currentPlayer.fadeCover) {
animateStyle(coverElement, "opacity", 0, 0.5, finishShow);
} else {
finishShow();
}
}
});
});
};
function finishShow() {
if (currentPlayer) {
setStyle(coverElement, "display", "none");
toggleWindowHandlers(1);
toggleMouseMoveHandler(1);
toggleKeyDownHandler(1);
if (isFunction(currentOptions.onDone))
currentOptions.onDone(currentPlayer);
}
}
/**
* Opens the previous item in the gallery.
*/
shadowbox.showPrevious = function () {
shadowbox.show(getPreviousIndex());
};
/**
* Gets the index of the previous item in the gallery, -1 if there is none.
*/
function getPreviousIndex() {
if (currentIndex === 0)
return currentOptions.continuous ? (currentGallery.length - 1) : -1;
return currentIndex - 1;
}
/**
* Opens the next item in the gallery.
*/
shadowbox.showNext = function () {
shadowbox.show(getNextIndex());
};
/**
* Gets the index of the next item in the gallery, -1 if there is none.
*/
function getNextIndex() {
if (currentIndex === currentGallery.length - 1)
return (currentOptions.continuous && currentIndex !== 0) ? 0 : -1;
return currentIndex + 1;
}
/**
* Closes Shadowbox immediately.
*/
shadowbox.close = function () {
if (shadowbox.isOpen()) {
currentIndex = -1;
currentPlayer = null;
setStyle(wrapperElement, "visibility", "hidden");
setStyle(coverElement, "opacity", 1);
contentElement.innerHTML = "";
toggleControls(0);
toggleWindowHandlers(0);
toggleMouseMoveHandler(0);
toggleKeyDownHandler(0);
animateStyle(overlayElement, "opacity", 0, 0.5, function () {
setStyle(containerElement, "visibility", "hidden");
setStyle(containerElement, "display", "none");
toggleTroubleElements(1);
if (isFunction(currentOptions.onClose))
currentOptions.onClose();
});
}
};
/**
* Returns true if Shadowbox is currently open.
*/
shadowbox.isOpen = function () {
return currentIndex !== -1;
};
/**
* Gets the current player instance.
*/
shadowbox.getPlayer = function () {
return currentPlayer;
};
/**
* Gets the size that should be used for the wrapper element. Should be
* called when Shadowbox is open and has a player that is ready.
*/
function getWrapperSize() {
var margin = Math.max(currentOptions.margin, 20); // Minimum 20px margin.
return constrainSize(currentPlayer.width, currentPlayer.height,
overlayElement.offsetWidth, overlayElement.offsetHeight, margin);
}
/**
* Sets the size and position of the wrapper.
*/
function setWrapperSize(size) {
setStyle(wrapperElement, "width", size.width + "px");
setStyle(wrapperElement, "marginLeft", (-size.width / 2) + "px");
setStyle(wrapperElement, "height", size.height + "px");
setStyle(wrapperElement, "marginTop", (-size.height / 2) + "px");
}
/**
* Scales the given width and height to be within the bounds of the given
* maximum width and height, allowing for margin. Returns an array of the
* constrained [width, height].
*/
function constrainSize(width, height, maxWidth, maxHeight, margin) {
var originalWidth = width, originalHeight = height;
// Constrain width/height to max.
var marginWidth = 2 * margin;
if (width + marginWidth > maxWidth)
width = maxWidth - marginWidth;
var marginHeight = 2 * margin;
if (height + marginHeight > maxHeight)
height = maxHeight - marginHeight;
var changeWidth = (originalWidth - width) / originalWidth;
var changeHeight = (originalHeight - height) / originalHeight;
// Adjust width/height if oversized.
if (changeWidth > 0 || changeHeight > 0) {
// Preserve original aspect ratio according to greatest change.
if (changeWidth > changeHeight) {
height = Math.round((originalHeight / originalWidth) * width);
} else if (changeHeight > changeWidth) {
width = Math.round((originalWidth / originalHeight) * height);
}
}
return { width: width, height: height };
}
/**
* Sets the size of the container element to the size of the window.
*/
function setContainerSize() {
setStyle(containerElement, "width", documentElement.clientWidth + "px");
setStyle(containerElement, "height", documentElement.clientHeight + "px");
if (currentPlayer)
setWrapperSize(getWrapperSize());
}
/**
* Sets the position of the container element to the top left corner of
* the window. Necessary when using absolute positioning instead of fixed.
*/
function setContainerPosition() {
setStyle(containerElement, "left", documentElement.scrollLeft + "px");
setStyle(containerElement, "top", documentElement.scrollTop + "px");
}
var troubleElementTagNames = [ "select", "object", "embed", "canvas" ];
var troubleVisibilityCache = [];
/**
* Toggles the visibility of elements that are troublesome for overlays.
*/
function toggleTroubleElements(on) {
if (on) {
forEach(troubleVisibilityCache, function (item) {
setStyle(item.element, "visibility", item.visibility || "");
});
} else {
troubleVisibilityCache = [];
forEach(troubleElementTagNames, function (tagName) {
forEach(document.getElementsByTagName(tagName), function (element) {
troubleVisibilityCache.push({
element: element,
visibility: getStyle(element, "visibility")
});
setStyle(element, "visibility", "hidden");
});
});
}
}
/**
* Creates a new player object based on the properties of the given object.
* Valid properties include:
*
* - url The URL of the content to display
* - width (optional) The width of the content
* - height (optional) The height of the content
* - playerClass (optional) The player class to use to play the content.
* Can be guessed in most cases from the URL
* - encodings (video only) Encoding name/URL pairs of alternate URL's
* for the video. Possible encoding names are "h264", "ogg"
* "webm", and "flv"
* - posterUrl (video only) The URL to a poster image of the video
* - flashParams (flash only) Name/value pairs of <param>'s to use for
* the Flash <object>
* - flashVars (flash only) Name/value pairs of variables to pass to
* the Flash object as variables
*
* If a string is given, it will be used as the value of the URL. If a DOM
* element is given, it should have an href property (i.e. either an <a> or
* an <area> element) which will be used as the URL. It may also contain
* a data-shadowbox attribute that has any of the other options formatted
* in a JSON string.
*
* If no player is specified, it will be guessed using the registered player
* for the URL's file extension (see shadowbox.registerPlayer).
*
* Returns null if no player is able to be created, or this browser does
* not have proper support for that content.
*/
shadowbox.makePlayer = function (object) {
if (typeof object === "string") {
object = { url: object };
} else if (isElement(object) && object.href) {
// The object is a DOM element. Should be an <a> or <area>. The
// data-shadowbox attribute may contain a string specifying
// options for the player object (see parseData).
var data = object.getAttribute("data-shadowbox");
object = { url: object.href };
if (data)
mergeProperties(object, parseData(data));
}
if (object && typeof object.url === "string") {
var playerClass;
if (object.playerClass) {
playerClass = object.playerClass;
} else {
// Guess the player class using the URL's file extension.
var match = object.url.match(/\.([0-9a-z]+)(\?.*)?$/i);
if (match) {
var extension = match[1].toLowerCase();
playerClass = shadowbox.players[extension];
}
}
playerClass = playerClass || FramePlayer;
var player = new playerClass(object, "sb-player-" + String(shadowbox.guid++));
if (player.isSupported())
return player;
}
return null;
};
// Toggles the visibility of clickable controls.
function toggleControls(on) {
var name = "";
if (on) {
name += "active";
if (getNextIndex() !== -1)
name += " has-next";
if (getPreviousIndex() !== -1)
name += " has-prev";
}
containerElement.className = name;
}
var resizeTimer, scrollTimer, mouseMoveTimer;
// Toggles window resize and scroll event handlers.
function toggleWindowHandlers(on) {
var addOrRemoveEvent;
if (on) {
addOrRemoveEvent = addEvent;
} else {
addOrRemoveEvent = removeEvent;
// Clear cached timers.
if (resizeTimer) {
clearTimeout(resizeTimer);
resizeTimer = null;
}
if (scrollTimer) {
clearTimeout(scrollTimer);
scrollTimer = null;
}
}
addOrRemoveEvent(window, "resize", handleWindowResize);
if (!supportsFixed)
addOrRemoveEvent(window, "scroll", handleWindowScroll);
}
// Updates the size of the container when the window size changes.
function handleWindowResize() {
if (resizeTimer) {
clearTimeout(resizeTimer);
resizeTimer = null;
}
resizeTimer = setTimeout(function () {
resizeTimer = null;
setContainerSize();
}, 10);
}
// Updates the position of the container when the window scrolls.
function handleWindowScroll() {
if (scrollTimer) {
clearTimeout(scrollTimer);
scrollTimer = null;
}
scrollTimer = setTimeout(function () {
scrollTimer = null;
setContainerPosition();
}, 10);
}
// Toggles document mouse move handler on/off.
function toggleMouseMoveHandler(on) {
if (supportsTouch) {
toggleControls(on);
return;
}
var addOrRemoveEvent;
if (on) {
addOrRemoveEvent = addEvent;
} else {
addOrRemoveEvent = removeEvent;
// Clear cached timers.
if (mouseMoveTimer) {
clearTimeout(mouseMoveTimer);
mouseMoveTimer = null;
}
}
addOrRemoveEvent(document, "mousemove", handleMouseMove);
}
var lastMouseX, lastMouseY;
// Shows clickable controls when the mouse moves.
function handleMouseMove(event) {
// Ignore consecutive mousemove events from the same location.
if (lastMouseX !== event.clientX || lastMouseY !== event.clientY) {
lastMouseX = event.clientX;
lastMouseY = event.clientY;
if (mouseMoveTimer) {
clearTimeout(mouseMoveTimer);
mouseMoveTimer = null;
} else {
toggleControls(1);
}
mouseMoveTimer = setTimeout(function () {
mouseMoveTimer = null;
toggleControls(0);
}, 1500);
}
}
function toggleKeyDownHandler(on) {
if (currentOptions.enableKeys)
(on ? addEvent : removeEvent)(document, "keydown", handleDocumentKeyDown);
}
var KEY_ESCAPE = 27;
var KEY_SPACE = 32;
var KEY_LEFT = 37;
var KEY_RIGHT = 39;
var KEY_Q = 81;
var KEY_X = 88;
function handleDocumentKeyDown(event) {
if (eventHasModifierKey(event))
return;
var keycode = event.which || event.keyCode;
switch (keycode) {
case KEY_ESCAPE:
case KEY_Q:
case KEY_X:
event.preventDefault();
shadowbox.close();
break;
case KEY_LEFT:
event.preventDefault();
shadowbox.showPrevious();
break;
case KEY_RIGHT:
event.preventDefault();
shadowbox.showNext();
break;
case KEY_SPACE:
if (currentPlayer && isFunction(currentPlayer.togglePlay)) {
event.preventDefault();
currentPlayer.togglePlay();
}
break;
}
}
function eventHasModifierKey(event) {
return event.ctrlKey || event.metaKey;
}
function toggleClickHandler(on) {
(on ? addEvent : removeEvent)(document, 'click', handleDocumentClick);
}
function handleDocumentClick(event) {
var target = event.target;
if (isElement(target)) {
var matcher = /^(?:shadow|light)box(?:\[(\w+)\])?$/i,
links = [],
index = 0,
match;
// Find an ancestor node with rel="shadowbox" attribute.
while (target) {
match = (target.rel || "").match(matcher);
if (match) {
var galleryName = match[1];
// Look for other <a> elements in the document that also have
// rel="shadowbox" attribute with the same gallery.
if (galleryName) {
var galleryMatcher = new RegExp("^(shadow|light)box\\[" + galleryName + "\\]$", "i");
forEach(document.getElementsByTagName('a'), function (link) {
if (link.rel && galleryMatcher.test(link.rel)) {
if (link == target)
index = links.length;
links.push(link);
}
});
} else {
links.push(target);
}
break;
}
target = target.parentNode;
}
// Good for debugging.
// event.preventDefault();
if (links.length > 0 && shadowbox.open(links, index) > 0)
event.preventDefault(); // Prevent the browser from following the link.
}
}
//// PLAYERS ////
shadowbox.FramePlayer = FramePlayer;
/**
* A player that displays its content inside an <iframe>. This is the default
* player for Shadowbox that is used when no other player is suitable for a
* piece of content.
*/
function FramePlayer(object, id) {
this.url = object.url;
this.width = object.width ? parseInt(object.width, 10) : documentElement.clientWidth;
this.height = object.height ? parseInt(object.height, 10) : documentElement.clientHeight;
this.id = id;
// Preload the iframe so it's ready when needed.
this.isReady = false;
this._preload();
}
mergeProperties(FramePlayer.prototype, {
_preload: function () {
var iframe = makeDom("iframe");
iframe.id = this.id;
iframe.name = this.id;
iframe.width = "0px";
iframe.height = "0px";
iframe.frameBorder = "0";
iframe.marginWidth = "0";
iframe.marginHeight = "0";
iframe.scrolling = "auto";
iframe.allowTransparency = "true";
iframe.src = this.url;
var self = this;
if (iframe.attachEvent) {
iframe.attachEvent("onload", function () {
self.isReady = true;
});
} else {
iframe.onload = function () {
self.isReady = true;
};
}
// Starts the actual loading of the iframe.
makeDom(document.body, iframe);
this.element = iframe;
},
/**
* Returns true if this player is supported on this browser.
*/
isSupported: function () {
return true;
},
/**
* Inserts this object as the only child of the given DOM element.
*/
injectInto: function (element) {
removeChildren(element);
var iframe = this.element;
iframe.style.visibility = "hidden";
iframe.width = "100%";
iframe.height = "100%";
element.appendChild(iframe);
iframe.style.visibility = "";
},
/**
* Removes this object from the DOM.
*/
remove: function () {
if (this.element) {
removeElement(this.element);
delete this.element;
// Needed for Firefox, IE <= 8 throws error.
try {
delete window.frames[this.id];
} catch (error) {}
}
}
});
shadowbox.PhotoPlayer = PhotoPlayer;
/**
* A player that is used for displaying images.
*/
function PhotoPlayer(object, id) {
this.url = object.url;
this.width = parseInt(object.width, 10);
this.height = parseInt(object.height, 10);
this.id = id;
// Preload the image so it's ready when needed.
this.isReady = false;
this._preload();
}
mergeProperties(PhotoPlayer.prototype, {
fadeCover: true,
_preload: function () {
var preloader = new Image;
var self = this;
preloader.onload = function () {
// Width and height default to image dimensions.
self.width = self.width || preloader.width;
self.height = self.height || preloader.height;
// Ready to go.
self.isReady = true;
// Clean up to prevent memory leak in IE.
preloader.onload = preloader = null;
};
// Start loading the image.
preloader.src = this.url;
},
/**
* Returns true if this player is supported on this browser.
*/
isSupported: function () {
return true;
},
/**
* Inserts this object as the only child of the given DOM element.
*/
injectInto: function (element) {
element.innerHTML = '<img id="' + this.id + '" src="' + this.url + '" width="100%" height="100%">';
this.element = element.firstChild;
},
/**
* Removes this object from the DOM.
*/
remove: function () {
if (this.element) {
removeElement(this.element);
delete this.element;
}
}
});
//// JAVASCRIPT UTILITIES ////
function isFunction(object) {
return typeof object === "function";
}
function isArray(object) {
if (isFunction(Array.isArray))
return Array.isArray(object);
return Object.prototype.toString.call(object) === "[object Array]";
}
/**
* Calls the given callback function for each element in the given object,
* which must be an array-like object. Return false from any callback to
* stop execution.
*/
function forEach(object, callback) {
var length = object.length, index = 0, item;
for (item = object[0]; index < length && callback.call(object, item, index, object) !== false; item = object[++index]) {}
}
/**
* Merges all properties of extension into the given object.
*/
function mergeProperties(object, extension) {
for (var property in extension) {
if (extension.hasOwnProperty(property))
object[property] = extension[property];
}
return object;
}
/**
* Gets the current time in milliseconds.
*/