fix
[wolnelektury.git] / src / catalogue / static / player / player.js
old mode 100755 (executable)
new mode 100644 (file)
index a41d2c0..d804463
@@ -13,7 +13,7 @@
 
             ready: function() {
                 var player = $(this);
-                var setMedia = function(elem) {
+                var setMedia = function(elem, time=0) {
                     var li = $(elem).parent();
                     var media = {}
 
@@ -21,7 +21,9 @@
                     media['oga'] = li.attr('data-ogg');
 
                     $(".title", $root).html(li.html());
-                    return player.jPlayer("setMedia", media);
+                    $root.attr('data-media-id', li.attr('data-media-id'));
+                    player.jPlayer("setMedia", media);
+                    player.jPlayer("pause", time);
                 };
 
                 $('.play-next', $root).click(function() {
                     $number.text(next)
                 });
 
-                setMedia($('.play', $root).first());
+                var initialElem = $('.play', $root).first();
+                var initialTime = 0;
+                if (Modernizr.localstorage) {
+                    audiobooks = JSON.parse(localStorage["audiobook-history"]);
+                    last = audiobooks[$root.attr("data-book-id")]
+                    if (last) {
+                        initialElem = $('[data-media-id="' + last[1] + '"] .play', $root).first();
+                        initialTime = last[2];
+                    }
+                }
+                setMedia(initialElem, initialTime);
+            },
 
+            timeupdate: function(event) {
+                if (Modernizr.localstorage) {
+                    try {
+                        audiobooks = JSON.parse(localStorage["audiobook-history"]);
+                    } catch {
+                        audiobooks = {};
+                    }
+                    audiobooks[$root.attr("data-book-id")] = [Date.now(), $root.attr("data-media-id"), event.jPlayer.status.currentTime];
+                    localStorage["audiobook-history"] = JSON.stringify(audiobooks);
+                }
             }
         });
       });