/*
* fancybox - jquery plugin
* simple and fancy lightbox alternative
*
* examples and documentation at: http://fancybox.net
*
* copyright (c) 2008 - 2010 janis skarnelis
*
* version: 1.3.1 (05/03/2010)
* requires: jquery v1.3+
*
* dual licensed under the mit and gpl licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
(function($) {
var tmp, loading, overlay, wrap, outer, inner, close, nav_left, nav_right,
selectedindex = 0, selectedopts = {}, selectedarray = [], currentindex = 0, currentopts = {}, currentarray = [],
ajaxloader = null, imgpreloader = new image(), imgregexp = /\.(jpg|gif|png|bmp|jpeg)(.*)?$/i, swfregexp = /[^\.]\.(swf)\s*$/i,
loadingtimer, loadingframe = 1,
start_pos, final_pos, busy = false, shadow = 20, fx = $.extend($('
')[0], { prop: 0 }), titleh = 0,
isie6 = !$.support.opacity && !window.xmlhttprequest,
/*
* private methods
*/
fancybox_abort = function() {
loading.hide();
imgpreloader.onerror = imgpreloader.onload = null;
if (ajaxloader) {
ajaxloader.abort();
}
tmp.empty();
},
fancybox_error = function() {
$.fancybox('the requested content cannot be loaded.
please try again later.
', {
'scrolling' : 'no',
'padding' : 20,
'transitionin' : 'none',
'transitionout' : 'none'
});
},
fancybox_get_viewport = function() {
return [ $(window).width(), $(window).height(), $(document).scrollleft(), $(document).scrolltop() ];
},
fancybox_get_zoom_to = function () {
var view = fancybox_get_viewport(),
to = {},
margin = currentopts.margin,
resize = currentopts.autoscale,
horizontal_space = (shadow + margin) * 2,
vertical_space = (shadow + margin) * 2,
double_padding = (currentopts.padding * 2),
ratio;
if (currentopts.width.tostring().indexof('%') > -1) {
to.width = ((view[0] * parsefloat(currentopts.width)) / 100) - (shadow * 2) ;
resize = false;
} else {
to.width = currentopts.width + double_padding;
}
if (currentopts.height.tostring().indexof('%') > -1) {
to.height = ((view[1] * parsefloat(currentopts.height)) / 100) - (shadow * 2);
resize = false;
} else {
to.height = currentopts.height + double_padding;
}
if (resize && (to.width > (view[0] - horizontal_space) || to.height > (view[1] - vertical_space))) {
if (selectedopts.type == 'image' || selectedopts.type == 'swf') {
horizontal_space += double_padding;
vertical_space += double_padding;
ratio = math.min(math.min( view[0] - horizontal_space, currentopts.width) / currentopts.width, math.min( view[1] - vertical_space, currentopts.height) / currentopts.height);
to.width = math.round(ratio * (to.width - double_padding)) + double_padding;
to.height = math.round(ratio * (to.height - double_padding)) + double_padding;
} else {
to.width = math.min(to.width, (view[0] - horizontal_space));
to.height = math.min(to.height, (view[1] - vertical_space));
}
}
to.top = view[3] + ((view[1] - (to.height + (shadow * 2 ))) * 0.5);
to.left = view[2] + ((view[0] - (to.width + (shadow * 2 ))) * 0.5);
if (currentopts.autoscale === false) {
to.top = math.max(view[3] + margin, to.top);
to.left = math.max(view[2] + margin, to.left);
}
return to;
},
fancybox_format_title = function(title) {
if (title && title.length) {
switch (currentopts.titleposition) {
case 'inside':
return title;
case 'over':
return '' + title + '';
default:
return '' + title + '';
}
}
return false;
},
fancybox_process_title = function() {
var title = currentopts.title,
width = final_pos.width - (currentopts.padding * 2),
titlec = 'fancybox-title-' + currentopts.titleposition;
$('#fancybox-title').remove();
titleh = 0;
if (currentopts.titleshow === false) {
return;
}
title = $.isfunction(currentopts.titleformat) ? currentopts.titleformat(title, currentarray, currentindex, currentopts) : fancybox_format_title(title);
if (!title || title === '') {
return;
}
$('').css({
'width' : width,
'paddingleft' : currentopts.padding,
'paddingright' : currentopts.padding
}).html(title).appendto('body');
switch (currentopts.titleposition) {
case 'inside':
titleh = $("#fancybox-title").outerheight(true) - currentopts.padding;
final_pos.height += titleh;
break;
case 'over':
$('#fancybox-title').css('bottom', currentopts.padding);
break;
default:
$('#fancybox-title').css('bottom', $("#fancybox-title").outerheight(true) * -1);
break;
}
$('#fancybox-title').appendto( outer ).hide();
},
fancybox_set_navigation = function() {
$(document).unbind('keydown.fb').bind('keydown.fb', function(e) {
if (e.keycode == 27 && currentopts.enableescapebutton) {
e.preventdefault();
$.fancybox.close();
} else if (e.keycode == 37) {
e.preventdefault();
$.fancybox.prev();
} else if (e.keycode == 39) {
e.preventdefault();
$.fancybox.next();
}
});
if ($.fn.mousewheel) {
wrap.unbind('mousewheel.fb');
if (currentarray.length > 1) {
wrap.bind('mousewheel.fb', function(e, delta) {
e.preventdefault();
if (busy || delta === 0) {
return;
}
if (delta > 0) {
$.fancybox.prev();
} else {
$.fancybox.next();
}
});
}
}
if (!currentopts.shownavarrows) { return; }
if ((currentopts.cyclic && currentarray.length > 1) || currentindex !== 0) {
nav_left.show();
}
if ((currentopts.cyclic && currentarray.length > 1) || currentindex != (currentarray.length -1)) {
nav_right.show();
}
},
fancybox_preload_images = function() {
var href,
objnext;
if ((currentarray.length -1) > currentindex) {
href = currentarray[ currentindex + 1 ].href;
if (typeof href !== 'undefined' && href.match(imgregexp)) {
objnext = new image();
objnext.src = href;
}
}
if (currentindex > 0) {
href = currentarray[ currentindex - 1 ].href;
if (typeof href !== 'undefined' && href.match(imgregexp)) {
objnext = new image();
objnext.src = href;
}
}
},
_finish = function () {
inner.css('overflow', (currentopts.scrolling == 'auto' ? (currentopts.type == 'image' || currentopts.type == 'iframe' || currentopts.type == 'swf' ? 'hidden' : 'auto') : (currentopts.scrolling == 'yes' ? 'auto' : 'visible')));
if (!$.support.opacity) {
inner.get(0).style.removeattribute('filter');
wrap.get(0).style.removeattribute('filter');
}
$('#fancybox-title').show();
if (currentopts.hideoncontentclick) {
inner.one('click', $.fancybox.close);
}
if (currentopts.hideonoverlayclick) {
overlay.one('click', $.fancybox.close);
}
if (currentopts.showclosebutton) {
close.show();
}
fancybox_set_navigation();
$(window).bind("resize.fb", $.fancybox.center);
if (currentopts.centeronscroll) {
$(window).bind("scroll.fb", $.fancybox.center);
} else {
$(window).unbind("scroll.fb");
}
if ($.isfunction(currentopts.oncomplete)) {
currentopts.oncomplete(currentarray, currentindex, currentopts);
}
busy = false;
fancybox_preload_images();
},
fancybox_draw = function(pos) {
var width = math.round(start_pos.width + (final_pos.width - start_pos.width) * pos),
height = math.round(start_pos.height + (final_pos.height - start_pos.height) * pos),
top = math.round(start_pos.top + (final_pos.top - start_pos.top) * pos),
left = math.round(start_pos.left + (final_pos.left - start_pos.left) * pos);
wrap.css({
'width' : width + 'px',
'height' : height + 'px',
'top' : top + 'px',
'left' : left + 'px'
});
width = math.max(width - currentopts.padding * 2, 0);
height = math.max(height - (currentopts.padding * 2 + (titleh * pos)), 0);
inner.css({
'width' : width + 'px',
'height' : height + 'px'
});
if (typeof final_pos.opacity !== 'undefined') {
wrap.css('opacity', (pos < 0.5 ? 0.5 : pos));
}
},
fancybox_get_obj_pos = function(obj) {
var pos = obj.offset();
pos.top += parsefloat( obj.css('paddingtop') ) || 0;
pos.left += parsefloat( obj.css('paddingleft') ) || 0;
pos.top += parsefloat( obj.css('border-top-width') ) || 0;
pos.left += parsefloat( obj.css('border-left-width') ) || 0;
pos.width = obj.width();
pos.height = obj.height();
return pos;
},
fancybox_get_zoom_from = function() {
var orig = selectedopts.orig ? $(selectedopts.orig) : false,
from = {},
pos,
view;
if (orig && orig.length) {
pos = fancybox_get_obj_pos(orig);
from = {
width : (pos.width + (currentopts.padding * 2)),
height : (pos.height + (currentopts.padding * 2)),
top : (pos.top - currentopts.padding - shadow),
left : (pos.left - currentopts.padding - shadow)
};
} else {
view = fancybox_get_viewport();
from = {
width : 1,
height : 1,
top : view[3] + view[1] * 0.5,
left : view[2] + view[0] * 0.5
};
}
return from;
},
fancybox_show = function() {
loading.hide();
if (wrap.is(":visible") && $.isfunction(currentopts.oncleanup)) {
if (currentopts.oncleanup(currentarray, currentindex, currentopts) === false) {
$.event.trigger('fancybox-cancel');
busy = false;
return;
}
}
currentarray = selectedarray;
currentindex = selectedindex;
currentopts = selectedopts;
inner.get(0).scrolltop = 0;
inner.get(0).scrollleft = 0;
if (currentopts.overlayshow) {
if (isie6) {
$('select:not(#fancybox-tmp select)').filter(function() {
return this.style.visibility !== 'hidden';
}).css({'visibility':'hidden'}).one('fancybox-cleanup', function() {
this.style.visibility = 'inherit';
});
}
overlay.css({
'background-color' : currentopts.overlaycolor,
'opacity' : currentopts.overlayopacity
}).unbind().show();
}
final_pos = fancybox_get_zoom_to();
fancybox_process_title();
if (wrap.is(":visible")) {
$( close.add( nav_left ).add( nav_right ) ).hide();
var pos = wrap.position(),
equal;
start_pos = {
top : pos.top ,
left : pos.left,
width : wrap.width(),
height : wrap.height()
};
equal = (start_pos.width == final_pos.width && start_pos.height == final_pos.height);
inner.fadeout(currentopts.changefade, function() {
var finish_resizing = function() {
inner.html( tmp.contents() ).fadein(currentopts.changefade, _finish);
};
$.event.trigger('fancybox-change');
inner.empty().css('overflow', 'hidden');
if (equal) {
inner.css({
top : currentopts.padding,
left : currentopts.padding,
width : math.max(final_pos.width - (currentopts.padding * 2), 1),
height : math.max(final_pos.height - (currentopts.padding * 2) - titleh, 1)
});
finish_resizing();
} else {
inner.css({
top : currentopts.padding,
left : currentopts.padding,
width : math.max(start_pos.width - (currentopts.padding * 2), 1),
height : math.max(start_pos.height - (currentopts.padding * 2), 1)
});
fx.prop = 0;
$(fx).animate({ prop: 1 }, {
duration : currentopts.changespeed,
easing : currentopts.easingchange,
step : fancybox_draw,
complete : finish_resizing
});
}
});
return;
}
wrap.css('opacity', 1);
if (currentopts.transitionin == 'elastic') {
start_pos = fancybox_get_zoom_from();
inner.css({
top : currentopts.padding,
left : currentopts.padding,
width : math.max(start_pos.width - (currentopts.padding * 2), 1),
height : math.max(start_pos.height - (currentopts.padding * 2), 1)
})
.html( tmp.contents() );
wrap.css(start_pos).show();
if (currentopts.opacity) {
final_pos.opacity = 0;
}
fx.prop = 0;
$(fx).animate({ prop: 1 }, {
duration : currentopts.speedin,
easing : currentopts.easingin,
step : fancybox_draw,
complete : _finish
});
} else {
inner.css({
top : currentopts.padding,
left : currentopts.padding,
width : math.max(final_pos.width - (currentopts.padding * 2), 1),
height : math.max(final_pos.height - (currentopts.padding * 2) - titleh, 1)
})
.html( tmp.contents() );
wrap.css( final_pos ).fadein( currentopts.transitionin == 'none' ? 0 : currentopts.speedin, _finish );
}
},
fancybox_process_inline = function() {
tmp.width( selectedopts.width );
tmp.height( selectedopts.height );
if (selectedopts.width == 'auto') {
selectedopts.width = tmp.width();
}
if (selectedopts.height == 'auto') {
selectedopts.height = tmp.height();
}
fancybox_show();
},
fancybox_process_image = function() {
busy = true;
selectedopts.width = imgpreloader.width;
selectedopts.height = imgpreloader.height;
$("").attr({
'id' : 'fancybox-img',
'src' : imgpreloader.src,
'alt' : selectedopts.title
}).appendto( tmp );
fancybox_show();
},
fancybox_start = function() {
fancybox_abort();
var obj = selectedarray[ selectedindex ],
href,
type,
title,
str,
emb,
selector,
data;
selectedopts = $.extend({}, $.fn.fancybox.defaults, (typeof $(obj).data('fancybox') == 'undefined' ? selectedopts : $(obj).data('fancybox')));
title = obj.title || $(obj).title || selectedopts.title || '';
if (obj.nodename && !selectedopts.orig) {
selectedopts.orig = $(obj).children("img:first").length ? $(obj).children("img:first") : $(obj);
}
if (title === '' && selectedopts.orig) {
title = selectedopts.orig.attr('alt');
}
if (obj.nodename && (/^(?:javascript|#)/i).test(obj.href)) {
href = selectedopts.href || null;
} else {
href = selectedopts.href || obj.href || null;
}
if (selectedopts.type) {
type = selectedopts.type;
if (!href) {
href = selectedopts.content;
}
} else if (selectedopts.content) {
type = 'html';
} else if (href) {
if (href.match(imgregexp)) {
type = 'image';
} else if (href.match(swfregexp)) {
type = 'swf';
} else if ($(obj).hasclass("iframe")) {
type = 'iframe';
} else if (href.match(/#/)) {
obj = href.substr(href.indexof("#"));
type = $(obj).length > 0 ? 'inline' : 'ajax';
} else {
type = 'ajax';
}
} else {
type = 'inline';
}
selectedopts.type = type;
selectedopts.href = href;
selectedopts.title = title;
if (selectedopts.autodimensions && selectedopts.type !== 'iframe' && selectedopts.type !== 'swf') {
selectedopts.width = 'auto';
selectedopts.height = 'auto';
}
if (selectedopts.modal) {
selectedopts.overlayshow = true;
selectedopts.hideonoverlayclick = false;
selectedopts.hideoncontentclick = false;
selectedopts.enableescapebutton = false;
selectedopts.showclosebutton = false;
}
if ($.isfunction(selectedopts.onstart)) {
if (selectedopts.onstart(selectedarray, selectedindex, selectedopts) === false) {
busy = false;
return;
}
}
tmp.css('padding', (shadow + selectedopts.padding + selectedopts.margin));
$('.fancybox-inline-tmp').unbind('fancybox-cancel').bind('fancybox-change', function() {
$(this).replacewith(inner.children());
});
switch (type) {
case 'html' :
tmp.html( selectedopts.content );
fancybox_process_inline();
break;
case 'inline' :
$('').hide().insertbefore( $(obj) ).bind('fancybox-cleanup', function() {
$(this).replacewith(inner.children());
}).bind('fancybox-cancel', function() {
$(this).replacewith(tmp.children());
});
$(obj).appendto(tmp);
fancybox_process_inline();
break;
case 'image':
busy = false;
$.fancybox.showactivity();
imgpreloader = new image();
imgpreloader.onerror = function() {
fancybox_error();
};
imgpreloader.onload = function() {
imgpreloader.onerror = null;
imgpreloader.onload = null;
fancybox_process_image();
};
imgpreloader.src = href;
break;
case 'swf':
str = '';
tmp.html(str);
fancybox_process_inline();
break;
case 'ajax':
selector = href.split('#', 2);
data = selectedopts.ajax.data || {};
if (selector.length > 1) {
href = selector[0];
if (typeof data == "string") {
data += '&selector=' + selector[1];
} else {
data.selector = selector[1];
}
}
busy = false;
$.fancybox.showactivity();
ajaxloader = $.ajax($.extend(selectedopts.ajax, {
url : href,
data : data,
error : fancybox_error,
success : function(data, textstatus, xmlhttprequest) {
if (ajaxloader.status == 200) {
tmp.html( data );
fancybox_process_inline();
}
}
}));
break;
case 'iframe' :
$('').appendto(tmp);
fancybox_show();
break;
}
},
fancybox_animate_loading = function() {
if (!loading.is(':visible')){
clearinterval(loadingtimer);
return;
}
$('div', loading).css('top', (loadingframe * -40) + 'px');
loadingframe = (loadingframe + 1) % 12;
},
fancybox_init = function() {
if ($("#fancybox-wrap").length) {
return;
}
$('body').append(
tmp = $(''),
loading = $(''),
overlay = $(''),
wrap = $('')
);
if (!$.support.opacity) {
wrap.addclass('fancybox-ie');
loading.addclass('fancybox-ie');
}
outer = $('')
.append('')
.appendto( wrap );
outer.append(
inner = $(''),
close = $(''),
nav_left = $(''),
nav_right = $('')
);
close.click($.fancybox.close);
loading.click($.fancybox.cancel);
nav_left.click(function(e) {
e.preventdefault();
$.fancybox.prev();
});
nav_right.click(function(e) {
e.preventdefault();
$.fancybox.next();
});
if (isie6) {
overlay.get(0).style.setexpression('height', "document.body.scrollheight > document.body.offsetheight ? document.body.scrollheight : document.body.offsetheight + 'px'");
loading.get(0).style.setexpression('top', "(-20 + (document.documentelement.clientheight ? document.documentelement.clientheight/2 : document.body.clientheight/2 ) + ( ignoreme = document.documentelement.scrolltop ? document.documentelement.scrolltop : document.body.scrolltop )) + 'px'");
outer.prepend('');
}
};
/*
* public methods
*/
$.fn.fancybox = function(options) {
$(this)
.data('fancybox', $.extend({}, options, ($.metadata ? $(this).metadata() : {})))
.unbind('click.fb').bind('click.fb', function(e) {
e.preventdefault();
if (busy) {
return;
}
busy = true;
$(this).blur();
selectedarray = [];
selectedindex = 0;
var rel = $(this).attr('rel') || '';
if (!rel || rel == '' || rel === 'nofollow') {
selectedarray.push(this);
} else {
selectedarray = $("a[rel=" + rel + "], area[rel=" + rel + "]");
selectedindex = selectedarray.index( this );
}
fancybox_start();
return false;
});
return this;
};
$.fancybox = function(obj) {
if (busy) {
return;
}
busy = true;
var opts = typeof arguments[1] !== 'undefined' ? arguments[1] : {};
selectedarray = [];
selectedindex = opts.index || 0;
if ($.isarray(obj)) {
for (var i = 0, j = obj.length; i < j; i++) {
if (typeof obj[i] == 'object') {
$(obj[i]).data('fancybox', $.extend({}, opts, obj[i]));
} else {
obj[i] = $({}).data('fancybox', $.extend({content : obj[i]}, opts));
}
}
selectedarray = jquery.merge(selectedarray, obj);
} else {
if (typeof obj == 'object') {
$(obj).data('fancybox', $.extend({}, opts, obj));
} else {
obj = $({}).data('fancybox', $.extend({content : obj}, opts));
}
selectedarray.push(obj);
}
if (selectedindex > selectedarray.length || selectedindex < 0) {
selectedindex = 0;
}
fancybox_start();
};
$.fancybox.showactivity = function() {
clearinterval(loadingtimer);
loading.show();
loadingtimer = setinterval(fancybox_animate_loading, 66);
};
$.fancybox.hideactivity = function() {
loading.hide();
};
$.fancybox.next = function() {
return $.fancybox.pos( currentindex + 1);
};
$.fancybox.prev = function() {
return $.fancybox.pos( currentindex - 1);
};
$.fancybox.pos = function(pos) {
if (busy) {
return;
}
pos = parseint(pos, 10);
if (pos > -1 && currentarray.length > pos) {
selectedindex = pos;
fancybox_start();
}
if (currentopts.cyclic && currentarray.length > 1 && pos < 0) {
selectedindex = currentarray.length - 1;
fancybox_start();
}
if (currentopts.cyclic && currentarray.length > 1 && pos >= currentarray.length) {
selectedindex = 0;
fancybox_start();
}
return;
};
$.fancybox.cancel = function() {
if (busy) {
return;
}
busy = true;
$.event.trigger('fancybox-cancel');
fancybox_abort();
if (selectedopts && $.isfunction(selectedopts.oncancel)) {
selectedopts.oncancel(selectedarray, selectedindex, selectedopts);
}
busy = false;
};
// note: within an iframe use - parent.$.fancybox.close();
$.fancybox.close = function() {
if (busy || wrap.is(':hidden')) {
return;
}
busy = true;
if (currentopts && $.isfunction(currentopts.oncleanup)) {
if (currentopts.oncleanup(currentarray, currentindex, currentopts) === false) {
busy = false;
return;
}
}
fancybox_abort();
$(close.add( nav_left ).add( nav_right )).hide();
$('#fancybox-title').remove();
wrap.add(inner).add(overlay).unbind();
$(window).unbind("resize.fb scroll.fb");
$(document).unbind('keydown.fb');
function _cleanup() {
overlay.fadeout('fast');
wrap.hide();
$.event.trigger('fancybox-cleanup');
inner.empty();
if ($.isfunction(currentopts.onclosed)) {
currentopts.onclosed(currentarray, currentindex, currentopts);
}
currentarray = selectedopts = [];
currentindex = selectedindex = 0;
currentopts = selectedopts = {};
busy = false;
}
inner.css('overflow', 'hidden');
if (currentopts.transitionout == 'elastic') {
start_pos = fancybox_get_zoom_from();
var pos = wrap.position();
final_pos = {
top : pos.top ,
left : pos.left,
width : wrap.width(),
height : wrap.height()
};
if (currentopts.opacity) {
final_pos.opacity = 1;
}
fx.prop = 1;
$(fx).animate({ prop: 0 }, {
duration : currentopts.speedout,
easing : currentopts.easingout,
step : fancybox_draw,
complete : _cleanup
});
} else {
wrap.fadeout( currentopts.transitionout == 'none' ? 0 : currentopts.speedout, _cleanup);
}
};
$.fancybox.resize = function() {
var c, h;
if (busy || wrap.is(':hidden')) {
return;
}
busy = true;
c = inner.wrapinner("").children();
h = c.height();
wrap.css({height: h + (currentopts.padding * 2) + titleh});
inner.css({height: h});
c.replacewith(c.children());
$.fancybox.center();
};
$.fancybox.center = function() {
busy = true;
var view = fancybox_get_viewport(),
margin = currentopts.margin,
to = {};
to.top = view[3] + ((view[1] - ((wrap.height() - titleh) + (shadow * 2 ))) * 0.5);
to.left = view[2] + ((view[0] - (wrap.width() + (shadow * 2 ))) * 0.5);
to.top = math.max(view[3] + margin, to.top);
to.left = math.max(view[2] + margin, to.left);
wrap.css(to);
busy = false;
};
$.fn.fancybox.defaults = {
padding : 10,
margin : 20,
opacity : false,
modal : false,
cyclic : false,
scrolling : 'auto', // 'auto', 'yes' or 'no'
width : 560,
height : 340,
autoscale : true,
autodimensions : true,
centeronscroll : false,
ajax : {},
swf : { wmode: 'transparent' },
hideonoverlayclick : true,
hideoncontentclick : false,
overlayshow : true,
overlayopacity : 0.3,
overlaycolor : '#666',
titleshow : true,
titleposition : 'outside', // 'outside', 'inside' or 'over'
titleformat : null,
transitionin : 'fade', // 'elastic', 'fade' or 'none'
transitionout : 'fade', // 'elastic', 'fade' or 'none'
speedin : 300,
speedout : 300,
changespeed : 300,
changefade : 'fast',
easingin : 'swing',
easingout : 'swing',
showclosebutton : true,
shownavarrows : true,
enableescapebutton : true,
onstart : null,
oncancel : null,
oncomplete : null,
oncleanup : null,
onclosed : null
};
$(document).ready(function() {
fancybox_init();
});
})(jquery);