/vendors/jquery/colorbox/colorbox/jquery.colorbox.js

https://github.com/fragilbert/Elgg · JavaScript · 926 lines · 709 code · 151 blank · 66 comment · 155 complexity · 5f6f6fb1da2f569029c97b3832c3fa78 MD5 · raw file

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