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);
29 $('.play-next', $root).click(function() {
30 var next = parseInt($number.text()) + 1;
31 var p = $('.play:eq(' + next + ')', $root);
33 setMedia(p).jPlayer("play");
37 $('.play-prev', $root).click(function() {
38 var next = parseInt($number.text()) - 1;
41 var p = $('.play:eq(' + next + ')', $root);
42 setMedia(p).jPlayer("play");
46 var initialElem = $('.play', $root).first();
48 if (Modernizr.localstorage) {
50 audiobooks = JSON.parse(localStorage["audiobook-history"]);
54 last = audiobooks[$root.attr("data-book-slug")]
55 // Fallback for book id;
57 last = audiobooks[$root.attr("data-book-id")]
61 initialElem = $('[data-media-id="' + last[1] + '"] .play', $root).first();
62 initialTime = last[2];
65 setMedia(initialElem, initialTime);
68 timeupdate: function(event) {
69 if (event.jPlayer.status.currentTime && Modernizr.localstorage) {
71 audiobooks = JSON.parse(localStorage["audiobook-history"]);
75 t = event.jPlayer.status.currentTime;
76 if (t && event.jPlayer.status.duration - t > 10) {
77 audiobooks[$root.attr("data-book-slug")] = [
79 event.jPlayer.status.media.id,
80 event.jPlayer.status.currentTime
83 delete audiobooks[$root.attr("data-book-slug")];
85 // Remove old book id, if present.
86 delete audiobooks[$root.attr("data-book-id")];
87 localStorage["audiobook-history"] = JSON.stringify(audiobooks);