aboutsummaryrefslogtreecommitdiffhomepage
path: root/assets/js/main.js
blob: f0c19410476390358b19f6582b9fad2be3c7fa57 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// compile regular expressions

var numRegExp = new RegExp("^[0-9]*$");

// sound setup

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

// Play a sound three minutes before the end of each lesson.

var loaded = new Date();
var loadedYear = loaded.getYear();
var loadedMonth = loaded.getMonth();
var loadedDay = loaded.getDay();
var lessonEndWarningTimes = [[9,  37],
                             [10, 27],
                             [11, 17],
                             [12,  7],
                             [13, 37],
                             [14, 27]];

function watchEndOfLesson ()
{
    var now = new Date();
    var hours = now.getHours()
    var minutes = now.getMinutes()
    for (var i = 0; i < lessonEndWarningTimes.length; i++)
    {
        if (lessonEndWarningTimes[i][0] == hours && lessonEndWarningTimes[i][1] == minutes)
        {
            $.ionSound.play("bell_ring");
            break;
        }
    }
}

// At the next top of the minute, start checking for three minutes
// before the end of the lesson.

var startWatching = 1000 - loaded.getMilliseconds() + 1000 * (60 - loaded.getSeconds())
window.setTimeout(function () { watchEndOfLesson(); window.setInterval(watchEndOfLesson, 60000); }, startWatching);

// random integers

// courtesy of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min)) + min;
}

// famous functions from <http://www.quirksmode.org/js/cookies.html>

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    var path = $(location).attr('pathname');
    var dir = path.substring(0, path.lastIndexOf("/"));
    document.cookie = name+"="+value+expires+"; path="+dir;
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

// start a new class

function startLesson()
{
    var oldCookie = readCookie("class_cookie");
    // bail out if we've already started a class (the cookie will
    // always be set cos our CGI monad always sets it)
    if (oldCookie != "Nothing")
        return false;

    // get input
    var theGrade = prompt("What grade?", "");
    var theClass = prompt("What class?", "");
    cookieString = theGrade + "-" + theClass

    // validate
    var valRegExp = new RegExp("^[0-9]-[0-9]$");
    if (valRegExp.test(cookieString) == false)
    {
        alert ("invalid class!");
        return false;
    }

    // set the cookie and reload to start the session
    createCookie("class_cookie", cookieString, 1);
    // reset clock because it's currently dangerous to do so when
    // ending a lesson
    timeWastingClock.reset();
    // reload, preventing Firefox from resubmitting POST data
    window.location.assign(document.URL.split("#")[0]);
}

// end a class

function endLesson()
{
    // elements
    var $points = $('#class_points');
    var $form = $('#end_of_class_form');
    var $password = $('#teachers_password');
    var $time = $('#class_time_wasted');

    var oldCookie = readCookie("class_cookie");
    // bail out if we've already started a class (the cookie will
    // always be set cos our CGI monad always sets it)
    if (oldCookie == "Nothing")
        return false;

    // validate
    var numRegExp = new RegExp("^[0-9]*$");
    if (numRegExp.test($points.val()) == false)
    {
        alert ("invalid points!");
        return false;
    }
    if ($password.val() == "")
    {
        alert ("invalid password!");
        return false;
    }

    // submit
    timeWastingClock.stop();
    // dangerous to reset clock here in case user inputs wrong
    // password: instead reset when *starting* a class
    // timeWastingClock.reset();
    $form.submit();
}

// toggle date style

// 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 + "<sup>" + nth(date) + "</sup> " + month + " " + today.getFullYear();
    var American = day + " " + month + " " + date + "<sup>" + nth(date) + "</sup>, " + 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();


// choose a student

function luckyNumber()
{
    var students = parseInt(readCookie("ss_cookie"));
    alert(getRandomInt(1, students));
}

// toggle the count-down and count-up clocks

function leftClockToggle()
{
    // determine what the current clock is by seeing what div exists
    if ($("#activity-countup").length)
        createCookie("clock_cookie", "0", 1);
    else
        createCookie("clock_cookie", "1", 1);

    // reload, preventing Firefox from resubmitting POST data
    window.location.assign(document.URL.split("#")[0]);
}

// function to make a FlipClock with a few additional features.  Not
// as neat a constructor as I would like because I don't fully
// understand how jQuery works
function MyFlipClock (jq, obj)
{
    var thisClock = new FlipClock(jq, obj);

    thisClock.go = $.proxy(function (seconds) {
        this.setTime(seconds);
        this.start();
    }, thisClock);
    thisClock.reset = $.proxy(function () {
        thisClock.stop();
        thisClock.setTime(0);
    }, thisClock);
    thisClock.custom = $.proxy(function () {
        var minutes = prompt('Number of minutes', '0');
        var seconds = prompt('Number of seconds', '0');

        // validate
        if (numRegExp.test(minutes) == false || numRegExp.test(seconds) == false)
            alert ("invalid input");
        else
            thisClock.go(parseInt(minutes) * 60 + parseInt(seconds));
    }, thisClock);

    return thisClock;
}

