4 $(".jp-jplayer").each(function() {
6 var $root = $self.parent();
7 var $number = $('.number', $root);
9 swfPath: "/static/jplayer/",
10 solution: "html,flash",
11 supplied: $self.attr('data-supplied'),
12 cssSelectorAncestor: "#" + $self.attr("data-player"),
16 var setMedia = function(elem, time=0) {
17 var li = $(elem).parent();
20 media['mp3'] = li.attr('data-mp3');
21 media['oga'] = li.attr('data-ogg');
22 media['id'] = li.attr('data-media-id');
24 $(".title", $root).html(li.html());
25 player.jPlayer("setMedia", media);
26 player.jPlayer("pause", time);
30 $('.play-next', $root).click(function() {
31 var next = parseInt($number.text()) + 1;
32 var p = $('.jp-playlist .play:eq(' + (next - 1) + ')', $root);
34 setMedia(p).jPlayer("play");
38 $('.play-prev', $root).click(function() {
39 var next = parseInt($number.text()) - 1;
42 var p = $('.jp-playlist .play:eq(' + (next - 1) + ')', $root);
43 setMedia(p).jPlayer("play");
47 var initialElem = $('.jp-playlist .play', $root).first();
49 if (Modernizr.localstorage) {
51 audiobooks = JSON.parse(localStorage["audiobook-history"]);
55 last = audiobooks[$root.attr("data-book-slug")]
56 // Fallback for book id;
58 last = audiobooks[$root.attr("data-book-id")]
62 initialElem = $('[data-media-id="' + last[1] + '"] .play', $root).first();
63 initialTime = last[2];
64 $number.text($(".jp-playlist .play", $root).index(initialElem) + 1);
67 setMedia(initialElem, initialTime);
70 timeupdate: function(event) {
71 if (event.jPlayer.status.currentTime && Modernizr.localstorage) {
73 audiobooks = JSON.parse(localStorage["audiobook-history"]);
77 t = event.jPlayer.status.currentTime;
78 if (t && event.jPlayer.status.duration - t > 10) {
79 audiobooks[$root.attr("data-book-slug")] = [
81 event.jPlayer.status.media.id,
82 event.jPlayer.status.currentTime
85 delete audiobooks[$root.attr("data-book-slug")];
87 // Remove old book id, if present.
88 delete audiobooks[$root.attr("data-book-id")];
89 localStorage["audiobook-history"] = JSON.stringify(audiobooks);