d80446318ebd54335c804312bd98689a11e021e5
[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
23                     $(".title", $root).html(li.html());
24                     $root.attr('data-media-id', li.attr('data-media-id'));
25                     player.jPlayer("setMedia", media);
26                     player.jPlayer("pause", time);
27                 };
28
29                 $('.play-next', $root).click(function() {
30                     var next = parseInt($number.text()) + 1;
31                     var p = $('.play:eq(' + next + ')', $root);
32                     if (p.length) {
33                         setMedia(p).jPlayer("play");
34                         $number.text(next)
35                     }
36                 });
37                 $('.play-prev', $root).click(function() {
38                     var next = parseInt($number.text()) - 1;
39                     if (next < 1)
40                         return;
41                     var p = $('.play:eq(' + next + ')', $root);
42                     setMedia(p).jPlayer("play");
43                     $number.text(next)
44                 });
45
46                 var initialElem = $('.play', $root).first();
47                 var initialTime = 0;
48                 if (Modernizr.localstorage) {
49                     audiobooks = JSON.parse(localStorage["audiobook-history"]);
50                     last = audiobooks[$root.attr("data-book-id")]
51                     if (last) {
52                         initialElem = $('[data-media-id="' + last[1] + '"] .play', $root).first();
53                         initialTime = last[2];
54                     }
55                 }
56                 setMedia(initialElem, initialTime);
57             },
58
59             timeupdate: function(event) {
60                 if (Modernizr.localstorage) {
61                     try {
62                         audiobooks = JSON.parse(localStorage["audiobook-history"]);
63                     } catch {
64                         audiobooks = {};
65                     }
66                     audiobooks[$root.attr("data-book-id")] = [Date.now(), $root.attr("data-media-id"), event.jPlayer.status.currentTime];
67                     localStorage["audiobook-history"] = JSON.stringify(audiobooks);
68                 }
69             }
70         });
71       });
72
73
74
75     });
76 })(jQuery)