jquery.fancybox.js 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020
  1. /*!
  2. * fancyBox - jQuery Plugin
  3. * version: 2.1.5 (Fri, 14 Jun 2013)
  4. * requires jQuery v1.6 or later
  5. *
  6. * Examples at http://fancyapps.com/fancybox/
  7. * License: www.fancyapps.com/fancybox/#license
  8. *
  9. * Copyright 2012 Janis Skarnelis - janis@fancyapps.com
  10. *
  11. */
  12. ;(function (window, document, $, undefined) {
  13. "use strict";
  14. var H = $("html"),
  15. W = $(window),
  16. D = $(document),
  17. F = $.fancybox = function () {
  18. F.open.apply(this, arguments);
  19. },
  20. IE = navigator.userAgent.match(/msie/i),
  21. didUpdate = null,
  22. isTouch = document.createTouch !== undefined,
  23. isQuery = function (obj) {
  24. return obj && obj.hasOwnProperty && obj instanceof $;
  25. },
  26. isString = function (str) {
  27. return str && $.type(str) === "string";
  28. },
  29. isPercentage = function (str) {
  30. return isString(str) && str.indexOf('%') > 0;
  31. },
  32. isScrollable = function (el) {
  33. return (el && !(el.style.overflow && el.style.overflow === 'hidden') && ((el.clientWidth && el.scrollWidth > el.clientWidth) || (el.clientHeight && el.scrollHeight > el.clientHeight)));
  34. },
  35. getScalar = function (orig, dim) {
  36. var value = parseInt(orig, 10) || 0;
  37. if (dim && isPercentage(orig)) {
  38. value = F.getViewport()[dim] / 100 * value;
  39. }
  40. return Math.ceil(value);
  41. },
  42. getValue = function (value, dim) {
  43. return getScalar(value, dim) + 'px';
  44. };
  45. $.extend(F, {
  46. // The current version of fancyBox
  47. version: '2.1.5',
  48. defaults: {
  49. padding: 15,
  50. margin: 20,
  51. width: 800,
  52. height: 600,
  53. minWidth: 100,
  54. minHeight: 100,
  55. maxWidth: 9999,
  56. maxHeight: 9999,
  57. pixelRatio: 1, // Set to 2 for retina display support
  58. autoSize: true,
  59. autoHeight: false,
  60. autoWidth: false,
  61. autoResize: true,
  62. autoCenter: !isTouch,
  63. fitToView: true,
  64. aspectRatio: false,
  65. topRatio: 0.5,
  66. leftRatio: 0.5,
  67. scrolling: 'auto', // 'auto', 'yes' or 'no'
  68. wrapCSS: '',
  69. arrows: true,
  70. closeBtn: true,
  71. closeClick: false,
  72. nextClick: false,
  73. mouseWheel: true,
  74. autoPlay: false,
  75. playSpeed: 3000,
  76. preload: 3,
  77. modal: false,
  78. loop: true,
  79. ajax: {
  80. dataType: 'html',
  81. headers: {'X-fancyBox': true}
  82. },
  83. iframe: {
  84. scrolling: 'auto',
  85. preload: true
  86. },
  87. swf: {
  88. wmode: 'transparent',
  89. allowfullscreen: 'true',
  90. allowscriptaccess: 'always'
  91. },
  92. keys: {
  93. next: {
  94. 13: 'left', // enter
  95. 34: 'up', // page down
  96. 39: 'left', // right arrow
  97. 40: 'up' // down arrow
  98. },
  99. prev: {
  100. 8: 'right', // backspace
  101. 33: 'down', // page up
  102. 37: 'right', // left arrow
  103. 38: 'down' // up arrow
  104. },
  105. close: [27], // escape key
  106. play: [32], // space - start/stop slideshow
  107. toggle: [70] // letter "f" - toggle fullscreen
  108. },
  109. direction: {
  110. next: 'left',
  111. prev: 'right'
  112. },
  113. scrollOutside: true,
  114. // Override some properties
  115. index: 0,
  116. type: null,
  117. href: null,
  118. content: null,
  119. title: null,
  120. // HTML templates
  121. tpl: {
  122. wrap: '<div class="fancybox-wrap" tabIndex="-1"><div class="fancybox-skin"><div class="fancybox-outer"><div class="fancybox-inner"></div></div></div></div>',
  123. image: '<img class="fancybox-image" src="{href}" alt="" />',
  124. iframe: '<iframe id="fancybox-frame{rnd}" name="fancybox-frame{rnd}" class="fancybox-iframe" frameborder="0" vspace="0" hspace="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen' + (IE ? ' allowtransparency="true"' : '') + '></iframe>',
  125. error: '<p class="fancybox-error">The requested content cannot be loaded.<br/>Please try again later.</p>',
  126. closeBtn: '<a title="Close" class="fancybox-item fancybox-close" href="javascript:;"></a>',
  127. next: '<a title="Next" class="fancybox-nav fancybox-next" href="javascript:;"><span></span></a>',
  128. prev: '<a title="Previous" class="fancybox-nav fancybox-prev" href="javascript:;"><span></span></a>',
  129. loading: '<div id="fancybox-loading"><div></div></div>'
  130. },
  131. // Properties for each animation type
  132. // Opening fancyBox
  133. openEffect: 'fade', // 'elastic', 'fade' or 'none'
  134. openSpeed: 250,
  135. openEasing: 'swing',
  136. openOpacity: true,
  137. openMethod: 'zoomIn',
  138. // Closing fancyBox
  139. closeEffect: 'fade', // 'elastic', 'fade' or 'none'
  140. closeSpeed: 250,
  141. closeEasing: 'swing',
  142. closeOpacity: true,
  143. closeMethod: 'zoomOut',
  144. // Changing next gallery item
  145. nextEffect: 'elastic', // 'elastic', 'fade' or 'none'
  146. nextSpeed: 250,
  147. nextEasing: 'swing',
  148. nextMethod: 'changeIn',
  149. // Changing previous gallery item
  150. prevEffect: 'elastic', // 'elastic', 'fade' or 'none'
  151. prevSpeed: 250,
  152. prevEasing: 'swing',
  153. prevMethod: 'changeOut',
  154. // Enable default helpers
  155. helpers: {
  156. overlay: true,
  157. title: true
  158. },
  159. // Callbacks
  160. onCancel: $.noop, // If canceling
  161. beforeLoad: $.noop, // Before loading
  162. afterLoad: $.noop, // After loading
  163. beforeShow: $.noop, // Before changing in current item
  164. afterShow: $.noop, // After opening
  165. beforeChange: $.noop, // Before changing gallery item
  166. beforeClose: $.noop, // Before closing
  167. afterClose: $.noop // After closing
  168. },
  169. //Current state
  170. group: {}, // Selected group
  171. opts: {}, // Group options
  172. previous: null, // Previous element
  173. coming: null, // Element being loaded
  174. current: null, // Currently loaded element
  175. isActive: false, // Is activated
  176. isOpen: false, // Is currently open
  177. isOpened: false, // Have been fully opened at least once
  178. wrap: null,
  179. skin: null,
  180. outer: null,
  181. inner: null,
  182. player: {
  183. timer: null,
  184. isActive: false
  185. },
  186. // Loaders
  187. ajaxLoad: null,
  188. imgPreload: null,
  189. // Some collections
  190. transitions: {},
  191. helpers: {},
  192. /*
  193. * Static methods
  194. */
  195. open: function (group, opts) {
  196. if (!group) {
  197. return;
  198. }
  199. if (!$.isPlainObject(opts)) {
  200. opts = {};
  201. }
  202. // Close if already active
  203. if (false === F.close(true)) {
  204. return;
  205. }
  206. // Normalize group
  207. if (!$.isArray(group)) {
  208. group = isQuery(group) ? $(group).get() : [group];
  209. }
  210. // Recheck if the type of each element is `object` and set content type (image, ajax, etc)
  211. $.each(group, function (i, element) {
  212. var obj = {},
  213. href,
  214. title,
  215. content,
  216. type,
  217. rez,
  218. hrefParts,
  219. selector;
  220. if ($.type(element) === "object") {
  221. // Check if is DOM element
  222. if (element.nodeType) {
  223. element = $(element);
  224. }
  225. if (isQuery(element)) {
  226. obj = {
  227. href: element.data('fancybox-href') || element.attr('href'),
  228. title: $('<div/>').text(element.data('fancybox-title') || element.attr('title') || '').html(),
  229. isDom: true,
  230. element: element
  231. };
  232. if ($.metadata) {
  233. $.extend(true, obj, element.metadata());
  234. }
  235. } else {
  236. obj = element;
  237. }
  238. }
  239. href = opts.href || obj.href || (isString(element) ? element : null);
  240. title = opts.title !== undefined ? opts.title : obj.title || '';
  241. content = opts.content || obj.content;
  242. type = content ? 'html' : (opts.type || obj.type);
  243. if (!type && obj.isDom) {
  244. type = element.data('fancybox-type');
  245. if (!type) {
  246. rez = element.prop('class').match(/fancybox\.(\w+)/);
  247. type = rez ? rez[1] : null;
  248. }
  249. }
  250. if (isString(href)) {
  251. // Try to guess the content type
  252. if (!type) {
  253. if (F.isImage(href)) {
  254. type = 'image';
  255. } else if (F.isSWF(href)) {
  256. type = 'swf';
  257. } else if (href.charAt(0) === '#') {
  258. type = 'inline';
  259. } else if (isString(element)) {
  260. type = 'html';
  261. content = element;
  262. }
  263. }
  264. // Split url into two pieces with source url and content selector, e.g,
  265. // "/mypage.html #my_id" will load "/mypage.html" and display element having id "my_id"
  266. if (type === 'ajax') {
  267. hrefParts = href.split(/\s+/, 2);
  268. href = hrefParts.shift();
  269. selector = hrefParts.shift();
  270. }
  271. }
  272. if (!content) {
  273. if (type === 'inline') {
  274. if (href) {
  275. content = $(isString(href) ? href.replace(/.*(?=#[^\s]+$)/, '') : href); //strip for ie7
  276. } else if (obj.isDom) {
  277. content = element;
  278. }
  279. } else if (type === 'html') {
  280. content = href;
  281. } else if (!type && !href && obj.isDom) {
  282. type = 'inline';
  283. content = element;
  284. }
  285. }
  286. $.extend(obj, {
  287. href: href,
  288. type: type,
  289. content: content,
  290. title: title,
  291. selector: selector
  292. });
  293. group[i] = obj;
  294. });
  295. // Extend the defaults
  296. F.opts = $.extend(true, {}, F.defaults, opts);
  297. // All options are merged recursive except keys
  298. if (opts.keys !== undefined) {
  299. F.opts.keys = opts.keys ? $.extend({}, F.defaults.keys, opts.keys) : false;
  300. }
  301. F.group = group;
  302. return F._start(F.opts.index);
  303. },
  304. // Cancel image loading or abort ajax request
  305. cancel: function () {
  306. var coming = F.coming;
  307. if (coming && false === F.trigger('onCancel')) {
  308. return;
  309. }
  310. F.hideLoading();
  311. if (!coming) {
  312. return;
  313. }
  314. if (F.ajaxLoad) {
  315. F.ajaxLoad.abort();
  316. }
  317. F.ajaxLoad = null;
  318. if (F.imgPreload) {
  319. F.imgPreload.onload = F.imgPreload.onerror = null;
  320. }
  321. if (coming.wrap) {
  322. coming.wrap.stop(true, true).trigger('onReset').remove();
  323. }
  324. F.coming = null;
  325. // If the first item has been canceled, then clear everything
  326. if (!F.current) {
  327. F._afterZoomOut(coming);
  328. }
  329. },
  330. // Start closing animation if is open; remove immediately if opening/closing
  331. close: function (event) {
  332. F.cancel();
  333. if (false === F.trigger('beforeClose')) {
  334. return;
  335. }
  336. F.unbindEvents();
  337. if (!F.isActive) {
  338. return;
  339. }
  340. if (!F.isOpen || event === true) {
  341. $('.fancybox-wrap').stop(true).trigger('onReset').remove();
  342. F._afterZoomOut();
  343. } else {
  344. F.isOpen = F.isOpened = false;
  345. F.isClosing = true;
  346. $('.fancybox-item, .fancybox-nav').remove();
  347. F.wrap.stop(true, true).removeClass('fancybox-opened');
  348. F.transitions[F.current.closeMethod]();
  349. }
  350. },
  351. // Manage slideshow:
  352. // $.fancybox.play(); - toggle slideshow
  353. // $.fancybox.play( true ); - start
  354. // $.fancybox.play( false ); - stop
  355. play: function (action) {
  356. var clear = function () {
  357. clearTimeout(F.player.timer);
  358. },
  359. set = function () {
  360. clear();
  361. if (F.current && F.player.isActive) {
  362. F.player.timer = setTimeout(F.next, F.current.playSpeed);
  363. }
  364. },
  365. stop = function () {
  366. clear();
  367. D.unbind('.player');
  368. F.player.isActive = false;
  369. F.trigger('onPlayEnd');
  370. },
  371. start = function () {
  372. if (F.current && (F.current.loop || F.current.index < F.group.length - 1)) {
  373. F.player.isActive = true;
  374. D.bind({
  375. 'onCancel.player beforeClose.player': stop,
  376. 'onUpdate.player': set,
  377. 'beforeLoad.player': clear
  378. });
  379. set();
  380. F.trigger('onPlayStart');
  381. }
  382. };
  383. if (action === true || (!F.player.isActive && action !== false)) {
  384. start();
  385. } else {
  386. stop();
  387. }
  388. },
  389. // Navigate to next gallery item
  390. next: function (direction) {
  391. var current = F.current;
  392. if (current) {
  393. if (!isString(direction)) {
  394. direction = current.direction.next;
  395. }
  396. F.jumpto(current.index + 1, direction, 'next');
  397. }
  398. },
  399. // Navigate to previous gallery item
  400. prev: function (direction) {
  401. var current = F.current;
  402. if (current) {
  403. if (!isString(direction)) {
  404. direction = current.direction.prev;
  405. }
  406. F.jumpto(current.index - 1, direction, 'prev');
  407. }
  408. },
  409. // Navigate to gallery item by index
  410. jumpto: function (index, direction, router) {
  411. var current = F.current;
  412. if (!current) {
  413. return;
  414. }
  415. index = getScalar(index);
  416. F.direction = direction || current.direction[(index >= current.index ? 'next' : 'prev')];
  417. F.router = router || 'jumpto';
  418. if (current.loop) {
  419. if (index < 0) {
  420. index = current.group.length + (index % current.group.length);
  421. }
  422. index = index % current.group.length;
  423. }
  424. if (current.group[index] !== undefined) {
  425. F.cancel();
  426. F._start(index);
  427. }
  428. },
  429. // Center inside viewport and toggle position type to fixed or absolute if needed
  430. reposition: function (e, onlyAbsolute) {
  431. var current = F.current,
  432. wrap = current ? current.wrap : null,
  433. pos;
  434. if (wrap) {
  435. pos = F._getPosition(onlyAbsolute);
  436. if (e && e.type === 'scroll') {
  437. delete pos.position;
  438. wrap.stop(true, true).animate(pos, 200);
  439. } else {
  440. wrap.css(pos);
  441. current.pos = $.extend({}, current.dim, pos);
  442. }
  443. }
  444. },
  445. update: function (e) {
  446. var type = (e && e.originalEvent && e.originalEvent.type),
  447. anyway = !type || type === 'orientationchange';
  448. if (anyway) {
  449. clearTimeout(didUpdate);
  450. didUpdate = null;
  451. }
  452. if (!F.isOpen || didUpdate) {
  453. return;
  454. }
  455. didUpdate = setTimeout(function () {
  456. var current = F.current;
  457. if (!current || F.isClosing) {
  458. return;
  459. }
  460. F.wrap.removeClass('fancybox-tmp');
  461. if (anyway || type === 'load' || (type === 'resize' && current.autoResize)) {
  462. F._setDimension();
  463. }
  464. if (!(type === 'scroll' && current.canShrink)) {
  465. F.reposition(e);
  466. }
  467. F.trigger('onUpdate');
  468. didUpdate = null;
  469. }, (anyway && !isTouch ? 0 : 300));
  470. },
  471. // Shrink content to fit inside viewport or restore if resized
  472. toggle: function (action) {
  473. if (F.isOpen) {
  474. F.current.fitToView = $.type(action) === "boolean" ? action : !F.current.fitToView;
  475. // Help browser to restore document dimensions
  476. if (isTouch) {
  477. F.wrap.removeAttr('style').addClass('fancybox-tmp');
  478. F.trigger('onUpdate');
  479. }
  480. F.update();
  481. }
  482. },
  483. hideLoading: function () {
  484. D.unbind('.loading');
  485. $('#fancybox-loading').remove();
  486. },
  487. showLoading: function () {
  488. var el, viewport;
  489. F.hideLoading();
  490. el = $(F.opts.tpl.loading).click(F.cancel).appendTo('body');
  491. // If user will press the escape-button, the request will be canceled
  492. D.bind('keydown.loading', function (e) {
  493. if ((e.which || e.keyCode) === 27) {
  494. e.preventDefault();
  495. F.cancel();
  496. }
  497. });
  498. if (!F.defaults.fixed) {
  499. viewport = F.getViewport();
  500. el.css({
  501. position: 'absolute',
  502. top: (viewport.h * 0.5) + viewport.y,
  503. left: (viewport.w * 0.5) + viewport.x
  504. });
  505. }
  506. F.trigger('onLoading');
  507. },
  508. getViewport: function () {
  509. var locked = (F.current && F.current.locked) || false,
  510. rez = {
  511. x: W.scrollLeft(),
  512. y: W.scrollTop()
  513. };
  514. if (locked && locked.length) {
  515. rez.w = locked[0].clientWidth;
  516. rez.h = locked[0].clientHeight;
  517. } else {
  518. // See http://bugs.jquery.com/ticket/6724
  519. rez.w = isTouch && window.innerWidth ? window.innerWidth : W.width();
  520. rez.h = isTouch && window.innerHeight ? window.innerHeight : W.height();
  521. }
  522. return rez;
  523. },
  524. // Unbind the keyboard / clicking actions
  525. unbindEvents: function () {
  526. if (F.wrap && isQuery(F.wrap)) {
  527. F.wrap.unbind('.fb');
  528. }
  529. D.unbind('.fb');
  530. W.unbind('.fb');
  531. },
  532. bindEvents: function () {
  533. var current = F.current,
  534. keys;
  535. if (!current) {
  536. return;
  537. }
  538. // Changing document height on iOS devices triggers a 'resize' event,
  539. // that can change document height... repeating infinitely
  540. W.bind('orientationchange.fb' + (isTouch ? '' : ' resize.fb') + (current.autoCenter && !current.locked ? ' scroll.fb' : ''), F.update);
  541. keys = current.keys;
  542. if (keys) {
  543. D.bind('keydown.fb', function (e) {
  544. var code = e.which || e.keyCode,
  545. target = e.target || e.srcElement;
  546. // Skip esc key if loading, because showLoading will cancel preloading
  547. if (code === 27 && F.coming) {
  548. return false;
  549. }
  550. // Ignore key combinations and key events within form elements
  551. if (!e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey && !(target && (target.type || $(target).is('[contenteditable]')))) {
  552. $.each(keys, function (i, val) {
  553. if (current.group.length > 1 && val[code] !== undefined) {
  554. F[i](val[code]);
  555. e.preventDefault();
  556. return false;
  557. }
  558. if ($.inArray(code, val) > -1) {
  559. F[i]();
  560. e.preventDefault();
  561. return false;
  562. }
  563. });
  564. }
  565. });
  566. }
  567. if ($.fn.mousewheel && current.mouseWheel) {
  568. F.wrap.bind('mousewheel.fb', function (e, delta, deltaX, deltaY) {
  569. var target = e.target || null,
  570. parent = $(target),
  571. canScroll = false;
  572. while (parent.length) {
  573. if (canScroll || parent.is('.fancybox-skin') || parent.is('.fancybox-wrap')) {
  574. break;
  575. }
  576. canScroll = isScrollable(parent[0]);
  577. parent = $(parent).parent();
  578. }
  579. if (delta !== 0 && !canScroll) {
  580. if (F.group.length > 1 && !current.canShrink) {
  581. if (deltaY > 0 || deltaX > 0) {
  582. F.prev(deltaY > 0 ? 'down' : 'left');
  583. } else if (deltaY < 0 || deltaX < 0) {
  584. F.next(deltaY < 0 ? 'up' : 'right');
  585. }
  586. e.preventDefault();
  587. }
  588. }
  589. });
  590. }
  591. },
  592. trigger: function (event, o) {
  593. var ret, obj = o || F.coming || F.current;
  594. if (obj) {
  595. if ($.isFunction(obj[event])) {
  596. ret = obj[event].apply(obj, Array.prototype.slice.call(arguments, 1));
  597. }
  598. if (ret === false) {
  599. return false;
  600. }
  601. if (obj.helpers) {
  602. $.each(obj.helpers, function (helper, opts) {
  603. if (opts && F.helpers[helper] && $.isFunction(F.helpers[helper][event])) {
  604. F.helpers[helper][event]($.extend(true, {}, F.helpers[helper].defaults, opts), obj);
  605. }
  606. });
  607. }
  608. }
  609. D.trigger(event);
  610. },
  611. isImage: function (str) {
  612. return isString(str) && str.match(/(^data:image\/.*,)|(\.(jp(e|g|eg)|gif|png|bmp|webp|svg)((\?|#).*)?$)/i);
  613. },
  614. isSWF: function (str) {
  615. return isString(str) && str.match(/\.(swf)((\?|#).*)?$/i);
  616. },
  617. _start: function (index) {
  618. var coming = {},
  619. obj,
  620. href,
  621. type,
  622. margin,
  623. padding;
  624. index = getScalar(index);
  625. obj = F.group[index] || null;
  626. if (!obj) {
  627. return false;
  628. }
  629. coming = $.extend(true, {}, F.opts, obj);
  630. // Convert margin and padding properties to array - top, right, bottom, left
  631. margin = coming.margin;
  632. padding = coming.padding;
  633. if ($.type(margin) === 'number') {
  634. coming.margin = [margin, margin, margin, margin];
  635. }
  636. if ($.type(padding) === 'number') {
  637. coming.padding = [padding, padding, padding, padding];
  638. }
  639. // 'modal' propery is just a shortcut
  640. if (coming.modal) {
  641. $.extend(true, coming, {
  642. closeBtn: false,
  643. closeClick: false,
  644. nextClick: false,
  645. arrows: false,
  646. mouseWheel: false,
  647. keys: null,
  648. helpers: {
  649. overlay: {
  650. closeClick: false
  651. }
  652. }
  653. });
  654. }
  655. // 'autoSize' property is a shortcut, too
  656. if (coming.autoSize) {
  657. coming.autoWidth = coming.autoHeight = true;
  658. }
  659. if (coming.width === 'auto') {
  660. coming.autoWidth = true;
  661. }
  662. if (coming.height === 'auto') {
  663. coming.autoHeight = true;
  664. }
  665. /*
  666. * Add reference to the group, so it`s possible to access from callbacks, example:
  667. * afterLoad : function() {
  668. * this.title = 'Image ' + (this.index + 1) + ' of ' + this.group.length + (this.title ? ' - ' + this.title : '');
  669. * }
  670. */
  671. coming.group = F.group;
  672. coming.index = index;
  673. // Give a chance for callback or helpers to update coming item (type, title, etc)
  674. F.coming = coming;
  675. if (false === F.trigger('beforeLoad')) {
  676. F.coming = null;
  677. return;
  678. }
  679. type = coming.type;
  680. href = coming.href;
  681. if (!type) {
  682. F.coming = null;
  683. //If we can not determine content type then drop silently or display next/prev item if looping through gallery
  684. if (F.current && F.router && F.router !== 'jumpto') {
  685. F.current.index = index;
  686. return F[F.router](F.direction);
  687. }
  688. return false;
  689. }
  690. F.isActive = true;
  691. if (type === 'image' || type === 'swf') {
  692. coming.autoHeight = coming.autoWidth = false;
  693. coming.scrolling = 'visible';
  694. }
  695. if (type === 'image') {
  696. coming.aspectRatio = true;
  697. }
  698. if (type === 'iframe' && isTouch) {
  699. coming.scrolling = 'scroll';
  700. }
  701. // Build the neccessary markup
  702. coming.wrap = $(coming.tpl.wrap).addClass('fancybox-' + (isTouch ? 'mobile' : 'desktop') + ' fancybox-type-' + type + ' fancybox-tmp ' + coming.wrapCSS).appendTo(coming.parent || 'body');
  703. $.extend(coming, {
  704. skin: $('.fancybox-skin', coming.wrap),
  705. outer: $('.fancybox-outer', coming.wrap),
  706. inner: $('.fancybox-inner', coming.wrap)
  707. });
  708. $.each(["Top", "Right", "Bottom", "Left"], function (i, v) {
  709. coming.skin.css('padding' + v, getValue(coming.padding[i]));
  710. });
  711. F.trigger('onReady');
  712. // Check before try to load; 'inline' and 'html' types need content, others - href
  713. if (type === 'inline' || type === 'html') {
  714. if (!coming.content || !coming.content.length) {
  715. return F._error('content');
  716. }
  717. } else if (!href) {
  718. return F._error('href');
  719. }
  720. if (type === 'image') {
  721. F._loadImage();
  722. } else if (type === 'ajax') {
  723. F._loadAjax();
  724. } else if (type === 'iframe') {
  725. F._loadIframe();
  726. } else {
  727. F._afterLoad();
  728. }
  729. },
  730. _error: function (type) {
  731. $.extend(F.coming, {
  732. type: 'html',
  733. autoWidth: true,
  734. autoHeight: true,
  735. minWidth: 0,
  736. minHeight: 0,
  737. scrolling: 'no',
  738. hasError: type,
  739. content: F.coming.tpl.error
  740. });
  741. F._afterLoad();
  742. },
  743. _loadImage: function () {
  744. // Reset preload image so it is later possible to check "complete" property
  745. var img = F.imgPreload = new Image();
  746. img.onload = function () {
  747. this.onload = this.onerror = null;
  748. F.coming.width = this.width / F.opts.pixelRatio;
  749. F.coming.height = this.height / F.opts.pixelRatio;
  750. F._afterLoad();
  751. };
  752. img.onerror = function () {
  753. this.onload = this.onerror = null;
  754. F._error('image');
  755. };
  756. img.src = F.coming.href;
  757. if (img.complete !== true) {
  758. F.showLoading();
  759. }
  760. },
  761. _loadAjax: function () {
  762. var coming = F.coming;
  763. F.showLoading();
  764. F.ajaxLoad = $.ajax($.extend({}, coming.ajax, {
  765. url: coming.href,
  766. error: function (jqXHR, textStatus) {
  767. if (F.coming && textStatus !== 'abort') {
  768. F._error('ajax', jqXHR);
  769. } else {
  770. F.hideLoading();
  771. }
  772. },
  773. success: function (data, textStatus) {
  774. if (textStatus === 'success') {
  775. coming.content = data;
  776. F._afterLoad();
  777. }
  778. }
  779. }));
  780. },
  781. _loadIframe: function () {
  782. var coming = F.coming,
  783. iframe = $(coming.tpl.iframe.replace(/\{rnd\}/g, new Date().getTime()))
  784. .attr('scrolling', isTouch ? 'auto' : coming.iframe.scrolling)
  785. .attr('src', coming.href);
  786. // This helps IE
  787. $(coming.wrap).bind('onReset', function () {
  788. try {
  789. $(this).find('iframe').hide().attr('src', '//about:blank').end().empty();
  790. } catch (e) {
  791. }
  792. });
  793. if (coming.iframe.preload) {
  794. F.showLoading();
  795. iframe.one('load', function () {
  796. $(this).data('ready', 1);
  797. // iOS will lose scrolling if we resize
  798. if (!isTouch) {
  799. $(this).bind('load.fb', F.update);
  800. }
  801. // Without this trick:
  802. // - iframe won't scroll on iOS devices
  803. // - IE7 sometimes displays empty iframe
  804. $(this).parents('.fancybox-wrap').width('100%').removeClass('fancybox-tmp').show();
  805. F._afterLoad();
  806. });
  807. }
  808. coming.content = iframe.appendTo(coming.inner);
  809. if (!coming.iframe.preload) {
  810. F._afterLoad();
  811. }
  812. },
  813. _preloadImages: function () {
  814. var group = F.group,
  815. current = F.current,
  816. len = group.length,
  817. cnt = current.preload ? Math.min(current.preload, len - 1) : 0,
  818. item,
  819. i;
  820. for (i = 1; i <= cnt; i += 1) {
  821. item = group[(current.index + i ) % len];
  822. if (item.type === 'image' && item.href) {
  823. new Image().src = item.href;
  824. }
  825. }
  826. },
  827. _afterLoad: function () {
  828. var coming = F.coming,
  829. previous = F.current,
  830. placeholder = 'fancybox-placeholder',
  831. current,
  832. content,
  833. type,
  834. scrolling,
  835. href,
  836. embed;
  837. F.hideLoading();
  838. if (!coming || F.isActive === false) {
  839. return;
  840. }
  841. if (false === F.trigger('afterLoad', coming, previous)) {
  842. coming.wrap.stop(true).trigger('onReset').remove();
  843. F.coming = null;
  844. return;
  845. }
  846. if (previous) {
  847. F.trigger('beforeChange', previous);
  848. previous.wrap.stop(true).removeClass('fancybox-opened')
  849. .find('.fancybox-item, .fancybox-nav')
  850. .remove();
  851. }
  852. F.unbindEvents();
  853. current = coming;
  854. content = coming.content;
  855. type = coming.type;
  856. scrolling = coming.scrolling;
  857. $.extend(F, {
  858. wrap: current.wrap,
  859. skin: current.skin,
  860. outer: current.outer,
  861. inner: current.inner,
  862. current: current,
  863. previous: previous
  864. });
  865. href = current.href;
  866. switch (type) {
  867. case 'inline':
  868. case 'ajax':
  869. case 'html':
  870. if (current.selector) {
  871. content = $('<div>').html(content).find(current.selector);
  872. } else if (isQuery(content)) {
  873. if (!content.data(placeholder)) {
  874. content.data(placeholder, $('<div class="' + placeholder + '"></div>').insertAfter(content).hide());
  875. }
  876. content = content.show().detach();
  877. current.wrap.bind('onReset', function () {
  878. if ($(this).find(content).length) {
  879. content.hide().replaceAll(content.data(placeholder)).data(placeholder, false);
  880. }
  881. });
  882. }
  883. break;
  884. case 'image':
  885. content = current.tpl.image.replace(/\{href\}/g, href);
  886. break;
  887. case 'swf':
  888. content = '<object id="fancybox-swf" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100%" height="100%"><param name="movie" value="' + href + '"></param>';
  889. embed = '';
  890. $.each(current.swf, function (name, val) {
  891. content += '<param name="' + name + '" value="' + val + '"></param>';
  892. embed += ' ' + name + '="' + val + '"';
  893. });
  894. content += '<embed src="' + href + '" type="application/x-shockwave-flash" width="100%" height="100%"' + embed + '></embed></object>';
  895. break;
  896. }
  897. if (!(isQuery(content) && content.parent().is(current.inner))) {
  898. current.inner.append(content);
  899. }
  900. // Give a chance for helpers or callbacks to update elements
  901. F.trigger('beforeShow');
  902. // Set scrolling before calculating dimensions
  903. current.inner.css('overflow', scrolling === 'yes' ? 'scroll' : (scrolling === 'no' ? 'hidden' : scrolling));
  904. // Set initial dimensions and start position
  905. F._setDimension();
  906. F.reposition();
  907. F.isOpen = false;
  908. F.coming = null;
  909. F.bindEvents();
  910. if (!F.isOpened) {
  911. $('.fancybox-wrap').not(current.wrap).stop(true).trigger('onReset').remove();
  912. } else if (previous.prevMethod) {
  913. F.transitions[previous.prevMethod]();
  914. }
  915. F.transitions[F.isOpened ? current.nextMethod : current.openMethod]();
  916. F._preloadImages();
  917. },
  918. _setDimension: function () {
  919. var viewport = F.getViewport(),
  920. steps = 0,
  921. canShrink = false,
  922. canExpand = false,
  923. wrap = F.wrap,
  924. skin = F.skin,
  925. inner = F.inner,
  926. current = F.current,
  927. width = current.width,
  928. height = current.height,
  929. minWidth = current.minWidth,
  930. minHeight = current.minHeight,
  931. maxWidth = current.maxWidth,
  932. maxHeight = current.maxHeight,
  933. scrolling = current.scrolling,
  934. scrollOut = current.scrollOutside ? current.scrollbarWidth : 0,
  935. margin = current.margin,
  936. wMargin = getScalar(margin[1] + margin[3]),
  937. hMargin = getScalar(margin[0] + margin[2]),
  938. wPadding,
  939. hPadding,
  940. wSpace,
  941. hSpace,
  942. origWidth,
  943. origHeight,
  944. origMaxWidth,
  945. origMaxHeight,
  946. ratio,
  947. width_,
  948. height_,
  949. maxWidth_,
  950. maxHeight_,
  951. iframe,
  952. body;
  953. // Reset dimensions so we could re-check actual size
  954. wrap.add(skin).add(inner).width('auto').height('auto').removeClass('fancybox-tmp');
  955. wPadding = getScalar(skin.outerWidth(true) - skin.width());
  956. hPadding = getScalar(skin.outerHeight(true) - skin.height());
  957. // Any space between content and viewport (margin, padding, border, title)
  958. wSpace = wMargin + wPadding;
  959. hSpace = hMargin + hPadding;
  960. origWidth = isPercentage(width) ? (viewport.w - wSpace) * getScalar(width) / 100 : width;
  961. origHeight = isPercentage(height) ? (viewport.h - hSpace) * getScalar(height) / 100 : height;
  962. if (current.type === 'iframe') {
  963. iframe = current.content;
  964. if (current.autoHeight && iframe && iframe.data('ready') === 1) {
  965. try {
  966. if (iframe[0].contentWindow.document.location) {
  967. inner.width(origWidth).height(9999);
  968. body = iframe.contents().find('body');
  969. if (scrollOut) {
  970. body.css('overflow-x', 'hidden');
  971. }
  972. origHeight = body.outerHeight(true);
  973. }
  974. } catch (e) {
  975. }
  976. }
  977. } else if (current.autoWidth || current.autoHeight) {
  978. inner.addClass('fancybox-tmp');
  979. // Set width or height in case we need to calculate only one dimension
  980. if (!current.autoWidth) {
  981. inner.width(origWidth);
  982. }
  983. if (!current.autoHeight) {
  984. inner.height(origHeight);
  985. }
  986. if (current.autoWidth) {
  987. origWidth = inner.width();
  988. }
  989. if (current.autoHeight) {
  990. origHeight = inner.height();
  991. }
  992. inner.removeClass('fancybox-tmp');
  993. }
  994. width = getScalar(origWidth);
  995. height = getScalar(origHeight);
  996. ratio = origWidth / origHeight;
  997. // Calculations for the content
  998. minWidth = getScalar(isPercentage(minWidth) ? getScalar(minWidth, 'w') - wSpace : minWidth);
  999. maxWidth = getScalar(isPercentage(maxWidth) ? getScalar(maxWidth, 'w') - wSpace : maxWidth);
  1000. minHeight = getScalar(isPercentage(minHeight) ? getScalar(minHeight, 'h') - hSpace : minHeight);
  1001. maxHeight = getScalar(isPercentage(maxHeight) ? getScalar(maxHeight, 'h') - hSpace : maxHeight);
  1002. // These will be used to determine if wrap can fit in the viewport
  1003. origMaxWidth = maxWidth;
  1004. origMaxHeight = maxHeight;
  1005. if (current.fitToView) {
  1006. maxWidth = Math.min(viewport.w - wSpace, maxWidth);
  1007. maxHeight = Math.min(viewport.h - hSpace, maxHeight);
  1008. }
  1009. maxWidth_ = viewport.w - wMargin;
  1010. maxHeight_ = viewport.h - hMargin;
  1011. if (current.aspectRatio) {
  1012. if (width > maxWidth) {
  1013. width = maxWidth;
  1014. height = getScalar(width / ratio);
  1015. }
  1016. if (height > maxHeight) {
  1017. height = maxHeight;
  1018. width = getScalar(height * ratio);
  1019. }
  1020. if (width < minWidth) {
  1021. width = minWidth;
  1022. height = getScalar(width / ratio);
  1023. }
  1024. if (height < minHeight) {
  1025. height = minHeight;
  1026. width = getScalar(height * ratio);
  1027. }
  1028. } else {
  1029. width = Math.max(minWidth, Math.min(width, maxWidth));
  1030. if (current.autoHeight && current.type !== 'iframe') {
  1031. inner.width(width);
  1032. height = inner.height();
  1033. }
  1034. height = Math.max(minHeight, Math.min(height, maxHeight));
  1035. }
  1036. // Try to fit inside viewport (including the title)
  1037. if (current.fitToView) {
  1038. inner.width(width).height(height);
  1039. wrap.width(width + wPadding);
  1040. // Real wrap dimensions
  1041. width_ = wrap.width();
  1042. height_ = wrap.height();
  1043. if (current.aspectRatio) {
  1044. while ((width_ > maxWidth_ || height_ > maxHeight_) && width > minWidth && height > minHeight) {
  1045. if (steps++ > 19) {
  1046. break;
  1047. }
  1048. height = Math.max(minHeight, Math.min(maxHeight, height - 10));
  1049. width = getScalar(height * ratio);
  1050. if (width < minWidth) {
  1051. width = minWidth;
  1052. height = getScalar(width / ratio);
  1053. }
  1054. if (width > maxWidth) {
  1055. width = maxWidth;
  1056. height = getScalar(width / ratio);
  1057. }
  1058. inner.width(width).height(height);
  1059. wrap.width(width + wPadding);
  1060. width_ = wrap.width();
  1061. height_ = wrap.height();
  1062. }
  1063. } else {
  1064. width = Math.max(minWidth, Math.min(width, width - (width_ - maxWidth_)));
  1065. height = Math.max(minHeight, Math.min(height, height - (height_ - maxHeight_)));
  1066. }
  1067. }
  1068. if (scrollOut && scrolling === 'auto' && height < origHeight && (width + wPadding + scrollOut) < maxWidth_) {
  1069. width += scrollOut;
  1070. }
  1071. inner.width(width).height(height);
  1072. wrap.width(width + wPadding);
  1073. width_ = wrap.width();
  1074. height_ = wrap.height();
  1075. canShrink = (width_ > maxWidth_ || height_ > maxHeight_) && width > minWidth && height > minHeight;
  1076. canExpand = current.aspectRatio ? (width < origMaxWidth && height < origMaxHeight && width < origWidth && height < origHeight) : ((width < origMaxWidth || height < origMaxHeight) && (width < origWidth || height < origHeight));
  1077. $.extend(current, {
  1078. dim: {
  1079. width: getValue(width_),
  1080. height: getValue(height_)
  1081. },
  1082. origWidth: origWidth,
  1083. origHeight: origHeight,
  1084. canShrink: canShrink,
  1085. canExpand: canExpand,
  1086. wPadding: wPadding,
  1087. hPadding: hPadding,
  1088. wrapSpace: height_ - skin.outerHeight(true),
  1089. skinSpace: skin.height() - height
  1090. });
  1091. if (!iframe && current.autoHeight && height > minHeight && height < maxHeight && !canExpand) {
  1092. inner.height('auto');
  1093. }
  1094. },
  1095. _getPosition: function (onlyAbsolute) {
  1096. var current = F.current,
  1097. viewport = F.getViewport(),
  1098. margin = current.margin,
  1099. width = F.wrap.width() + margin[1] + margin[3],
  1100. height = F.wrap.height() + margin[0] + margin[2],
  1101. rez = {
  1102. position: 'absolute',
  1103. top: margin[0],
  1104. left: margin[3]
  1105. };
  1106. if (current.autoCenter && current.fixed && !onlyAbsolute && height <= viewport.h && width <= viewport.w) {
  1107. rez.position = 'fixed';
  1108. } else if (!current.locked) {
  1109. rez.top += viewport.y;
  1110. rez.left += viewport.x;
  1111. }
  1112. rez.top = getValue(Math.max(rez.top, rez.top + ((viewport.h - height) * current.topRatio)));
  1113. rez.left = getValue(Math.max(rez.left, rez.left + ((viewport.w - width) * current.leftRatio)));
  1114. return rez;
  1115. },
  1116. _afterZoomIn: function () {
  1117. var current = F.current;
  1118. if (!current) {
  1119. return;
  1120. }
  1121. F.isOpen = F.isOpened = true;
  1122. F.wrap.css('overflow', 'visible').addClass('fancybox-opened').hide().show(0);
  1123. F.update();
  1124. // Assign a click event
  1125. if (current.closeClick || (current.nextClick && F.group.length > 1)) {
  1126. F.inner.css('cursor', 'pointer').bind('click.fb', function (e) {
  1127. if (!$(e.target).is('a') && !$(e.target).parent().is('a')) {
  1128. e.preventDefault();
  1129. F[current.closeClick ? 'close' : 'next']();
  1130. }
  1131. });
  1132. }
  1133. // Create a close button
  1134. if (current.closeBtn) {
  1135. $(current.tpl.closeBtn).appendTo(F.skin).bind('click.fb', function (e) {
  1136. e.preventDefault();
  1137. F.close();
  1138. });
  1139. }
  1140. // Create navigation arrows
  1141. if (current.arrows && F.group.length > 1) {
  1142. if (current.loop || current.index > 0) {
  1143. $(current.tpl.prev).appendTo(F.outer).bind('click.fb', F.prev);
  1144. }
  1145. if (current.loop || current.index < F.group.length - 1) {
  1146. $(current.tpl.next).appendTo(F.outer).bind('click.fb', F.next);
  1147. }
  1148. }
  1149. F.trigger('afterShow');
  1150. // Stop the slideshow if this is the last item
  1151. if (!current.loop && current.index === current.group.length - 1) {
  1152. F.play(false);
  1153. } else if (F.opts.autoPlay && !F.player.isActive) {
  1154. F.opts.autoPlay = false;
  1155. F.play(true);
  1156. }
  1157. },
  1158. _afterZoomOut: function (obj) {
  1159. obj = obj || F.current;
  1160. $('.fancybox-wrap').trigger('onReset').remove();
  1161. $.extend(F, {
  1162. group: {},
  1163. opts: {},
  1164. router: false,
  1165. current: null,
  1166. isActive: false,
  1167. isOpened: false,
  1168. isOpen: false,
  1169. isClosing: false,
  1170. wrap: null,
  1171. skin: null,
  1172. outer: null,
  1173. inner: null
  1174. });
  1175. F.trigger('afterClose', obj);
  1176. }
  1177. });
  1178. /*
  1179. * Default transitions
  1180. */
  1181. F.transitions = {
  1182. getOrigPosition: function () {
  1183. var current = F.current,
  1184. element = current.element,
  1185. orig = current.orig,
  1186. pos = {},
  1187. width = 50,
  1188. height = 50,
  1189. hPadding = current.hPadding,
  1190. wPadding = current.wPadding,
  1191. viewport = F.getViewport();
  1192. if (!orig && current.isDom && element.is(':visible')) {
  1193. orig = element.find('img:first');
  1194. if (!orig.length) {
  1195. orig = element;
  1196. }
  1197. }
  1198. if (isQuery(orig)) {
  1199. pos = orig.offset();
  1200. if (orig.is('img')) {
  1201. width = orig.outerWidth();
  1202. height = orig.outerHeight();
  1203. }
  1204. } else {
  1205. pos.top = viewport.y + (viewport.h - height) * current.topRatio;
  1206. pos.left = viewport.x + (viewport.w - width) * current.leftRatio;
  1207. }
  1208. if (F.wrap.css('position') === 'fixed' || current.locked) {
  1209. pos.top -= viewport.y;
  1210. pos.left -= viewport.x;
  1211. }
  1212. pos = {
  1213. top: getValue(pos.top - hPadding * current.topRatio),
  1214. left: getValue(pos.left - wPadding * current.leftRatio),
  1215. width: getValue(width + wPadding),
  1216. height: getValue(height + hPadding)
  1217. };
  1218. return pos;
  1219. },
  1220. step: function (now, fx) {
  1221. var ratio,
  1222. padding,
  1223. value,
  1224. prop = fx.prop,
  1225. current = F.current,
  1226. wrapSpace = current.wrapSpace,
  1227. skinSpace = current.skinSpace;
  1228. if (prop === 'width' || prop === 'height') {
  1229. ratio = fx.end === fx.start ? 1 : (now - fx.start) / (fx.end - fx.start);
  1230. if (F.isClosing) {
  1231. ratio = 1 - ratio;
  1232. }
  1233. padding = prop === 'width' ? current.wPadding : current.hPadding;
  1234. value = now - padding;
  1235. F.skin[prop](getScalar(prop === 'width' ? value : value - (wrapSpace * ratio)));
  1236. F.inner[prop](getScalar(prop === 'width' ? value : value - (wrapSpace * ratio) - (skinSpace * ratio)));
  1237. }
  1238. },
  1239. zoomIn: function () {
  1240. var current = F.current,
  1241. startPos = current.pos,
  1242. effect = current.openEffect,
  1243. elastic = effect === 'elastic',
  1244. endPos = $.extend({opacity: 1}, startPos);
  1245. // Remove "position" property that breaks older IE
  1246. delete endPos.position;
  1247. if (elastic) {
  1248. startPos = this.getOrigPosition();
  1249. if (current.openOpacity) {
  1250. startPos.opacity = 0.1;
  1251. }
  1252. } else if (effect === 'fade') {
  1253. startPos.opacity = 0.1;
  1254. }
  1255. F.wrap.css(startPos).animate(endPos, {
  1256. duration: effect === 'none' ? 0 : current.openSpeed,
  1257. easing: current.openEasing,
  1258. step: elastic ? this.step : null,
  1259. complete: F._afterZoomIn
  1260. });
  1261. },
  1262. zoomOut: function () {
  1263. var current = F.current,
  1264. effect = current.closeEffect,
  1265. elastic = effect === 'elastic',
  1266. endPos = {opacity: 0.1};
  1267. if (elastic) {
  1268. endPos = this.getOrigPosition();
  1269. if (current.closeOpacity) {
  1270. endPos.opacity = 0.1;
  1271. }
  1272. }
  1273. F.wrap.animate(endPos, {
  1274. duration: effect === 'none' ? 0 : current.closeSpeed,
  1275. easing: current.closeEasing,
  1276. step: elastic ? this.step : null,
  1277. complete: F._afterZoomOut
  1278. });
  1279. },
  1280. changeIn: function () {
  1281. var current = F.current,
  1282. effect = current.nextEffect,
  1283. startPos = current.pos,
  1284. endPos = {opacity: 1},
  1285. direction = F.direction,
  1286. distance = 200,
  1287. field;
  1288. startPos.opacity = 0.1;
  1289. if (effect === 'elastic') {
  1290. field = direction === 'down' || direction === 'up' ? 'top' : 'left';
  1291. if (direction === 'down' || direction === 'right') {
  1292. startPos[field] = getValue(getScalar(startPos[field]) - distance);
  1293. endPos[field] = '+=' + distance + 'px';
  1294. } else {
  1295. startPos[field] = getValue(getScalar(startPos[field]) + distance);
  1296. endPos[field] = '-=' + distance + 'px';
  1297. }
  1298. }
  1299. // Workaround for http://bugs.jquery.com/ticket/12273
  1300. if (effect === 'none') {
  1301. F._afterZoomIn();
  1302. } else {
  1303. F.wrap.css(startPos).animate(endPos, {
  1304. duration: current.nextSpeed,
  1305. easing: current.nextEasing,
  1306. complete: F._afterZoomIn
  1307. });
  1308. }
  1309. },
  1310. changeOut: function () {
  1311. var previous = F.previous,
  1312. effect = previous.prevEffect,
  1313. endPos = {opacity: 0.1},
  1314. direction = F.direction,
  1315. distance = 200;
  1316. if (effect === 'elastic') {
  1317. endPos[direction === 'down' || direction === 'up' ? 'top' : 'left'] = ( direction === 'up' || direction === 'left' ? '-' : '+' ) + '=' + distance + 'px';
  1318. }
  1319. previous.wrap.animate(endPos, {
  1320. duration: effect === 'none' ? 0 : previous.prevSpeed,
  1321. easing: previous.prevEasing,
  1322. complete: function () {
  1323. $(this).trigger('onReset').remove();
  1324. }
  1325. });
  1326. }
  1327. };
  1328. /*
  1329. * Overlay helper
  1330. */
  1331. F.helpers.overlay = {
  1332. defaults: {
  1333. closeClick: true, // if true, fancyBox will be closed when user clicks on the overlay
  1334. speedOut: 200, // duration of fadeOut animation
  1335. showEarly: true, // indicates if should be opened immediately or wait until the content is ready
  1336. css: {}, // custom CSS properties
  1337. locked: !isTouch, // if true, the content will be locked into overlay
  1338. fixed: true // if false, the overlay CSS position property will not be set to "fixed"
  1339. },
  1340. overlay: null, // current handle
  1341. fixed: false, // indicates if the overlay has position "fixed"
  1342. el: $('html'), // element that contains "the lock"
  1343. // Public methods
  1344. create: function (opts) {
  1345. var parent;
  1346. opts = $.extend({}, this.defaults, opts);
  1347. if (this.overlay) {
  1348. this.close();
  1349. }
  1350. parent = F.coming ? F.coming.parent : opts.parent;
  1351. this.overlay = $('<div class="fancybox-overlay"></div>').appendTo(parent && parent.length ? parent : 'body');
  1352. this.fixed = false;
  1353. if (opts.fixed && F.defaults.fixed) {
  1354. this.overlay.addClass('fancybox-overlay-fixed');
  1355. this.fixed = true;
  1356. }
  1357. },
  1358. open: function (opts) {
  1359. var that = this;
  1360. opts = $.extend({}, this.defaults, opts);
  1361. if (this.overlay) {
  1362. this.overlay.unbind('.overlay').width('auto').height('auto');
  1363. } else {
  1364. this.create(opts);
  1365. }
  1366. if (!this.fixed) {
  1367. W.bind('resize.overlay', $.proxy(this.update, this));
  1368. this.update();
  1369. }
  1370. if (opts.closeClick) {
  1371. this.overlay.bind('click.overlay', function (e) {
  1372. if ($(e.target).hasClass('fancybox-overlay')) {
  1373. if (F.isActive) {
  1374. F.close();
  1375. } else {
  1376. that.close();
  1377. }
  1378. return false;
  1379. }
  1380. });
  1381. }
  1382. this.overlay.css(opts.css).show();
  1383. },
  1384. close: function () {
  1385. W.unbind('resize.overlay');
  1386. if (this.el.hasClass('fancybox-lock')) {
  1387. $('.fancybox-margin').removeClass('fancybox-margin');
  1388. this.el.removeClass('fancybox-lock');
  1389. W.scrollTop(this.scrollV).scrollLeft(this.scrollH);
  1390. }
  1391. $('.fancybox-overlay').remove().hide();
  1392. $.extend(this, {
  1393. overlay: null,
  1394. fixed: false
  1395. });
  1396. },
  1397. // Private, callbacks
  1398. update: function () {
  1399. var width = '100%', offsetWidth;
  1400. // Reset width/height so it will not mess
  1401. this.overlay.width(width).height('100%');
  1402. // jQuery does not return reliable result for IE
  1403. if (IE) {
  1404. offsetWidth = Math.max(document.documentElement.offsetWidth, document.body.offsetWidth);
  1405. if (D.width() > offsetWidth) {
  1406. width = D.width();
  1407. }
  1408. } else if (D.width() > W.width()) {
  1409. width = D.width();
  1410. }
  1411. this.overlay.width(width).height(D.height());
  1412. },
  1413. // This is where we can manipulate DOM, because later it would cause iframes to reload
  1414. onReady: function (opts, obj) {
  1415. var overlay = this.overlay;
  1416. $('.fancybox-overlay').stop(true, true);
  1417. if (!overlay) {
  1418. this.create(opts);
  1419. }
  1420. if (opts.locked && this.fixed && obj.fixed) {
  1421. obj.locked = this.overlay.append(obj.wrap);
  1422. obj.fixed = false;
  1423. }
  1424. if (opts.showEarly === true) {
  1425. this.beforeShow.apply(this, arguments);
  1426. }
  1427. },
  1428. beforeShow: function (opts, obj) {
  1429. if (obj.locked && !this.el.hasClass('fancybox-lock')) {
  1430. if (this.fixPosition !== false) {
  1431. $('*:not(object)').filter(function () {
  1432. return ($(this).css('position') === 'fixed' && !$(this).hasClass("fancybox-overlay") && !$(this).hasClass("fancybox-wrap") );
  1433. }).addClass('fancybox-margin');
  1434. }
  1435. this.el.addClass('fancybox-margin');
  1436. this.scrollV = W.scrollTop();
  1437. this.scrollH = W.scrollLeft();
  1438. this.el.addClass('fancybox-lock');
  1439. W.scrollTop(this.scrollV).scrollLeft(this.scrollH);
  1440. }
  1441. this.open(opts);
  1442. },
  1443. onUpdate: function () {
  1444. if (!this.fixed) {
  1445. this.update();
  1446. }
  1447. },
  1448. afterClose: function (opts) {
  1449. // Remove overlay if exists and fancyBox is not opening
  1450. // (e.g., it is not being open using afterClose callback)
  1451. if (this.overlay && !F.coming) {
  1452. this.overlay.fadeOut(opts.speedOut, $.proxy(this.close, this));
  1453. }
  1454. }
  1455. };
  1456. /*
  1457. * Title helper
  1458. */
  1459. F.helpers.title = {
  1460. defaults: {
  1461. type: 'float', // 'float', 'inside', 'outside' or 'over',
  1462. position: 'bottom' // 'top' or 'bottom'
  1463. },
  1464. beforeShow: function (opts) {
  1465. var current = F.current,
  1466. text = current.title,
  1467. type = opts.type,
  1468. title,
  1469. target;
  1470. if ($.isFunction(text)) {
  1471. text = text.call(current.element, current);
  1472. }
  1473. if (!isString(text) || $.trim(text) === '') {
  1474. return;
  1475. }
  1476. title = $('<div class="fancybox-title fancybox-title-' + type + '-wrap">' + text + '</div>');
  1477. switch (type) {
  1478. case 'inside':
  1479. target = F.skin;
  1480. break;
  1481. case 'outside':
  1482. target = F.wrap;
  1483. break;
  1484. case 'over':
  1485. target = F.inner;
  1486. break;
  1487. default: // 'float'
  1488. target = F.skin;
  1489. title.appendTo('body');
  1490. if (IE) {
  1491. title.width(title.width());
  1492. }
  1493. title.wrapInner('<span class="child"></span>');
  1494. //Increase bottom margin so this title will also fit into viewport
  1495. F.current.margin[2] += Math.abs(getScalar(title.css('margin-bottom')));
  1496. break;
  1497. }
  1498. title[(opts.position === 'top' ? 'prependTo' : 'appendTo')](target);
  1499. }
  1500. };
  1501. // jQuery plugin initialization
  1502. $.fn.fancybox = function (options) {
  1503. var index,
  1504. that = $(this),
  1505. selector = this.selector || '',
  1506. run = function (e) {
  1507. var what = $(this).blur(), idx = index, relType, relVal;
  1508. if (!(e.ctrlKey || e.altKey || e.shiftKey || e.metaKey) && !what.is('.fancybox-wrap')) {
  1509. relType = options.groupAttr || 'data-fancybox-group';
  1510. relVal = what.attr(relType);
  1511. if (!relVal) {
  1512. relType = 'rel';
  1513. relVal = what.get(0)[relType];
  1514. }
  1515. if (relVal && relVal !== '' && relVal !== 'nofollow') {
  1516. what = selector.length ? $(selector) : that;
  1517. what = what.filter('[' + relType + '="' + relVal + '"]');
  1518. idx = what.index(this);
  1519. }
  1520. options.index = idx;
  1521. // Stop an event from bubbling if everything is fine
  1522. if (F.open(what, options) !== false) {
  1523. e.preventDefault();
  1524. }
  1525. }
  1526. };
  1527. options = options || {};
  1528. index = options.index || 0;
  1529. if (!selector || options.live === false) {
  1530. that.unbind('click.fb-start').bind('click.fb-start', run);
  1531. } else {
  1532. D.undelegate(selector, 'click.fb-start').delegate(selector + ":not('.fancybox-item, .fancybox-nav')", 'click.fb-start', run);
  1533. }
  1534. this.filter('[data-fancybox-start=1]').trigger('click');
  1535. return this;
  1536. };
  1537. // Tests that need a body at doc ready
  1538. D.ready(function () {
  1539. var w1, w2;
  1540. if ($.scrollbarWidth === undefined) {
  1541. // http://benalman.com/projects/jquery-misc-plugins/#scrollbarwidth
  1542. $.scrollbarWidth = function () {
  1543. var parent = $('<div style="width:50px;height:50px;overflow:auto"><div/></div>').appendTo('body'),
  1544. child = parent.children(),
  1545. width = child.innerWidth() - child.height(99).innerWidth();
  1546. parent.remove();
  1547. return width;
  1548. };
  1549. }
  1550. if ($.support.fixedPosition === undefined) {
  1551. $.support.fixedPosition = (function () {
  1552. var elem = $('<div style="position:fixed;top:20px;"></div>').appendTo('body'),
  1553. fixed = ( elem[0].offsetTop === 20 || elem[0].offsetTop === 15 );
  1554. elem.remove();
  1555. return fixed;
  1556. }());
  1557. }
  1558. $.extend(F.defaults, {
  1559. scrollbarWidth: $.scrollbarWidth(),
  1560. fixed: $.support.fixedPosition,
  1561. parent: $('body')
  1562. });
  1563. //Get real width of page scroll-bar
  1564. w1 = $(window).width();
  1565. H.addClass('fancybox-lock-test');
  1566. w2 = $(window).width();
  1567. H.removeClass('fancybox-lock-test');
  1568. $("<style type='text/css'>.fancybox-margin{margin-right:" + (w2 - w1) + "px;}</style>").appendTo("head");
  1569. });
  1570. }(window, document, jQuery));