|
1 /* |
|
2 * JavaScript file |
|
3 */ |
|
4 |
|
5 var SETUP_ID = 1; |
|
6 |
|
7 var timervalue = 0; |
|
8 var initialTimer = 0; |
|
9 var timeoutId = null; |
|
10 |
|
11 function init() |
|
12 { |
|
13 showMainWindow(); |
|
14 widget.setDisplayPortrait(); |
|
15 } |
|
16 |
|
17 function startTimer() { |
|
18 var hr = parseInt(document.getElementById("hrs").value); |
|
19 var min = parseInt(document.getElementById("min").value); |
|
20 var sec = parseInt(document.getElementById("sec").value); |
|
21 |
|
22 timervalue = hr * 3600 + min * 60 + sec; |
|
23 initialTimer = timervalue; |
|
24 showMainWindow(); |
|
25 startCountdown(); |
|
26 } |
|
27 |
|
28 function startCountdown() { |
|
29 if (timeoutId) { |
|
30 clearInterval(timeoutId); |
|
31 } |
|
32 timeoutId = window.setInterval(tick, 1000); |
|
33 tick(); |
|
34 } |
|
35 |
|
36 function tick() { |
|
37 if (timervalue > 0) { |
|
38 timervalue = timervalue - 1; |
|
39 var hrs = Math.floor(timervalue / 3600); |
|
40 var mins = Math.floor((timervalue % 3600) / 60); |
|
41 var sec = timervalue % 60; |
|
42 |
|
43 var sz = pad(hrs) + ":" + pad(mins) + ":" + pad(sec); |
|
44 document.getElementById("timervalue").innerHTML = sz; |
|
45 document.getElementById("sand").style.top=(50 * (initialTimer-timervalue)/initialTimer).toFixed(0) + "%"; |
|
46 document.getElementById("sand-bottom").style.top=(50+(50 * timervalue/initialTimer)).toFixed(0) + "%"; |
|
47 if (timervalue == 0) { |
|
48 cancelTimer(); |
|
49 } |
|
50 } |
|
51 } |
|
52 |
|
53 function pad(num) { |
|
54 if (num < 10) { |
|
55 return '0' + num; |
|
56 } else { |
|
57 return num; |
|
58 } |
|
59 } |
|
60 |
|
61 function cancelTimer() { |
|
62 clearInterval(timeoutId); |
|
63 timeoutId = 0; |
|
64 timervalue = 0; |
|
65 document.getElementById("timervalue").innerHTML = "00:00:00"; |
|
66 document.getElementById("sand").style.top="50%"; |
|
67 document.getElementById("sand-bottom").style.top="50%"; |
|
68 } |
|
69 |
|
70 function showMainWindow() { |
|
71 document.getElementById("main-window").style.display = "inherit"; |
|
72 document.getElementById("time-setup").style.display = "none"; |
|
73 var item = new MenuItem("Setup timer", SETUP_ID); |
|
74 item.onSelect = showTimerSetup; |
|
75 menu.append(item); |
|
76 menu.setRightSoftkeyLabel("Exit", null); |
|
77 } |
|
78 |
|
79 function showTimerSetup() { |
|
80 document.getElementById("main-window").style.display = "none"; |
|
81 document.getElementById("time-setup").style.display = "inherit"; |
|
82 var item = menu.getMenuItemById(SETUP_ID); |
|
83 menu.remove(item); |
|
84 menu.setRightSoftkeyLabel("Cancel", showMainWindow); |
|
85 } |
|
86 |
|
87 var paused = false; |
|
88 |
|
89 function pauseStart() { |
|
90 if (paused) { |
|
91 startCountdown(); |
|
92 document.getElementById("pausestart").style.backgroundPosition = "0px 0px"; |
|
93 paused = false; |
|
94 } else { |
|
95 if (timeoutId) { |
|
96 window.clearInterval(timeoutId); |
|
97 timeoutId = null; |
|
98 } |
|
99 document.getElementById("pausestart").style.backgroundPosition = "0px 32px"; |
|
100 paused = true; |
|
101 } |
|
102 } |