2012-06-02 09:48:07 +03:00
|
|
|
/*!
|
|
|
|
* Thumbnail helper for fancyBox
|
2012-09-30 16:38:09 +03:00
|
|
|
* version: 1.0.6
|
2012-06-02 09:48:07 +03:00
|
|
|
* @requires fancyBox v2.0 or later
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* $(".fancybox").fancybox({
|
2012-09-30 16:38:09 +03:00
|
|
|
* helpers : {
|
|
|
|
* thumbs: {
|
|
|
|
* width : 50,
|
|
|
|
* height : 50
|
|
|
|
* }
|
2012-06-02 09:48:07 +03:00
|
|
|
* }
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* Options:
|
|
|
|
* width - thumbnail width
|
|
|
|
* height - thumbnail height
|
|
|
|
* source - function to obtain the URL of the thumbnail image
|
|
|
|
* position - 'top' or 'bottom'
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
(function ($) {
|
|
|
|
//Shortcut for fancyBox object
|
|
|
|
var F = $.fancybox;
|
|
|
|
|
|
|
|
//Add helper object
|
|
|
|
F.helpers.thumbs = {
|
2012-09-30 16:38:09 +03:00
|
|
|
wrap : null,
|
|
|
|
list : null,
|
|
|
|
width : 0,
|
2012-06-02 09:48:07 +03:00
|
|
|
|
|
|
|
//Default function to obtain the URL of the thumbnail image
|
2012-09-30 16:38:09 +03:00
|
|
|
source: function ( item ) {
|
|
|
|
var href;
|
2012-06-02 09:48:07 +03:00
|
|
|
|
2012-09-30 16:38:09 +03:00
|
|
|
if (item.element) {
|
|
|
|
href = $(item.element).find('img').attr('src');
|
2012-06-02 09:48:07 +03:00
|
|
|
}
|
|
|
|
|
2012-09-30 16:38:09 +03:00
|
|
|
if (!href && item.type === 'image' && item.href) {
|
|
|
|
href = item.href;
|
|
|
|
}
|
2012-06-02 09:48:07 +03:00
|
|
|
|
2012-09-30 16:38:09 +03:00
|
|
|
return href;
|
2012-06-02 09:48:07 +03:00
|
|
|
},
|
|
|
|
|
2012-09-30 16:38:09 +03:00
|
|
|
init: function (opts, obj) {
|
2012-06-02 09:48:07 +03:00
|
|
|
var that = this,
|
|
|
|
list,
|
2012-09-30 16:38:09 +03:00
|
|
|
thumbWidth = opts.width || 50,
|
2012-06-02 09:48:07 +03:00
|
|
|
thumbHeight = opts.height || 50,
|
|
|
|
thumbSource = opts.source || this.source;
|
|
|
|
|
|
|
|
//Build list structure
|
|
|
|
list = '';
|
|
|
|
|
2012-09-30 16:38:09 +03:00
|
|
|
for (var n = 0; n < obj.group.length; n++) {
|
2012-06-02 09:48:07 +03:00
|
|
|
list += '<li><a style="width:' + thumbWidth + 'px;height:' + thumbHeight + 'px;" href="javascript:jQuery.fancybox.jumpto(' + n + ');"></a></li>';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.wrap = $('<div id="fancybox-thumbs"></div>').addClass(opts.position || 'bottom').appendTo('body');
|
|
|
|
this.list = $('<ul>' + list + '</ul>').appendTo(this.wrap);
|
|
|
|
|
|
|
|
//Load each thumbnail
|
2012-09-30 16:38:09 +03:00
|
|
|
$.each(obj.group, function (i) {
|
|
|
|
var href = thumbSource( obj.group[ i ] );
|
|
|
|
|
|
|
|
if (!href) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-02 09:48:07 +03:00
|
|
|
$("<img />").load(function () {
|
2012-09-30 16:38:09 +03:00
|
|
|
var width = this.width,
|
2012-06-02 09:48:07 +03:00
|
|
|
height = this.height,
|
|
|
|
widthRatio, heightRatio, parent;
|
|
|
|
|
|
|
|
if (!that.list || !width || !height) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Calculate thumbnail width/height and center it
|
2012-09-30 16:38:09 +03:00
|
|
|
widthRatio = width / thumbWidth;
|
2012-06-02 09:48:07 +03:00
|
|
|
heightRatio = height / thumbHeight;
|
2012-09-30 16:38:09 +03:00
|
|
|
|
2012-06-02 09:48:07 +03:00
|
|
|
parent = that.list.children().eq(i).find('a');
|
|
|
|
|
|
|
|
if (widthRatio >= 1 && heightRatio >= 1) {
|
|
|
|
if (widthRatio > heightRatio) {
|
2012-09-30 16:38:09 +03:00
|
|
|
width = Math.floor(width / heightRatio);
|
2012-06-02 09:48:07 +03:00
|
|
|
height = thumbHeight;
|
|
|
|
|
|
|
|
} else {
|
2012-09-30 16:38:09 +03:00
|
|
|
width = thumbWidth;
|
2012-06-02 09:48:07 +03:00
|
|
|
height = Math.floor(height / widthRatio);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$(this).css({
|
2012-09-30 16:38:09 +03:00
|
|
|
width : width,
|
|
|
|
height : height,
|
|
|
|
top : Math.floor(thumbHeight / 2 - height / 2),
|
|
|
|
left : Math.floor(thumbWidth / 2 - width / 2)
|
2012-06-02 09:48:07 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
parent.width(thumbWidth).height(thumbHeight);
|
|
|
|
|
|
|
|
$(this).hide().appendTo(parent).fadeIn(300);
|
|
|
|
|
2012-09-30 16:38:09 +03:00
|
|
|
}).attr('src', href);
|
2012-06-02 09:48:07 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
//Set initial width
|
|
|
|
this.width = this.list.children().eq(0).outerWidth(true);
|
|
|
|
|
2012-09-30 16:38:09 +03:00
|
|
|
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)));
|
2012-06-02 09:48:07 +03:00
|
|
|
},
|
|
|
|
|
2012-09-30 16:38:09 +03:00
|
|
|
beforeLoad: function (opts, obj) {
|
2012-06-02 09:48:07 +03:00
|
|
|
//Remove self if gallery do not have at least two items
|
2012-09-30 16:38:09 +03:00
|
|
|
if (obj.group.length < 2) {
|
|
|
|
obj.helpers.thumbs = false;
|
2012-06-02 09:48:07 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Increase bottom margin to give space for thumbs
|
2012-09-30 16:38:09 +03:00
|
|
|
obj.margin[ opts.position === 'top' ? 0 : 2 ] += ((opts.height || 50) + 15);
|
2012-06-02 09:48:07 +03:00
|
|
|
},
|
|
|
|
|
2012-09-30 16:38:09 +03:00
|
|
|
afterShow: function (opts, obj) {
|
2012-06-02 09:48:07 +03:00
|
|
|
//Check if exists and create or update list
|
|
|
|
if (this.list) {
|
2012-09-30 16:38:09 +03:00
|
|
|
this.onUpdate(opts, obj);
|
2012-06-02 09:48:07 +03:00
|
|
|
|
|
|
|
} else {
|
2012-09-30 16:38:09 +03:00
|
|
|
this.init(opts, obj);
|
2012-06-02 09:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//Set active element
|
2012-09-30 16:38:09 +03:00
|
|
|
this.list.children().removeClass('active').eq(obj.index).addClass('active');
|
2012-06-02 09:48:07 +03:00
|
|
|
},
|
|
|
|
|
2012-09-30 16:38:09 +03:00
|
|
|
//Center list
|
|
|
|
onUpdate: function (opts, obj) {
|
|
|
|
if (this.list) {
|
|
|
|
this.list.stop(true).animate({
|
|
|
|
'left': Math.floor($(window).width() * 0.5 - (obj.index * this.width + this.width * 0.5))
|
|
|
|
}, 150);
|
|
|
|
}
|
2012-06-02 09:48:07 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
beforeClose: function () {
|
|
|
|
if (this.wrap) {
|
|
|
|
this.wrap.remove();
|
|
|
|
}
|
|
|
|
|
2012-09-30 16:38:09 +03:00
|
|
|
this.wrap = null;
|
|
|
|
this.list = null;
|
2012-06-02 09:48:07 +03:00
|
|
|
this.width = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}(jQuery));
|