/*!
* fancyBox - jQuery Plugin
* version: 2.0.6 (16/04/2012)
* @requires jQuery v1.6 or later
*
* Examples at http://fancyapps.com/fancybox/
* License: www.fancyapps.com/fancybox/#license
*
* Copyright 2012 Janis Skarnelis - janis@fancyapps.com
*
*/
(function (window, document, $, undefined) {
"use strict";
var W = $(window),
D = $(document),
F = $.fancybox = function () {
F.open.apply( this, arguments );
},
didResize = false,
resizeTimer = null,
isTouch = document.createTouch !== undefined,
isString = function(str) {
return $.type(str) === "string";
},
isPercentage = function(str) {
return isString(str) && str.indexOf('%') > 0;
},
getValue = function(value, dim) {
if (dim && isPercentage(value)) {
value = F.getViewport()[ dim ] / 100 * parseInt(value, 10);
}
return Math.round(value) + 'px';
};
$.extend(F, {
// The current version of fancyBox
version: '2.0.5',
defaults: {
padding: 15,
margin: 20,
width: 800,
height: 600,
minWidth: 100,
minHeight: 100,
maxWidth: 9999,
maxHeight: 9999,
autoSize: true,
autoResize: !isTouch,
autoCenter : !isTouch,
fitToView: true,
aspectRatio: false,
topRatio: 0.5,
fixed: false,
scrolling: 'auto', // 'auto', 'yes' or 'no'
wrapCSS: '',
arrows: true,
closeBtn: true,
closeClick: false,
nextClick : false,
mouseWheel: true,
autoPlay: false,
playSpeed: 3000,
preload : 3,
modal: false,
loop: true,
ajax: { dataType: 'html', headers: { 'X-fancyBox': true } },
keys: {
next: [13, 32, 34, 39, 40], // enter, space, page down, right arrow, down arrow
prev: [8, 33, 37, 38], // backspace, page up, left arrow, up arrow
close: [27] // escape key
},
// Override some properties
index: 0,
type: null,
href: null,
content: null,
title: null,
// HTML templates
tpl: {
wrap: '
The requested content cannot be loaded.
Please try again later.
',
closeBtn: '').html(content).find(current.selector);
} else if (content instanceof $) {
if (content.parent().hasClass('fancybox-inner')) {
content.parents('.fancybox-wrap').unbind('onReset');
}
content = content.show().detach();
$(F.wrap).bind('onReset', function () {
content.appendTo('body').hide();
});
}
if (current.autoSize) {
loadingBay = $('
')
.appendTo('body')
.css({
minWidth : getValue(minWidth, 'w'),
minHeight : getValue(minHeight, 'h'),
maxWidth : getValue(maxWidth, 'w'),
maxHeight : getValue(maxHeight, 'h')
})
.append(content);
current.width = loadingBay.width();
current.height = loadingBay.height();
// Re-check to fix 1px bug in some browsers
loadingBay.width( F.current.width );
if (loadingBay.height() > current.height) {
loadingBay.width(current.width + 1);
current.width = loadingBay.width();
current.height = loadingBay.height();
}
content = loadingBay.contents().detach();
loadingBay.remove();
}
break;
case 'image':
content = current.tpl.image.replace('{href}', current.href);
current.aspectRatio = true;
break;
case 'swf':
content = current.tpl.swf.replace(/\{width\}/g, current.width).replace(/\{height\}/g, current.height).replace(/\{href\}/g, current.href);
break;
case 'iframe':
content = $(current.tpl.iframe.replace('{rnd}', new Date().getTime()) )
.attr('scrolling', current.scrolling)
.attr('src', current.href);
current.scrolling = isTouch ? 'scroll' : 'auto';
break;
}
if (type === 'image' || type === 'swf') {
current.autoSize = false;
current.scrolling = 'visible';
}
if (type === 'iframe' && current.autoSize) {
F.showLoading();
F._setDimension();
F.inner.css('overflow', current.scrolling);
content.bind({
onCancel : function() {
$(this).unbind();
F._afterZoomOut();
},
load : function() {
F.hideLoading();
try {
if (this.contentWindow.document.location) {
F.current.height = $(this).contents().find('body').height();
}
} catch (e) {
F.current.autoSize = false;
}
F[ F.isOpen ? '_afterZoomIn' : '_beforeShow']();
}
}).appendTo(F.inner);
} else {
F.inner.append(content);
F._beforeShow();
}
},
_beforeShow : function() {
F.coming = null;
//Give a chance for helpers or callbacks to update elements
F.trigger('beforeShow');
//Set initial dimensions and hide
F._setDimension();
F.wrap.hide().removeClass('fancybox-tmp');
F.bindEvents();
F._preloadImages();
F.transitions[ F.isOpened ? F.current.nextMethod : F.current.openMethod ]();
},
_setDimension: function () {
var wrap = F.wrap,
inner = F.inner,
current = F.current,
viewport = F.getViewport(),
margin = current.margin,
padding2 = current.padding * 2,
width = current.width,
height = current.height,
maxWidth = current.maxWidth + padding2,
maxHeight = current.maxHeight + padding2,
minWidth = current.minWidth + padding2,
minHeight = current.minHeight + padding2,
ratio,
height_;
viewport.w -= (margin[1] + margin[3]);
viewport.h -= (margin[0] + margin[2]);
if (isPercentage(width)) {
width = (((viewport.w - padding2) * parseFloat(width)) / 100);
}
if (isPercentage(height)) {
height = (((viewport.h - padding2) * parseFloat(height)) / 100);
}
ratio = width / height;
width += padding2;
height += padding2;
if (current.fitToView) {
maxWidth = Math.min(viewport.w, maxWidth);
maxHeight = Math.min(viewport.h, maxHeight);
}
if (current.aspectRatio) {
if (width > maxWidth) {
width = maxWidth;
height = ((width - padding2) / ratio) + padding2;
}
if (height > maxHeight) {
height = maxHeight;
width = ((height - padding2) * ratio) + padding2;
}
if (width < minWidth) {
width = minWidth;
height = ((width - padding2) / ratio) + padding2;
}
if (height < minHeight) {
height = minHeight;
width = ((height - padding2) * ratio) + padding2;
}
} else {
width = Math.max(minWidth, Math.min(width, maxWidth));
height = Math.max(minHeight, Math.min(height, maxHeight));
}
width = Math.round(width);
height = Math.round(height);
//Reset dimensions
$(wrap.add(inner)).width('auto').height('auto');
inner.width(width - padding2).height(height - padding2);
wrap.width(width);
height_ = wrap.height(); // Real wrap height
//Fit wrapper inside
if (width > maxWidth || height_ > maxHeight) {
while ((width > maxWidth || height_ > maxHeight) && width > minWidth && height_ > minHeight) {
height = height - 10;
if (current.aspectRatio) {
width = Math.round(((height - padding2) * ratio) + padding2);
if (width < minWidth) {
width = minWidth;
height = ((width - padding2) / ratio) + padding2;
}
} else {
width = width - 10;
}
inner.width(width - padding2).height(height - padding2);
wrap.width(width);
height_ = wrap.height();
}
}
current.dim = {
width : getValue(width),
height : getValue(height_)
};
current.canGrow = current.autoSize && height > minHeight && height < maxHeight;
current.canShrink = false;
current.canExpand = false;
if ((width - padding2) < current.width || (height - padding2) < current.height) {
current.canExpand = true;
} else if ((width > viewport.w || height_ > viewport.h) && width > minWidth && height > minHeight) {
current.canShrink = true;
}
F.innerSpace = height_ - padding2 - inner.height();
},
_getPosition: function (onlyAbsolute) {
var current = F.current,
viewport = F.getViewport(),
margin = current.margin,
width = F.wrap.width() + margin[1] + margin[3],
height = F.wrap.height() + margin[0] + margin[2],
rez = {
position: 'absolute',
top : margin[0] + viewport.y,
left : margin[3] + viewport.x
};
if (current.autoCenter && current.fixed && !onlyAbsolute && height <= viewport.h && width <= viewport.w) {
rez = {
position: 'fixed',
top : margin[0],
left : margin[3]
};
}
rez.top = getValue(Math.max(rez.top, rez.top + ((viewport.h - height) * current.topRatio)));
rez.left = getValue(Math.max(rez.left, rez.left + ((viewport.w - width) * 0.5)));
return rez;
},
_afterZoomIn: function () {
var current = F.current, scrolling = current ? current.scrolling : 'no';
if (!current) {
return;
}
F.isOpen = F.isOpened = true;
F.wrap.addClass('fancybox-opened');
F.inner.css('overflow', scrolling === 'yes' ? 'scroll' : (scrolling === 'no' ? 'hidden' : scrolling));
F.trigger('afterShow');
F.update();
//Assign a click event
if (current.closeClick || current.nextClick) {
F.inner.css('cursor', 'pointer').bind('click.fb', function(e) {
if (!$(e.target).is('a') && !$(e.target).parent().is('a')) {
F[ current.closeClick ? 'close' : 'next' ]();
}
});
}
//Create a close button
if (current.closeBtn) {
$(current.tpl.closeBtn).appendTo(F.skin).bind('click.fb', F.close);
}
//Create navigation arrows
if (current.arrows && F.group.length > 1) {
if (current.loop || current.index > 0) {
$(current.tpl.prev).appendTo(F.outer).bind('click.fb', F.prev);
}
if (current.loop || current.index < F.group.length - 1) {
$(current.tpl.next).appendTo(F.outer).bind('click.fb', F.next);
}
}
if (F.opts.autoPlay && !F.player.isActive) {
F.opts.autoPlay = false;
F.play();
}
},
_afterZoomOut: function () {
var current = F.current;
F.wrap.trigger('onReset').remove();
$.extend(F, {
group: {},
opts: {},
current: null,
isActive: false,
isOpened: false,
isOpen: false,
wrap: null,
skin: null,
outer: null,
inner: null
});
F.trigger('afterClose', current);
}
});
/*
* Default transitions
*/
F.transitions = {
getOrigPosition: function () {
var current = F.current,
element = current.element,
padding = current.padding,
orig = $(current.orig),
pos = {},
width = 50,
height = 50,
viewport;
if (!orig.length && current.isDom && $(element).is(':visible')) {
orig = $(element).find('img:first');
if (!orig.length) {
orig = $(element);
}
}
if (orig.length) {
pos = orig.offset();
if (orig.is('img')) {
width = orig.outerWidth();
height = orig.outerHeight();
}
} else {
viewport = F.getViewport();
pos.top = viewport.y + (viewport.h - height) * 0.5;
pos.left = viewport.x + (viewport.w - width) * 0.5;
}
pos = {
top : getValue(pos.top - padding),
left : getValue(pos.left - padding),
width : getValue(width + padding * 2),
height : getValue(height + padding * 2)
};
return pos;
},
step: function (now, fx) {
var prop = fx.prop, value, ratio;
if (prop === 'width' || prop === 'height') {
value = Math.ceil(now - (F.current.padding * 2));
if (prop === 'height') {
ratio = (now - fx.start) / (fx.end - fx.start);
if (fx.start > fx.end) {
ratio = 1 - ratio;
}
value -= F.innerSpace * ratio;
}
F.inner[prop](value);
}
},
zoomIn: function () {
var wrap = F.wrap,
current = F.current,
effect = current.openEffect,
elastic = effect === 'elastic',
dim = current.dim,
startPos = $.extend({}, dim, F._getPosition( elastic )),
endPos = $.extend({opacity : 1}, startPos);
//Remove "position" property that breaks older IE
delete endPos.position;
if (elastic) {
startPos = this.getOrigPosition();
if (current.openOpacity) {
startPos.opacity = 0;
}
F.outer.add(F.inner).width('auto').height('auto');
} else if (effect === 'fade') {
startPos.opacity = 0;
}
wrap.css(startPos)
.show()
.animate(endPos, {
duration : effect === 'none' ? 0 : current.openSpeed,
easing : current.openEasing,
step : elastic ? this.step : null,
complete : F._afterZoomIn
});
},
zoomOut: function () {
var wrap = F.wrap,
current = F.current,
effect = current.openEffect,
elastic = effect === 'elastic',
endPos = {opacity : 0};
if (elastic) {
if (wrap.css('position') === 'fixed') {
wrap.css(F._getPosition(true));
}
endPos = this.getOrigPosition();
if (current.closeOpacity) {
endPos.opacity = 0;
}
}
wrap.animate(endPos, {
duration : effect === 'none' ? 0 : current.closeSpeed,
easing : current.closeEasing,
step : elastic ? this.step : null,
complete : F._afterZoomOut
});
},
changeIn: function () {
var wrap = F.wrap,
current = F.current,
effect = current.nextEffect,
elastic = effect === 'elastic',
startPos = F._getPosition( elastic ),
endPos = { opacity : 1 };
startPos.opacity = 0;
if (elastic) {
startPos.top = getValue(parseInt(startPos.top, 10) - 200);
endPos.top = '+=200px';
}
wrap.css(startPos)
.show()
.animate(endPos, {
duration : effect === 'none' ? 0 : current.nextSpeed,
easing : current.nextEasing,
complete : F._afterZoomIn
});
},
changeOut: function () {
var wrap = F.wrap,
current = F.current,
effect = current.prevEffect,
endPos = { opacity : 0 },
cleanUp = function () {
$(this).trigger('onReset').remove();
};
wrap.removeClass('fancybox-opened');
if (effect === 'elastic') {
endPos.top = '+=200px';
}
wrap.animate(endPos, {
duration : effect === 'none' ? 0 : current.prevSpeed,
easing : current.prevEasing,
complete : cleanUp
});
}
};
/*
* Overlay helper
*/
F.helpers.overlay = {
overlay: null,
update: function () {
var width, scrollWidth, offsetWidth;
//Reset width/height so it will not mess
this.overlay.width('100%').height('100%');
if ($.browser.msie || isTouch) {
scrollWidth = Math.max(document.documentElement.scrollWidth, document.body.scrollWidth);
offsetWidth = Math.max(document.documentElement.offsetWidth, document.body.offsetWidth);
width = scrollWidth < offsetWidth ? W.width() : scrollWidth;
} else {
width = D.width();
}
this.overlay.width(width).height(D.height());
},
beforeShow: function (opts) {
if (this.overlay) {
return;
}
opts = $.extend(true, {}, F.defaults.helpers.overlay, opts);
this.overlay = $('
').css(opts.css).appendTo('body');
if (opts.closeClick) {
this.overlay.bind('click.fb', F.close);
}
if (F.current.fixed && !isTouch) {
this.overlay.addClass('overlay-fixed');
} else {
this.update();
this.onUpdate = function () {
this.update();
};
}
this.overlay.fadeTo(opts.speedIn, opts.opacity);
},
afterClose: function (opts) {
if (this.overlay) {
this.overlay.fadeOut(opts.speedOut || 0, function () {
$(this).remove();
});
}
this.overlay = null;
}
};
/*
* Title helper
*/
F.helpers.title = {
beforeShow: function (opts) {
var title, text = F.current.title;
if (text) {
title = $('
' + text + '
').appendTo('body');
if (opts.type === 'float') {
//This helps for some browsers
title.width(title.width());
title.wrapInner('
');
//Increase bottom margin so this title will also fit into viewport
F.current.margin[2] += Math.abs(parseInt(title.css('margin-bottom'), 10));
}
title.appendTo(opts.type === 'over' ? F.inner : (opts.type === 'outside' ? F.wrap : F.skin));
}
}
};
// jQuery plugin initialization
$.fn.fancybox = function (options) {
var that = $(this),
selector = this.selector || '',
index,
run = function(e) {
var what = this, idx = index, relType, relVal;
if (!(e.ctrlKey || e.altKey || e.shiftKey || e.metaKey) && !$(what).is('.fancybox-wrap')) {
e.preventDefault();
relType = options.groupAttr || 'data-fancybox-group';
relVal = $(what).attr(relType);
if (!relVal) {
relType = 'rel';
relVal = what[ relType ];
}
if (relVal && relVal !== '' && relVal !== 'nofollow') {
what = selector.length ? $(selector) : that;
what = what.filter('[' + relType + '="' + relVal + '"]');
idx = what.index(this);
}
options.index = idx;
F.open(what, options);
}
};
options = options || {};
index = options.index || 0;
if (selector) {
D.undelegate(selector, 'click.fb-start').delegate(selector, 'click.fb-start', run);
} else {
that.unbind('click.fb-start').bind('click.fb-start', run);
}
return this;
};
// Test for fixedPosition needs a body at doc ready
$(document).ready(function() {
F.defaults.fixed = $.support.fixedPosition || (!($.browser.msie && $.browser.version <= 6) && !isTouch);
});
}(window, document, jQuery));