summaryrefslogtreecommitdiffhomepage
path: root/js/main.js
blob: 190e7ded5ce8f590e1eabcae5f5da6b82799b0b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * TODO:
 *
 * - rework use of global variables and functions into OO idioms
 * - initialise time wasting clock from storage in OO idiom rather
 *   than just stray function call
 * - bind a key to toggle the date
 */

$.ionSound({
    sounds: [
        {
            name: "klaxon"
        },
        {
            name: "button_tiny",
        },
        {
            name: "cheonjae",
        },
        {
            name: "onetwothree",
        },
        {
            name: "school_bell",
        }
    ],
    // volume: 0.5,
    path: "sounds/",
    preload: true
});

var timeWastingClock = new FlipClock($('.time-wasting-clock'), {
    autoStart:false,
    callbacks:{
        interval:function () {
            $.ionSound.play("button_tiny");
            var time = timeWastingClock.getTime().time;
            $.jStorage.set("time_wasted", time);
        }
    }
});

timeWastingClock.setTime($.jStorage.get("time_wasted", 0));

var $timeWastingClockRunning = false;

var activityClock = new FlipClock($('.activity-countdown'), {
    autoStart:false,
    countdown:true,
    callbacks:{
        stop:function () {
            $.ionSound.play("cheonjae");
        }
    }
});

function timeWastingClockToggle()
{
    if ($timeWastingClockRunning)
    {
        $('#timeWastingClockGo').html('Start <u>t</u>imer');
        timeWastingClock.stop();
        $timeWastingClockRunning = false;
    }
    else
    {
        $('#timeWastingClockGo').html('Stop <u>t</u>imer');
        timeWastingClock.start();
        $timeWastingClockRunning = true;
    }
}

$(document).bind('keydown', 't', timeWastingClockToggle);
$(document).bind('keydown', 'j', timeWastingClockToggle);
$(document).bind('keydown', 'space', timeWastingClockToggle);

$(document).bind('keydown', 'k', function (){$.ionSound.play("klaxon");});
$(document).bind('keydown', 'o', function (){$.ionSound.play("onetwothree");});
$(document).bind('keydown', 'b', function (){$.ionSound.play("school_bell");});

function activityClockGo($seconds)
{
    activityClock.setTime($seconds);
    activityClock.start();
}

function timeWastingClockReset()
{
  if (timeWastingClock.getTime() != 0)
  {
    var $timeWastingClockResetConfirm = confirm("Are you sure?");
    if ($timeWastingClockResetConfirm)
    {
      if ($timeWastingClockRunning)
      {
        $('#timeWastingClockGo').html('Start <u>t</u>imer');
        timeWastingClock.stop();
        $timeWastingClockRunning = false;
      }
      $.jStorage.set("time_wasted", 0);
      timeWastingClock.setTime(0);
    }
  }
}

function activityClockReset()
{
    activityClock.stop();
    activityClock.setTime(0);
}

function activityClockCustom()
{
    var minutes = prompt("Number of minutes", "0");
    var seconds = prompt("Number of seconds", "0");
    activityClockGo(parseInt(minutes) * 60 + parseInt(seconds));
}

// mplungjan on stack overflow: http://stackoverflow.com/a/15397495
function nth(d) {
  if(d>3 && d<21) return 'th'; // thanks kennebec
  switch (d % 10) {
        case 1:  return "st";
        case 2:  return "nd";
        case 3:  return "rd";
        default: return "th";
    }
}

function toggleDateStyle()
{
    var currentDate = $('#date').html();
    var today = new Date();

    var month = "January,February,March,April,May,June,July,August,September,October,November,December"
  .split(",")[today.getMonth()];
    var day = "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday"
  .split(",")[today.getDay()];
    var date = today.getDate();
    var British = day + " " + date + nth(date) + " " + month + " " + today.getFullYear();
    var American = day + " " + month + " " + date + nth(date) + ", " + today.getFullYear();

    if ($.jStorage.get("date_style", 0) == 0)
    {
        $('#date').html(American);
        $.jStorage.set("date_style", 1);
    }
    else
    {
        $('#date').html(British);
        $.jStorage.set("date_style", 0);
    }
}

// set initial date to British style
if ($.jStorage.get("date_style", 0) == 0)
    $.jStorage.set("date_style", 1);
else
    $.jStorage.set("date_style", 0);
toggleDateStyle();

// expire chosen date style each day
$.jStorage.setTTL("date_style", 86400);

$(document).bind('keydown', 's', timeWastingClockReset);
$(document).bind('keydown', 'r', activityClockReset);
$(document).bind('keydown', 'c', activityClockCustom);

$(document).bind('keydown', '0', function (){activityClockGo(30);});
$(document).bind('keydown', '1', function (){activityClockGo(60);});
$(document).bind('keydown', '9', function (){activityClockGo(90);});
$(document).bind('keydown', '2', function (){activityClockGo(120);});
$(document).bind('keydown', '3', function (){activityClockGo(180);});
$(document).bind('keydown', '4', function (){activityClockGo(240);});
$(document).bind('keydown', '5', function (){activityClockGo(300);});
$(document).bind('keydown', '6', function (){activityClockGo(360);});
$(document).bind('keydown', '7', function (){activityClockGo(420);});
$(document).bind('keydown', '8', function (){activityClockGo(480);});

$(document).ready(function(){
    $('#klaxon').button();
    $('#klaxon').click(function (){$.ionSound.play("klaxon");});

    $('#bell').button();
    $('#bell').click(function (){$.ionSound.play("school_bell");});

    $('#one-two-three').button();
    $('#one-two-three').click(function (){$.ionSound.play("onetwothree");});

    $('#timeWastingClockGo').button();
    $('#timeWastingClockGo').click(timeWastingClockToggle);

    $('#timeWastingClockReset').button();
    $('#timeWastingClockReset').click(timeWastingClockReset);

    $('#activityClockReset').button();
    $('#activityClockReset').click(activityClockReset);

    $('#activityClockCustom').button();
    $('#activityClockCustom').click(activityClockCustom);

    $('#activityClock30s').button();
    $('#activityClock30s').click(function (){activityClockGo(30);})

    $('#activityClock60s').button();
    $('#activityClock60s').click(function (){activityClockGo(60);})

    $('#activityClock90s').button();
    $('#activityClock90s').click(function (){activityClockGo(90);})

    $('#activityClock120s').button();
    $('#activityClock120s').click(function (){activityClockGo(120);})

    $('#activityClock180s').button();
    $('#activityClock180s').click(function (){activityClockGo(180);})
    
    $('#activityClock240s').button();
    $('#activityClock240s').click(function (){activityClockGo(240);})

    $('#activityClock300s').button();
    $('#activityClock300s').click(function (){activityClockGo(300);})

    $('#date-toggle').button();
    $('#date-toggle').click(function (){toggleDateStyle();});
});