source: extensions/lightbox/js/jquery.colorbox.js @ 9976

Last change on this file since 9976 was 9976, checked in by patdenice, 13 years ago

Update colorbox to 1.3.16

File size: 24.6 KB
Line 
1// ColorBox v1.3.16 - a full featured, light-weight, customizable lightbox based on jQuery 1.3+
2// Copyright (c) 2011 Jack Moore - jack@colorpowered.com
3// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
4(function ($, document, window) {
5        var
6        // ColorBox Default Settings.   
7        // See http://colorpowered.com/colorbox for details.
8        defaults = {
9                transition: "elastic",
10                speed: 300,
11                width: false,
12                initialWidth: "600",
13                innerWidth: false,
14                maxWidth: false,
15                height: false,
16                initialHeight: "450",
17                innerHeight: false,
18                maxHeight: false,
19                scalePhotos: true,
20                scrolling: true,
21                inline: false,
22                html: false,
23                iframe: false,
24                fastIframe: true,
25                photo: false,
26                href: false,
27                title: false,
28                rel: false,
29                opacity: 0.9,
30                preloading: true,
31                current: "image {current} of {total}",
32                previous: "previous",
33                next: "next",
34                close: "close",
35                open: false,
36                returnFocus: true,
37                loop: true,
38                slideshow: false,
39                slideshowAuto: true,
40                slideshowSpeed: 2500,
41                slideshowStart: "start slideshow",
42                slideshowStop: "stop slideshow",
43                onOpen: false,
44                onLoad: false,
45                onComplete: false,
46                onCleanup: false,
47                onClosed: false,
48                overlayClose: true,             
49                escKey: true,
50                arrowKey: true
51        },
52       
53        // Abstracting the HTML and event identifiers for easy rebranding
54        colorbox = 'colorbox',
55        prefix = 'cbox',
56       
57        // Events       
58        event_open = prefix + '_open',
59        event_load = prefix + '_load',
60        event_complete = prefix + '_complete',
61        event_cleanup = prefix + '_cleanup',
62        event_closed = prefix + '_closed',
63        event_purge = prefix + '_purge',
64       
65        // Special Handling for IE
66        isIE = $.browser.msie && !$.support.opacity, // feature detection alone gave a false positive on at least one phone browser and on some development versions of Chrome.
67        isIE6 = isIE && $.browser.version < 7,
68        event_ie6 = prefix + '_IE6',
69
70        // Cached jQuery Object Variables
71        $overlay,
72        $box,
73        $wrap,
74        $content,
75        $topBorder,
76        $leftBorder,
77        $rightBorder,
78        $bottomBorder,
79        $related,
80        $window,
81        $loaded,
82        $loadingBay,
83        $loadingOverlay,
84        $title,
85        $current,
86        $slideshow,
87        $next,
88        $prev,
89        $close,
90        $groupControls,
91
92        // Variables for cached values or use across multiple functions
93        settings = {},
94        interfaceHeight,
95        interfaceWidth,
96        loadedHeight,
97        loadedWidth,
98        element,
99        index,
100        photo,
101        open,
102        active,
103        closing = false,
104       
105        publicMethod,
106        boxElement = prefix + 'Element';
107       
108        // ****************
109        // HELPER FUNCTIONS
110        // ****************
111
112        // jQuery object generator to reduce code size
113        function $div(id, cssText) { 
114                var div = document.createElement('div');
115                if (id) {
116            div.id = prefix + id;
117        }
118                div.style.cssText = cssText || false;
119                return $(div);
120        }
121
122        // Convert % values to pixels
123        function setSize(size, dimension) {
124                dimension = dimension === 'x' ? $window.width() : $window.height();
125                return (typeof size === 'string') ? Math.round((/%/.test(size) ? (dimension / 100) * parseInt(size, 10) : parseInt(size, 10))) : size;
126        }
127       
128        // Checks an href to see if it is a photo.
129        // There is a force photo option (photo: true) for hrefs that cannot be matched by this regex.
130        function isImage(url) {
131                return settings.photo || /\.(gif|png|jpg|jpeg|bmp)(?:\?([^#]*))?(?:#(\.*))?$/i.test(url);
132        }
133       
134        // Assigns function results to their respective settings.  This allows functions to be used as values.
135        function process(settings) {
136                for (var i in settings) {
137                        if ($.isFunction(settings[i]) && i.substring(0, 2) !== 'on') { // checks to make sure the function isn't one of the callbacks, they will be handled at the appropriate time.
138                            settings[i] = settings[i].call(element);
139                        }
140                }
141                settings.rel = settings.rel || element.rel || 'nofollow';
142                settings.href = $.trim(settings.href || $(element).attr('href'));
143                settings.title = settings.title || element.title;
144        }
145
146        function trigger(event, callback) {
147                if (callback) {
148                        callback.call(element);
149                }
150                $.event.trigger(event);
151        }
152
153        // Slideshow functionality
154        function slideshow() {
155                var
156                timeOut,
157                className = prefix + "Slideshow_",
158                click = "click." + prefix,
159                start,
160                stop,
161                clear;
162               
163                if (settings.slideshow && $related[1]) {
164                        start = function () {
165                                $slideshow
166                                        .text(settings.slideshowStop)
167                                        .unbind(click)
168                                        .bind(event_complete, function () {
169                                                if (index < $related.length - 1 || settings.loop) {
170                                                        timeOut = setTimeout(publicMethod.next, settings.slideshowSpeed);
171                                                }
172                                        })
173                                        .bind(event_load, function () {
174                                                clearTimeout(timeOut);
175                                        })
176                                        .one(click + ' ' + event_cleanup, stop);
177                                $box.removeClass(className + "off").addClass(className + "on");
178                                timeOut = setTimeout(publicMethod.next, settings.slideshowSpeed);
179                        };
180                       
181                        stop = function () {
182                                clearTimeout(timeOut);
183                                $slideshow
184                                        .text(settings.slideshowStart)
185                                        .unbind([event_complete, event_load, event_cleanup, click].join(' '))
186                                        .one(click, start);
187                                $box.removeClass(className + "on").addClass(className + "off");
188                        };
189                       
190                        if (settings.slideshowAuto) {
191                                start();
192                        } else {
193                                stop();
194                        }
195                }
196        }
197
198        function launch(elem) {
199                if (!closing) {
200                       
201                        element = elem;
202                       
203                        process($.extend(settings, $.data(element, colorbox)));
204                       
205                        $related = $(element);
206                       
207                        index = 0;
208                       
209                        if (settings.rel !== 'nofollow') {
210                                $related = $('.' + boxElement).filter(function () {
211                                        var relRelated = $.data(this, colorbox).rel || this.rel;
212                                        return (relRelated === settings.rel);
213                                });
214                                index = $related.index(element);
215                               
216                                // Check direct calls to ColorBox.
217                                if (index === -1) {
218                                        $related = $related.add(element);
219                                        index = $related.length - 1;
220                                }
221                        }
222                       
223                        if (!open) {
224                                open = active = true; // Prevents the page-change action from queuing up if the visitor holds down the left or right keys.
225                               
226                                $box.show();
227                               
228                                if (settings.returnFocus) {
229                                        try {
230                                                element.blur();
231                                                $(element).one(event_closed, function () {
232                                                        try {
233                                                                this.focus();
234                                                        } catch (e) {
235                                                                // do nothing
236                                                        }
237                                                });
238                                        } catch (e) {
239                                                // do nothing
240                                        }
241                                }
242                               
243                                // +settings.opacity avoids a problem in IE when using non-zero-prefixed-string-values, like '.5'
244                                $overlay.css({"opacity": +settings.opacity, "cursor": settings.overlayClose ? "pointer" : "auto"}).show();
245                               
246                                // Opens inital empty ColorBox prior to content being loaded.
247                                settings.w = setSize(settings.initialWidth, 'x');
248                                settings.h = setSize(settings.initialHeight, 'y');
249                                publicMethod.position(0);
250                               
251                                if (isIE6) {
252                                        $window.bind('resize.' + event_ie6 + ' scroll.' + event_ie6, function () {
253                                                $overlay.css({width: $window.width(), height: $window.height(), top: $window.scrollTop(), left: $window.scrollLeft()});
254                                        }).trigger('resize.' + event_ie6);
255                                }
256                               
257                                trigger(event_open, settings.onOpen);
258                               
259                                $groupControls.add($title).hide();
260                               
261                                $close.html(settings.close).show();
262                        }
263                       
264                        publicMethod.load(true);
265                }
266        }
267
268        // ****************
269        // PUBLIC FUNCTIONS
270        // Usage format: $.fn.colorbox.close();
271        // Usage from within an iframe: parent.$.fn.colorbox.close();
272        // ****************
273       
274        publicMethod = $.fn[colorbox] = $[colorbox] = function (options, callback) {
275                var $this = this, autoOpen;
276               
277                if (!$this[0] && $this.selector) { // if a selector was given and it didn't match any elements, go ahead and exit.
278                        return $this;
279                }
280               
281                options = options || {};
282               
283                if (callback) {
284                        options.onComplete = callback;
285                }
286               
287                if (!$this[0] || $this.selector === undefined) { // detects $.colorbox() and $.fn.colorbox()
288                        $this = $('<a/>');
289                        options.open = true; // assume an immediate open
290                }
291               
292                $this.each(function () {
293                        $.data(this, colorbox, $.extend({}, $.data(this, colorbox) || defaults, options));
294                        $(this).addClass(boxElement);
295                });
296               
297                autoOpen = options.open;
298               
299                if ($.isFunction(autoOpen)) {
300                        autoOpen = autoOpen.call($this);
301                }
302               
303                if (autoOpen) {
304                        launch($this[0]);
305                }
306               
307                return $this;
308        };
309
310        // Initialize ColorBox: store common calculations, preload the interface graphics, append the html.
311        // This preps colorbox for a speedy open when clicked, and lightens the burdon on the browser by only
312        // having to run once, instead of each time colorbox is opened.
313        publicMethod.init = function () {
314                // Create & Append jQuery Objects
315                $window = $(window);
316                $box = $div().attr({id: colorbox, 'class': isIE ? prefix + (isIE6 ? 'IE6' : 'IE') : ''});
317                $overlay = $div("Overlay", isIE6 ? 'position:absolute' : '').hide();
318               
319                $wrap = $div("Wrapper");
320                $content = $div("Content").append(
321                        $loaded = $div("LoadedContent", 'width:0; height:0; overflow:hidden'),
322                        $loadingOverlay = $div("LoadingOverlay").add($div("LoadingGraphic")),
323                        $title = $div("Title"),
324                        $current = $div("Current"),
325                        $next = $div("Next"),
326                        $prev = $div("Previous"),
327                        $slideshow = $div("Slideshow").bind(event_open, slideshow),
328                        $close = $div("Close")
329                );
330                $wrap.append( // The 3x3 Grid that makes up ColorBox
331                        $div().append(
332                                $div("TopLeft"),
333                                $topBorder = $div("TopCenter"),
334                                $div("TopRight")
335                        ),
336                        $div(false, 'clear:left').append(
337                                $leftBorder = $div("MiddleLeft"),
338                                $content,
339                                $rightBorder = $div("MiddleRight")
340                        ),
341                        $div(false, 'clear:left').append(
342                                $div("BottomLeft"),
343                                $bottomBorder = $div("BottomCenter"),
344                                $div("BottomRight")
345                        )
346                ).children().children().css({'float': 'left'});
347               
348                $loadingBay = $div(false, 'position:absolute; width:9999px; visibility:hidden; display:none');
349               
350                $('body').prepend($overlay, $box.append($wrap, $loadingBay));
351               
352                $content.children()
353                .hover(function () {
354                        $(this).addClass('hover');
355                }, function () {
356                        $(this).removeClass('hover');
357                }).addClass('hover');
358               
359                // Cache values needed for size calculations
360                interfaceHeight = $topBorder.height() + $bottomBorder.height() + $content.outerHeight(true) - $content.height();//Subtraction needed for IE6
361                interfaceWidth = $leftBorder.width() + $rightBorder.width() + $content.outerWidth(true) - $content.width();
362                loadedHeight = $loaded.outerHeight(true);
363                loadedWidth = $loaded.outerWidth(true);
364               
365                // Setting padding to remove the need to do size conversions during the animation step.
366                $box.css({"padding-bottom": interfaceHeight, "padding-right": interfaceWidth}).hide();
367               
368                // Setup button events.
369                $next.click(function () {
370                        publicMethod.next();
371                });
372                $prev.click(function () {
373                        publicMethod.prev();
374                });
375                $close.click(function () {
376                        publicMethod.close();
377                });
378               
379                $groupControls = $next.add($prev).add($current).add($slideshow);
380               
381                // Adding the 'hover' class allowed the browser to load the hover-state
382                // background graphics.  The class can now can be removed.
383                $content.children().removeClass('hover');
384               
385                $('.' + boxElement).live('click', function (e) {
386                        // checks to see if it was a non-left mouse-click and for clicks modified with ctrl, shift, or alt.
387                        if (!((e.button !== 0 && typeof e.button !== 'undefined') || e.ctrlKey || e.shiftKey || e.altKey)) {
388                                e.preventDefault();
389                                launch(this);
390                        }
391                });
392               
393                $overlay.click(function () {
394                        if (settings.overlayClose) {
395                                publicMethod.close();
396                        }
397                });
398               
399                // Set Navigation Key Bindings
400                $(document).bind('keydown.' + prefix, function (e) {
401                        var key = e.keyCode;
402                        if (open && settings.escKey && key === 27) {
403                                e.preventDefault();
404                                publicMethod.close();
405                        }
406                        if (open && settings.arrowKey && $related[1]) {
407                                if (key === 37) {
408                                        e.preventDefault();
409                                        $prev.click();
410                                } else if (key === 39) {
411                                        e.preventDefault();
412                                        $next.click();
413                                }
414                        }
415                });
416        };
417       
418        publicMethod.remove = function () {
419                $box.add($overlay).remove();
420                $('.' + boxElement).die('click').removeData(colorbox).removeClass(boxElement);
421        };
422
423        publicMethod.position = function (speed, loadedCallback) {
424                var
425                animate_speed,
426                // keeps the top and left positions within the browser's viewport.
427                posTop = Math.max(document.documentElement.clientHeight - settings.h - loadedHeight - interfaceHeight, 0) / 2 + $window.scrollTop(),
428                posLeft = Math.max($window.width() - settings.w - loadedWidth - interfaceWidth, 0) / 2 + $window.scrollLeft();
429               
430                // setting the speed to 0 to reduce the delay between same-sized content.
431                animate_speed = ($box.width() === settings.w + loadedWidth && $box.height() === settings.h + loadedHeight) ? 0 : speed;
432               
433                // this gives the wrapper plenty of breathing room so it's floated contents can move around smoothly,
434                // but it has to be shrank down around the size of div#colorbox when it's done.  If not,
435                // it can invoke an obscure IE bug when using iframes.
436                $wrap[0].style.width = $wrap[0].style.height = "9999px";
437               
438                function modalDimensions(that) {
439                        // loading overlay height has to be explicitly set for IE6.
440                        $topBorder[0].style.width = $bottomBorder[0].style.width = $content[0].style.width = that.style.width;
441                        $loadingOverlay[0].style.height = $loadingOverlay[1].style.height = $content[0].style.height = $leftBorder[0].style.height = $rightBorder[0].style.height = that.style.height;
442                }
443               
444                $box.dequeue().animate({width: settings.w + loadedWidth, height: settings.h + loadedHeight, top: posTop, left: posLeft}, {
445                        duration: animate_speed,
446                        complete: function () {
447                                modalDimensions(this);
448                               
449                                active = false;
450                               
451                                // shrink the wrapper down to exactly the size of colorbox to avoid a bug in IE's iframe implementation.
452                                $wrap[0].style.width = (settings.w + loadedWidth + interfaceWidth) + "px";
453                                $wrap[0].style.height = (settings.h + loadedHeight + interfaceHeight) + "px";
454                               
455                                if (loadedCallback) {
456                                        loadedCallback();
457                                }
458                        },
459                        step: function () {
460                                modalDimensions(this);
461                        }
462                });
463        };
464
465        publicMethod.resize = function (options) {
466                if (open) {
467                        options = options || {};
468                       
469                        if (options.width) {
470                                settings.w = setSize(options.width, 'x') - loadedWidth - interfaceWidth;
471                        }
472                        if (options.innerWidth) {
473                                settings.w = setSize(options.innerWidth, 'x');
474                        }
475                        $loaded.css({width: settings.w});
476                       
477                        if (options.height) {
478                                settings.h = setSize(options.height, 'y') - loadedHeight - interfaceHeight;
479                        }
480                        if (options.innerHeight) {
481                                settings.h = setSize(options.innerHeight, 'y');
482                        }
483                        if (!options.innerHeight && !options.height) {                         
484                                var $child = $loaded.wrapInner("<div style='overflow:auto'></div>").children(); // temporary wrapper to get an accurate estimate of just how high the total content should be.
485                                settings.h = $child.height();
486                                $child.replaceWith($child.children()); // ditch the temporary wrapper div used in height calculation
487                        }
488                        $loaded.css({height: settings.h});
489                       
490                        publicMethod.position(settings.transition === "none" ? 0 : settings.speed);
491                }
492        };
493
494        publicMethod.prep = function (object) {
495                if (!open) {
496                        return;
497                }
498               
499                var speed = settings.transition === "none" ? 0 : settings.speed;
500               
501                $window.unbind('resize.' + prefix);
502                $loaded.remove();
503                $loaded = $div('LoadedContent').html(object);
504               
505                function getWidth() {
506                        settings.w = settings.w || $loaded.width();
507                        settings.w = settings.mw && settings.mw < settings.w ? settings.mw : settings.w;
508                        return settings.w;
509                }
510                function getHeight() {
511                        settings.h = settings.h || $loaded.height();
512                        settings.h = settings.mh && settings.mh < settings.h ? settings.mh : settings.h;
513                        return settings.h;
514                }
515               
516                $loaded.hide()
517                .appendTo($loadingBay.show())// content has to be appended to the DOM for accurate size calculations.
518                .css({width: getWidth(), overflow: settings.scrolling ? 'auto' : 'hidden'})
519                .css({height: getHeight()})// sets the height independently from the width in case the new width influences the value of height.
520                .prependTo($content);
521               
522                $loadingBay.hide();
523               
524                // 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.
525                //$(photo).css({'float': 'none', marginLeft: 'auto', marginRight: 'auto'});
526               
527                $(photo).css({'float': 'none'});
528               
529                // Hides SELECT elements in IE6 because they would otherwise sit on top of the overlay.
530                if (isIE6) {
531                        $('select').not($box.find('select')).filter(function () {
532                                return this.style.visibility !== 'hidden';
533                        }).css({'visibility': 'hidden'}).one(event_cleanup, function () {
534                                this.style.visibility = 'inherit';
535                        });
536                }
537               
538                function setPosition(s) {
539                        publicMethod.position(s, function () {
540                                var prev, prevSrc, next, nextSrc, total = $related.length, iframe, complete;
541                               
542                                if (!open) {
543                                        return;
544                                }
545                               
546                                complete = function () {
547                                        $loadingOverlay.hide();
548                                        trigger(event_complete, settings.onComplete);
549                                };
550                               
551                                if (isIE) {
552                                        //This fadeIn helps the bicubic resampling to kick-in.
553                                        if (photo) {
554                                                $loaded.fadeIn(100);
555                                        }
556                                }
557                               
558                                $title.html(settings.title).add($loaded).show();
559                               
560                                if (total > 1) { // handle grouping
561                                        if (typeof settings.current === "string") {
562                                                $current.html(settings.current.replace(/\{current\}/, index + 1).replace(/\{total\}/, total)).show();
563                                        }
564                                       
565                                        $next[(settings.loop || index < total - 1) ? "show" : "hide"]().html(settings.next);
566                                        $prev[(settings.loop || index) ? "show" : "hide"]().html(settings.previous);
567                                       
568                                        prev = index ? $related[index - 1] : $related[total - 1];
569                                        next = index < total - 1 ? $related[index + 1] : $related[0];
570                                       
571                                        if (settings.slideshow) {
572                                                $slideshow.show();
573                                        }
574                                       
575                                        // Preloads images within a rel group
576                                        if (settings.preloading) {
577                                                nextSrc = $.data(next, colorbox).href || next.href;
578                                                prevSrc = $.data(prev, colorbox).href || prev.href;
579                                               
580                                                nextSrc = $.isFunction(nextSrc) ? nextSrc.call(next) : nextSrc;
581                                                prevSrc = $.isFunction(prevSrc) ? prevSrc.call(prev) : prevSrc;
582                                               
583                                                if (isImage(nextSrc)) {
584                                                        $('<img/>')[0].src = nextSrc;
585                                                }
586                                               
587                                                if (isImage(prevSrc)) {
588                                                        $('<img/>')[0].src = prevSrc;
589                                                }
590                                        }
591                                } else {
592                                        $groupControls.hide();
593                                }
594                               
595                                if (settings.iframe) {
596                                        iframe = $('<iframe/>').addClass(prefix + 'Iframe')[0];
597                                       
598                                        if (settings.fastIframe) {
599                                                complete();
600                                        } else {
601                                                $(iframe).load(complete);
602                                        }
603                                        iframe.name = prefix + (+new Date());
604                                        iframe.src = settings.href;
605                                       
606                                        if (!settings.scrolling) {
607                                                iframe.scrolling = "no";
608                                        }
609                                       
610                                        if (isIE) {
611                        iframe.frameBorder=0;
612                                                iframe.allowTransparency = "true";
613                                        }
614                                       
615                                        $(iframe).appendTo($loaded).one(event_purge, function () {
616                                                iframe.src = "//about:blank";
617                                        });
618                                } else {
619                                        complete();
620                                }
621                               
622                                if (settings.transition === 'fade') {
623                                        $box.fadeTo(speed, 1, function () {
624                                                $box[0].style.filter = "";
625                                        });
626                                } else {
627                     $box[0].style.filter = "";
628                                }
629                               
630                                $window.bind('resize.' + prefix, function () {
631                                        publicMethod.position(0);
632                                });
633                        });
634                }
635               
636                if (settings.transition === 'fade') {
637                        $box.fadeTo(speed, 0, function () {
638                                setPosition(0);
639                        });
640                } else {
641                        setPosition(speed);
642                }
643        };
644
645        publicMethod.load = function (launched) {
646                var href, setResize, prep = publicMethod.prep;
647               
648                active = true;
649               
650                photo = false;
651               
652                element = $related[index];
653               
654                if (!launched) {
655                        process($.extend(settings, $.data(element, colorbox)));
656                }
657               
658                trigger(event_purge);
659               
660                trigger(event_load, settings.onLoad);
661               
662                settings.h = settings.height ?
663                                setSize(settings.height, 'y') - loadedHeight - interfaceHeight :
664                                settings.innerHeight && setSize(settings.innerHeight, 'y');
665               
666                settings.w = settings.width ?
667                                setSize(settings.width, 'x') - loadedWidth - interfaceWidth :
668                                settings.innerWidth && setSize(settings.innerWidth, 'x');
669               
670                // Sets the minimum dimensions for use in image scaling
671                settings.mw = settings.w;
672                settings.mh = settings.h;
673               
674                // Re-evaluate the minimum width and height based on maxWidth and maxHeight values.
675                // If the width or height exceed the maxWidth or maxHeight, use the maximum values instead.
676                if (settings.maxWidth) {
677                        settings.mw = setSize(settings.maxWidth, 'x') - loadedWidth - interfaceWidth;
678                        settings.mw = settings.w && settings.w < settings.mw ? settings.w : settings.mw;
679                }
680                if (settings.maxHeight) {
681                        settings.mh = setSize(settings.maxHeight, 'y') - loadedHeight - interfaceHeight;
682                        settings.mh = settings.h && settings.h < settings.mh ? settings.h : settings.mh;
683                }
684               
685                href = settings.href;
686               
687                $loadingOverlay.show();
688
689                if (settings.inline) {
690                        // Inserts an empty placeholder where inline content is being pulled from.
691                        // An event is bound to put inline content back when ColorBox closes or loads new content.
692                        $div().hide().insertBefore($(href)[0]).one(event_purge, function () {
693                                $(this).replaceWith($loaded.children());
694                        });
695                        prep($(href));
696                } else if (settings.iframe) {
697                        // IFrame element won't be added to the DOM until it is ready to be displayed,
698                        // to avoid problems with DOM-ready JS that might be trying to run in that iframe.
699                        prep(" ");
700                } else if (settings.html) {
701                        prep(settings.html);
702                } else if (isImage(href)) {
703                        $(photo = new Image())
704                        .addClass(prefix + 'Photo')
705                        .error(function () {
706                                settings.title = false;
707                                prep($div('Error').text('This image could not be loaded'));
708                        })
709                        .load(function () {
710                                var percent;
711                                photo.onload = null; //stops animated gifs from firing the onload repeatedly.
712                               
713                                if (settings.scalePhotos) {
714                                        setResize = function () {
715                                                photo.height -= photo.height * percent;
716                                                photo.width -= photo.width * percent;   
717                                        };
718                                        if (settings.mw && photo.width > settings.mw) {
719                                                percent = (photo.width - settings.mw) / photo.width;
720                                                setResize();
721                                        }
722                                        if (settings.mh && photo.height > settings.mh) {
723                                                percent = (photo.height - settings.mh) / photo.height;
724                                                setResize();
725                                        }
726                                }
727                               
728                                if (settings.h) {
729                                        photo.style.marginTop = Math.max(settings.h - photo.height, 0) / 2 + 'px';
730                                }
731                               
732                                if ($related[1] && (index < $related.length - 1 || settings.loop)) {
733                                        photo.style.cursor = 'pointer';
734                                        photo.onclick = function () {
735                                                publicMethod.next();
736                                        };
737                                }
738                               
739                                if (isIE) {
740                                        photo.style.msInterpolationMode = 'bicubic';
741                                }
742                               
743                                setTimeout(function () { // A pause because Chrome will sometimes report a 0 by 0 size otherwise.
744                                        prep(photo);
745                                }, 1);
746                        });
747                       
748                        setTimeout(function () { // A pause because Opera 10.6+ will sometimes not run the onload function otherwise.
749                                photo.src = href;
750                        }, 1);
751                } else if (href) {
752                        $loadingBay.load(href, function (data, status, xhr) {
753                                prep(status === 'error' ? $div('Error').text('Request unsuccessful: ' + xhr.statusText) : $(this).contents());
754                        });
755                }
756        };
757       
758        // Navigates to the next page/image in a set.
759        publicMethod.next = function () {
760                if (!active && $related[1] && (index < $related.length - 1 || settings.loop)) {
761                        index = index < $related.length - 1 ? index + 1 : 0;
762                        publicMethod.load();
763                }
764        };
765       
766        publicMethod.prev = function () {
767                if (!active && $related[1] && (index || settings.loop)) {
768                        index = index ? index - 1 : $related.length - 1;
769                        publicMethod.load();
770                }
771        };
772
773        // Note: to use this within an iframe use the following format: parent.$.fn.colorbox.close();
774        publicMethod.close = function () {
775                if (open && !closing) {
776                       
777                        closing = true;
778                       
779                        open = false;
780                       
781                        trigger(event_cleanup, settings.onCleanup);
782                       
783                        $window.unbind('.' + prefix + ' .' + event_ie6);
784                       
785                        $overlay.fadeTo(200, 0);
786                       
787                        $box.stop().fadeTo(300, 0, function () {
788                               
789                                $box.add($overlay).css({'opacity': 1, cursor: 'auto'}).hide();
790                               
791                                trigger(event_purge);
792                               
793                                $loaded.remove();
794                               
795                                setTimeout(function () {
796                                        closing = false;
797                                        trigger(event_closed, settings.onClosed);
798                                }, 1);
799                        });
800                }
801        };
802
803        // A method for fetching the current element ColorBox is referencing.
804        // returns a jQuery object.
805        publicMethod.element = function () {
806                return $(element);
807        };
808
809        publicMethod.settings = defaults;
810
811        // Initializes ColorBox when the DOM has loaded
812        $(publicMethod.init);
813
814}(jQuery, document, this));
Note: See TracBrowser for help on using the repository browser.