Major fixes for Song page
[songbook.git] / web / songbook.js
1 // Javascript functions for controling audio
2 starttime = null;
3 endtime   = null;
4
5 spacebar       = 32;
6 return2start   = 82;
7 backfewsecs    = 66;
8 forwardfewsecs = 70;
9 seta           = 65;
10 cleara         = 67;
11 howmanysecs    = 10;
12
13 window.onload = function() {
14   song = document.getElementById('song');
15
16   starttime = song.currentTime;
17   endtime   = song.duration;
18   body      = document.getElementsByTagName('body')[0]
19
20   body.onkeydown = 
21     function(e) {
22       var ev = e || event;
23       if (ev.keyCode == spacebar) {
24         if (song.paused) {
25           playing = false;
26         } else {
27           playing = true;
28         } // if
29
30         if (playing) {
31           song.pause();
32           playing = false;
33         } else {
34           if (starttime != 0) {
35             song.currentTime = starttime
36           } // if
37
38           song.play();
39           playing = true;
40         } // if
41
42         e.preventDefault();
43         return;
44       } else if (ev.keyCode == return2start) {
45         if (starttime != null) {
46           song.currentTime = starttime;
47         } else {
48           song.currentTime = 0;
49         } // if
50
51         return;
52       } else if (ev.keyCode == backfewsecs) {
53         song.currentTime -= howmanysecs;
54         song.play()
55
56         return;
57       } else if (ev.keyCode == forwardfewsecs) {
58         song.currentTime += howmanysecs;
59         song.play();
60
61         return;
62       } else if (ev.keyCode == seta) {
63         starttime = song.currentTime;
64
65         return;
66       } else if (ev.keyCode == cleara) {
67         starttime = 0;
68
69         return;
70       } // if
71     } // function
72   }  // getElementByTagName