d93bbad1c545842e02bfce88592a13158f6ffce0
[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                     try {
50                         audiobooks = JSON.parse(localStorage["audiobook-history"]);
51                     } catch {
52                         audiobooks = {};
53                     }
54                     last = audiobooks[$root.attr("data-book-id")]
55                     if (last) {
56                         initialElem = $('[data-media-id="' + last[1] + '"] .play', $root).first();
57                         initialTime = last[2];
58                     }
59                 }
60                 setMedia(initialElem, initialTime);
61             },
62
63             timeupdate: function(event) {
64                 if (Modernizr.localstorage) {
65                     try {
66                         audiobooks = JSON.parse(localStorage["audiobook-history"]);
67                     } catch {
68                         audiobooks = {};
69                     }
70                     audiobooks[$root.attr("data-book-id")] = [Date.now(), $root.attr("data-media-id"), event.jPlayer.status.currentTime];
71                     localStorage["audiobook-history"] = JSON.stringify(audiobooks);
72                 }
73             }
74         });
75       });
76
77
78
79     });
80 })(jQuery)