(function($) { /* * Some easing functions borrowed from the effects library to save loading the whole lot. * Glide is really quartic out. Boing is back out. Bounce is bounce out. */ $.easing.glide = function (x, t, b, c, d) { return -c * ((t=t/d-1)*t*t*t - 1) + b; } $.easing.boing = function (x, t, b, c, d, s) { if (s == undefined) s = 1.70158; return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b; }; function Gallery() { var self = this; var container = $('
').hide().appendTo($('body')); $.extend(self, { container: container, zoomer: new Zoomer(), image: container.find('img.preview'), caption: container.find('p.caption'), controls: container.find('div.controls'), closer: container.find('div.closer'), stack: [], item: null, click_outside: null, escape_key: null, add: function (a) { self.stack.push(new GalleryItem(a)); }, display: function (item) { self.item = item; if (self.visible()) self.crossfade(); else self.zoomUp(); }, mimic: function () { self.zoomer.setImage(self.item.src); self.image.attr('src', self.item.src); self.controls.find('a.download').attr('href', self.item.download_url()); if (self.item.caption.html()) self.caption.html(self.item.caption.html()).show(); else self.caption.hide(); }, zoomUp: function () { self.hide(); self.mimic(); self.zoomer.zoomUp(self.item, self.show); self.setClosers(); }, zoomDown: function () { self.zoomer.zoomDown(self.item); self.unsetClosers(); self.hide(); }, crossfade: function () { self.fadeDown(self.fadeUp); }, fadeDown: function (onFade) { self.caption.fadeTo('fast', 0.2); self.image.fadeTo('fast', 0.2, onFade); }, fadeUp: function (onFade) { self.mimic(); self.resize(); self.caption.fadeTo('fast', 1); self.image.fadeTo('fast', 1, onFade); }, current: function () { return self.stack.indexOf(self.item); }, next: function (e) { if (e) e.preventDefault(); var at = self.current(); var next = (at == self.stack.length-1) ? 0 : at + 1; self.display(self.stack[next]); }, previous: function (e) { if (e) e.preventDefault(); var at = self.current(); var previous = (at == 0) ? self.stack.length-1 : at - 1; self.display(self.stack[previous]); }, close: function (e) { if (e) e.preventDefault(); if (self.visible) self.zoomDown(); }, setClosers: function (argument) { self.escape_key = $(document).keyup(function(e) { if (e.keyCode == 27) self.close(e); if (!e.metaKey) { if (e.keyCode == 39) self.next(e); if (e.keyCode == 37) self.previous(e); } return false; }); }, unsetClosers: function (argument) { if (self.escape_key) $(document).unbind('keyup', self.escape_key); }, show: function () { self.zoomer.hide(); self.container.show(); }, hide: function () { self.container.hide(); }, visible: function () { return self.container.is(':visible'); }, showControls: function (e) { self.controls.fadeIn("fast"); self.closer.fadeIn("fast"); }, hideControls: function (e) { self.controls.fadeOut("fast"); self.closer.fadeOut("fast"); }, resize: function (item) { if (!item) item = self.item; var w = $(window); var d = item.imageSize(); var p = self.container.offset(); var r = { left: p.left + (self.image.innerWidth() - d.width)/2, top: p.top + (self.image.innerHeight() - d.height)/2 }; if (r.top <= 10) r.top = 10; self.image.animate(d, 'fast'); self.container.animate(r, 'fast'); self.controls.css({left: (d.width - 96)/2}); }, reposition: function (item) { if (!item) item = self.item; var w = $(window); var d = item.imageSize(); var p = { top: w.scrollTop() + (w.height() - d.height)/2, left: w.scrollLeft() + (w.width() - d.width)/2 }; if (p.top <= 10) p.top = 10; if (self.visible) { self.image.animate(d, 'fast'); self.container.animate(p, 'fast'); } else { self.image.css(d); self.container.css(p); } self.controls.css({left: (d.width - 96)/2}); return $.extend(d,p); }, currentPosition: function () { var p = self.container.offset(); return { left: p.left, top: p.top, width: self.image.innerWidth(), height: self.image.innerHeight() }; } }); self.closer.find('a').click(self.close); self.controls.find('a.previous').click(self.previous); self.controls.find('a.next').click(self.next); self.container.hover(self.showControls, self.hideControls); }; function Zoomer() { var self = this; var sprite = $('