jquery.fancybox.js 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /*
  2. * FancyBox - jQuery Plugin
  3. * Simple and fancy lightbox alternative
  4. *
  5. * Examples and documentation at: http://fancybox.net
  6. *
  7. * Copyright (c) 2008 - 2010 Janis Skarnelis
  8. * That said, it is hardly a one-person project. Many people have submitted bugs, code, and offered their advice freely. Their support is greatly appreciated.
  9. *
  10. * Version: 1.3.4 (11/11/2010)
  11. * Requires: jQuery v1.3+
  12. *
  13. * Dual licensed under the MIT and GPL licenses:
  14. * http://www.opensource.org/licenses/mit-license.php
  15. * http://www.gnu.org/licenses/gpl.html
  16. */
  17. ;(function($) {
  18. var tmp, loading, overlay, wrap, outer, content, close, title, nav_left, nav_right,
  19. selectedIndex = 0, selectedOpts = {}, selectedArray = [], currentIndex = 0, currentOpts = {}, currentArray = [],
  20. ajaxLoader = null, imgPreloader = new Image(), imgRegExp = /\.(jpg|gif|png|bmp|jpeg)(.*)?$/i, swfRegExp = /[^\.]\.(swf)\s*$/i,
  21. loadingTimer, loadingFrame = 1,
  22. titleHeight = 0, titleStr = '', start_pos, final_pos, busy = false, fx = $.extend($('<div/>')[0], { prop: 0 }),
  23. isIE6 = $.browser.msie && $.browser.version < 7 && !window.XMLHttpRequest,
  24. /*
  25. * Private methods
  26. */
  27. _abort = function() {
  28. loading.hide();
  29. imgPreloader.onerror = imgPreloader.onload = null;
  30. if (ajaxLoader) {
  31. ajaxLoader.abort();
  32. }
  33. tmp.empty();
  34. },
  35. _error = function() {
  36. if (false === selectedOpts.onError(selectedArray, selectedIndex, selectedOpts)) {
  37. loading.hide();
  38. busy = false;
  39. return;
  40. }
  41. selectedOpts.titleShow = false;
  42. selectedOpts.width = 'auto';
  43. selectedOpts.height = 'auto';
  44. tmp.html( '<p id="fancybox-error">The requested content cannot be loaded.<br />Please try again later.</p>' );
  45. _process_inline();
  46. },
  47. _start = function() {
  48. var obj = selectedArray[ selectedIndex ],
  49. href,
  50. type,
  51. title,
  52. str,
  53. emb,
  54. ret;
  55. _abort();
  56. selectedOpts = $.extend({}, $.fn.fancybox.defaults, (typeof $(obj).data('fancybox') == 'undefined' ? selectedOpts : $(obj).data('fancybox')));
  57. ret = selectedOpts.onStart(selectedArray, selectedIndex, selectedOpts);
  58. if (ret === false) {
  59. busy = false;
  60. return;
  61. } else if (typeof ret == 'object') {
  62. selectedOpts = $.extend(selectedOpts, ret);
  63. }
  64. title = selectedOpts.title || (obj.nodeName ? $(obj).attr('title') : obj.title) || '';
  65. if (obj.nodeName && !selectedOpts.orig) {
  66. selectedOpts.orig = $(obj).children("img:first").length ? $(obj).children("img:first") : $(obj);
  67. }
  68. if (title === '' && selectedOpts.orig && selectedOpts.titleFromAlt) {
  69. title = selectedOpts.orig.attr('alt');
  70. }
  71. href = selectedOpts.href || (obj.nodeName ? $(obj).attr('href') : obj.href) || null;
  72. if ((/^(?:javascript)/i).test(href) || href == '#') {
  73. href = null;
  74. }
  75. if (selectedOpts.type) {
  76. type = selectedOpts.type;
  77. if (!href) {
  78. href = selectedOpts.content;
  79. }
  80. } else if (selectedOpts.content) {
  81. type = 'html';
  82. } else if (href) {
  83. if (href.match(imgRegExp)) {
  84. type = 'image';
  85. } else if (href.match(swfRegExp)) {
  86. type = 'swf';
  87. } else if ($(obj).hasClass("iframe")) {
  88. type = 'iframe';
  89. } else if (href.indexOf("#") === 0) {
  90. type = 'inline';
  91. } else {
  92. type = 'ajax';
  93. }
  94. }
  95. if (!type) {
  96. _error();
  97. return;
  98. }
  99. if (type == 'inline') {
  100. obj = href.substr(href.indexOf("#"));
  101. type = $(obj).length > 0 ? 'inline' : 'ajax';
  102. }
  103. selectedOpts.type = type;
  104. selectedOpts.href = href;
  105. selectedOpts.title = title;
  106. if (selectedOpts.autoDimensions) {
  107. if (selectedOpts.type == 'html' || selectedOpts.type == 'inline' || selectedOpts.type == 'ajax') {
  108. selectedOpts.width = 'auto';
  109. selectedOpts.height = 'auto';
  110. } else {
  111. selectedOpts.autoDimensions = false;
  112. }
  113. }
  114. if (selectedOpts.modal) {
  115. selectedOpts.overlayShow = true;
  116. selectedOpts.hideOnOverlayClick = false;
  117. selectedOpts.hideOnContentClick = false;
  118. selectedOpts.enableEscapeButton = false;
  119. selectedOpts.showCloseButton = false;
  120. }
  121. selectedOpts.padding = parseInt(selectedOpts.padding, 10);
  122. selectedOpts.margin = parseInt(selectedOpts.margin, 10);
  123. tmp.css('padding', (selectedOpts.padding + selectedOpts.margin));
  124. $('.fancybox-inline-tmp').unbind('fancybox-cancel').bind('fancybox-change', function() {
  125. $(this).replaceWith(content.children());
  126. });
  127. switch (type) {
  128. case 'html' :
  129. tmp.html( selectedOpts.content );
  130. _process_inline();
  131. break;
  132. case 'inline' :
  133. if ( $(obj).parent().is('#fancybox-content') === true) {
  134. busy = false;
  135. return;
  136. }
  137. $('<div class="fancybox-inline-tmp" />')
  138. .hide()
  139. .insertBefore( $(obj) )
  140. .bind('fancybox-cleanup', function() {
  141. $(this).replaceWith(content.children());
  142. }).bind('fancybox-cancel', function() {
  143. $(this).replaceWith(tmp.children());
  144. });
  145. $(obj).appendTo(tmp);
  146. _process_inline();
  147. break;
  148. case 'image':
  149. busy = false;
  150. $.fancybox.showActivity();
  151. imgPreloader = new Image();
  152. imgPreloader.onerror = function() {
  153. _error();
  154. };
  155. imgPreloader.onload = function() {
  156. busy = true;
  157. imgPreloader.onerror = imgPreloader.onload = null;
  158. _process_image();
  159. };
  160. imgPreloader.src = href;
  161. break;
  162. case 'swf':
  163. selectedOpts.scrolling = 'no';
  164. str = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + selectedOpts.width + '" height="' + selectedOpts.height + '"><param name="movie" value="' + href + '"></param>';
  165. emb = '';
  166. $.each(selectedOpts.swf, function(name, val) {
  167. str += '<param name="' + name + '" value="' + val + '"></param>';
  168. emb += ' ' + name + '="' + val + '"';
  169. });
  170. str += '<embed src="' + href + '" type="application/x-shockwave-flash" width="' + selectedOpts.width + '" height="' + selectedOpts.height + '"' + emb + '></embed></object>';
  171. tmp.html(str);
  172. _process_inline();
  173. break;
  174. case 'ajax':
  175. busy = false;
  176. $.fancybox.showActivity();
  177. selectedOpts.ajax.win = selectedOpts.ajax.success;
  178. ajaxLoader = $.ajax($.extend({}, selectedOpts.ajax, {
  179. url : href,
  180. data : selectedOpts.ajax.data || {},
  181. error : function(XMLHttpRequest, textStatus, errorThrown) {
  182. if ( XMLHttpRequest.status > 0 ) {
  183. _error();
  184. }
  185. },
  186. success : function(data, textStatus, XMLHttpRequest) {
  187. var o = typeof XMLHttpRequest == 'object' ? XMLHttpRequest : ajaxLoader;
  188. if (o.status == 200) {
  189. if ( typeof selectedOpts.ajax.win == 'function' ) {
  190. ret = selectedOpts.ajax.win(href, data, textStatus, XMLHttpRequest);
  191. if (ret === false) {
  192. loading.hide();
  193. return;
  194. } else if (typeof ret == 'string' || typeof ret == 'object') {
  195. data = ret;
  196. }
  197. }
  198. tmp.html( data );
  199. _process_inline();
  200. }
  201. }
  202. }));
  203. break;
  204. case 'iframe':
  205. _show();
  206. break;
  207. }
  208. },
  209. _process_inline = function() {
  210. var
  211. w = selectedOpts.width,
  212. h = selectedOpts.height;
  213. if (w.toString().indexOf('%') > -1) {
  214. w = parseInt( ($(window).width() - (selectedOpts.margin * 2)) * parseFloat(w) / 100, 10) + 'px';
  215. } else {
  216. w = w == 'auto' ? 'auto' : w + 'px';
  217. }
  218. if (h.toString().indexOf('%') > -1) {
  219. h = parseInt( ($(window).height() - (selectedOpts.margin * 2)) * parseFloat(h) / 100, 10) + 'px';
  220. } else {
  221. h = h == 'auto' ? 'auto' : h + 'px';
  222. }
  223. tmp.wrapInner('<div style="width:' + w + ';height:' + h + ';overflow: ' + (selectedOpts.scrolling == 'auto' ? 'auto' : (selectedOpts.scrolling == 'yes' ? 'scroll' : 'hidden')) + ';position:relative;"></div>');
  224. selectedOpts.width = tmp.width();
  225. selectedOpts.height = tmp.height();
  226. _show();
  227. },
  228. _process_image = function() {
  229. selectedOpts.width = imgPreloader.width;
  230. selectedOpts.height = imgPreloader.height;
  231. $("<img />").attr({
  232. 'id' : 'fancybox-img',
  233. 'src' : imgPreloader.src,
  234. 'alt' : selectedOpts.title
  235. }).appendTo( tmp );
  236. _show();
  237. },
  238. _show = function() {
  239. var pos, equal;
  240. loading.hide();
  241. if (wrap.is(":visible") && false === currentOpts.onCleanup(currentArray, currentIndex, currentOpts)) {
  242. $.event.trigger('fancybox-cancel');
  243. busy = false;
  244. return;
  245. }
  246. busy = true;
  247. $(content.add( overlay )).unbind();
  248. $(window).unbind("resize.fb scroll.fb");
  249. $(document).unbind('keydown.fb');
  250. if (wrap.is(":visible") && currentOpts.titlePosition !== 'outside') {
  251. wrap.css('height', wrap.height());
  252. }
  253. currentArray = selectedArray;
  254. currentIndex = selectedIndex;
  255. currentOpts = selectedOpts;
  256. if (currentOpts.overlayShow) {
  257. overlay.css({
  258. 'background-color' : currentOpts.overlayColor,
  259. 'opacity' : currentOpts.overlayOpacity,
  260. 'cursor' : currentOpts.hideOnOverlayClick ? 'pointer' : 'auto',
  261. 'height' : $(document).height()
  262. });
  263. if (!overlay.is(':visible')) {
  264. if (isIE6) {
  265. $('select:not(#fancybox-tmp select)').filter(function() {
  266. return this.style.visibility !== 'hidden';
  267. }).css({'visibility' : 'hidden'}).one('fancybox-cleanup', function() {
  268. this.style.visibility = 'inherit';
  269. });
  270. }
  271. overlay.show();
  272. }
  273. } else {
  274. overlay.hide();
  275. }
  276. final_pos = _get_zoom_to();
  277. _process_title();
  278. if (wrap.is(":visible")) {
  279. $( close.add( nav_left ).add( nav_right ) ).hide();
  280. pos = wrap.position(),
  281. start_pos = {
  282. top : pos.top,
  283. left : pos.left,
  284. width : wrap.width(),
  285. height : wrap.height()
  286. };
  287. equal = (start_pos.width == final_pos.width && start_pos.height == final_pos.height);
  288. content.fadeTo(currentOpts.changeFade, 0.3, function() {
  289. var finish_resizing = function() {
  290. content.html( tmp.contents() ).fadeTo(currentOpts.changeFade, 1, _finish);
  291. };
  292. $.event.trigger('fancybox-change');
  293. content
  294. .empty()
  295. .removeAttr('filter')
  296. .css({
  297. 'border-width' : currentOpts.padding,
  298. 'width' : final_pos.width - currentOpts.padding * 2,
  299. 'height' : selectedOpts.autoDimensions ? 'auto' : final_pos.height - titleHeight - currentOpts.padding * 2
  300. });
  301. if (equal) {
  302. finish_resizing();
  303. } else {
  304. fx.prop = 0;
  305. $(fx).animate({prop: 1}, {
  306. duration : currentOpts.changeSpeed,
  307. easing : currentOpts.easingChange,
  308. step : _draw,
  309. complete : finish_resizing
  310. });
  311. }
  312. });
  313. return;
  314. }
  315. wrap.removeAttr("style");
  316. content.css('border-width', currentOpts.padding);
  317. if (currentOpts.transitionIn == 'elastic') {
  318. start_pos = _get_zoom_from();
  319. content.html( tmp.contents() );
  320. wrap.show();
  321. if (currentOpts.opacity) {
  322. final_pos.opacity = 0;
  323. }
  324. fx.prop = 0;
  325. $(fx).animate({prop: 1}, {
  326. duration : currentOpts.speedIn,
  327. easing : currentOpts.easingIn,
  328. step : _draw,
  329. complete : _finish
  330. });
  331. return;
  332. }
  333. if (currentOpts.titlePosition == 'inside' && titleHeight > 0) {
  334. title.show();
  335. }
  336. content
  337. .css({
  338. 'width' : final_pos.width - currentOpts.padding * 2,
  339. 'height' : selectedOpts.autoDimensions ? 'auto' : final_pos.height - titleHeight - currentOpts.padding * 2
  340. })
  341. .html( tmp.contents() );
  342. wrap
  343. .css(final_pos)
  344. .fadeIn( currentOpts.transitionIn == 'none' ? 0 : currentOpts.speedIn, _finish );
  345. },
  346. _format_title = function(title) {
  347. if (title && title.length) {
  348. if (currentOpts.titlePosition == 'float') {
  349. return '<table id="fancybox-title-float-wrap" cellpadding="0" cellspacing="0"><tr><td id="fancybox-title-float-left"></td><td id="fancybox-title-float-main">' + title + '</td><td id="fancybox-title-float-right"></td></tr></table>';
  350. }
  351. return '<div id="fancybox-title-' + currentOpts.titlePosition + '">' + title + '</div>';
  352. }
  353. return false;
  354. },
  355. _process_title = function() {
  356. titleStr = currentOpts.title || '';
  357. titleHeight = 0;
  358. title
  359. .empty()
  360. .removeAttr('style')
  361. .removeClass();
  362. if (currentOpts.titleShow === false) {
  363. title.hide();
  364. return;
  365. }
  366. titleStr = $.isFunction(currentOpts.titleFormat) ? currentOpts.titleFormat(titleStr, currentArray, currentIndex, currentOpts) : _format_title(titleStr);
  367. if (!titleStr || titleStr === '') {
  368. title.hide();
  369. return;
  370. }
  371. title
  372. .addClass('fancybox-title-' + currentOpts.titlePosition)
  373. .html( titleStr )
  374. .appendTo( 'body' )
  375. .show();
  376. switch (currentOpts.titlePosition) {
  377. case 'inside':
  378. title
  379. .css({
  380. 'width' : final_pos.width - (currentOpts.padding * 2),
  381. 'marginLeft' : currentOpts.padding,
  382. 'marginRight' : currentOpts.padding
  383. });
  384. titleHeight = title.outerHeight(true);
  385. title.appendTo( outer );
  386. final_pos.height += titleHeight;
  387. break;
  388. case 'over':
  389. title
  390. .css({
  391. 'marginLeft' : currentOpts.padding,
  392. 'width' : final_pos.width - (currentOpts.padding * 2),
  393. 'bottom' : currentOpts.padding
  394. })
  395. .appendTo( outer );
  396. break;
  397. case 'float':
  398. title
  399. .css('left', parseInt((title.width() - final_pos.width - 40)/ 2, 10) * -1)
  400. .appendTo( wrap );
  401. break;
  402. default:
  403. title
  404. .css({
  405. 'width' : final_pos.width - (currentOpts.padding * 2),
  406. 'paddingLeft' : currentOpts.padding,
  407. 'paddingRight' : currentOpts.padding
  408. })
  409. .appendTo( wrap );
  410. break;
  411. }
  412. title.hide();
  413. },
  414. _set_navigation = function() {
  415. if (currentOpts.enableEscapeButton || currentOpts.enableKeyboardNav) {
  416. $(document).bind('keydown.fb', function(e) {
  417. if (e.keyCode == 27 && currentOpts.enableEscapeButton) {
  418. e.preventDefault();
  419. $.fancybox.close();
  420. } else if ((e.keyCode == 37 || e.keyCode == 39) && currentOpts.enableKeyboardNav && e.target.tagName !== 'INPUT' && e.target.tagName !== 'TEXTAREA' && e.target.tagName !== 'SELECT') {
  421. e.preventDefault();
  422. $.fancybox[ e.keyCode == 37 ? 'prev' : 'next']();
  423. }
  424. });
  425. }
  426. if (!currentOpts.showNavArrows) {
  427. nav_left.hide();
  428. nav_right.hide();
  429. return;
  430. }
  431. if ((currentOpts.cyclic && currentArray.length > 1) || currentIndex !== 0) {
  432. nav_left.show();
  433. }
  434. if ((currentOpts.cyclic && currentArray.length > 1) || currentIndex != (currentArray.length -1)) {
  435. nav_right.show();
  436. }
  437. },
  438. _finish = function () {
  439. if (!$.support.opacity) {
  440. content.get(0).style.removeAttribute('filter');
  441. wrap.get(0).style.removeAttribute('filter');
  442. }
  443. if (selectedOpts.autoDimensions) {
  444. content.css('height', 'auto');
  445. }
  446. wrap.css('height', 'auto');
  447. if (titleStr && titleStr.length) {
  448. title.show();
  449. }
  450. if (currentOpts.showCloseButton) {
  451. close.show();
  452. }
  453. _set_navigation();
  454. if (currentOpts.hideOnContentClick) {
  455. content.bind('click', $.fancybox.close);
  456. }
  457. if (currentOpts.hideOnOverlayClick) {
  458. overlay.bind('click', $.fancybox.close);
  459. }
  460. $(window).bind("resize.fb", $.fancybox.resize);
  461. if (currentOpts.centerOnScroll) {
  462. $(window).bind("scroll.fb", $.fancybox.center);
  463. }
  464. if (currentOpts.type == 'iframe') {
  465. $('<iframe id="fancybox-frame" name="fancybox-frame' + new Date().getTime() + '" frameborder="0" hspace="0" ' + ($.browser.msie ? 'allowtransparency="true""' : '') + ' scrolling="' + selectedOpts.scrolling + '" src="' + currentOpts.href + '"></iframe>').appendTo(content);
  466. }
  467. wrap.show();
  468. busy = false;
  469. $.fancybox.center();
  470. currentOpts.onComplete(currentArray, currentIndex, currentOpts);
  471. _preload_images();
  472. },
  473. _preload_images = function() {
  474. var href,
  475. objNext;
  476. if ((currentArray.length -1) > currentIndex) {
  477. href = currentArray[ currentIndex + 1 ].href;
  478. if (typeof href !== 'undefined' && href.match(imgRegExp)) {
  479. objNext = new Image();
  480. objNext.src = href;
  481. }
  482. }
  483. if (currentIndex > 0) {
  484. href = currentArray[ currentIndex - 1 ].href;
  485. if (typeof href !== 'undefined' && href.match(imgRegExp)) {
  486. objNext = new Image();
  487. objNext.src = href;
  488. }
  489. }
  490. },
  491. _draw = function(pos) {
  492. var dim = {
  493. width : parseInt(start_pos.width + (final_pos.width - start_pos.width) * pos, 10),
  494. height : parseInt(start_pos.height + (final_pos.height - start_pos.height) * pos, 10),
  495. top : parseInt(start_pos.top + (final_pos.top - start_pos.top) * pos, 10),
  496. left : parseInt(start_pos.left + (final_pos.left - start_pos.left) * pos, 10)
  497. };
  498. if (typeof final_pos.opacity !== 'undefined') {
  499. dim.opacity = pos < 0.5 ? 0.5 : pos;
  500. }
  501. wrap.css(dim);
  502. content.css({
  503. 'width' : dim.width - currentOpts.padding * 2,
  504. 'height' : dim.height - (titleHeight * pos) - currentOpts.padding * 2
  505. });
  506. },
  507. _get_viewport = function() {
  508. return [
  509. $(window).width() - (currentOpts.margin * 2),
  510. $(window).height() - (currentOpts.margin * 2),
  511. $(document).scrollLeft() + currentOpts.margin,
  512. $(document).scrollTop() + currentOpts.margin
  513. ];
  514. },
  515. _get_zoom_to = function () {
  516. var view = _get_viewport(),
  517. to = {},
  518. resize = currentOpts.autoScale,
  519. double_padding = currentOpts.padding * 2,
  520. ratio;
  521. if (currentOpts.width.toString().indexOf('%') > -1) {
  522. to.width = parseInt((view[0] * parseFloat(currentOpts.width)) / 100, 10);
  523. } else {
  524. to.width = currentOpts.width + double_padding;
  525. }
  526. if (currentOpts.height.toString().indexOf('%') > -1) {
  527. to.height = parseInt((view[1] * parseFloat(currentOpts.height)) / 100, 10);
  528. } else {
  529. to.height = currentOpts.height + double_padding;
  530. }
  531. if (resize && (to.width > view[0] || to.height > view[1])) {
  532. if (selectedOpts.type == 'image' || selectedOpts.type == 'swf') {
  533. ratio = (currentOpts.width ) / (currentOpts.height );
  534. if ((to.width ) > view[0]) {
  535. to.width = view[0];
  536. to.height = parseInt(((to.width - double_padding) / ratio) + double_padding, 10);
  537. }
  538. if ((to.height) > view[1]) {
  539. to.height = view[1];
  540. to.width = parseInt(((to.height - double_padding) * ratio) + double_padding, 10);
  541. }
  542. } else {
  543. to.width = Math.min(to.width, view[0]);
  544. to.height = Math.min(to.height, view[1]);
  545. }
  546. }
  547. to.top = parseInt(Math.max(view[3] - 20, view[3] + ((view[1] - to.height - 40) * 0.5)), 10);
  548. to.left = parseInt(Math.max(view[2] - 20, view[2] + ((view[0] - to.width - 40) * 0.5)), 10);
  549. return to;
  550. },
  551. _get_obj_pos = function(obj) {
  552. var pos = obj.offset();
  553. pos.top += parseInt( obj.css('paddingTop'), 10 ) || 0;
  554. pos.left += parseInt( obj.css('paddingLeft'), 10 ) || 0;
  555. pos.top += parseInt( obj.css('border-top-width'), 10 ) || 0;
  556. pos.left += parseInt( obj.css('border-left-width'), 10 ) || 0;
  557. pos.width = obj.width();
  558. pos.height = obj.height();
  559. return pos;
  560. },
  561. _get_zoom_from = function() {
  562. var orig = selectedOpts.orig ? $(selectedOpts.orig) : false,
  563. from = {},
  564. pos,
  565. view;
  566. if (orig && orig.length) {
  567. pos = _get_obj_pos(orig);
  568. from = {
  569. width : pos.width + (currentOpts.padding * 2),
  570. height : pos.height + (currentOpts.padding * 2),
  571. top : pos.top - currentOpts.padding - 20,
  572. left : pos.left - currentOpts.padding - 20
  573. };
  574. } else {
  575. view = _get_viewport();
  576. from = {
  577. width : currentOpts.padding * 2,
  578. height : currentOpts.padding * 2,
  579. top : parseInt(view[3] + view[1] * 0.5, 10),
  580. left : parseInt(view[2] + view[0] * 0.5, 10)
  581. };
  582. }
  583. return from;
  584. },
  585. _animate_loading = function() {
  586. if (!loading.is(':visible')){
  587. clearInterval(loadingTimer);
  588. return;
  589. }
  590. $('div', loading).css('top', (loadingFrame * -40) + 'px');
  591. loadingFrame = (loadingFrame + 1) % 12;
  592. };
  593. /*
  594. * Public methods
  595. */
  596. $.fn.fancybox = function(options) {
  597. if (!$(this).length) {
  598. return this;
  599. }
  600. $(this)
  601. .data('fancybox', $.extend({}, options, ($.metadata ? $(this).metadata() : {})))
  602. .unbind('click.fb')
  603. .bind('click.fb', function(e) {
  604. e.preventDefault();
  605. if (busy) {
  606. return;
  607. }
  608. busy = true;
  609. $(this).blur();
  610. selectedArray = [];
  611. selectedIndex = 0;
  612. var rel = $(this).attr('rel') || '';
  613. if (!rel || rel == '' || rel === 'nofollow') {
  614. selectedArray.push(this);
  615. } else {
  616. selectedArray = $("a[rel=" + rel + "], area[rel=" + rel + "]");
  617. selectedIndex = selectedArray.index( this );
  618. }
  619. _start();
  620. return;
  621. });
  622. return this;
  623. };
  624. $.fancybox = function(obj) {
  625. var opts;
  626. if (busy) {
  627. return;
  628. }
  629. busy = true;
  630. opts = typeof arguments[1] !== 'undefined' ? arguments[1] : {};
  631. selectedArray = [];
  632. selectedIndex = parseInt(opts.index, 10) || 0;
  633. if ($.isArray(obj)) {
  634. for (var i = 0, j = obj.length; i < j; i++) {
  635. if (typeof obj[i] == 'object') {
  636. $(obj[i]).data('fancybox', $.extend({}, opts, obj[i]));
  637. } else {
  638. obj[i] = $({}).data('fancybox', $.extend({content : obj[i]}, opts));
  639. }
  640. }
  641. selectedArray = jQuery.merge(selectedArray, obj);
  642. } else {
  643. if (typeof obj == 'object') {
  644. $(obj).data('fancybox', $.extend({}, opts, obj));
  645. } else {
  646. obj = $({}).data('fancybox', $.extend({content : obj}, opts));
  647. }
  648. selectedArray.push(obj);
  649. }
  650. if (selectedIndex > selectedArray.length || selectedIndex < 0) {
  651. selectedIndex = 0;
  652. }
  653. _start();
  654. };
  655. $.fancybox.showActivity = function() {
  656. clearInterval(loadingTimer);
  657. loading.show();
  658. loadingTimer = setInterval(_animate_loading, 66);
  659. };
  660. $.fancybox.hideActivity = function() {
  661. loading.hide();
  662. };
  663. $.fancybox.next = function() {
  664. return $.fancybox.pos( currentIndex + 1);
  665. };
  666. $.fancybox.prev = function() {
  667. return $.fancybox.pos( currentIndex - 1);
  668. };
  669. $.fancybox.pos = function(pos) {
  670. if (busy) {
  671. return;
  672. }
  673. pos = parseInt(pos);
  674. selectedArray = currentArray;
  675. if (pos > -1 && pos < currentArray.length) {
  676. selectedIndex = pos;
  677. _start();
  678. } else if (currentOpts.cyclic && currentArray.length > 1) {
  679. selectedIndex = pos >= currentArray.length ? 0 : currentArray.length - 1;
  680. _start();
  681. }
  682. return;
  683. };
  684. $.fancybox.cancel = function() {
  685. if (busy) {
  686. return;
  687. }
  688. busy = true;
  689. $.event.trigger('fancybox-cancel');
  690. _abort();
  691. selectedOpts.onCancel(selectedArray, selectedIndex, selectedOpts);
  692. busy = false;
  693. };
  694. // Note: within an iframe use - parent.$.fancybox.close();
  695. $.fancybox.close = function() {
  696. if (busy || wrap.is(':hidden')) {
  697. return;
  698. }
  699. busy = true;
  700. if (currentOpts && false === currentOpts.onCleanup(currentArray, currentIndex, currentOpts)) {
  701. busy = false;
  702. return;
  703. }
  704. _abort();
  705. $(close.add( nav_left ).add( nav_right )).hide();
  706. $(content.add( overlay )).unbind();
  707. $(window).unbind("resize.fb scroll.fb");
  708. $(document).unbind('keydown.fb');
  709. content.find('iframe').attr('src', isIE6 && /^https/i.test(window.location.href || '') ? 'javascript:void(false)' : 'about:blank');
  710. if (currentOpts.titlePosition !== 'inside') {
  711. title.empty();
  712. }
  713. wrap.stop();
  714. function _cleanup() {
  715. overlay.fadeOut('fast');
  716. title.empty().hide();
  717. wrap.hide();
  718. $.event.trigger('fancybox-cleanup');
  719. content.empty();
  720. currentOpts.onClosed(currentArray, currentIndex, currentOpts);
  721. currentArray = selectedOpts = [];
  722. currentIndex = selectedIndex = 0;
  723. currentOpts = selectedOpts = {};
  724. busy = false;
  725. }
  726. if (currentOpts.transitionOut == 'elastic') {
  727. start_pos = _get_zoom_from();
  728. var pos = wrap.position();
  729. final_pos = {
  730. top : pos.top ,
  731. left : pos.left,
  732. width : wrap.width(),
  733. height : wrap.height()
  734. };
  735. if (currentOpts.opacity) {
  736. final_pos.opacity = 1;
  737. }
  738. title.empty().hide();
  739. fx.prop = 1;
  740. $(fx).animate({ prop: 0 }, {
  741. duration : currentOpts.speedOut,
  742. easing : currentOpts.easingOut,
  743. step : _draw,
  744. complete : _cleanup
  745. });
  746. } else {
  747. wrap.fadeOut( currentOpts.transitionOut == 'none' ? 0 : currentOpts.speedOut, _cleanup);
  748. }
  749. };
  750. $.fancybox.resize = function() {
  751. if (overlay.is(':visible')) {
  752. overlay.css('height', $(document).height());
  753. }
  754. $.fancybox.center(true);
  755. };
  756. $.fancybox.center = function() {
  757. var view, align;
  758. if (busy) {
  759. return;
  760. }
  761. align = arguments[0] === true ? 1 : 0;
  762. view = _get_viewport();
  763. if (!align && (wrap.width() > view[0] || wrap.height() > view[1])) {
  764. return;
  765. }
  766. wrap
  767. .stop()
  768. .animate({
  769. 'top' : parseInt(Math.max(view[3] - 20, view[3] + ((view[1] - content.height() - 40) * 0.5) - currentOpts.padding)),
  770. 'left' : parseInt(Math.max(view[2] - 20, view[2] + ((view[0] - content.width() - 40) * 0.5) - currentOpts.padding))
  771. }, typeof arguments[0] == 'number' ? arguments[0] : 200);
  772. };
  773. $.fancybox.init = function() {
  774. if ($("#fancybox-wrap").length) {
  775. return;
  776. }
  777. $('body').append(
  778. tmp = $('<div id="fancybox-tmp"></div>'),
  779. loading = $('<div id="fancybox-loading"><div></div></div>'),
  780. overlay = $('<div id="fancybox-overlay"></div>'),
  781. wrap = $('<div id="fancybox-wrap"></div>')
  782. );
  783. outer = $('<div id="fancybox-outer"></div>')
  784. .append('<div class="fancybox-bg" id="fancybox-bg-n"></div><div class="fancybox-bg" id="fancybox-bg-ne"></div><div class="fancybox-bg" id="fancybox-bg-e"></div><div class="fancybox-bg" id="fancybox-bg-se"></div><div class="fancybox-bg" id="fancybox-bg-s"></div><div class="fancybox-bg" id="fancybox-bg-sw"></div><div class="fancybox-bg" id="fancybox-bg-w"></div><div class="fancybox-bg" id="fancybox-bg-nw"></div>')
  785. .appendTo( wrap );
  786. outer.append(
  787. content = $('<div id="fancybox-content"></div>'),
  788. close = $('<a id="fancybox-close"></a>'),
  789. title = $('<div id="fancybox-title"></div>'),
  790. nav_left = $('<a href="javascript:;" id="fancybox-left"><span class="fancy-ico" id="fancybox-left-ico"></span></a>'),
  791. nav_right = $('<a href="javascript:;" id="fancybox-right"><span class="fancy-ico" id="fancybox-right-ico"></span></a>')
  792. );
  793. close.click($.fancybox.close);
  794. loading.click($.fancybox.cancel);
  795. nav_left.click(function(e) {
  796. e.preventDefault();
  797. $.fancybox.prev();
  798. });
  799. nav_right.click(function(e) {
  800. e.preventDefault();
  801. $.fancybox.next();
  802. });
  803. if ($.fn.mousewheel) {
  804. wrap.bind('mousewheel.fb', function(e, delta) {
  805. if (busy) {
  806. e.preventDefault();
  807. } else if ($(e.target).get(0).clientHeight == 0 || $(e.target).get(0).scrollHeight === $(e.target).get(0).clientHeight) {
  808. e.preventDefault();
  809. $.fancybox[ delta > 0 ? 'prev' : 'next']();
  810. }
  811. });
  812. }
  813. if (!$.support.opacity) {
  814. wrap.addClass('fancybox-ie');
  815. }
  816. if (isIE6) {
  817. loading.addClass('fancybox-ie6');
  818. wrap.addClass('fancybox-ie6');
  819. $('<iframe id="fancybox-hide-sel-frame" src="' + (/^https/i.test(window.location.href || '') ? 'javascript:void(false)' : 'about:blank' ) + '" scrolling="no" border="0" frameborder="0" tabindex="-1"></iframe>').prependTo(outer);
  820. }
  821. };
  822. $.fn.fancybox.defaults = {
  823. padding : 10,
  824. margin : 40,
  825. opacity : false,
  826. modal : false,
  827. cyclic : false,
  828. scrolling : 'auto', // 'auto', 'yes' or 'no'
  829. width : 560,
  830. height : 340,
  831. autoScale : true,
  832. autoDimensions : true,
  833. centerOnScroll : false,
  834. ajax : {},
  835. swf : { wmode: 'transparent' },
  836. hideOnOverlayClick : true,
  837. hideOnContentClick : false,
  838. overlayShow : true,
  839. overlayOpacity : 0.7,
  840. overlayColor : '#777',
  841. titleShow : true,
  842. titlePosition : 'float', // 'float', 'outside', 'inside' or 'over'
  843. titleFormat : null,
  844. titleFromAlt : false,
  845. transitionIn : 'fade', // 'elastic', 'fade' or 'none'
  846. transitionOut : 'fade', // 'elastic', 'fade' or 'none'
  847. speedIn : 300,
  848. speedOut : 300,
  849. changeSpeed : 300,
  850. changeFade : 'fast',
  851. easingIn : 'swing',
  852. easingOut : 'swing',
  853. showCloseButton : true,
  854. showNavArrows : true,
  855. enableEscapeButton : true,
  856. enableKeyboardNav : true,
  857. onStart : function(){},
  858. onCancel : function(){},
  859. onComplete : function(){},
  860. onCleanup : function(){},
  861. onClosed : function(){},
  862. onError : function(){}
  863. };
  864. $(document).ready(function() {
  865. $.fancybox.init();
  866. });
  867. })(jQuery);