File size: 31,134 Bytes
4851729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43f0e42
7d08b8b
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Playable In-Ear Device Protocols (B&W)</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://d3js.org/d3.v7.min.js"></script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap" rel="stylesheet">
    <style>
        /* Custom styles */
        body {
            font-family: 'Inter', sans-serif;
            background-color: #f9fafb; /* Light gray background */
        }
        .protocol-container {
            margin-bottom: 1.5rem; /* Space between protocols */
            padding: 1.5rem;
            background-color: white;
            border-radius: 0.5rem; /* Rounded corners */
            box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06); /* Subtle shadow */
            position: relative; /* Needed for absolute positioning of progress line */
        }
        .protocol-title {
            font-size: 1.1rem; /* Slightly smaller title */
            font-weight: 600;
            margin-bottom: 1rem;
            color: #1f2937; /* Dark gray text */
        }
        svg {
            display: block;
            margin: auto;
            overflow: visible;
        }
        .bar {
            stroke: #6b7280; /* Medium Gray border for bars */
            stroke-width: 0.5;
            transition: fill 0.1s ease-in-out, stroke-width 0.1s ease-in-out; /* Smooth transition for highlight */
        }
        .bar.active {
             fill: #000000 !important; /* Black for active */
             stroke-width: 1.5;
             stroke: #000000;
        }
        .tooltip {
            position: absolute;
            text-align: center;
            padding: 6px 10px;
            font-size: 12px;
            background: #374151; /* Darker gray background */
            color: white;
            border-radius: 4px;
            pointer-events: none;
            opacity: 0;
            transition: opacity 0.2s;
            white-space: nowrap;
            z-index: 10; /* Ensure tooltip is above elements */
        }
        .axis path,
        .axis line {
            fill: none;
            stroke: #9ca3af; /* Lighter gray axis */
            shape-rendering: crispEdges;
        }
        .axis text {
            font-size: 10px;
            fill: #6b7280; /* Medium gray text for axis */
        }
        .progress-line {
            stroke: #ef4444; /* Red color for progress */
            stroke-width: 1.5;
            pointer-events: none; /* Prevent line from interfering */
        }
        /* Control Buttons Styling */
        .control-button {
            background-color: #3b82f6; /* Blue */
            color: white;
            padding: 0.5rem 1rem;
            border: none;
            border-radius: 0.375rem; /* Rounded */
            font-weight: 600;
            cursor: pointer;
            transition: background-color 0.2s;
            margin: 0 0.25rem; /* Spacing between buttons */
        }
        .control-button:hover {
            background-color: #2563eb; /* Darker Blue */
        }
        .control-button:disabled {
            background-color: #9ca3af; /* Gray when disabled */
            cursor: not-allowed;
        }
        .time-display {
            font-weight: 600;
            color: #1f2937;
            min-width: 100px; /* Ensure space for time */
            text-align: right;
        }
    </style>
