new layout fixes
[wolnelektury.git] / src / catalogue / static / player / player.js
1 (function($) {
2     $(function() {
3
4       $(".jp-jplayer").each(function() {
5         var $self = $(this);
6         var $root = $self.parent();
7         var $number = $('.number', $root);
8         $self.jPlayer({
9             swfPath: "/static/jplayer/",
10             solution: "html,flash",
11             supplied: $self.attr('data-supplied'),
12             cssSelectorAncestor: "#" + $self.attr("data-player"),
13
14             ready: function() {
15                 var player = $(this);
16                 var setMedia = function(elem, time=0) {
17                     var li = $(elem).parent();
18                     var media = {}
19
20                     media['mp3'] = li.attr('data-mp3');
21                     media['oga'] = li.attr('data-ogg');
22                     media['id'] = li.attr('data-media-id');
23
24                     $(".title", $root).html(li.html());
25                     player.jPlayer("setMedia", media);
26                     player.jPlayer("pause", time);
27                     return player;
28                 };
29
30                 $('.play-next', $root).click(function() {
31                     var next = parseInt($number.text()) + 1;
32                     var p = $('.jp-playlist .play:eq(' + (next - 1) + ')', $root);
33                     if (p.length) {
34                         setMedia(p).jPlayer("play");
35                         $number.text(next)
36                     }
37                 });
38                 $('.play-prev', $root).click(function() {
39                     var next = parseInt($number.text()) - 1;
40                     if (next < 1)
41                         return;
42                     var p = $('.jp-playlist .play:eq(' + (next - 1) + ')', $root);
43                     setMedia(p).jPlayer("play");
44                     $number.text(next)
45                 });
46
47                 var initialElem = $('.jp-playlist .play', $root).first();
48                 var initialTime = 0;
49                 if (Modernizr.localstorage) {
50                     try {
51                         audiobooks = JSON.parse(localStorage["audiobook-history"]);
52                     } catch {
53                         audiobooks = {};
54                     }
55                     last = audiobooks[$root.attr("data-book-slug")]
56                     // Fallback for book id;
57                     if (!last) {
58                         last = audiobooks[$root.attr("data-book-id")]
59                     }
60
61                     if (last) {
62                         initialElem = $('[data-media-id="' + last[1] + '"] .play', $root).first();
63                         initialTime = last[2];
64                         $number.text($(".jp-playlist .play", $root).index(initialElem) + 1);
65                     }
66                 }
67                 setMedia(initialElem, initialTime);
68             },
69
70             timeupdate: function(event) {
71                 if (event.jPlayer.status.currentTime && Modernizr.localstorage) {
72                     try {
73                         audiobooks = JSON.parse(localStorage["audiobook-history"]);
74                     } catch {
75                         audiobooks = {};
76                     }
77                     t = event.jPlayer.status.currentTime;
78                     if (t && event.jPlayer.status.duration - t > 10) {
79                         audiobooks[$root.attr("data-book-slug")] = [
80                             Date.now(),
81                             event.jPlayer.status.media.id,
82                             event.jPlayer.status.currentTime
83                         ];
84                     } else {
85                         delete audiobooks[$root.attr("data-book-slug")];
86                     }
87                     // Remove old book id, if present.
88                     delete audiobooks[$root.attr("data-book-id")];
89                     localStorage["audiobook-history"] = JSON.stringify(audiobooks);
90                 }
91             }
92         });
93       });
94
95
96
97     });
98 })(jQuery)