/*!
* 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: 0,
margin: 0,
width: 700,
height: 400,
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));