</head>
<body class="p-6 bg-gray-50">

    <h1 class="text-2xl font-bold text-center mb-6 text-gray-800">In-Ear Recording Protocols Player</h1>

    <div class="flex justify-center items-center mb-6 p-4 bg-white rounded-lg shadow">
        <button id="play-pause-button" class="control-button">Play</button>
        <button id="reset-button" class="control-button">Reset</button>
        <div class="ml-4 time-display">
            Time: <span id="current-time">0.0</span>s / <span id="total-duration">0.0</span>s
        </div>
         <input type="range" id="speed-slider" min="0.1" max="5" step="0.1" value="1" class="ml-4 w-32 cursor-pointer">
         <label for="speed-slider" class="ml-2 text-sm text-gray-600">Speed: <span id="speed-value">1.0</span>x</label>
    </div>

    <div id="jaw-protocol" class="protocol-container">
        <h2 class="protocol-title">Jaw Movements Protocol</h2>
        <svg id="jaw-chart"></svg>
    </div>
    <div id="tongue-protocol" class="protocol-container">
        <h2 class="protocol-title">Tongue Movements Protocol</h2>
        <svg id="tongue-chart"></svg>
    </div>
    <div id="spoken-aloud-protocol" class="protocol-container">
        <h2 class="protocol-title">Spoken Words Protocol (Aloud)</h2>
        <svg id="spoken-aloud-chart"></svg>
    </div>
    <div id="spoken-whisper-protocol" class="protocol-container">
        <h2 class="protocol-title">Spoken Words Protocol (Whisper)</h2>
        <svg id="spoken-whisper-chart"></svg>
    </div>
    <div id="spoken-closed-protocol" class="protocol-container">
        <h2 class="protocol-title">Spoken Words Protocol (Mouth Closed)</h2>
        <svg id="spoken-closed-chart"></svg>
    </div>
    <div id="eyes-protocol" class="protocol-container">
        <h2 class="protocol-title">Eyes Open/Closed Protocol</h2>
        <svg id="eyes-chart"></svg>
    </div>
    <div id="clench-protocol" class="protocol-container">
        <h2 class="protocol-title">Clench Jaw Protocol</h2>
        <svg id="clench-chart"></svg>
    </div>

    <div id="tooltip" class="tooltip"></div>

    <script>
        // --- Protocol Data (Keep as is) ---
        const JAW_PROTOCOL = [
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"},
            {"name": "Open jaw", "duration": 2, "color": "#FF9999"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"}, {"name": "Open jaw", "duration": 2, "color": "#FF9999"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"},
            {"name": "Close jaw", "duration": 2, "color": "#99FF99"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"}, {"name": "Close jaw", "duration": 2, "color": "#99FF99"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"},
            {"name": "Shift to right", "duration": 2, "color": "#9999FF"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"}, {"name": "Shift to right", "duration": 2, "color": "#9999FF"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"},
            {"name": "Shift left", "duration": 2, "color": "#FFCC99"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"}, {"name": "Shift left", "duration": 2, "color": "#FFCC99"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"},
            {"name": "Post-Repetition Rest", "duration": 15, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"},
            {"name": "Open jaw", "duration": 2, "color": "#FF9999"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"}, {"name": "Open jaw", "duration": 2, "color": "#FF9999"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"},
            {"name": "Close jaw", "duration": 2, "color": "#99FF99"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"}, {"name": "Close jaw", "duration": 2, "color": "#99FF99"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"},
            {"name": "Shift to right", "duration": 2, "color": "#9999FF"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"}, {"name": "Shift to right", "duration": 2, "color": "#9999FF"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"},
            {"name": "Shift left", "duration": 2, "color": "#FFCC99"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"}, {"name": "Shift left", "duration": 2, "color": "#FFCC99"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"},
            {"name": "Post-Repetition Rest", "duration": 15, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"},
            {"name": "Open jaw", "duration": 2, "color": "#FF9999"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"}, {"name": "Open jaw", "duration": 2, "color": "#FF9999"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"},
            {"name": "Close jaw", "duration": 2, "color": "#99FF99"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"}, {"name": "Close jaw", "duration": 2, "color": "#99FF99"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"},
            {"name": "Shift to right", "duration": 2, "color": "#9999FF"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"}, {"name": "Shift to right", "duration": 2, "color": "#9999FF"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"},
            {"name": "Shift left", "duration": 2, "color": "#FFCC99"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"}, {"name": "Shift left", "duration": 2, "color": "#FFCC99"}, {"name": "Rest", "duration": 1, "color": "#CCCCCC"}
        ];
        const TONGUE_PROTOCOL = [
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Tongue Right", "duration": 6, "color": "#9999FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Tongue Left", "duration": 6, "color": "#FFCC99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Push Upper Teeth", "duration": 6, "color": "#FF9999"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Push Lower Teeth", "duration": 6, "color": "#99FF99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Retraction", "duration": 6, "color": "#CC99FF"}, {"name": "Post-Repetition Rest", "duration": 30, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Tongue Right", "duration": 6, "color": "#9999FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Tongue Left", "duration": 6, "color": "#FFCC99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Push Upper Teeth", "duration": 6, "color": "#FF9999"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Push Lower Teeth", "duration": 6, "color": "#99FF99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Retraction", "duration": 6, "color": "#CC99FF"}, {"name": "Post-Repetition Rest", "duration": 30, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Tongue Right", "duration": 6, "color": "#9999FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Tongue Left", "duration": 6, "color": "#FFCC99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Push Upper Teeth", "duration": 6, "color": "#FF9999"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Push Lower Teeth", "duration": 6, "color": "#99FF99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Retraction", "duration": 6, "color": "#CC99FF"}
        ];
        const SPOKEN_ALOUD_PROTOCOL = [
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'Yes'", "duration": 6, "color": "#FF9999"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'No'", "duration": 6, "color": "#99FF99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'Reply'", "duration": 6, "color": "#9999FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'Call'", "duration": 6, "color": "#FFCC99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'You'", "duration": 6, "color": "#FF99FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'Later'", "duration": 6, "color": "#99CCFF"}, {"name": "Post-Repetition Rest", "duration": 30, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'Yes'", "duration": 6, "color": "#FF9999"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'No'", "duration": 6, "color": "#99FF99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'Reply'", "duration": 6, "color": "#9999FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'Call'", "duration": 6, "color": "#FFCC99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'You'", "duration": 6, "color": "#FF99FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'Later'", "duration": 6, "color": "#99CCFF"}, {"name": "Post-Repetition Rest", "duration": 30, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'Yes'", "duration": 6, "color": "#FF9999"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'No'", "duration": 6, "color": "#99FF99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'Reply'", "duration": 6, "color": "#9999FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'Call'", "duration": 6, "color": "#FFCC99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'You'", "duration": 6, "color": "#FF99FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Say Aloud: 'Later'", "duration": 6, "color": "#99CCFF"}
        ];
        const SPOKEN_WHISPER_PROTOCOL = [
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'Yes'", "duration": 6, "color": "#FF9999"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'No'", "duration": 6, "color": "#99FF99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'Reply'", "duration": 6, "color": "#9999FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'Call'", "duration": 6, "color": "#FFCC99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'You'", "duration": 6, "color": "#FF99FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'Later'", "duration": 6, "color": "#99CCFF"}, {"name": "Post-Repetition Rest", "duration": 30, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'Yes'", "duration": 6, "color": "#FF9999"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'No'", "duration": 6, "color": "#99FF99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'Reply'", "duration": 6, "color": "#9999FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'Call'", "duration": 6, "color": "#FFCC99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'You'", "duration": 6, "color": "#FF99FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'Later'", "duration": 6, "color": "#99CCFF"}, {"name": "Post-Repetition Rest", "duration": 30, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'Yes'", "duration": 6, "color": "#FF9999"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'No'", "duration": 6, "color": "#99FF99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'Reply'", "duration": 6, "color": "#9999FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'Call'", "duration": 6, "color": "#FFCC99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'You'", "duration": 6, "color": "#FF99FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Whisper: 'Later'", "duration": 6, "color": "#99CCFF"}
        ];
        const SPOKEN_CLOSED_PROTOCOL = [
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'Yes'", "duration": 6, "color": "#FF9999"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'No'", "duration": 6, "color": "#99FF99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'Reply'", "duration": 6, "color": "#9999FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'Call'", "duration": 6, "color": "#FFCC99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'You'", "duration": 6, "color": "#FF99FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'Later'", "duration": 6, "color": "#99CCFF"}, {"name": "Post-Repetition Rest", "duration": 30, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'Yes'", "duration": 6, "color": "#FF9999"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'No'", "duration": 6, "color": "#99FF99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'Reply'", "duration": 6, "color": "#9999FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'Call'", "duration": 6, "color": "#FFCC99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'You'", "duration": 6, "color": "#FF99FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'Later'", "duration": 6, "color": "#99CCFF"}, {"name": "Post-Repetition Rest", "duration": 30, "color": "#CCCCCC"},
            {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'Yes'", "duration": 6, "color": "#FF9999"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'No'", "duration": 6, "color": "#99FF99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'Reply'", "duration": 6, "color": "#9999FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'Call'", "duration": 6, "color": "#FFCC99"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'You'", "duration": 6, "color": "#FF99FF"}, {"name": "Rest", "duration": 3, "color": "#CCCCCC"}, {"name": "Mouth closed: 'Later'", "duration": 6, "color": "#99CCFF"}
        ];
        const EYES_PROTOCOL = [
            {"name": "Rest", "duration": 5, "color": "#CCCCCC"},
            {"name": "Eyes Closed", "duration": 30, "color": "#FF9999"}, {"name": "Eyes Open", "duration": 30, "color": "#99FF99"},
            {"name": "Eyes Closed", "duration": 30, "color": "#FF9999"}, {"name": "Eyes Open", "duration": 30, "color": "#99FF99"},
            {"name": "Eyes Closed", "duration": 30, "color": "#FF9999"}, {"name": "Eyes Open", "duration": 30, "color": "#99FF99"}
        ];
        const CLENCH_PROTOCOL = [
            {"name": "Rest", "duration": 15, "color": "#99FF99"}, {"name": "Clench", "duration": 15, "color": "#FF9999"},
            {"name": "Rest", "duration": 15, "color": "#99FF99"}, {"name": "Clench", "duration": 15, "color": "#FF9999"},
            {"name": "Rest", "duration": 15, "color": "#99FF99"}, {"name": "Clench", "duration": 15, "color": "#FF9999"}
        ];

        const ALL_PROTOCOLS = {
            "jaw-chart": JAW_PROTOCOL,
            "tongue-chart": TONGUE_PROTOCOL,
            "spoken-aloud-chart": SPOKEN_ALOUD_PROTOCOL,
            "spoken-whisper-chart": SPOKEN_WHISPER_PROTOCOL,
            "spoken-closed-chart": SPOKEN_CLOSED_PROTOCOL,
            "eyes-chart": EYES_PROTOCOL,
            "clench-chart": CLENCH_PROTOCOL
        };

        // --- Global State ---
        let timerInterval = null;
        let currentTime = 0; // Current time in seconds
        let isPlaying = false;
        let playbackSpeed = 1.0;
        const timeStep = 0.1; // Update interval in seconds (100ms)
        let maxDuration = 0; // Maximum duration across all protocols
        const scales = {}; // Store scales for each chart
        const processedDataStore = {}; // Store processed data for each chart

        // --- DOM Elements ---
        const playPauseButton = document.getElementById('play-pause-button');
        const resetButton = document.getElementById('reset-button');
        const currentTimeDisplay = document.getElementById('current-time');
        const totalDurationDisplay = document.getElementById('total-duration');
        const tooltip = d3.select("#tooltip");
        const speedSlider = document.getElementById('speed-slider');
        const speedValueDisplay = document.getElementById('speed-value');

        // --- Helper Functions ---
        // Function to calculate cumulative duration and add start/end times
        function processProtocolData(protocolData) {
            let cumulativeDuration = 0;
            return protocolData.map(d => {
                const start = cumulativeDuration;
                cumulativeDuration += d.duration;
                return { ...d, start: start, end: cumulativeDuration };
            });
        }

        // Function to calculate total duration of a processed protocol
        function calculateTotalDuration(processedData) {
            return processedData.length > 0 ? processedData[processedData.length - 1].end : 0;
        }

        // --- Visualization Function ---
        function createTimeline(chartId, processedData, globalMaxDuration) {
            const container = d3.select(`#${chartId}`);
            if (container.empty()) {
                console.error(`Container element #${chartId} not found.`);
                return;
            }

            const containerWidth = container.node().getBoundingClientRect().width;
            const margin = { top: 5, right: 15, bottom: 25, left: 15 };
            const width = containerWidth - margin.left - margin.right;
            const height = 60 - margin.top - margin.bottom; // Reduced height
            const barHeight = 25; // Reduced bar height

            // --- Scales ---
            // Use the global max duration for the domain
            const xScale = d3.scaleLinear()
                .domain([0, globalMaxDuration])
                .range([0, width]);
            scales[chartId] = xScale; // Store the scale

            // --- SVG Setup ---
            container.selectAll("*").remove(); // Clear previous chart
            const svg = container
                .attr("width", width + margin.left + margin.right)
                .attr("height", height + margin.top + margin.bottom)
              .append("g")
                .attr("transform", `translate(${margin.left},${margin.top})`);

            // --- Draw Bars ---
            svg.selectAll(".bar")
                .data(processedData)
                .enter()
              .append("rect")
                .attr("class", "bar")
                .attr("data-chart-id", chartId) // Add chart ID for easier selection later
                .attr("x", d => xScale(d.start))
                .attr("y", (height - barHeight) / 2)
                .attr("width", d => Math.max(0, xScale(d.end) - xScale(d.start))) // Ensure width is non-negative
                .attr("height", barHeight)
                .attr("fill", d => d.name.toLowerCase().includes("rest") ? "#D3D3D3" : "#696969") // Initial colors
                .on("mouseover", function(event, d) {
                    tooltip.style("opacity", 1);
                })
                .on("mousemove", function(event, d) {
                    tooltip.html(`${d.name}<br>Duration: ${d.duration}s<br>Time: ${d.start.toFixed(1)}s - ${d.end.toFixed(1)}s`)
                        .style("left", (event.pageX + 10) + "px")
                        .style("top", (event.pageY - 28) + "px");
                })
                .on("mouseout", function() {
                    tooltip.style("opacity", 0);
                });

             // --- Add Time Axis ---
            const xAxis = d3.axisBottom(xScale)
                            .ticks(Math.min(10, Math.ceil(globalMaxDuration / 15))) // Adjust ticks
                            .tickFormat(d => `${d}s`);

            svg.append("g")
                .attr("class", "axis x-axis")
                .attr("transform", `translate(0,${height})`)
                .call(xAxis);

            // --- Add Progress Line ---
            svg.append("line")
                .attr("class", "progress-line")
                .attr("x1", xScale(0))
                .attr("x2", xScale(0))
                .attr("y1", -margin.top / 2) // Position slightly above the bars
                .attr("y2", height + margin.bottom / 2) // Extend slightly below axis
                .style("opacity", 0); // Initially hidden
        }

        // --- Update Highlighting and Progress ---
        function updateVisuals(time) {
            currentTimeDisplay.textContent = time.toFixed(1);

            // Update highlights for all bars
            d3.selectAll(".bar").each(function(d) {
                const bar = d3.select(this);
                const isActive = time >= d.start && time < d.end;
                bar.classed("active", isActive)
                   // Reset fill if not active (needed because !important overrides direct fill)
                   .style("fill", isActive ? null : (d.name.toLowerCase().includes("rest") ? "#D3D3D3" : "#696969"));
            });

            // Update progress lines for all charts
            d3.selectAll(".progress-line").each(function() {
                const line = d3.select(this);
                const chartId = d3.select(this.parentNode).select('.bar').attr('data-chart-id'); // Get chart ID from sibling bar
                if (chartId && scales[chartId]) {
                    const xScale = scales[chartId];
                    line.attr("x1", xScale(time))
                        .attr("x2", xScale(time))
                        .style("opacity", 1); // Make visible
                }
            });
        }


        // --- Playback Control Functions ---
        function play() {
            if (isPlaying) return;
            isPlaying = true;
            playPauseButton.textContent = 'Pause';
            resetButton.disabled = true; // Disable reset while playing

            // Clear existing interval if any
            if (timerInterval) clearInterval(timerInterval);

            let lastTimestamp = performance.now();

            function tick(currentTimestamp) {
                if (!isPlaying) return; // Stop if paused

                const elapsedRealTime = (currentTimestamp - lastTimestamp) / 1000; // Time since last frame in seconds
                lastTimestamp = currentTimestamp;

                currentTime += elapsedRealTime * playbackSpeed; // Increment time based on speed

                if (currentTime >= maxDuration) {
                    currentTime = maxDuration; // Clamp to end
                    pause(); // Automatically pause at the end
                    resetButton.disabled = false; // Re-enable reset
                }

                updateVisuals(currentTime);

                // Continue the loop only if playing
                if (isPlaying) {
                   requestAnimationFrame(tick);
                }
            }
             requestAnimationFrame(tick); // Start the loop
        }

        function pause() {
            if (!isPlaying) return;
            isPlaying = false;
            playPauseButton.textContent = 'Play';
            resetButton.disabled = false; // Re-enable reset
             // No need to clear interval with requestAnimationFrame
        }

        function reset() {
            pause(); // Ensure it's paused
            currentTime = 0;
            updateVisuals(currentTime);
            d3.selectAll(".progress-line").style("opacity", 0); // Hide progress line on reset
        }

        // --- Initialization ---
        function initialize() {
            // Process all data and find max duration
            let currentMax = 0;
            for (const chartId in ALL_PROTOCOLS) {
                const processedData = processProtocolData(ALL_PROTOCOLS[chartId]);
                processedDataStore[chartId] = processedData; // Store processed data
                const duration = calculateTotalDuration(processedData);
                if (duration > currentMax) {
                    currentMax = duration;
                }
            }
            maxDuration = currentMax;
            totalDurationDisplay.textContent = maxDuration.toFixed(1);

            // Create all timelines using the global max duration
            for (const chartId in processedDataStore) {
                createTimeline(chartId, processedDataStore[chartId], maxDuration);
            }

            // Set initial visuals
            updateVisuals(0);
            d3.selectAll(".progress-line").style("opacity", 0); // Ensure lines are hidden initially

            // Setup Speed Slider
             speedSlider.addEventListener('input', (event) => {
                playbackSpeed = parseFloat(event.target.value);
                speedValueDisplay.textContent = playbackSpeed.toFixed(1);
            });
            speedValueDisplay.textContent = playbackSpeed.toFixed(1); // Initial display

            // Add event listeners
            playPauseButton.addEventListener('click', () => {
                if (isPlaying) {
                    pause();
                } else {
                     if (currentTime >= maxDuration) { // If at end, reset before playing
                        reset();
                    }
                    play();
                }
            });
            resetButton.addEventListener('click', reset);
        }

        // --- Run Initialization on Load ---
        window.addEventListener('load', initialize);

        // --- Redraw charts on resize ---
        // Debounce resize event for performance
        let resizeTimeout;
        window.addEventListener('resize', () => {
            clearTimeout(resizeTimeout);
            resizeTimeout = setTimeout(() => {
                // Re-initialize everything on resize to recalculate widths/scales
                // Store current state
                const wasPlaying = isPlaying;
                const timeBeforeResize = currentTime;
                // Pause before re-initializing
                pause();
                // Re-initialize
                initialize();
                // Restore state
                currentTime = timeBeforeResize;
                updateVisuals(currentTime);
                if (wasPlaying) {
                    play(); // Resume playing if it was playing before
                }

            }, 250); // Wait 250ms after last resize event
        });

    </script>

</body>
</html>