-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathscript.js
More file actions
1433 lines (1219 loc) · 42.4 KB
/
script.js
File metadata and controls
1433 lines (1219 loc) · 42.4 KB
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// script.js - Enhanced Stopwatch with Modern Features
// Author: Hector JS
// ============================================
// STATE VARIABLES
// ============================================
var hr = 0,
min = 0,
sec = 0,
count = 0;
var prev_hr = 0,
prev_min = 0,
prev_sec = 0,
prev_count = 0;
var diff_hr = 0,
diff_min = 0,
diff_sec = 0,
diff_count = 0;
var timer = false;
var lapCounter = 1;
let timerInterval = null;
// --- ADDED FOR PiP ---
let pipWindow = null;
let pipRequestInProgress = false;
// --- END PiP ---
// ============================================
// SOUND EFFECTS
// ============================================
const tickSound = new Audio("audio/ticking.mp3");
tickSound.loop = true;
const beepSound = new Audio("audio/beep_cut.mp3");
const startSound = new Audio("audio/sound_trim.mp3");
let tickToggle = null;
let isTickEnabled = false;
// ============================================
// INITIALIZATION & LOCAL STORAGE
// ============================================
function preloadAudio() {
const files = ['audio/beep_cut.mp3', 'audio/sound_trim.mp3', 'audio/ticking.mp3'];
files.forEach(src => {
const audio = new Audio(src);
audio.preload = 'auto';
audio.load();
});
}
document.addEventListener("DOMContentLoaded", function () {
preloadAudio();
loadStopwatchState();
// Load dark mode preference
loadDarkModePreference();
// Initialize tick toggle
tickToggle = document.getElementById("tickToggle");
if (tickToggle) {
isTickEnabled = tickToggle.checked;
// Listen for checkbox change
tickToggle.addEventListener("change", (e) => {
isTickEnabled = e.target.checked;
// Handle real-time toggle during stopwatch running
if (timer) {
if (isTickEnabled) {
tickSound.play().catch(() => { });
} else {
tickSound.pause();
tickSound.currentTime = 0;
}
}
});
}
// Setup dark mode toggle
setupDarkModeToggle();
// Initialize voice control (ENHANCED)
initializeVoiceControl();
// Load voice preference
const savedVoiceEnabled = localStorage.getItem('voiceEnabled');
if (savedVoiceEnabled === 'true') {
// Delay to ensure proper initialization
setTimeout(() => {
if (voiceRecognition) {
toggleVoiceControl();
}
}, 1000);
}
});
// Save stopwatch state to localStorage
function saveStopwatchState() {
const state = {
hr: hr,
min: min,
sec: sec,
count: count,
timer: timer,
lapCounter: lapCounter,
timestamp: Date.now()
};
localStorage.setItem('stopwatchState', JSON.stringify(state));
}
// Load stopwatch state from localStorage
function loadStopwatchState() {
const savedState = localStorage.getItem('stopwatchState');
if (savedState) {
try {
const state = JSON.parse(savedState);
// Only restore if saved within last 24 hours
if (Date.now() - state.timestamp < 24 * 60 * 60 * 1000) {
hr = state.hr || 0;
min = state.min || 0;
sec = state.sec || 0;
count = state.count || 0;
lapCounter = state.lapCounter || 1;
// Update display
updateDisplay();
}
} catch (e) {
console.log('Error loading saved state:', e);
}
}
}
// Update display helper
function updateDisplay() {
if ($id("hr")) $id("hr").innerHTML = hr < 10 ? "0" + hr : "" + hr;
if ($id("min")) $id("min").innerHTML = min < 10 ? "0" + min : "" + min;
if ($id("sec")) $id("sec").innerHTML = sec < 10 ? "0" + sec : "" + sec;
if ($id("count")) $id("count").innerHTML = count < 10 ? "0" + count : "" + count;
}
// ============================================
// DARK MODE FUNCTIONALITY
// ============================================
function setupDarkModeToggle() {
const checkbox = document.getElementById("light");
if (checkbox) {
checkbox.addEventListener("change", function () {
document.body.classList.toggle("dark-mode");
// Save preference
localStorage.setItem('darkMode', document.body.classList.contains('dark-mode'));
});
}
}
function loadDarkModePreference() {
const darkMode = localStorage.getItem('darkMode');
const checkbox = document.getElementById("light");
if (darkMode === 'true') {
document.body.classList.add('dark-mode');
if (checkbox) checkbox.checked = true;
}
}
// ============================================
// HELPER FUNCTIONS
// ============================================
function $id(id) {
return document.getElementById(id);
}
// Play sound effect
function playSound(sound) {
if (sound && sound.readyState >= 2) {
sound.currentTime = 0;
sound.play().catch(() => { });
}
}
// ============================================
// STOPWATCH CONTROLS
// ============================================
// Start / Pause toggle
function start() {
if (!timer) {
timer = true;
// Play start sound
playSound(startSound);
if (isTickEnabled) {
tickSound.play().catch(() => { });
}
if ($id("start"))
$id("start").innerHTML = '<i class="far fa-pause-circle"></i> Pause';
enhancedStopwatch();
} else {
timer = false;
// Play beep sound on pause
playSound(beepSound);
tickSound.pause();
if ($id("start"))
$id("start").innerHTML = '<i class="far fa-play-circle"></i> Start';
}
// Save state
saveStopwatchState();
}
// -- Stop (explicit) -----------------
function stop() {
timer = false;
closePipWindow(); // --- ADDED FOR PiP ---
tickSound.pause();
tickSound.currentTime = 0;
if ($id("start"))
$id("start").innerHTML = '<i class="far fa-play-circle"></i> Start';
}
// Reset stopwatch
function reset() {
if ($id("record-container")) $id("record-container").style.display = "none";
timer = false;
closePipWindow(); // --- ADDED FOR PiP ---
// Play beep sound on reset
playSound(beepSound);
tickSound.pause();
tickSound.currentTime = 0;
if ($id("start"))
$id("start").innerHTML = '<i class="far fa-play-circle"></i> Start';
clearTimeout(timeoutId);
clearInterval(countdownInterval);
hr = 0;
min = 0;
sec = 0;
count = 0;
if ($id("hr")) $id("hr").innerHTML = "00";
if ($id("min")) $id("min").innerHTML = "00";
if ($id("sec")) $id("sec").innerHTML = "00";
if ($id("count")) $id("count").innerHTML = "00";
if ($id("record-table-body")) $id("record-table-body").innerHTML = "";
lapCounter = 1;
// CLEAR COUNTDOWN INPUT & PRESETS
const countdownInput = $id("countdown-minutes");
if (countdownInput) {
countdownInput.value = "";
countdownInput.style.border = "2px solid rgba(255, 255, 255, 0.3)";
countdownInput.style.background = "rgba(255, 255, 255, 0.08)";
countdownInput.style.color = "white";
countdownInput.style.transform = "scale(1)";
}
document.querySelectorAll('.preset-btn').forEach(btn => {
btn.classList.remove('active');
});
// Clear saved state
localStorage.removeItem('stopwatchState');
}
// ============================================
// STOPWATCH ENGINE
// ============================================
let timeoutId;
function stopwatch() {
clearTimeout(timeoutId);
clearInterval(countdownInterval);
if (timer === true) count = count + 1;
if (count == 99) {
sec = sec + 1;
count = 0;
}
if (sec == 59) {
min = min + 1;
sec = 0;
}
if (min == 59) {
hr = hr + 1;
min = 0;
sec = 0;
}
var hrString = hr < 10 ? "0" + hr : "" + hr;
var minString = min < 10 ? "0" + min : "" + min;
var secString = sec < 10 ? "0" + sec : "" + sec;
var countString = count < 10 ? "0" + count : "" + count;
if ($id("hr")) $id("hr").innerHTML = hrString;
if ($id("min")) $id("min").innerHTML = minString;
if ($id("sec")) $id("sec").innerHTML = secString;
if ($id("count")) $id("count").innerHTML = countString;
// --- ADDED FOR PiP ---
// If the PiP window is open, send it the new time
if (pipWindow) {
pipWindow.postMessage({
hr: hrString,
min: minString,
sec: secString,
count: countString // Send all four values
});
}
// --- END PiP ---
// Save state periodically (every second)
if (count % 100 === 0) {
saveStopwatchState();
}
timeoutId = setTimeout(stopwatch, 50);
}
// ============================================
// LAP FUNCTIONALITY
// ============================================
// Calculate lap time difference
function getdiff() {
diff_hr = hr - prev_hr;
diff_min = min - prev_min;
if (diff_min < 0) {
diff_min += 60;
diff_hr -= 1;
}
diff_sec = sec - prev_sec;
if (diff_sec < 0) {
diff_sec += 60;
diff_min -= 1;
}
diff_count = count - prev_count;
if (diff_count < 0) {
diff_count += 100;
diff_sec -= 1;
}
prev_count = count;
prev_sec = sec;
prev_min = min;
prev_hr = hr;
}
// Record lap time
function lap() {
if (timer) {
// Play beep sound
playSound(beepSound);
if ($id("record-container"))
$id("record-container").style.display = "block";
// Show export button when first lap is recorded
if ($id("exportlap") && lapCounter === 1) {
$id("exportlap").style.display = "inline-block";
}
getdiff();
var lap_time =
($id("hr") ? $id("hr").innerHTML : "00") +
":" +
($id("min") ? $id("min").innerHTML : "00") +
":" +
($id("sec") ? $id("sec").innerHTML : "00") +
":" +
($id("count") ? $id("count").innerHTML : "00");
const table = $id("record-table-body");
if (table) {
const row = table.insertRow(0);
const no_cell = row.insertCell(0);
const time_cell = row.insertCell(1);
const diff_cell = row.insertCell(2);
no_cell.innerHTML = lapCounter;
time_cell.innerHTML = lap_time;
var hrString = diff_hr < 10 ? "0" + diff_hr : "" + diff_hr;
var minString = diff_min < 10 ? "0" + diff_min : "" + diff_min;
var secString = diff_sec < 10 ? "0" + diff_sec : "" + diff_sec;
var countString = diff_count < 10 ? "0" + diff_count : "" + diff_count;
diff_cell.innerHTML =
hrString + ":" + minString + ":" + secString + ":" + countString;
lapCounter++;
}
}
}
// Clear all lap records
function clearLap() {
if ($id("record-container")) $id("record-container").style.display = "none";
if ($id("record-table-body")) $id("record-table-body").innerHTML = "";
if ($id("exportlap")) $id("exportlap").style.display = "none";
lapCounter = 1;
}
// ============================================
// LAP EXPORT FUNCTIONALITY (NEW FEATURE)
// ============================================
// Export lap times to CSV
function exportLaps() {
const tableBody = $id("record-table-body");
if (!tableBody || tableBody.children.length === 0) {
alert("No lap data to export!");
return;
}
const rows = Array.from(tableBody.children);
const lapData = [];
// Add header
lapData.push(['Lap Number', 'Total Time', 'Lap Time', 'Export Date']);
// Add lap data (reverse order since table shows newest first)
rows.reverse().forEach((row, index) => {
const cells = row.children;
if (cells.length >= 3) {
lapData.push([
cells[0].textContent, // Lap number
cells[1].textContent, // Total time
cells[2].textContent, // Lap difference
new Date().toLocaleString() // Export timestamp
]);
}
});
// Create CSV content
const csvContent = lapData.map(row =>
row.map(cell => `"${cell}"`).join(',')
).join('\n');
// Create and download file
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const link = document.createElement('a');
if (link.download !== undefined) {
const url = URL.createObjectURL(blob);
link.setAttribute('href', url);
link.setAttribute('download', `stopwatch-laps-${new Date().toISOString().split('T')[0]}.csv`);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// Show success message
showVoiceStatus('📁 **EXPORTED** Lap times to CSV', '#43c6ac');
} else {
// Fallback for older browsers
alert('Export not supported in this browser. Please use a modern browser.');
}
}
// Export lap times to JSON format
function exportLapsJSON() {
const tableBody = $id("record-table-body");
if (!tableBody || tableBody.children.length === 0) {
alert("No lap data to export!");
return;
}
const rows = Array.from(tableBody.children);
const lapData = {
exportDate: new Date().toISOString(),
totalLaps: rows.length,
laps: []
};
// Add lap data (reverse order since table shows newest first)
rows.reverse().forEach((row, index) => {
const cells = row.children;
if (cells.length >= 3) {
lapData.laps.push({
lapNumber: parseInt(cells[0].textContent),
totalTime: cells[1].textContent,
lapTime: cells[2].textContent,
timestamp: new Date().toISOString()
});
}
});
// Create JSON content
const jsonContent = JSON.stringify(lapData, null, 2);
// Create and download file
const blob = new Blob([jsonContent], { type: 'application/json;charset=utf-8;' });
const link = document.createElement('a');
if (link.download !== undefined) {
const url = URL.createObjectURL(blob);
link.setAttribute('href', url);
link.setAttribute('download', `stopwatch-laps-${new Date().toISOString().split('T')[0]}.json`);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// Show success message
showVoiceStatus('📁 **EXPORTED** Lap times to JSON', '#43c6ac');
}
}
// ============================================
// DATE DISPLAY
// ============================================
setInterval(() => {
var d = new Date();
var year = d.getFullYear();
var day;
switch (d.getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
}
var month;
switch (d.getMonth()) {
case 0:
month = "Jan";
break;
case 1:
month = "Feb";
break;
case 2:
month = "March";
break;
case 3:
month = "April";
break;
case 4:
month = "May";
break;
case 5:
month = "June";
break;
case 6:
month = "July";
break;
case 7:
month = "Aug";
break;
case 8:
month = "Sept";
break;
case 9:
month = "Oct";
break;
case 10:
month = "Nov";
break;
case 11:
month = "Dec";
break;
}
var dayn = d.getDate();
var dateStr = dayn + " " + month + " , " + year;
if ($id("d1")) $id("d1").innerHTML = dateStr;
}, 1000);
const stopwatchBtn = document.getElementById("stopwatch-btn");
const countdownBtn = document.getElementById("countdown-btn");
const countdownInputContainer = document.getElementById(
"countdown-input-container"
);
let mode = "stopwatch"; // default mode
stopwatchBtn.addEventListener("click", () => {
mode = "stopwatch";
stopwatchBtn.classList.add("active");
countdownBtn.classList.remove("active");
countdownInputContainer.style.display = "none";
reset(); // reset stopwatch
});
countdownBtn.addEventListener("click", () => {
mode = "countdown";
countdownBtn.classList.add("active");
stopwatchBtn.classList.remove("active");
countdownInputContainer.style.display = "block";
reset(); // reset stopwatch
});
// Countdown logic
let countdownInterval;
document.getElementById("start-countdown").addEventListener("click", () => {
let minutes = parseFloat(document.getElementById("countdown-minutes").value);
if (isNaN(minutes) || minutes <= 0) {
alert("Enter a valid number of minutes");
return;
}
let totalSeconds = Math.floor(minutes * 60);
clearInterval(countdownInterval);
if ($id("start"))
$id("start").innerHTML = '<i class="far fa-play-circle"></i> Start';
countdownInterval = setInterval(() => {
let hrs = Math.floor(totalSeconds / 3600);
let mins = Math.floor((totalSeconds % 3600) / 60);
let secs = totalSeconds % 60;
document.getElementById("hr").textContent = String(hrs).padStart(2, "0");
document.getElementById("min").textContent = String(mins).padStart(2, "0");
document.getElementById("sec").textContent = String(secs).padStart(2, "0");
document.getElementById("count").textContent = "00";
if (totalSeconds <= 0) {
clearInterval(countdownInterval);
alert("Time's up!");
}
totalSeconds--;
}, 1000);
});
// Timer Preset functionality
let presetSound = new Audio("../audio/beep_cut.mp3");
presetSound.volume = 0.3;
function setPresetTimer(minutes) {
// Play sound feedback
presetSound.play().catch(() => {
// Ignore audio play errors (browser restrictions)
});
// Set the input value
document.getElementById("countdown-minutes").value = minutes.toFixed(1);
// Update active preset button
document.querySelectorAll('.preset-btn').forEach(btn => {
btn.classList.remove('active');
});
// Find and activate the clicked preset
const clickedBtn = document.querySelector(`[data-minutes="${minutes}"]`);
if (clickedBtn) {
clickedBtn.classList.add('active');
}
// Add visual feedback to input field
const input = document.getElementById("countdown-minutes");
input.style.border = "2px solid #ffb703";
input.style.background = "rgba(255, 183, 3, 0.1)";
input.style.color = "white";
input.style.transform = "scale(1.02)";
setTimeout(() => {
input.style.transform = "scale(1)";
input.style.background = "rgba(255, 255, 255, 0.08)";
input.style.border = "2px solid rgba(255, 255, 255, 0.3)";
input.style.color = "white";
}, 300);
}
// ============================================
// VOICE COMMAND CONTROL (ENHANCED FEATURE)
// ============================================
let voiceRecognition = null;
let isVoiceEnabled = false;
function initializeVoiceControl() {
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const voiceStatus = $id('voice-command-status');
const voiceToggleBtn = $id('voice-toggle');
if (!SpeechRecognition) {
if (voiceStatus) {
voiceStatus.style.display = 'block';
voiceStatus.innerHTML = '❌ Voice control not supported in this browser.';
voiceStatus.style.color = '#ff0000';
}
if (voiceToggleBtn) {
voiceToggleBtn.style.display = 'none';
}
return;
}
// Show voice toggle button
if (voiceToggleBtn) {
voiceToggleBtn.style.display = 'inline-block';
voiceToggleBtn.addEventListener('click', toggleVoiceControl);
}
voiceRecognition = new SpeechRecognition();
voiceRecognition.continuous = true;
voiceRecognition.interimResults = false;
voiceRecognition.lang = 'en-US';
voiceRecognition.onstart = function() {
if (voiceStatus) {
voiceStatus.style.display = 'block';
voiceStatus.innerHTML = '<i class="fas fa-microphone-alt"></i> **LISTENING:** Say "Start", "Stop", "Reset", "Lap", "Export", or "Next Video"';
voiceStatus.style.color = '#43c6ac';
}
};
voiceRecognition.onresult = function(event) {
const last = event.results.length - 1;
const rawCommand = event.results[last][0].transcript.trim();
const command = rawCommand.toLowerCase();
if (voiceStatus) {
voiceStatus.innerHTML = `<i class="fas fa-bullhorn"></i> **COMMAND:** "${rawCommand}"`;
voiceStatus.style.color = '#ffd166';
}
// Enhanced command recognition with more variations
if (command.includes('start') || command.includes('go') || command.includes('begin')) {
if (!timer) {
start();
showVoiceStatus('▶️ **STARTED** Stopwatch', '#00ff00');
}
} else if (command.includes('stop') || command.includes('pause') || command.includes('halt')) {
if (timer) {
start(); // This toggles to pause
showVoiceStatus('⏸️ **PAUSED** Stopwatch', '#ffd166');
}
} else if (command.includes('reset') || command.includes('clear all') || command.includes('restart')) {
reset();
showVoiceStatus('🔄 **RESET** Stopwatch', '#ff6b35');
} else if (command.includes('lap') || command.includes('split') || command.includes('record')) {
if (timer) {
lap();
showVoiceStatus('🏁 **LAP** Recorded', '#00ff00');
} else {
showVoiceStatus('⚠️ Start stopwatch first', '#ff6b35');
}
} else if (command.includes('clear lap') || command.includes('delete lap')) {
clearLap();
showVoiceStatus('🗑️ **CLEARED** All laps', '#ff6b35');
} else if (command.includes('export') || command.includes('download') || command.includes('save lap')) {
exportLaps();
showVoiceStatus('📁 **EXPORTING** Lap times', '#43c6ac');
} else if (command.includes('next video') || command.includes('change video') || command.includes('switch video')) {
if (window.videoManager) {
window.videoManager.nextVideo();
showVoiceStatus('🎬 **SWITCHED** to next video', '#667eea');
}
} else if (command.includes('previous video') || command.includes('last video')) {
if (window.videoManager) {
window.videoManager.previousVideo();
showVoiceStatus('🎬 **SWITCHED** to previous video', '#667eea');
}
} else if (command.includes('video one') || command.includes('first video')) {
if (window.videoManager) {
window.videoManager.setVideo(0);
showVoiceStatus('🎬 **SWITCHED** to video 1', '#667eea');
}
} else if (command.includes('video two') || command.includes('second video')) {
if (window.videoManager) {
window.videoManager.setVideo(1);
showVoiceStatus('🎬 **SWITCHED** to video 2', '#667eea');
}
} else if (command.includes('video three') || command.includes('third video')) {
if (window.videoManager) {
window.videoManager.setVideo(2);
showVoiceStatus('🎬 **SWITCHED** to video 3', '#667eea');
}
} else if (command.includes('video four') || command.includes('fourth video')) {
if (window.videoManager) {
window.videoManager.setVideo(3);
showVoiceStatus('🎬 **SWITCHED** to video 4', '#667eea');
}
} else if (command.includes('random video') || command.includes('shuffle video')) {
if (window.videoManager) {
window.videoManager.switchToRandomVideo();
showVoiceStatus('🎲 **RANDOM** video selected', '#667eea');
}
} else if (command.includes('dark mode') || command.includes('night mode')) {
toggleDarkMode();
showVoiceStatus('🌙 **TOGGLED** Dark mode', '#43c6ac');
} else if (command.includes('light mode') || command.includes('day mode')) {
toggleLightMode();
showVoiceStatus('☀️ **TOGGLED** Light mode', '#43c6ac');
} else {
showVoiceStatus('🤷 Try: "Start", "Stop", "Reset", "Lap", or "Clear"', '#ff6b35');
}
};
voiceRecognition.onerror = function(event) {
console.log('Voice recognition error:', event.error);
if (voiceStatus) {
voiceStatus.innerHTML = `⚠️ **ERROR:** ${event.error}. Click voice button to restart.`;
voiceStatus.style.color = '#ff6b35';
}
isVoiceEnabled = false;
updateVoiceButton();
};
voiceRecognition.onend = function() {
if (isVoiceEnabled && mode === 'stopwatch') {
// Auto-restart if voice is enabled
setTimeout(() => {
if (isVoiceEnabled) {
voiceRecognition.start();
}
}, 1000);
}
};
}
function toggleVoiceControl() {
if (!voiceRecognition) return;
isVoiceEnabled = !isVoiceEnabled;
if (isVoiceEnabled) {
voiceRecognition.start();
showVoiceStatus('🎤 **VOICE ACTIVATED** - Say commands now!', '#43c6ac');
} else {
voiceRecognition.stop();
showVoiceStatus('🔇 **VOICE DISABLED**', '#666');
}
updateVoiceButton();
// Save preference
localStorage.setItem('voiceEnabled', isVoiceEnabled);
}
function updateVoiceButton() {
const voiceToggleBtn = $id('voice-toggle');
if (voiceToggleBtn) {
voiceToggleBtn.innerHTML = isVoiceEnabled ?
'<i class="fas fa-microphone"></i> Voice ON' :
'<i class="fas fa-microphone-slash"></i> Voice OFF';
voiceToggleBtn.style.background = isVoiceEnabled ?
'linear-gradient(135deg, #43c6ac, #191654)' :
'rgba(255, 255, 255, 0.1)';
}
}
function showVoiceStatus(message, color) {
const voiceStatus = $id('voice-command-status');
if (voiceStatus) {
voiceStatus.innerHTML = message;
voiceStatus.style.color = color;
voiceStatus.style.display = 'block';
// Auto-hide after 3 seconds
setTimeout(() => {
if (isVoiceEnabled) {
voiceStatus.innerHTML = '<i class="fas fa-microphone-alt"></i> **LISTENING** for commands...';
voiceStatus.style.color = '#43c6ac';
}
}, 3000);
}
}
function toggleDarkMode() {
const checkbox = document.getElementById("light");
if (checkbox && !document.body.classList.contains('dark-mode')) {
checkbox.checked = true;
document.body.classList.add('dark-mode');
localStorage.setItem('darkMode', true);
}
}
function toggleLightMode() {
const checkbox = document.getElementById("light");
if (checkbox && document.body.classList.contains('dark-mode')) {
checkbox.checked = false;
document.body.classList.remove('dark-mode');
localStorage.setItem('darkMode', false);
}
}
document.addEventListener('keydown', function (event) {
switch (event.key.toLowerCase()) {
case ' ':
event.preventDefault();
startPauseStopwatch();
break;
case 'r':
resetStopwatch();
break;
case 'l':
recordLap();
break;
case 'c':
startCountdownTimer();
break;
}
});
function startPauseStopwatch() {
start();
}
function resetStopwatch() {
reset();
}
function recordLap() {
lap();
}
function startCountdownTimer() {
if (mode === "countdown") {
document.getElementById("start-countdown").click();
} else {
mode = "countdown";
countdownBtn.click();
document.getElementById("start-countdown").click();
}
}
// ============================================
// ANIMATED PROGRESS CIRCLE FUNCTIONALITY
// ============================================
let progressCircle = null;
let lapProgressBars = [];
let maxLapTime = 0;
function initializeProgressCircle() {
progressCircle = document.getElementById('progress-ring-circle');
if (progressCircle) {
const radius = progressCircle.r.baseVal.value;
const circumference = radius * 2 * Math.PI;
progressCircle.style.strokeDasharray = `${circumference} ${circumference}`;
progressCircle.style.strokeDashoffset = circumference;
}
}
function updateProgressCircle() {
if (!progressCircle || !timer) return;
// Calculate progress based on seconds (0-60 seconds = full circle)
const totalSeconds = (hr * 3600) + (min * 60) + sec + (count / 100);
const progress = (totalSeconds % 60) / 60; // Reset every minute
const radius = progressCircle.r.baseVal.value;
const circumference = radius * 2 * Math.PI;
const offset = circumference - (progress * circumference);
progressCircle.style.strokeDashoffset = offset;
}
function createLapProgressBar(lapNumber, lapTime, isLatest = false) {
const lapBarsContainer = document.getElementById('lap-bars-container');
const lapProgressContainer = document.getElementById('lap-progress-bars');
if (!lapBarsContainer || !lapProgressContainer) return;
// Show lap progress container
lapProgressContainer.style.display = 'block';
// Parse lap time to get total milliseconds
const timeParts = lapTime.split(':');
const totalMs = (parseInt(timeParts[0]) * 3600000) +
(parseInt(timeParts[1]) * 60000) +
(parseInt(timeParts[2]) * 1000) +
(parseInt(timeParts[3]) * 10);
// Update max lap time for scaling
if (totalMs > maxLapTime) {
maxLapTime = totalMs;
// Rescale all existing bars
updateAllLapBars();
}
// Create new lap bar
const lapBar = document.createElement('div');
lapBar.className = 'lap-bar';
lapBar.innerHTML = `