Created
August 18, 2011 18:05
-
-
Save tylr/1154694 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 len = _slides.length; | |
var idx = index_of(_current) + offset; | |
idx = (idx < 0) ? len + idx : idx; | |
idx = (idx > (len - 1)) ? idx - len : idx; | |
return idx; | |
}; | |
function next() { | |
change_slide(offset_current_by(1)); | |
preload([1, 2]); | |
}; | |
function prev() { | |
change_slide(offset_current_by(-1)); | |
preload([-2, -1]); | |
}; | |
function play() { | |
// console.log('slideshow play'); | |
}; | |
function open(slide) { | |
var idx = index_of(slide); | |
$('#wrap').hide(); | |
_o.container.show(); | |
attach(); | |
change_slide(slide); | |
preload([-1, 1, 2]); | |
DV.Slideshow.enabled = true; | |
}; | |
function change_slide(slide) { | |
var img = new Image(); | |
img.onload = load_image; | |
_o.title.hide(); | |
_o.img.fadeOut(25, function() { | |
if (!is_loaded(slide)) { | |
_o.spinner.fadeIn(25); | |
} | |
img.src = slide.url; | |
}); | |
_current = slide; | |
} | |
function is_loaded(slide) { | |
return (_loaded.indexOf(slide.url) != -1); | |
}; | |
function load_image() { | |
DV.Slideshow.current_img = this; | |
DV.trackEvent(_current.url); | |
fit(); | |
_loaded.push(_current.url); | |
_o.img.attr('src', _current.url); | |
_o.title.text(_current.title.substr(0, _current.title.lastIndexOf('.'))); | |
_o.anchor.attr('href', _current.href); | |
_o.spinner.fadeOut(25, function() { | |
_o.img.fadeIn(25, function() { | |
_o.img.css('display', 'block'); | |
_o.title.show(); | |
}); | |
}); | |
}; | |
function preload(offsets) { | |
$.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