Projet

Général

Profil

Paste
Télécharger (27,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / libraries / colorbox-version / jquery.colorbox.js @ 41cc1b08

1
/*!
2
        Colorbox v1.4.31 - 2013-09-25
3
        jQuery lightbox and modal window plugin
4
        (c) 2013 Jack Moore - http://www.jacklmoore.com/colorbox
5
        license: http://www.opensource.org/licenses/mit-license.php
6
*/
7
(function ($, document, window) {
8
        var
9
        // Default settings object.
10
        // See http://jacklmoore.com/colorbox for details.
11
        defaults = {
12
                // data sources
13
                html: false,
14
                photo: false,
15
                iframe: false,
16
                inline: false,
17

    
18
                // behavior and appearance
19
                transition: "elastic",
20
                speed: 300,
21
                fadeOut: 300,
22
                width: false,
23
                initialWidth: "600",
24
                innerWidth: false,
25
                maxWidth: false,
26
                height: false,
27
                initialHeight: "450",
28
                innerHeight: false,
29
                maxHeight: false,
30
                scalePhotos: true,
31
                scrolling: true,
32
                href: false,
33
                title: false,
34
                rel: false,
35
                opacity: 0.9,
36
                preloading: true,
37
                className: false,
38
                overlayClose: true,
39
                escKey: true,
40
                arrowKey: true,
41
                top: false,
42
                bottom: false,
43
                left: false,
44
                right: false,
45
                fixed: false,
46
                data: undefined,
47
                closeButton: true,
48
                fastIframe: true,
49
                open: false,
50
                reposition: true,
51
                loop: true,
52
                slideshow: false,
53
                slideshowAuto: true,
54
                slideshowSpeed: 2500,
55
                slideshowStart: "start slideshow",
56
                slideshowStop: "stop slideshow",
57
                photoRegex: /\.(gif|png|jp(e|g|eg)|bmp|ico|webp)((#|\?).*)?$/i,
58

    
59
                // alternate image paths for high-res displays
60
                retinaImage: false,
61
                retinaUrl: false,
62
                retinaSuffix: '@2x.$1',
63

    
64
                // internationalization
65
                current: "image {current} of {total}",
66
                previous: "previous",
67
                next: "next",
68
                close: "close",
69
                xhrError: "This content failed to load.",
70
                imgError: "This image failed to load.",
71

    
72
                // accessbility
73
                returnFocus: true,
74
                trapFocus: true,
75

    
76
                // callbacks
77
                onOpen: false,
78
                onLoad: false,
79
                onComplete: false,
80
                onCleanup: false,
81
                onClosed: false
82
        },
83
        
84
        // Abstracting the HTML and event identifiers for easy rebranding
85
        colorbox = 'colorbox',
86
        prefix = 'cbox',
87
        boxElement = prefix + 'Element',
88
        
89
        // Events
90
        event_open = prefix + '_open',
91
        event_load = prefix + '_load',
92
        event_complete = prefix + '_complete',
93
        event_cleanup = prefix + '_cleanup',
94
        event_closed = prefix + '_closed',
95
        event_purge = prefix + '_purge',
96

    
97
        // Cached jQuery Object Variables
98
        $overlay,
99
        $box,
100
        $wrap,
101
        $content,
102
        $topBorder,
103
        $leftBorder,
104
        $rightBorder,
105
        $bottomBorder,
106
        $related,
107
        $window,
108
        $loaded,
109
        $loadingBay,
110
        $loadingOverlay,
111
        $title,
112
        $current,
113
        $slideshow,
114
        $next,
115
        $prev,
116
        $close,
117
        $groupControls,
118
        $events = $('<a/>'),
119
        
120
        // Variables for cached values or use across multiple functions
121
        settings,
122
        interfaceHeight,
123
        interfaceWidth,
124
        loadedHeight,
125
        loadedWidth,
126
        element,
127
        index,
128
        photo,
129
        open,
130
        active,
131
        closing,
132
        loadingTimer,
133
        publicMethod,
134
        div = "div",
135
        className,
136
        requests = 0,
137
        previousCSS = {},
138
        init;
139

    
140
        // ****************
141
        // HELPER FUNCTIONS
142
        // ****************
143
        
144
        // Convenience function for creating new jQuery objects
145
        function $tag(tag, id, css) {
146
                var element = document.createElement(tag);
147

    
148
                if (id) {
149
                        element.id = prefix + id;
150
                }
151

    
152
                if (css) {
153
                        element.style.cssText = css;
154
                }
155

    
156
                return $(element);
157
        }
158
        
159
        // Get the window height using innerHeight when available to avoid an issue with iOS
160
        // http://bugs.jquery.com/ticket/6724
161
        function winheight() {
162
                return window.innerHeight ? window.innerHeight : $(window).height();
163
        }
164

    
165
        // Determine the next and previous members in a group.
166
        function getIndex(increment) {
167
                var
168
                max = $related.length,
169
                newIndex = (index + increment) % max;
170
                
171
                return (newIndex < 0) ? max + newIndex : newIndex;
172
        }
173

    
174
        // Convert '%' and 'px' values to integers
175
        function setSize(size, dimension) {
176
                return Math.round((/%/.test(size) ? ((dimension === 'x' ? $window.width() : winheight()) / 100) : 1) * parseInt(size, 10));
177
        }
178
        
179
        // Checks an href to see if it is a photo.
180
        // There is a force photo option (photo: true) for hrefs that cannot be matched by the regex.
181
        function isImage(settings, url) {
182
                return settings.photo || settings.photoRegex.test(url);
183
        }
184

    
185
        function retinaUrl(settings, url) {
186
                return settings.retinaUrl && window.devicePixelRatio > 1 ? url.replace(settings.photoRegex, settings.retinaSuffix) : url;
187
        }
188

    
189
        function trapFocus(e) {
190
                if ('contains' in $box[0] && !$box[0].contains(e.target)) {
191
                        e.stopPropagation();
192
                        $box.focus();
193
                }
194
        }
195

    
196
        // Assigns function results to their respective properties
197
        function makeSettings() {
198
                var i,
199
                        data = $.data(element, colorbox);
200
                
201
                if (data == null) {
202
                        settings = $.extend({}, defaults);
203
                        if (console && console.log) {
204
                                console.log('Error: cboxElement missing settings object');
205
                        }
206
                } else {
207
                        settings = $.extend({}, data);
208
                }
209
                
210
                for (i in settings) {
211
                        if ($.isFunction(settings[i]) && i.slice(0, 2) !== 'on') { // checks to make sure the function isn't one of the callbacks, they will be handled at the appropriate time.
212
                                settings[i] = settings[i].call(element);
213
                        }
214
                }
215
                
216
                settings.rel = settings.rel || element.rel || $(element).data('rel') || 'nofollow';
217
                settings.href = settings.href || $(element).attr('href');
218
                settings.title = settings.title || element.title;
219
                
220
                if (typeof settings.href === "string") {
221
                        settings.href = $.trim(settings.href);
222
                }
223
        }
224

    
225
        function trigger(event, callback) {
226
                // for external use
227
                $(document).trigger(event);
228

    
229
                // for internal use
230
                $events.trigger(event);
231

    
232
                if ($.isFunction(callback)) {
233
                        callback.call(element);
234
                }
235
        }
236

    
237

    
238
        var slideshow = (function(){
239
                var active,
240
                        className = prefix + "Slideshow_",
241
                        click = "click." + prefix,
242
                        timeOut;
243

    
244
                function clear () {
245
                        clearTimeout(timeOut);
246
                }
247

    
248
                function set() {
249
                        if (settings.loop || $related[index + 1]) {
250
                                clear();
251
                                timeOut = setTimeout(publicMethod.next, settings.slideshowSpeed);
252
                        }
253
                }
254

    
255
                function start() {
256
                        $slideshow
257
                                .html(settings.slideshowStop)
258
                                .unbind(click)
259
                                .one(click, stop);
260

    
261
                        $events
262
                                .bind(event_complete, set)
263
                                .bind(event_load, clear);
264

    
265
                        $box.removeClass(className + "off").addClass(className + "on");
266
                }
267

    
268
                function stop() {
269
                        clear();
270
                        
271
                        $events
272
                                .unbind(event_complete, set)
273
                                .unbind(event_load, clear);
274

    
275
                        $slideshow
276
                                .html(settings.slideshowStart)
277
                                .unbind(click)
278
                                .one(click, function () {
279
                                        publicMethod.next();
280
                                        start();
281
                                });
282

    
283
                        $box.removeClass(className + "on").addClass(className + "off");
284
                }
285

    
286
                function reset() {
287
                        active = false;
288
                        $slideshow.hide();
289
                        clear();
290
                        $events
291
                                .unbind(event_complete, set)
292
                                .unbind(event_load, clear);
293
                        $box.removeClass(className + "off " + className + "on");
294
                }
295

    
296
                return function(){
297
                        if (active) {
298
                                if (!settings.slideshow) {
299
                                        $events.unbind(event_cleanup, reset);
300
                                        reset();
301
                                }
302
                        } else {
303
                                if (settings.slideshow && $related[1]) {
304
                                        active = true;
305
                                        $events.one(event_cleanup, reset);
306
                                        if (settings.slideshowAuto) {
307
                                                start();
308
                                        } else {
309
                                                stop();
310
                                        }
311
                                        $slideshow.show();
312
                                }
313
                        }
314
                };
315

    
316
        }());
317

    
318

    
319
        function launch(target) {
320
                if (!closing) {
321
                        
322
                        element = target;
323
                        
324
                        makeSettings();
325
                        
326
                        $related = $(element);
327
                        
328
                        index = 0;
329
                        
330
                        if (settings.rel !== 'nofollow') {
331
                                $related = $('.' + boxElement).filter(function () {
332
                                        var data = $.data(this, colorbox),
333
                                                relRelated;
334

    
335
                                        if (data) {
336
                                                relRelated =  $(this).data('rel') || data.rel || this.rel;
337
                                        }
338
                                        
339
                                        return (relRelated === settings.rel);
340
                                });
341
                                index = $related.index(element);
342
                                
343
                                // Check direct calls to Colorbox.
344
                                if (index === -1) {
345
                                        $related = $related.add(element);
346
                                        index = $related.length - 1;
347
                                }
348
                        }
349
                        
350
                        $overlay.css({
351
                                opacity: parseFloat(settings.opacity),
352
                                cursor: settings.overlayClose ? "pointer" : "auto",
353
                                visibility: 'visible'
354
                        }).show();
355
                        
356

    
357
                        if (className) {
358
                                $box.add($overlay).removeClass(className);
359
                        }
360
                        if (settings.className) {
361
                                $box.add($overlay).addClass(settings.className);
362
                        }
363
                        className = settings.className;
364

    
365
                        if (settings.closeButton) {
366
                                $close.html(settings.close).appendTo($content);
367
                        } else {
368
                                $close.appendTo('<div/>');
369
                        }
370

    
371
                        if (!open) {
372
                                open = active = true; // Prevents the page-change action from queuing up if the visitor holds down the left or right keys.
373
                                
374
                                // Show colorbox so the sizes can be calculated in older versions of jQuery
375
                                $box.css({visibility:'hidden', display:'block'});
376
                                
377
                                $loaded = $tag(div, 'LoadedContent', 'width:0; height:0; overflow:hidden');
378
                                $content.css({width:'', height:''}).append($loaded);
379

    
380
                                // Cache values needed for size calculations
381
                                interfaceHeight = $topBorder.height() + $bottomBorder.height() + $content.outerHeight(true) - $content.height();
382
                                interfaceWidth = $leftBorder.width() + $rightBorder.width() + $content.outerWidth(true) - $content.width();
383
                                loadedHeight = $loaded.outerHeight(true);
384
                                loadedWidth = $loaded.outerWidth(true);
385

    
386
                                // Opens inital empty Colorbox prior to content being loaded.
387
                                settings.w = setSize(settings.initialWidth, 'x');
388
                                settings.h = setSize(settings.initialHeight, 'y');
389
                                $loaded.css({width:'', height:settings.h});
390
                                publicMethod.position();
391

    
392
                                trigger(event_open, settings.onOpen);
393
                                
394
                                $groupControls.add($title).hide();
395

    
396
                                $box.focus();
397
                                
398
                                if (settings.trapFocus) {
399
                                        // Confine focus to the modal
400
                                        // Uses event capturing that is not supported in IE8-
401
                                        if (document.addEventListener) {
402

    
403
                                                document.addEventListener('focus', trapFocus, true);
404
                                                
405
                                                $events.one(event_closed, function () {
406
                                                        document.removeEventListener('focus', trapFocus, true);
407
                                                });
408
                                        }
409
                                }
410

    
411
                                // Return focus on closing
412
                                if (settings.returnFocus) {
413
                                        $events.one(event_closed, function () {
414
                                                $(element).focus();
415
                                        });
416
                                }
417
                        }
418
                        load();
419
                }
420
        }
421

    
422
        // Colorbox's markup needs to be added to the DOM prior to being called
423
        // so that the browser will go ahead and load the CSS background images.
424
        function appendHTML() {
425
                if (!$box && document.body) {
426
                        init = false;
427
                        $window = $(window);
428
                        $box = $tag(div).attr({
429
                                id: colorbox,
430
                                'class': $.support.opacity === false ? prefix + 'IE' : '', // class for optional IE8 & lower targeted CSS.
431
                                role: 'dialog',
432
                                tabindex: '-1'
433
                        }).hide();
434
                        $overlay = $tag(div, "Overlay").hide();
435
                        $loadingOverlay = $([$tag(div, "LoadingOverlay")[0],$tag(div, "LoadingGraphic")[0]]);
436
                        $wrap = $tag(div, "Wrapper");
437
                        $content = $tag(div, "Content").append(
438
                                $title = $tag(div, "Title"),
439
                                $current = $tag(div, "Current"),
440
                                $prev = $('<button type="button"/>').attr({id:prefix+'Previous'}),
441
                                $next = $('<button type="button"/>').attr({id:prefix+'Next'}),
442
                                $slideshow = $tag('button', "Slideshow"),
443
                                $loadingOverlay
444
                        );
445

    
446
                        $close = $('<button type="button"/>').attr({id:prefix+'Close'});
447
                        
448
                        $wrap.append( // The 3x3 Grid that makes up Colorbox
449
                                $tag(div).append(
450
                                        $tag(div, "TopLeft"),
451
                                        $topBorder = $tag(div, "TopCenter"),
452
                                        $tag(div, "TopRight")
453
                                ),
454
                                $tag(div, false, 'clear:left').append(
455
                                        $leftBorder = $tag(div, "MiddleLeft"),
456
                                        $content,
457
                                        $rightBorder = $tag(div, "MiddleRight")
458
                                ),
459
                                $tag(div, false, 'clear:left').append(
460
                                        $tag(div, "BottomLeft"),
461
                                        $bottomBorder = $tag(div, "BottomCenter"),
462
                                        $tag(div, "BottomRight")
463
                                )
464
                        ).find('div div').css({'float': 'left'});
465
                        
466
                        $loadingBay = $tag(div, false, 'position:absolute; width:9999px; visibility:hidden; display:none');
467
                        
468
                        $groupControls = $next.add($prev).add($current).add($slideshow);
469

    
470
                        $(document.body).append($overlay, $box.append($wrap, $loadingBay));
471
                }
472
        }
473

    
474
        // Add Colorbox's event bindings
475
        function addBindings() {
476
                function clickHandler(e) {
477
                        // ignore non-left-mouse-clicks and clicks modified with ctrl / command, shift, or alt.
478
                        // See: http://jacklmoore.com/notes/click-events/
479
                        if (!(e.which > 1 || e.shiftKey || e.altKey || e.metaKey || e.ctrlKey)) {
480
                                e.preventDefault();
481
                                launch(this);
482
                        }
483
                }
484

    
485
                if ($box) {
486
                        if (!init) {
487
                                init = true;
488

    
489
                                // Anonymous functions here keep the public method from being cached, thereby allowing them to be redefined on the fly.
490
                                $next.click(function () {
491
                                        publicMethod.next();
492
                                });
493
                                $prev.click(function () {
494
                                        publicMethod.prev();
495
                                });
496
                                $close.click(function () {
497
                                        publicMethod.close();
498
                                });
499
                                $overlay.click(function () {
500
                                        if (settings.overlayClose) {
501
                                                publicMethod.close();
502
                                        }
503
                                });
504
                                
505
                                // Key Bindings
506
                                $(document).bind('keydown.' + prefix, function (e) {
507
                                        var key = e.keyCode;
508
                                        if (open && settings.escKey && key === 27) {
509
                                                e.preventDefault();
510
                                                publicMethod.close();
511
                                        }
512
                                        if (open && settings.arrowKey && $related[1] && !e.altKey) {
513
                                                if (key === 37) {
514
                                                        e.preventDefault();
515
                                                        $prev.click();
516
                                                } else if (key === 39) {
517
                                                        e.preventDefault();
518
                                                        $next.click();
519
                                                }
520
                                        }
521
                                });
522

    
523
                                if ($.isFunction($.fn.on)) {
524
                                        // For jQuery 1.7+
525
                                        $(document).on('click.'+prefix, '.'+boxElement, clickHandler);
526
                                } else {
527
                                        // For jQuery 1.3.x -> 1.6.x
528
                                        // This code is never reached in jQuery 1.9, so do not contact me about 'live' being removed.
529
                                        // This is not here for jQuery 1.9, it's here for legacy users.
530
                                        $('.'+boxElement).live('click.'+prefix, clickHandler);
531
                                }
532
                        }
533
                        return true;
534
                }
535
                return false;
536
        }
537

    
538
        // Don't do anything if Colorbox already exists.
539
        if ($.colorbox) {
540
                return;
541
        }
542

    
543
        // Append the HTML when the DOM loads
544
        $(appendHTML);
545

    
546

    
547
        // ****************
548
        // PUBLIC FUNCTIONS
549
        // Usage format: $.colorbox.close();
550
        // Usage from within an iframe: parent.jQuery.colorbox.close();
551
        // ****************
552
        
553
        publicMethod = $.fn[colorbox] = $[colorbox] = function (options, callback) {
554
                var $this = this;
555
                
556
                options = options || {};
557
                
558
                appendHTML();
559

    
560
                if (addBindings()) {
561
                        if ($.isFunction($this)) { // assume a call to $.colorbox
562
                                $this = $('<a/>');
563
                                options.open = true;
564
                        } else if (!$this[0]) { // colorbox being applied to empty collection
565
                                return $this;
566
                        }
567
                        
568
                        if (callback) {
569
                                options.onComplete = callback;
570
                        }
571
                        
572
                        $this.each(function () {
573
                                $.data(this, colorbox, $.extend({}, $.data(this, colorbox) || defaults, options));
574
                        }).addClass(boxElement);
575
                        
576
                        if (($.isFunction(options.open) && options.open.call($this)) || options.open) {
577
                                launch($this[0]);
578
                        }
579
                }
580
                
581
                return $this;
582
        };
583

    
584
        publicMethod.position = function (speed, loadedCallback) {
585
                var
586
                css,
587
                top = 0,
588
                left = 0,
589
                offset = $box.offset(),
590
                scrollTop,
591
                scrollLeft;
592
                
593
                $window.unbind('resize.' + prefix);
594

    
595
                // remove the modal so that it doesn't influence the document width/height
596
                $box.css({top: -9e4, left: -9e4});
597

    
598
                scrollTop = $window.scrollTop();
599
                scrollLeft = $window.scrollLeft();
600

    
601
                if (settings.fixed) {
602
                        offset.top -= scrollTop;
603
                        offset.left -= scrollLeft;
604
                        $box.css({position: 'fixed'});
605
                } else {
606
                        top = scrollTop;
607
                        left = scrollLeft;
608
                        $box.css({position: 'absolute'});
609
                }
610

    
611
                // keeps the top and left positions within the browser's viewport.
612
                if (settings.right !== false) {
613
                        left += Math.max($window.width() - settings.w - loadedWidth - interfaceWidth - setSize(settings.right, 'x'), 0);
614
                } else if (settings.left !== false) {
615
                        left += setSize(settings.left, 'x');
616
                } else {
617
                        left += Math.round(Math.max($window.width() - settings.w - loadedWidth - interfaceWidth, 0) / 2);
618
                }
619
                
620
                if (settings.bottom !== false) {
621
                        top += Math.max(winheight() - settings.h - loadedHeight - interfaceHeight - setSize(settings.bottom, 'y'), 0);
622
                } else if (settings.top !== false) {
623
                        top += setSize(settings.top, 'y');
624
                } else {
625
                        top += Math.round(Math.max(winheight() - settings.h - loadedHeight - interfaceHeight, 0) / 2);
626
                }
627

    
628
                $box.css({top: offset.top, left: offset.left, visibility:'visible'});
629
                
630
                // this gives the wrapper plenty of breathing room so it's floated contents can move around smoothly,
631
                // but it has to be shrank down around the size of div#colorbox when it's done.  If not,
632
                // it can invoke an obscure IE bug when using iframes.
633
                $wrap[0].style.width = $wrap[0].style.height = "9999px";
634
                
635
                function modalDimensions() {
636
                        $topBorder[0].style.width = $bottomBorder[0].style.width = $content[0].style.width = (parseInt($box[0].style.width,10) - interfaceWidth)+'px';
637
                        $content[0].style.height = $leftBorder[0].style.height = $rightBorder[0].style.height = (parseInt($box[0].style.height,10) - interfaceHeight)+'px';
638
                }
639

    
640
                css = {width: settings.w + loadedWidth + interfaceWidth, height: settings.h + loadedHeight + interfaceHeight, top: top, left: left};
641

    
642
                // setting the speed to 0 if the content hasn't changed size or position
643
                if (speed) {
644
                        var tempSpeed = 0;
645
                        $.each(css, function(i){
646
                                if (css[i] !== previousCSS[i]) {
647
                                        tempSpeed = speed;
648
                                        return;
649
                                }
650
                        });
651
                        speed = tempSpeed;
652
                }
653

    
654
                previousCSS = css;
655

    
656
                if (!speed) {
657
                        $box.css(css);
658
                }
659

    
660
                $box.dequeue().animate(css, {
661
                        duration: speed || 0,
662
                        complete: function () {
663
                                modalDimensions();
664
                                
665
                                active = false;
666
                                
667
                                // shrink the wrapper down to exactly the size of colorbox to avoid a bug in IE's iframe implementation.
668
                                $wrap[0].style.width = (settings.w + loadedWidth + interfaceWidth) + "px";
669
                                $wrap[0].style.height = (settings.h + loadedHeight + interfaceHeight) + "px";
670
                                
671
                                if (settings.reposition) {
672
                                        setTimeout(function () {  // small delay before binding onresize due to an IE8 bug.
673
                                                $window.bind('resize.' + prefix, publicMethod.position);
674
                                        }, 1);
675
                                }
676

    
677
                                if (loadedCallback) {
678
                                        loadedCallback();
679
                                }
680
                        },
681
                        step: modalDimensions
682
                });
683
        };
684

    
685
        publicMethod.resize = function (options) {
686
                var scrolltop;
687
                
688
                if (open) {
689
                        options = options || {};
690
                        
691
                        if (options.width) {
692
                                settings.w = setSize(options.width, 'x') - loadedWidth - interfaceWidth;
693
                        }
694

    
695
                        if (options.innerWidth) {
696
                                settings.w = setSize(options.innerWidth, 'x');
697
                        }
698

    
699
                        $loaded.css({width: settings.w});
700
                        
701
                        if (options.height) {
702
                                settings.h = setSize(options.height, 'y') - loadedHeight - interfaceHeight;
703
                        }
704

    
705
                        if (options.innerHeight) {
706
                                settings.h = setSize(options.innerHeight, 'y');
707
                        }
708

    
709
                        if (!options.innerHeight && !options.height) {
710
                                scrolltop = $loaded.scrollTop();
711
                                $loaded.css({height: "auto"});
712
                                settings.h = $loaded.height();
713
                        }
714

    
715
                        $loaded.css({height: settings.h});
716

    
717
                        if(scrolltop) {
718
                                $loaded.scrollTop(scrolltop);
719
                        }
720
                        
721
                        publicMethod.position(settings.transition === "none" ? 0 : settings.speed);
722
                }
723
        };
724

    
725
        publicMethod.prep = function (object) {
726
                if (!open) {
727
                        return;
728
                }
729
                
730
                var callback, speed = settings.transition === "none" ? 0 : settings.speed;
731

    
732
                $loaded.empty().remove(); // Using empty first may prevent some IE7 issues.
733

    
734
                $loaded = $tag(div, 'LoadedContent').append(object);
735
                
736
                function getWidth() {
737
                        settings.w = settings.w || $loaded.width();
738
                        settings.w = settings.mw && settings.mw < settings.w ? settings.mw : settings.w;
739
                        return settings.w;
740
                }
741
                function getHeight() {
742
                        settings.h = settings.h || $loaded.height();
743
                        settings.h = settings.mh && settings.mh < settings.h ? settings.mh : settings.h;
744
                        return settings.h;
745
                }
746
                
747
                $loaded.hide()
748
                .appendTo($loadingBay.show())// content has to be appended to the DOM for accurate size calculations.
749
                .css({width: getWidth(), overflow: settings.scrolling ? 'auto' : 'hidden'})
750
                .css({height: getHeight()})// sets the height independently from the width in case the new width influences the value of height.
751
                .prependTo($content);
752
                
753
                $loadingBay.hide();
754
                
755
                // floating the IMG removes the bottom line-height and fixed a problem where IE miscalculates the width of the parent element as 100% of the document width.
756
                
757
                $(photo).css({'float': 'none'});
758

    
759
                callback = function () {
760
                        var total = $related.length,
761
                                iframe,
762
                                frameBorder = 'frameBorder',
763
                                allowTransparency = 'allowTransparency',
764
                                complete;
765
                        
766
                        if (!open) {
767
                                return;
768
                        }
769
                        
770
                        function removeFilter() { // Needed for IE7 & IE8 in versions of jQuery prior to 1.7.2
771
                                if ($.support.opacity === false) {
772
                                        $box[0].style.removeAttribute('filter');
773
                                }
774
                        }
775
                        
776
                        complete = function () {
777
                                clearTimeout(loadingTimer);
778
                                $loadingOverlay.hide();
779
                                trigger(event_complete, settings.onComplete);
780
                        };
781

    
782
                        
783
                        $title.html(settings.title).add($loaded).show();
784
                        
785
                        if (total > 1) { // handle grouping
786
                                if (typeof settings.current === "string") {
787
                                        $current.html(settings.current.replace('{current}', index + 1).replace('{total}', total)).show();
788
                                }
789
                                
790
                                $next[(settings.loop || index < total - 1) ? "show" : "hide"]().html(settings.next);
791
                                $prev[(settings.loop || index) ? "show" : "hide"]().html(settings.previous);
792
                                
793
                                slideshow();
794
                                
795
                                // Preloads images within a rel group
796
                                if (settings.preloading) {
797
                                        $.each([getIndex(-1), getIndex(1)], function(){
798
                                                var src,
799
                                                        img,
800
                                                        i = $related[this],
801
                                                        data = $.data(i, colorbox);
802

    
803
                                                if (data && data.href) {
804
                                                        src = data.href;
805
                                                        if ($.isFunction(src)) {
806
                                                                src = src.call(i);
807
                                                        }
808
                                                } else {
809
                                                        src = $(i).attr('href');
810
                                                }
811

    
812
                                                if (src && isImage(data, src)) {
813
                                                        src = retinaUrl(data, src);
814
                                                        img = document.createElement('img');
815
                                                        img.src = src;
816
                                                }
817
                                        });
818
                                }
819
                        } else {
820
                                $groupControls.hide();
821
                        }
822
                        
823
                        if (settings.iframe) {
824
                                iframe = $tag('iframe')[0];
825
                                
826
                                if (frameBorder in iframe) {
827
                                        iframe[frameBorder] = 0;
828
                                }
829
                                
830
                                if (allowTransparency in iframe) {
831
                                        iframe[allowTransparency] = "true";
832
                                }
833

    
834
                                if (!settings.scrolling) {
835
                                        iframe.scrolling = "no";
836
                                }
837
                                
838
                                $(iframe)
839
                                        .attr({
840
                                                src: settings.href,
841
                                                name: (new Date()).getTime(), // give the iframe a unique name to prevent caching
842
                                                'class': prefix + 'Iframe',
843
                                                allowFullScreen : true, // allow HTML5 video to go fullscreen
844
                                                webkitAllowFullScreen : true,
845
                                                mozallowfullscreen : true
846
                                        })
847
                                        .one('load', complete)
848
                                        .appendTo($loaded);
849
                                
850
                                $events.one(event_purge, function () {
851
                                        iframe.src = "//about:blank";
852
                                });
853

    
854
                                if (settings.fastIframe) {
855
                                        $(iframe).trigger('load');
856
                                }
857
                        } else {
858
                                complete();
859
                        }
860
                        
861
                        if (settings.transition === 'fade') {
862
                                $box.fadeTo(speed, 1, removeFilter);
863
                        } else {
864
                                removeFilter();
865
                        }
866
                };
867
                
868
                if (settings.transition === 'fade') {
869
                        $box.fadeTo(speed, 0, function () {
870
                                publicMethod.position(0, callback);
871
                        });
872
                } else {
873
                        publicMethod.position(speed, callback);
874
                }
875
        };
876

    
877
        function load () {
878
                var href, setResize, prep = publicMethod.prep, $inline, request = ++requests;
879
                
880
                active = true;
881
                
882
                photo = false;
883
                
884
                element = $related[index];
885
                
886
                makeSettings();
887
                
888
                trigger(event_purge);
889
                
890
                trigger(event_load, settings.onLoad);
891
                
892
                settings.h = settings.height ?
893
                                setSize(settings.height, 'y') - loadedHeight - interfaceHeight :
894
                                settings.innerHeight && setSize(settings.innerHeight, 'y');
895
                
896
                settings.w = settings.width ?
897
                                setSize(settings.width, 'x') - loadedWidth - interfaceWidth :
898
                                settings.innerWidth && setSize(settings.innerWidth, 'x');
899
                
900
                // Sets the minimum dimensions for use in image scaling
901
                settings.mw = settings.w;
902
                settings.mh = settings.h;
903
                
904
                // Re-evaluate the minimum width and height based on maxWidth and maxHeight values.
905
                // If the width or height exceed the maxWidth or maxHeight, use the maximum values instead.
906
                if (settings.maxWidth) {
907
                        settings.mw = setSize(settings.maxWidth, 'x') - loadedWidth - interfaceWidth;
908
                        settings.mw = settings.w && settings.w < settings.mw ? settings.w : settings.mw;
909
                }
910
                if (settings.maxHeight) {
911
                        settings.mh = setSize(settings.maxHeight, 'y') - loadedHeight - interfaceHeight;
912
                        settings.mh = settings.h && settings.h < settings.mh ? settings.h : settings.mh;
913
                }
914
                
915
                href = settings.href;
916
                
917
                loadingTimer = setTimeout(function () {
918
                        $loadingOverlay.show();
919
                }, 100);
920
                
921
                if (settings.inline) {
922
                        // Inserts an empty placeholder where inline content is being pulled from.
923
                        // An event is bound to put inline content back when Colorbox closes or loads new content.
924
                        $inline = $tag(div).hide().insertBefore($(href)[0]);
925

    
926
                        $events.one(event_purge, function () {
927
                                $inline.replaceWith($loaded.children());
928
                        });
929

    
930
                        prep($(href));
931
                } else if (settings.iframe) {
932
                        // IFrame element won't be added to the DOM until it is ready to be displayed,
933
                        // to avoid problems with DOM-ready JS that might be trying to run in that iframe.
934
                        prep(" ");
935
                } else if (settings.html) {
936
                        prep(settings.html);
937
                } else if (isImage(settings, href)) {
938

    
939
                        href = retinaUrl(settings, href);
940

    
941
                        photo = document.createElement('img');
942

    
943
                        $(photo)
944
                        .addClass(prefix + 'Photo')
945
                        .bind('error',function () {
946
                                settings.title = false;
947
                                prep($tag(div, 'Error').html(settings.imgError));
948
                        })
949
                        .one('load', function () {
950
                                var percent;
951

    
952
                                if (request !== requests) {
953
                                        return;
954
                                }
955

    
956
                                $.each(['alt', 'longdesc', 'aria-describedby'], function(i,val){
957
                                        var attr = $(element).attr(val) || $(element).attr('data-'+val);
958
                                        if (attr) {
959
                                                photo.setAttribute(val, attr);
960
                                        }
961
                                });
962

    
963
                                if (settings.retinaImage && window.devicePixelRatio > 1) {
964
                                        photo.height = photo.height / window.devicePixelRatio;
965
                                        photo.width = photo.width / window.devicePixelRatio;
966
                                }
967

    
968
                                if (settings.scalePhotos) {
969
                                        setResize = function () {
970
                                                photo.height -= photo.height * percent;
971
                                                photo.width -= photo.width * percent;
972
                                        };
973
                                        if (settings.mw && photo.width > settings.mw) {
974
                                                percent = (photo.width - settings.mw) / photo.width;
975
                                                setResize();
976
                                        }
977
                                        if (settings.mh && photo.height > settings.mh) {
978
                                                percent = (photo.height - settings.mh) / photo.height;
979
                                                setResize();
980
                                        }
981
                                }
982
                                
983
                                if (settings.h) {
984
                                        photo.style.marginTop = Math.max(settings.mh - photo.height, 0) / 2 + 'px';
985
                                }
986
                                
987
                                if ($related[1] && (settings.loop || $related[index + 1])) {
988
                                        photo.style.cursor = 'pointer';
989
                                        photo.onclick = function () {
990
                                                publicMethod.next();
991
                                        };
992
                                }
993

    
994
                                photo.style.width = photo.width + 'px';
995
                                photo.style.height = photo.height + 'px';
996

    
997
                                setTimeout(function () { // A pause because Chrome will sometimes report a 0 by 0 size otherwise.
998
                                        prep(photo);
999
                                }, 1);
1000
                        });
1001
                        
1002
                        setTimeout(function () { // A pause because Opera 10.6+ will sometimes not run the onload function otherwise.
1003
                                photo.src = href;
1004
                        }, 1);
1005
                } else if (href) {
1006
                        $loadingBay.load(href, settings.data, function (data, status) {
1007
                                if (request === requests) {
1008
                                        prep(status === 'error' ? $tag(div, 'Error').html(settings.xhrError) : $(this).contents());
1009
                                }
1010
                        });
1011
                }
1012
        }
1013
                
1014
        // Navigates to the next page/image in a set.
1015
        publicMethod.next = function () {
1016
                if (!active && $related[1] && (settings.loop || $related[index + 1])) {
1017
                        index = getIndex(1);
1018
                        launch($related[index]);
1019
                }
1020
        };
1021
        
1022
        publicMethod.prev = function () {
1023
                if (!active && $related[1] && (settings.loop || index)) {
1024
                        index = getIndex(-1);
1025
                        launch($related[index]);
1026
                }
1027
        };
1028

    
1029
        // Note: to use this within an iframe use the following format: parent.jQuery.colorbox.close();
1030
        publicMethod.close = function () {
1031
                if (open && !closing) {
1032
                        
1033
                        closing = true;
1034
                        
1035
                        open = false;
1036
                        
1037
                        trigger(event_cleanup, settings.onCleanup);
1038
                        
1039
                        $window.unbind('.' + prefix);
1040
                        
1041
                        $overlay.fadeTo(settings.fadeOut || 0, 0);
1042
                        
1043
                        $box.stop().fadeTo(settings.fadeOut || 0, 0, function () {
1044
                        
1045
                                $box.add($overlay).css({'opacity': 1, cursor: 'auto'}).hide();
1046
                                
1047
                                trigger(event_purge);
1048
                                
1049
                                $loaded.empty().remove(); // Using empty first may prevent some IE7 issues.
1050
                                
1051
                                setTimeout(function () {
1052
                                        closing = false;
1053
                                        trigger(event_closed, settings.onClosed);
1054
                                }, 1);
1055
                        });
1056
                }
1057
        };
1058

    
1059
        // Removes changes Colorbox made to the document, but does not remove the plugin.
1060
        publicMethod.remove = function () {
1061
                if (!$box) { return; }
1062

    
1063
                $box.stop();
1064
                $.colorbox.close();
1065
                $box.stop().remove();
1066
                $overlay.remove();
1067
                closing = false;
1068
                $box = null;
1069
                $('.' + boxElement)
1070
                        .removeData(colorbox)
1071
                        .removeClass(boxElement);
1072

    
1073
                $(document).unbind('click.'+prefix);
1074
        };
1075

    
1076
        // A method for fetching the current element Colorbox is referencing.
1077
        // returns a jQuery object.
1078
        publicMethod.element = function () {
1079
                return $(element);
1080
        };
1081

    
1082
        publicMethod.settings = defaults;
1083

    
1084
}(jQuery, document, window));