Skip to content

Instantly share code, notes, and snippets.

@tylr
Created August 10, 2011 02:20
Show Gist options
  • Save tylr/1135922 to your computer and use it in GitHub Desktop.
Save tylr/1135922 to your computer and use it in GitHub Desktop.
DV.Slideshow = (function() {
var _o = {};
var _enabled = null;
var _current = null;
var _current_img = null;
var _slides = [];
var _loaded = [];
function init(opts) {
_o = opts;
$(_o.slides).live('click', function(e) {
e.stopPropagation();
DV.Slideshow.open($.parseJSON($(this).attr('name')));
return false;
});
$(document).keyup(function(e) {
if (DV.Slideshow.enabled) {
if (e.keyCode == 27) DV.Slideshow.close();
if (e.keyCode == 39) DV.Slideshow.next();
if (e.keyCode == 37) DV.Slideshow.prev();
}
});
$(window).bind('resize', function() {
DV.Slideshow.fit(_current_img);
});
$.each($(_o.slides), function(idx, elmnt) {
_slides.push($.parseJSON($(elmnt).attr('name')));
});
};
function offset_current_by(offset) {
return _slides[offset_index(offset)];
};
function offset_index(offset) {
var last = _slides.length - 1;
var idx = index_of(_current) + offset;
idx = (idx >= 0) ? idx : last + idx;
idx = (idx <= last) ? idx : idx - last;
return idx;
};
function next() {
change_slide(offset_current_by(1));
};
function prev() {
change_slide(offset_current_by(-1));
};
function play() {
// console.log('slideshow play');
};
function open(slide) {
var idx = index_of(slide);
$('#wrap').hide();
$(_o.container).show();
attach();
change_slide(slide);
DV.Slideshow.enabled = true;
};
function change_slide(slide) {
var img_box = $(_o.img);
var spinner_box = $(_o.spinner);
var prev_slide = _current;
var img = new Image();
var title_box = $(_o.title);
_current = slide;
preload();
img.onload = load_image;
title_box.hide();
img_box.fadeOut(25, function() {
if (!is_loaded(_current)) {
spinner_box.fadeIn(25);
}
img.src = _current.url;
});
}
function is_loaded(slide) {
return (_loaded.indexOf(slide.url) != -1);
};
function load_image() {
var img_box = $(_o.img);
var spinner_box = $(_o.spinner);
var title_box = $(_o.title);
var anchor = $(_o.anchor);
DV.Slideshow.current_img = this;
DV.trackEvent(_current.url);
fit();
_loaded.push(_current.url);
img_box.attr('src', _current.url);
title_box.text(_current.title.substr(0, _current.title.lastIndexOf('.')));
anchor.attr('href', _current.href);
spinner_box.fadeOut(25, function() {
img_box.fadeIn(25, function() {
title_box.show();
});
});
};
function preload() {
var offsets = [-2, -1, 1, 2];
$.each(offsets, function(i,v) {
slide = offset_current_by(v);
if (! is_loaded(slide)) {
var img = new Image();
img.onload = function() { _loaded.push(slide.url); };
img.src = slide.url;
}
});
};
function index_of(slide) {
var index = -1;
$.each(_slides, function(idx, s) {
if(s.url == slide.url) {
index = idx;
return false;
}
});
return index;
};
function fit() {
var h = DV.Slideshow.current_img.height;
var w = DV.Slideshow.current_img.width;
var s = ($(window).height() - 150);
$(_o.img).height((h > s) ? s : h + 'px');
};
function close() {
$('#wrap').show();
$(_o.img).hide();
$(_o.spinner).show();
$(_o.container).hide();
detach();
_current = null;
DV.Slideshow.enabled = false;
};
function attach() {
$(_o.close).live('click', function(e) {
e.stopPropagation();
DV.Slideshow.close();
return false;
});
$(_o.next).live('click', function(e) {
e.stopPropagation();
DV.Slideshow.next();
return false;
});
$(_o.prev).live('click', function(e) {
e.stopPropagation();
DV.Slideshow.prev();
return false;
});
$(_o.play).live('click', function(e) {
e.stopPropagation();
DV.Slideshow.play();
return false;
});
};
function detach() {
$(_o.close).die('click');
$(_o.next).die('click');
$(_o.prev).die('click');
$(_o.play).die('click');
$(document).die('keyup');
};
return {
init: init,
close: close,
next: next,
prev: prev,
play: play,
open: open,
fit: fit,
enabled: _enabled,
current_img: _current_img
};
}());
$(document).ready(function() {
DV.Slideshow.init({
slides: '.photo',
close: '.slideshow .close, .slideshow .logo',
next: '.slideshow .controls .next',
prev: '.slideshow .controls .prev',
play: '.slideshow .controls .play',
img: '.slideshow .slide_box .slide',
title: '.slideshow .slide_box .filename',
container: '.slideshow',
spinner: '.slideshow .slide_box .spinner',
anchor: '.slideshow .slide_box a'
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment