This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.js
168 lines (138 loc) · 3.93 KB
/
application.js
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
window.oSounds = {};
var iItemCount
, iTimeoutID
;
function makeColor(p_iCounter, p_iLength){
var iCenter = 128
, iWidth = 127
, iLength = p_iLength
, p_aFrequency = [0.3, 0.3, 0.3]
, p_aPhase = [0, 2, 4]
;
function calculate(p_i, p_iIndex){
return Math.round(
Math.sin(p_aFrequency[p_iIndex]*p_i + p_aPhase[p_iIndex])
* iWidth + iCenter
);
}
function calculateRGB(p_iCounter){
return [
calculate(p_iCounter, 0)
, calculate(p_iCounter, 1)
, calculate(p_iCounter, 2)
];
}
return 'rgb(' + calculateRGB(p_iCounter).join(',') + ')';
};
function loadSound(p_sSound){
if(p_sSound !== ''){
var oSound = new Audio('sounds/' + p_sSound + '.mp3');
oSound.load();
window.oSounds[p_sSound] = oSound;
}
}
function playSound(p_sSound){
function play(){
window.oSounds[p_sSound].pause();
try {
window.oSounds[p_sSound].currentTime = 0;
} catch(oError){
// don't care
//console.log(oError);
}
window.oSounds[p_sSound].play();
}
if(p_sSound === ''){
// Nothing to do
} else if(typeof window.oSounds[p_sSound] === 'undefined'){
loadSound(p_sSound);
play();
} else {
play();
}
return window.oSounds[p_sSound];
}
function displayNext(p_iCurrentButtonId, p_iDelay){
window.setTimeout(function(){
var $NextButton = $('.button-' + (p_iCurrentButtonId+1));
hide($('.image-' + p_iCurrentButtonId));
if($NextButton.length > 0){
show($NextButton);
enable($NextButton);
} else {
// End of cycle
show($('.button-0'));
}
}, p_iDelay);
}
function disable(p_$Subject){
p_$Subject.attr('disabled','disabled');
}
function enable(p_$Subject){
p_$Subject.removeAttr('disabled');
}
function hide(p_$Subject){
p_$Subject.removeClass('visible');
p_$Subject.addClass('invisible');
}
function show(p_$Subject){
p_$Subject.removeClass('invisible');
p_$Subject.addClass('visible');
}
function loadItems(p_iIndex, p_oButton){
var $Button
, $Img
, sSound
;
$Button = $('<button></button>');
$Button.addClass('button-' + p_iIndex);
$Button.css('background-color', makeColor(p_iIndex, iItemCount));
$('body').append($Button);
// Hide everything but the first button
if(p_iIndex === 0){
show($Button);
} else {
hide($Button);
disable($Button);
}
$Img = $('<img class="invisible image-' + p_iIndex + '" src="images/' + p_oButton.image + '" />');
$Button.parent().prepend($Img);
sSound = p_oButton.sound;
loadSound(sSound);
$Button.on('click', function(){
var oSound;
function clickTimeOutHandler(p_iDuration){
show($Img);
disable($Button);
hide($Button);
playSound(sSound);
displayNext(parseInt(p_iIndex, 10), p_iDuration);
}
function setTimer(p_iDuration){
if(typeof iTimeoutID !== 'undefined'){
window.clearTimeout(iTimeoutID);
}
iTimeoutID = window.setTimeout(clickTimeOutHandler
, p_iDuration
, p_iDuration
);
}
oSound = playSound(sSound);
if(sSound === ''){
setTimer(650);
}else if(isNaN(oSound.duration)){
$(oSound).on('loadedmetadata', function() {
var iDuration = Math.ceil(oSound.duration * 1000);
setTimer(iDuration);
});
} else {
var iDuration = Math.ceil(oSound.duration * 1000);
setTimer(iDuration);
}
});
};
$.getJSON('list.json').done(function(p_oData){
iItemCount = p_oData.length;
$.each(p_oData, loadItems);
});
//EOF