$(window).on("load", function() {
var figureCount = 0;
function resizeImage($image) {
var maxWidth = $image.attr('data-lbir-max-width')
|| $image.attr('width') || $image.css('width'),
maxHeight = $(this).attr('data-lbir-max-height')
|| $image.attr('height') || $image.css('height'),
ratio = 0;
// Reset the image to natural dimensions so we can resize it with the
// correct scale
$image.css('width', 'auto');
$image.css('height', 'auto');
var width = $image.width(),
height = $image.height(),
resized = false;
if (width > maxWidth) {
ratio = maxWidth / width;
$image.css('width', maxWidth);
$image.css('height', height * ratio);
height = height * ratio;
width = width * ratio;
resized = true;
}
if (height > maxHeight) {
ratio = maxHeight / height;
$image.css('height', maxHeight);
$image.css('width', width * ratio);
width = width * ratio;
resized = true;
}
return resized;
}
function addLightbox($image, resized) {
var title = $image.attr('alt'),
href = $image.attr('src'),
figureText = 'Figure %n:',
expandText = 'Click on the image to expand it',
caption = title;
// Wrap WET4 lightbox anchor tag and a figure block around the image
if (resized) {
$image = $image.wrap(''
).parent()
}
$image.trigger('wb-init.wb-lbx');
var $figure = $image.wrap(
''
).parent();
// Create caption text
if (figureText)
caption = '' + figureText.replace('%n', ++figureCount)
+ ' ' + caption;
if (expandText && resized)
caption += ' (' + expandText + ')';
$figure.append('' + caption + '');
}
$('img.lb-resize').each(function() {
// Shrink all images that have lb-resize and are larger than the maximums
addLightbox($(this), resizeImage($(this)));
});
});