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 = $('.play:eq(' + next + ')', $root);
34 setMedia(p).jPlayer("play");
38 $('.play-prev', $root).click(function() {
39 var next = parseInt($number.text()) - 1;
42 var p = $('.play:eq(' + next + ')', $root);
43 setMedia(p).jPlayer("play");
47 var initialElem = $('.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];
66 setMedia(initialElem, initialTime);
69 timeupdate: function(event) {
70 if (event.jPlayer.status.currentTime && Modernizr.localstorage) {
72 audiobooks = JSON.parse(localStorage["audiobook-history"]);
76 t = event.jPlayer.status.currentTime;
77 if (t && event.jPlayer.status.duration - t > 10) {
78 audiobooks[$root.attr("data-book-slug")] = [
80 event.jPlayer.status.media.id,
81 event.jPlayer.status.currentTime
84 delete audiobooks[$root.attr("data-book-slug")];
86 // Remove old book id, if present.
87 delete audiobooks[$root.attr("data-book-id")];
88 localStorage["audiobook-history"] = JSON.stringify(audiobooks);