2 * Thumbnail helper for fancyBox
3 * version: 1.0.7 (Mon, 01 Oct 2012)
4 * @requires fancyBox v2.0 or later
7 * $(".fancybox").fancybox({
18 //Shortcut for fancyBox object
24 width : 50, // thumbnail width
25 height : 50, // thumbnail height
26 position : 'bottom', // 'top' or 'bottom'
27 source : function ( item ) { // function to obtain the URL of the thumbnail image
31 href = $(item.element).find('img').attr('src');
34 if (!href && item.type === 'image' && item.href) {
46 init: function (opts, obj) {
49 thumbWidth = opts.width,
50 thumbHeight = opts.height,
51 thumbSource = opts.source;
53 //Build list structure
56 for (var n = 0; n < obj.group.length; n++) {
57 list += '<li><a style="width:' + thumbWidth + 'px;height:' + thumbHeight + 'px;" href="javascript:jQuery.fancybox.jumpto(' + n + ');"></a></li>';
60 this.wrap = $('<div id="fancybox-thumbs"></div>').addClass(opts.position).appendTo('body');
61 this.list = $('<ul>' + list + '</ul>').appendTo(this.wrap);
64 $.each(obj.group, function (i) {
65 var href = thumbSource( obj.group[ i ] );
71 $("<img />").load(function () {
72 var width = this.width,
74 widthRatio, heightRatio, parent;
76 if (!that.list || !width || !height) {
80 //Calculate thumbnail width/height and center it
81 widthRatio = width / thumbWidth;
82 heightRatio = height / thumbHeight;
84 parent = that.list.children().eq(i).find('a');
86 if (widthRatio >= 1 && heightRatio >= 1) {
87 if (widthRatio > heightRatio) {
88 width = Math.floor(width / heightRatio);
93 height = Math.floor(height / widthRatio);
100 top : Math.floor(thumbHeight / 2 - height / 2),
101 left : Math.floor(thumbWidth / 2 - width / 2)
104 parent.width(thumbWidth).height(thumbHeight);
106 $(this).hide().appendTo(parent).fadeIn(300);
108 }).attr('src', href);
112 this.width = this.list.children().eq(0).outerWidth(true);
114 this.list.width(this.width * (obj.group.length + 1)).css('left', Math.floor($(window).width() * 0.5 - (obj.index * this.width + this.width * 0.5)));
117 beforeLoad: function (opts, obj) {
118 //Remove self if gallery do not have at least two items
119 if (obj.group.length < 2) {
120 obj.helpers.thumbs = false;
125 //Increase bottom margin to give space for thumbs
126 obj.margin[ opts.position === 'top' ? 0 : 2 ] += ((opts.height) + 15);
129 afterShow: function (opts, obj) {
130 //Check if exists and create or update list
132 this.onUpdate(opts, obj);
135 this.init(opts, obj);
139 this.list.children().removeClass('active').eq(obj.index).addClass('active');
143 onUpdate: function (opts, obj) {
145 this.list.stop(true).animate({
146 'left': Math.floor($(window).width() * 0.5 - (obj.index * this.width + this.width * 0.5))
151 beforeClose: function () {