jquery.ad-gallery.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /**
  2. * Copyright (c) 2010 Anders Ekdahl (http://coffeescripter.com/)
  3. * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
  4. * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
  5. *
  6. * Version: 1.2.4
  7. *
  8. * Demo and documentation: http://coffeescripter.com/code/ad-gallery/
  9. */
  10. (function($) {
  11. $.fn.adGallery = function(options) {
  12. var defaults = { loader_image: 'statics/images/yp/loader.gif',
  13. start_at_index: 0,
  14. description_wrapper: false,
  15. thumb_opacity: 0.7,
  16. animate_first_image: false,
  17. animation_speed: 400,
  18. width: false,
  19. height: false,
  20. display_next_and_prev: true,
  21. display_back_and_forward: true,
  22. scroll_jump: 0, // If 0, it jumps the width of the container
  23. slideshow: {
  24. enable: false,
  25. autostart: false,
  26. speed: 5000,
  27. start_label: 'Start',
  28. stop_label: 'Stop',
  29. stop_on_scroll: true,
  30. countdown_prefix: '(',
  31. countdown_sufix: ')',
  32. onStart: false,
  33. onStop: false
  34. },
  35. effect: 'slide-hori', // or 'slide-vert', 'fade', or 'resize', 'none'
  36. enable_keyboard_move: true,
  37. cycle: true,
  38. callbacks: {
  39. init: false,
  40. afterImageVisible: false,
  41. beforeImageVisible: false
  42. }
  43. };
  44. var settings = $.extend(false, defaults, options);
  45. if(options && options.slideshow) {
  46. settings.slideshow = $.extend(false, defaults.slideshow, options.slideshow);
  47. };
  48. if(!settings.slideshow.enable) {
  49. settings.slideshow.autostart = false;
  50. };
  51. var galleries = [];
  52. $(this).each(function() {
  53. var gallery = new AdGallery(this, settings);
  54. galleries[galleries.length] = gallery;
  55. });
  56. // Sorry, breaking the jQuery chain because the gallery instances
  57. // are returned so you can fiddle with them
  58. return galleries;
  59. };
  60. function VerticalSlideAnimation(img_container, direction, desc) {
  61. var current_top = parseInt(img_container.css('top'), 10);
  62. if(direction == 'left') {
  63. var old_image_top = '-'+ this.image_wrapper_height +'px';
  64. img_container.css('top', this.image_wrapper_height +'px');
  65. } else {
  66. var old_image_top = this.image_wrapper_height +'px';
  67. img_container.css('top', '-'+ this.image_wrapper_height +'px');
  68. };
  69. if(desc) {
  70. desc.css('bottom', '-'+ desc[0].offsetHeight +'px');
  71. desc.animate({bottom: 0}, this.settings.animation_speed * 2);
  72. };
  73. if(this.current_description) {
  74. this.current_description.animate({bottom: '-'+ this.current_description[0].offsetHeight +'px'}, this.settings.animation_speed * 2);
  75. };
  76. return {old_image: {top: old_image_top},
  77. new_image: {top: current_top}};
  78. };
  79. function HorizontalSlideAnimation(img_container, direction, desc) {
  80. var current_left = parseInt(img_container.css('left'), 10);
  81. if(direction == 'left') {
  82. var old_image_left = '-'+ this.image_wrapper_width +'px';
  83. img_container.css('left',this.image_wrapper_width +'px');
  84. } else {
  85. var old_image_left = this.image_wrapper_width +'px';
  86. img_container.css('left','-'+ this.image_wrapper_width +'px');
  87. };
  88. if(desc) {
  89. desc.css('bottom', '-'+ desc[0].offsetHeight +'px');
  90. desc.animate({bottom: 0}, this.settings.animation_speed * 2);
  91. };
  92. if(this.current_description) {
  93. this.current_description.animate({bottom: '-'+ this.current_description[0].offsetHeight +'px'}, this.settings.animation_speed * 2);
  94. };
  95. return {old_image: {left: old_image_left},
  96. new_image: {left: current_left}};
  97. };
  98. function ResizeAnimation(img_container, direction, desc) {
  99. var image_width = img_container.width();
  100. var image_height = img_container.height();
  101. var current_left = parseInt(img_container.css('left'), 10);
  102. var current_top = parseInt(img_container.css('top'), 10);
  103. img_container.css({width: 0, height: 0, top: this.image_wrapper_height / 2, left: this.image_wrapper_width / 2});
  104. return {old_image: {width: 0,
  105. height: 0,
  106. top: this.image_wrapper_height / 2,
  107. left: this.image_wrapper_width / 2},
  108. new_image: {width: image_width,
  109. height: image_height,
  110. top: current_top,
  111. left: current_left}};
  112. };
  113. function FadeAnimation(img_container, direction, desc) {
  114. img_container.css('opacity', 0);
  115. return {old_image: {opacity: 0},
  116. new_image: {opacity: 1}};
  117. };
  118. // Sort of a hack, will clean this up... eventually
  119. function NoneAnimation(img_container, direction, desc) {
  120. img_container.css('opacity', 0);
  121. return {old_image: {opacity: 0},
  122. new_image: {opacity: 1},
  123. speed: 0};
  124. };
  125. function AdGallery(wrapper, settings) {
  126. this.init(wrapper, settings);
  127. };
  128. AdGallery.prototype = {
  129. // Elements
  130. wrapper: false,
  131. image_wrapper: false,
  132. gallery_info: false,
  133. nav: false,
  134. loader: false,
  135. preloads: false,
  136. thumbs_wrapper: false,
  137. scroll_back: false,
  138. scroll_forward: false,
  139. next_link: false,
  140. prev_link: false,
  141. slideshow: false,
  142. image_wrapper_width: 0,
  143. image_wrapper_height: 0,
  144. current_index: 0,
  145. current_image: false,
  146. current_description: false,
  147. nav_display_width: 0,
  148. settings: false,
  149. images: false,
  150. in_transition: false,
  151. animations: false,
  152. init: function(wrapper, settings) {
  153. var context = this;
  154. this.wrapper = $(wrapper);
  155. this.settings = settings;
  156. this.setupElements();
  157. this.setupAnimations();
  158. if(this.settings.width) {
  159. this.image_wrapper_width = this.settings.width;
  160. this.image_wrapper.width(this.settings.width);
  161. this.wrapper.width(this.settings.width);
  162. } else {
  163. this.image_wrapper_width = this.image_wrapper.width();
  164. };
  165. if(this.settings.height) {
  166. this.image_wrapper_height = this.settings.height;
  167. this.image_wrapper.height(this.settings.height);
  168. } else {
  169. this.image_wrapper_height = this.image_wrapper.height();
  170. };
  171. this.nav_display_width = this.nav.width();
  172. this.current_index = 0;
  173. this.current_image = false;
  174. this.current_description = false;
  175. this.in_transition = false;
  176. this.findImages();
  177. if(this.settings.display_next_and_prev) {
  178. this.initNextAndPrev();
  179. };
  180. // The slideshow needs a callback to trigger the next image to be shown
  181. // but we don't want to give it access to the whole gallery instance
  182. var nextimage_callback = function(callback) {
  183. return context.nextImage(callback);
  184. };
  185. this.slideshow = new AdGallerySlideshow(nextimage_callback, this.settings.slideshow);
  186. this.controls.append(this.slideshow.create());
  187. if(this.settings.slideshow.enable) {
  188. this.slideshow.enable();
  189. } else {
  190. this.slideshow.disable();
  191. };
  192. if(this.settings.display_back_and_forward) {
  193. this.initBackAndForward();
  194. };
  195. if(this.settings.enable_keyboard_move) {
  196. this.initKeyEvents();
  197. };
  198. var start_at = parseInt(this.settings.start_at_index, 10);
  199. if(window.location.hash && window.location.hash.indexOf('#ad-image') === 0) {
  200. start_at = window.location.hash.replace(/[^0-9]+/g, '');
  201. // Check if it's a number
  202. if((start_at * 1) != start_at) {
  203. start_at = this.settings.start_at_index;
  204. };
  205. };
  206. this.loading(true);
  207. this.showImage(start_at,
  208. function() {
  209. // We don't want to start the slideshow before the image has been
  210. // displayed
  211. if(context.settings.slideshow.autostart) {
  212. context.preloadImage(start_at + 1);
  213. context.slideshow.start();
  214. };
  215. }
  216. );
  217. this.fireCallback(this.settings.callbacks.init);
  218. },
  219. setupAnimations: function() {
  220. this.animations = {
  221. 'slide-vert': VerticalSlideAnimation,
  222. 'slide-hori': HorizontalSlideAnimation,
  223. 'resize': ResizeAnimation,
  224. 'fade': FadeAnimation,
  225. 'none': NoneAnimation
  226. };
  227. },
  228. setupElements: function() {
  229. this.controls = this.wrapper.find('.ad-controls');
  230. this.gallery_info = $('<p class="ad-info"></p>');
  231. this.controls.append(this.gallery_info);
  232. this.image_wrapper = this.wrapper.find('.ad-image-wrapper');
  233. this.image_wrapper.empty();
  234. this.nav = this.wrapper.find('.ad-nav');
  235. this.thumbs_wrapper = this.nav.find('.ad-thumbs');
  236. this.preloads = $('<div class="ad-preloads"></div>');
  237. this.loader = $('<img class="ad-loader" src="'+ this.settings.loader_image +'">');
  238. this.image_wrapper.append(this.loader);
  239. this.loader.hide();
  240. $(document.body).append(this.preloads);
  241. },
  242. loading: function(bool) {
  243. if(bool) {
  244. this.loader.show();
  245. } else {
  246. this.loader.hide();
  247. };
  248. },
  249. addAnimation: function(name, fn) {
  250. if($.isFunction(fn)) {
  251. this.animations[name] = fn;
  252. };
  253. },
  254. findImages: function() {
  255. var context = this;
  256. this.images = [];
  257. var thumb_wrapper_width = 0;
  258. var thumbs_loaded = 0;
  259. var thumbs = this.thumbs_wrapper.find('a');
  260. var thumb_count = thumbs.length;
  261. if(this.settings.thumb_opacity < 1) {
  262. thumbs.find('img').css('opacity', this.settings.thumb_opacity);
  263. };
  264. thumbs.each(
  265. function(i) {
  266. var link = $(this);
  267. var image_src = link.attr('href');
  268. var thumb = link.find('img');
  269. // Check if the thumb has already loaded
  270. if(!context.isImageLoaded(thumb[0])) {
  271. thumb.load(
  272. function() {
  273. thumb_wrapper_width += this.parentNode.parentNode.offsetWidth;
  274. thumbs_loaded++;
  275. }
  276. );
  277. } else{
  278. thumb_wrapper_width += thumb[0].parentNode.parentNode.offsetWidth;
  279. thumbs_loaded++;
  280. };
  281. link.addClass('ad-thumb'+ i);
  282. link.click(
  283. function() {
  284. context.showImage(i);
  285. context.slideshow.stop();
  286. return false;
  287. }
  288. ).hover(
  289. function() {
  290. if(!$(this).is('.ad-active') && context.settings.thumb_opacity < 1) {
  291. $(this).find('img').fadeTo(300, 1);
  292. };
  293. context.preloadImage(i);
  294. },
  295. function() {
  296. if(!$(this).is('.ad-active') && context.settings.thumb_opacity < 1) {
  297. $(this).find('img').fadeTo(300, context.settings.thumb_opacity);
  298. };
  299. }
  300. );
  301. var link = false;
  302. if(thumb.data('ad-link')) {
  303. link = thumb.data('ad-link');
  304. } else if(thumb.attr('longdesc') && thumb.attr('longdesc').length) {
  305. link = thumb.attr('longdesc');
  306. };
  307. var desc = false;
  308. if(thumb.data('ad-desc')) {
  309. desc = thumb.data('ad-desc');
  310. } else if(thumb.attr('alt') && thumb.attr('alt').length) {
  311. desc = thumb.attr('alt');
  312. };
  313. var title = false;
  314. if(thumb.data('ad-title')) {
  315. title = thumb.data('ad-title');
  316. } else if(thumb.attr('title') && thumb.attr('title').length) {
  317. title = thumb.attr('title');
  318. };
  319. context.images[i] = { thumb: thumb.attr('src'), image: image_src, error: false,
  320. preloaded: false, desc: desc, title: title, size: false,
  321. link: link };
  322. }
  323. );
  324. // Wait until all thumbs are loaded, and then set the width of the ul
  325. var inter = setInterval(
  326. function() {
  327. if(thumb_count == thumbs_loaded) {
  328. thumb_wrapper_width -= 100;
  329. var list = context.nav.find('.ad-thumb-list'),
  330. list_num = list.children().length,
  331. list_width = list.children().eq(0).width();
  332. list.css('width', (list_width+5)*list_num +'px');
  333. clearInterval(inter);
  334. };
  335. },
  336. 100
  337. );
  338. },
  339. initKeyEvents: function() {
  340. var context = this;
  341. $(document).keydown(
  342. function(e) {
  343. if(e.keyCode == 39) {
  344. // right arrow
  345. context.nextImage();
  346. context.slideshow.stop();
  347. } else if(e.keyCode == 37) {
  348. // left arrow
  349. context.prevImage();
  350. context.slideshow.stop();
  351. };
  352. }
  353. );
  354. },
  355. initNextAndPrev: function() {
  356. this.next_link = $('<div class="ad-next"><div class="ad-next-image"></div></div>');
  357. this.prev_link = $('<div class="ad-prev"><div class="ad-prev-image"></div></div>');
  358. this.image_wrapper.append(this.next_link);
  359. this.image_wrapper.append(this.prev_link);
  360. var context = this;
  361. this.prev_link.add(this.next_link).mouseover(
  362. function(e) {
  363. // IE 6 hides the wrapper div, so we have to set it's width
  364. $(this).css('height', context.image_wrapper_height);
  365. $(this).find('div').show();
  366. }
  367. ).mouseout(
  368. function(e) {
  369. $(this).find('div').hide();
  370. }
  371. ).click(
  372. function() {
  373. if($(this).is('.ad-next')) {
  374. context.nextImage();
  375. context.slideshow.stop();
  376. } else {
  377. context.prevImage();
  378. context.slideshow.stop();
  379. };
  380. }
  381. ).find('div').css('opacity', 0.7);
  382. },
  383. initBackAndForward: function() {
  384. var context = this;
  385. this.scroll_forward = $('<div class="ad-forward"></div>');
  386. this.scroll_back = $('<div class="ad-back"></div>');
  387. this.nav.append(this.scroll_forward);
  388. this.nav.prepend(this.scroll_back);
  389. var has_scrolled = 0;
  390. var thumbs_scroll_interval = false;
  391. $(this.scroll_back).add(this.scroll_forward).click(
  392. function() {
  393. // We don't want to jump the whole width, since an image
  394. // might be cut at the edge
  395. var width = context.nav_display_width - 50;
  396. if(context.settings.scroll_jump > 0) {
  397. var width = context.settings.scroll_jump;
  398. };
  399. if($(this).is('.ad-forward')) {
  400. var left = context.thumbs_wrapper.scrollLeft() + width;
  401. } else {
  402. var left = context.thumbs_wrapper.scrollLeft() - width;
  403. };
  404. if(context.settings.slideshow.stop_on_scroll) {
  405. context.slideshow.stop();
  406. };
  407. context.thumbs_wrapper.animate({scrollLeft: left +'px'});
  408. return false;
  409. }
  410. ).css('opacity', 0.6).hover(
  411. function() {
  412. var direction = 'left';
  413. if($(this).is('.ad-forward')) {
  414. direction = 'right';
  415. };
  416. thumbs_scroll_interval = setInterval(
  417. function() {
  418. has_scrolled++;
  419. // Don't want to stop the slideshow just because we scrolled a pixel or two
  420. if(has_scrolled > 30 && context.settings.slideshow.stop_on_scroll) {
  421. context.slideshow.stop();
  422. };
  423. var left = context.thumbs_wrapper.scrollLeft() + 1;
  424. if(direction == 'left') {
  425. left = context.thumbs_wrapper.scrollLeft() - 1;
  426. };
  427. context.thumbs_wrapper.scrollLeft(left);
  428. },
  429. 10
  430. );
  431. $(this).css('opacity', 1);
  432. },
  433. function() {
  434. has_scrolled = 0;
  435. clearInterval(thumbs_scroll_interval);
  436. $(this).css('opacity', 0.6);
  437. }
  438. );
  439. },
  440. _afterShow: function() {
  441. this.gallery_info.html((this.current_index + 1) +' / '+ this.images.length);
  442. if(!this.settings.cycle) {
  443. // Needed for IE
  444. this.prev_link.show().css('height', this.image_wrapper_height);
  445. this.next_link.show().css('height', this.image_wrapper_height);
  446. if(this.current_index == (this.images.length - 1)) {
  447. this.next_link.hide();
  448. };
  449. if(this.current_index == 0) {
  450. this.prev_link.hide();
  451. };
  452. };
  453. this.fireCallback(this.settings.callbacks.afterImageVisible);
  454. },
  455. /**
  456. * Checks if the image is small enough to fit inside the container
  457. * If it's not, shrink it proportionally
  458. */
  459. _getContainedImageSize: function(image_width, image_height) {
  460. if(image_height > this.image_wrapper_height) {
  461. var ratio = image_width / image_height;
  462. image_height = this.image_wrapper_height;
  463. image_width = this.image_wrapper_height * ratio;
  464. };
  465. if(image_width > this.image_wrapper_width) {
  466. var ratio = image_height / image_width;
  467. image_width = this.image_wrapper_width;
  468. image_height = this.image_wrapper_width * ratio;
  469. };
  470. return {width: image_width, height: image_height};
  471. },
  472. /**
  473. * If the image dimensions are smaller than the wrapper, we position
  474. * it in the middle anyway
  475. */
  476. _centerImage: function(img_container, image_width, image_height) {
  477. img_container.css('top', '0px');
  478. if(image_height < this.image_wrapper_height) {
  479. var dif = this.image_wrapper_height - image_height;
  480. img_container.css('top', (dif / 2) +'px');
  481. };
  482. img_container.css('left', '0px');
  483. if(image_width < this.image_wrapper_width) {
  484. var dif = this.image_wrapper_width - image_width;
  485. img_container.css('left', (dif / 2) +'px');
  486. };
  487. },
  488. _getDescription: function(image) {
  489. var desc = false;
  490. if(image.desc.length || image.title.length) {
  491. var title = '';
  492. if(image.title.length) {
  493. title = '<strong class="ad-description-title">'+ image.title +'</strong>';
  494. };
  495. var desc = '';
  496. if(image.desc.length) {
  497. desc = '<span>'+ image.desc +'</span>';
  498. };
  499. desc = $('<p class="ad-image-description">'+ title + desc +'</p>');
  500. };
  501. return desc;
  502. },
  503. /**
  504. * @param function callback Gets fired when the image has loaded, is displaying
  505. * and it's animation has finished
  506. */
  507. showImage: function(index, callback) {
  508. if(this.images[index] && !this.in_transition) {
  509. var context = this;
  510. var image = this.images[index];
  511. this.in_transition = true;
  512. if(!image.preloaded) {
  513. this.loading(true);
  514. this.preloadImage(index, function() {
  515. context.loading(false);
  516. context._showWhenLoaded(index, callback);
  517. });
  518. } else {
  519. this._showWhenLoaded(index, callback);
  520. };
  521. };
  522. },
  523. /**
  524. * @param function callback Gets fired when the image has loaded, is displaying
  525. * and it's animation has finished
  526. */
  527. _showWhenLoaded: function(index, callback) {
  528. if(this.images[index]) {
  529. var context = this;
  530. var image = this.images[index];
  531. var img_container = $(document.createElement('div')).addClass('ad-image');
  532. var img = $(new Image()).attr('src', image.image);
  533. if(image.link) {
  534. var link = $('<a href="'+ image.link +'" target="_blank"></a>');
  535. link.append(img);
  536. img_container.append(link);
  537. } else {
  538. img_container.append(img);
  539. }
  540. this.image_wrapper.prepend(img_container);
  541. var size = this._getContainedImageSize(image.size.width, image.size.height);
  542. img.attr('width', size.width);
  543. img.attr('height', size.height);
  544. img_container.css({width: size.width +'px', height: size.height +'px'});
  545. this._centerImage(img_container, size.width, size.height);
  546. var desc = this._getDescription(image, img_container);
  547. if(desc) {
  548. if(!this.settings.description_wrapper) {
  549. img_container.append(desc);
  550. var width = size.width - parseInt(desc.css('padding-left'), 10) - parseInt(desc.css('padding-right'), 10);
  551. desc.css('width', width +'px');
  552. } else {
  553. this.settings.description_wrapper.append(desc);
  554. }
  555. };
  556. this.highLightThumb(this.nav.find('.ad-thumb'+ index));
  557. var direction = 'right';
  558. if(this.current_index < index) {
  559. direction = 'left';
  560. };
  561. this.fireCallback(this.settings.callbacks.beforeImageVisible);
  562. if(this.current_image || this.settings.animate_first_image) {
  563. var animation_speed = this.settings.animation_speed;
  564. var easing = 'swing';
  565. var animation = this.animations[this.settings.effect].call(this, img_container, direction, desc);
  566. if(typeof animation.speed != 'undefined') {
  567. animation_speed = animation.speed;
  568. };
  569. if(typeof animation.easing != 'undefined') {
  570. easing = animation.easing;
  571. };
  572. if(this.current_image) {
  573. var old_image = this.current_image;
  574. var old_description = this.current_description;
  575. old_image.animate(animation.old_image, animation_speed, easing,
  576. function() {
  577. old_image.remove();
  578. if(old_description) old_description.remove();
  579. }
  580. );
  581. };
  582. img_container.animate(animation.new_image, animation_speed, easing,
  583. function() {
  584. context.current_index = index;
  585. context.current_image = img_container;
  586. context.current_description = desc;
  587. context.in_transition = false;
  588. context._afterShow();
  589. context.fireCallback(callback);
  590. }
  591. );
  592. } else {
  593. this.current_index = index;
  594. this.current_image = img_container;
  595. context.current_description = desc;
  596. this.in_transition = false;
  597. context._afterShow();
  598. this.fireCallback(callback);
  599. };
  600. };
  601. },
  602. nextIndex: function() {
  603. if(this.current_index == (this.images.length - 1)) {
  604. if(!this.settings.cycle) {
  605. return false;
  606. };
  607. var next = 0;
  608. } else {
  609. var next = this.current_index + 1;
  610. };
  611. return next;
  612. },
  613. nextImage: function(callback) {
  614. var next = this.nextIndex();
  615. if(next === false) return false;
  616. this.preloadImage(next + 1);
  617. this.showImage(next, callback);
  618. return true;
  619. },
  620. prevIndex: function() {
  621. if(this.current_index == 0) {
  622. if(!this.settings.cycle) {
  623. return false;
  624. };
  625. var prev = this.images.length - 1;
  626. } else {
  627. var prev = this.current_index - 1;
  628. };
  629. return prev;
  630. },
  631. prevImage: function(callback) {
  632. var prev = this.prevIndex();
  633. if(prev === false) return false;
  634. this.preloadImage(prev - 1);
  635. this.showImage(prev, callback);
  636. return true;
  637. },
  638. preloadAll: function() {
  639. var context = this;
  640. var i = 0;
  641. function preloadNext() {
  642. if(i < context.images.length) {
  643. i++;
  644. context.preloadImage(i, preloadNext);
  645. };
  646. };
  647. context.preloadImage(i, preloadNext);
  648. },
  649. preloadImage: function(index, callback) {
  650. if(this.images[index]) {
  651. var image = this.images[index];
  652. if(!this.images[index].preloaded) {
  653. var img = $(new Image());
  654. img.attr('src', image.image);
  655. if(!this.isImageLoaded(img[0])) {
  656. this.preloads.append(img);
  657. var context = this;
  658. img.load(
  659. function() {
  660. image.preloaded = true;
  661. image.size = { width: this.width, height: this.height };
  662. context.fireCallback(callback);
  663. }
  664. ).error(
  665. function() {
  666. image.error = true;
  667. image.preloaded = false;
  668. image.size = false;
  669. }
  670. );
  671. } else {
  672. image.preloaded = true;
  673. image.size = { width: img[0].width, height: img[0].height };
  674. this.fireCallback(callback);
  675. };
  676. } else {
  677. this.fireCallback(callback);
  678. };
  679. };
  680. },
  681. isImageLoaded: function(img) {
  682. if(typeof img.complete != 'undefined' && !img.complete) {
  683. return false;
  684. };
  685. if(typeof img.naturalWidth != 'undefined' && img.naturalWidth == 0) {
  686. return false;
  687. };
  688. return true;
  689. },
  690. highLightThumb: function(thumb) {
  691. this.thumbs_wrapper.find('.ad-active').removeClass('ad-active');
  692. thumb.addClass('ad-active');
  693. if(this.settings.thumb_opacity < 1) {
  694. this.thumbs_wrapper.find('a:not(.ad-active) img').fadeTo(300, this.settings.thumb_opacity);
  695. thumb.find('img').fadeTo(300, 1);
  696. };
  697. var left = thumb[0].parentNode.offsetLeft;
  698. left -= (this.nav_display_width / 2) - (thumb[0].offsetWidth / 2);
  699. this.thumbs_wrapper.animate({scrollLeft: left +'px'});
  700. },
  701. fireCallback: function(fn) {
  702. if($.isFunction(fn)) {
  703. fn.call(this);
  704. };
  705. }
  706. };
  707. function AdGallerySlideshow(nextimage_callback, settings) {
  708. this.init(nextimage_callback, settings);
  709. };
  710. AdGallerySlideshow.prototype = {
  711. start_link: false,
  712. stop_link: false,
  713. countdown: false,
  714. controls: false,
  715. settings: false,
  716. nextimage_callback: false,
  717. enabled: false,
  718. running: false,
  719. countdown_interval: false,
  720. init: function(nextimage_callback, settings) {
  721. var context = this;
  722. this.nextimage_callback = nextimage_callback;
  723. this.settings = settings;
  724. },
  725. create: function() {
  726. this.start_link = $('<span class="ad-slideshow-start">'+ this.settings.start_label +'</span>');
  727. this.stop_link = $('<span class="ad-slideshow-stop">'+ this.settings.stop_label +'</span>');
  728. this.countdown = $('<span class="ad-slideshow-countdown"></span>');
  729. this.controls = $('<div class="ad-slideshow-controls"></div>');
  730. this.controls.append(this.start_link).append(this.stop_link).append(this.countdown);
  731. this.countdown.hide();
  732. var context = this;
  733. this.start_link.click(
  734. function() {
  735. context.start();
  736. }
  737. );
  738. this.stop_link.click(
  739. function() {
  740. context.stop();
  741. }
  742. );
  743. $(document).keydown(
  744. function(e) {
  745. if(e.keyCode == 83) {
  746. // 's'
  747. if(context.running) {
  748. context.stop();
  749. } else {
  750. context.start();
  751. };
  752. };
  753. }
  754. );
  755. return this.controls;
  756. },
  757. disable: function() {
  758. this.enabled = false;
  759. this.stop();
  760. this.controls.hide();
  761. },
  762. enable: function() {
  763. this.enabled = true;
  764. this.controls.show();
  765. },
  766. toggle: function() {
  767. if(this.enabled) {
  768. this.disable();
  769. } else {
  770. this.enable();
  771. };
  772. },
  773. start: function() {
  774. if(this.running || !this.enabled) return false;
  775. var context = this;
  776. this.running = true;
  777. this.controls.addClass('ad-slideshow-running');
  778. this._next();
  779. this.fireCallback(this.settings.onStart);
  780. return true;
  781. },
  782. stop: function() {
  783. if(!this.running) return false;
  784. this.running = false;
  785. this.countdown.hide();
  786. this.controls.removeClass('ad-slideshow-running');
  787. clearInterval(this.countdown_interval);
  788. this.fireCallback(this.settings.onStop);
  789. return true;
  790. },
  791. _next: function() {
  792. var context = this;
  793. var pre = this.settings.countdown_prefix;
  794. var su = this.settings.countdown_sufix;
  795. clearInterval(context.countdown_interval);
  796. this.countdown.show().html(pre + (this.settings.speed / 1000) + su);
  797. var slide_timer = 0;
  798. this.countdown_interval = setInterval(
  799. function() {
  800. slide_timer += 1000;
  801. if(slide_timer >= context.settings.speed) {
  802. var whenNextIsShown = function() {
  803. // A check so the user hasn't stoped the slideshow during the
  804. // animation
  805. if(context.running) {
  806. context._next();
  807. };
  808. slide_timer = 0;
  809. };
  810. if(!context.nextimage_callback(whenNextIsShown)) {
  811. context.stop();
  812. };
  813. slide_timer = 0;
  814. };
  815. var sec = parseInt(context.countdown.text().replace(/[^0-9]/g, ''), 10);
  816. sec--;
  817. if(sec > 0) {
  818. context.countdown.html(pre + sec + su);
  819. };
  820. },
  821. 1000
  822. );
  823. },
  824. fireCallback: function(fn) {
  825. if($.isFunction(fn)) {
  826. fn.call(this);
  827. };
  828. }
  829. };
  830. })(jQuery);