var timeWastingClock = MyFlipClock($('#time-wasting-clock'), {
    autoStart:false,
    callbacks:{
        interval:function () {
            $.ionSound.play("button_tiny");
            var time = timeWastingClock.getTime().time;
            $.jStorage.set("time_wasted", time);
            $("#class_time_wasted").val(time);
        }
    }
});
timeWastingClock.setTime($.jStorage.get("time_wasted", 0));

timeWastingClock.running = false;
timeWastingClock.reset = $.proxy(function () {
    if (this.getTime() != 1)
    {
        // if (confirm('Are you sure?'))
        // {
            if (this.running)
            {
                $('#timeWastingClockGo').html('Start <u>t</u>imer');
                this.stop();
                this.running = false;
            }
        $.jStorage.set('time_wasted', 0);
        $("#class_time_wasted").val(0);
        this.setTime(0);
        // }
    }
}, timeWastingClock);
timeWastingClock.toggle = $.proxy(function () {
    if (this.running)
    {
        $('#timeWastingClockGo').html('Start <u>t</u>imer');
        this.stop();
        this.running = false;
    }
    else
    {
        $('#timeWastingClockGo').html('Stop <u>t</u>imer');
        this.start();
        this.running = true;
    }
}, timeWastingClock);

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

var activityClockUp = MyFlipClock($('#activity-countup'), {
    autoStart:false,
    countdown:false
});
activityClockUp.toggle = $.proxy(function () {
    if (this.running)
    {
        $('#activityClockUpGo').html('Start stopwatch (<u>a</u>)');
        this.stop();
        this.running = false;
    }
    else
    {
        $('#activityClockUpGo').html('Stop stopwatch (<u>a</u>)');
        this.start();
        this.running = true;
    }
}, activityClockUp);
// activityClockUp.reset = $.proxy(function () {
//     if (this.getTime() != 1)
//     {
//         if (confirm('Are you sure?'))
//         {
//             if (this.running)
//             {
//                 $('#activityClockUpGo').html('Start <u>t</u>imer');
//                 this.stop();
//                 this.running = false;
//             }
//             this.setTime(0);
//         }
//     }
// }, activityClockUp);

// bind to keys

// only bind if the div exists (that is, a class is in session)
if ($("#time-wasting-clock").length)
{
    $(document).bind('keydown', 't', timeWastingClock.toggle);
    $(document).bind('keydown', 'j', timeWastingClock.toggle);
    // $(document).bind('keydown', 'space', timeWastingClock.toggle);
    $(document).bind('keydown', 'l', luckyNumber);
}
else
    $(document).bind('keydown', 'l', startLesson);

$(document).bind('keydown', 's', timeWastingClock.reset);
$(document).bind('keydown', 'r', activityClock.reset);
$(document).bind('keydown', 'c', activityClock.custom);

$(document).bind('keydown', 'z', activityClockUp.reset);
$(document).bind('keydown', 'a', activityClockUp.toggle);

$(document).bind('keydown', 'd', toggleDateStyle);

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

$(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");});

// bind to buttons

$(document).ready(function(){

    $('#why-so-noisy').button();
    $('#why-so-noisy').click(function (){$.ionSound.play("why_so_noisy");});

    $('#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");});

    $('#too-noisy').button();
    $('#too-noisy').click(function (){$.ionSound.play("too_noisy");});

    $('#sit-down-quickly').button();
    $('#sit-down-quickly').click(function (){$.ionSound.play("sit_down_quickly");});

    $('#start-lesson').button();
    $('#start-lesson').click(function (){ startLesson(); });

    $('#end-lesson').button();
    $('#end-lesson').click(function (){ endLesson(); });

    $('#lucky-number').button();
    $('#lucky-number').click(function (){ luckyNumber(); });

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

    $('#leftClockToggle').button();
    $('#leftClockToggle').click(function (){ leftClockToggle(); });

    $('#activityClockUpGo').button();
    $('#activityClockUpGo').click(activityClockUp.toggle);

    $('#activityClockUpReset').button();
    $('#activityClockUpReset').click(activityClockUp.reset);

    $('#timeWastingClockGo').button();
    $('#timeWastingClockGo').click(timeWastingClock.toggle);

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

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

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

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

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

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

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

    $('#activityClock180s').button();
    $('#activityClock180s').click(function (){activityClock.go(180);})

    $('#activityClock240s').button();
    $('#activityClock240s').click(function (){activityClock.go(240);})

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

});