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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Neural Network Background Animation
document.addEventListener('DOMContentLoaded', () => {
    // Set initialization flag
    window.backgroundAnimationInitialized = true;
    console.log('Background animation script initialized');

    // Canvas initialization function
    function initializeCanvas() {
        console.log('Initializing background animation canvas');
        const canvas = document.getElementById('background-canvas');
        if (!canvas) {
            console.error('Background animation canvas not found!');
            return;
        }
        
        const ctx = canvas.getContext('2d');
        if (!ctx) {
            console.error('Could not get 2D context for background animation canvas');
            return;
        }
        
        // Set canvas dimensions
        const container = canvas.parentElement;
        if (container) {
            canvas.width = container.clientWidth || 800;
            canvas.height = container.clientHeight || 400;
        } else {
            canvas.width = 800;
            canvas.height = 400;
        }
        
        // Clear canvas
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        
        // Reset animation state and redraw
        resetAnimation();
        updateCanvas(); // Initial draw
    }
    
    // Register the canvas initialization function with tab manager
    if (typeof window !== 'undefined') {
        window.initBackgroundCanvas = initializeCanvas;
    }
    
    // Canvas and context
    const canvas = document.getElementById('background-canvas');
    const ctx = canvas.getContext('2d');
    
    // Control elements
    const startButton = document.getElementById('start-background-animation');
    const pauseButton = document.getElementById('pause-background-animation');
    const resetButton = document.getElementById('reset-background-animation');
    
    // Slider controls
    const neuronCountSlider = document.getElementById('neuron-count');
    const neuronCountValue = document.getElementById('neuron-count-value');
    const connectionDistanceSlider = document.getElementById('connection-distance');
    const connectionDistanceValue = document.getElementById('connection-distance-value');
    const firingSpeedSlider = document.getElementById('firing-speed');
    const firingSpeedValue = document.getElementById('firing-speed-value');
    const firingColorSelect = document.getElementById('firing-color');
    
    // Stats display elements
    const activeNeuronsCount = document.getElementById('active-neurons-count');
    const connectionsCount = document.getElementById('connections-count');
    const firingRateElement = document.getElementById('firing-rate');
    
    // Animation state
    let animationState = {
        running: false,
        neurons: [],
        connections: [],
        config: {
            neuronCount: 150,
            connectionDistance: 100,
            firingSpeed: 5,
            firingColor: 'blue'
        },
        stats: {
            activeNeurons: 0,
            connectionCount: 0,
            firingRate: 0,
            firingHistory: []
        },
        animationFrameId: null,
        lastTimestamp: 0
    };
    
    // Neuron class
    class Neuron {
        constructor(x, y) {
            this.x = x;
            this.y = y;
            this.radius = 3 + Math.random() * 2; // 3-5 pixels
            this.connections = [];
            this.firing = false;
            this.fireProgress = 0; // 0-1 for animation progress
            this.lastFireTime = 0;
            this.refractionPeriod = 500 + Math.random() * 1500; // 0.5-2 seconds
            this.activated = false;
            this.activationLevel = 0; // 0-1
            this.threshold = 0.4 + Math.random() * 0.3; // 0.4-0.7
            this.speedMultiplier = 0.8 + Math.random() * 0.4; // 0.8-1.2x speed
            this.activationDecay = 0.02; // How fast activation decreases
        }
        
        // Add a connection to another neuron
        addConnection(neuron, strength) {
            this.connections.push({
                target: neuron,
                strength: strength,
                active: false,
                progress: 0
            });
        }
        
        // Update neuron state
        update(deltaTime, speed) {
            // Scale the time increment based on speed (0-10)
            const timeIncrement = deltaTime * (speed / 5);
            
            // Update activation level (decay over time)
            if (this.activationLevel > 0 && !this.firing) {
                this.activationLevel = Math.max(0, this.activationLevel - this.activationDecay * (timeIncrement / 16));
            }
            
            // Check if neuron should fire
            const currentTime = Date.now();
            if (!this.firing && this.activationLevel >= this.threshold && 
                (currentTime - this.lastFireTime > this.refractionPeriod)) {
                this.firing = true;
                this.fireProgress = 0;
                this.lastFireTime = currentTime;
                animationState.stats.firingHistory.push(currentTime);
            }
            
            // Update firing animation
            if (this.firing) {
                this.fireProgress += 0.05 * this.speedMultiplier * (timeIncrement / 16);
                
                // Activate connections when fireProgress reaches 1
                if (this.fireProgress >= 1) {
                    this.firing = false;
                    this.activationLevel = 0; // Reset activation after firing
                    
                    // Activate outgoing connections
                    this.connections.forEach(conn => {
                        if (Math.random() < conn.strength) {
                            conn.active = true;
                            conn.progress = 0;
                        }
                    });
                }
            }
            
            // Update connection animations
            this.connections.forEach(conn => {
                if (conn.active) {
                    conn.progress += 0.04 * this.speedMultiplier * (timeIncrement / 16);
                    
                    // Activate target neuron when signal reaches the end
                    if (conn.progress >= 1) {
                        conn.active = false;
                        conn.target.activationLevel += conn.strength;
                    }
                }
            });
        }
        
        // Draw the neuron
        draw(ctx, colorScheme) {
            // Draw connections
            this.connections.forEach(conn => {
                const { target, active, progress, strength } = conn;
                
                // Set line style based on connection state
                if (active) {
                    // Active connection (signal traveling)
                    const lineWidth = 1 + strength * 1.5;
                    
                    // Calculate position along the line based on progress
                    const signalX = this.x + (target.x - this.x) * progress;
                    const signalY = this.y + (target.y - this.y) * progress;
                    
                    // Draw the inactive part of the connection
                    ctx.beginPath();
                    ctx.moveTo(this.x, this.y);
                    ctx.lineTo(target.x, target.y);
                    ctx.strokeStyle = `rgba(200, 200, 200, ${strength * 0.3})`;
                    ctx.lineWidth = 0.5;
                    ctx.stroke();
                    
                    // Draw the active signal
                    ctx.beginPath();
                    ctx.arc(signalX, signalY, lineWidth + 1, 0, Math.PI * 2);
                    
                    // Use the appropriate color based on the selected scheme
                    if (colorScheme === 'rainbow') {
                        const hue = (Date.now() / 20) % 360;
                        ctx.fillStyle = `hsl(${hue}, 80%, 60%)`;
                    } else {
                        switch (colorScheme) {
                            case 'blue':
                                ctx.fillStyle = `rgba(52, 152, 219, ${0.7 + progress * 0.3})`;
                                break;
                            case 'purple':
                                ctx.fillStyle = `rgba(155, 89, 182, ${0.7 + progress * 0.3})`;
                                break;
                            case 'green':
                                ctx.fillStyle = `rgba(46, 204, 113, ${0.7 + progress * 0.3})`;
                                break;
                            default:
                                ctx.fillStyle = `rgba(52, 152, 219, ${0.7 + progress * 0.3})`;
                        }
                    }
                    ctx.fill();
                    
                } else if (this.firing || this.activationLevel > 0.2) {
                    // Connection from an active neuron
                    ctx.beginPath();
                    ctx.moveTo(this.x, this.y);
                    ctx.lineTo(target.x, target.y);
                    ctx.strokeStyle = `rgba(200, 200, 200, ${strength * 0.5})`;
                    ctx.lineWidth = 0.5;
                    ctx.stroke();
                } else {
                    // Inactive connection
                    ctx.beginPath();
                    ctx.moveTo(this.x, this.y);
                    ctx.lineTo(target.x, target.y);
                    ctx.strokeStyle = `rgba(200, 200, 200, ${strength * 0.2})`;
                    ctx.lineWidth = 0.2;
                    ctx.stroke();
                }
            });
            
            // Draw the neuron body
            ctx.beginPath();
            ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
            
            // Color based on firing/activation state
            if (this.firing) {
                // Use the selected color scheme for firing neurons
                if (colorScheme === 'rainbow') {
                    const hue = (Date.now() / 20) % 360;
                    ctx.fillStyle = `hsl(${hue}, 80%, 60%)`;
                } else {
                    switch (colorScheme) {
                        case 'blue':
                            ctx.fillStyle = `rgba(52, 152, 219, ${0.7 + this.fireProgress * 0.3})`;
                            break;
                        case 'purple':
                            ctx.fillStyle = `rgba(155, 89, 182, ${0.7 + this.fireProgress * 0.3})`;
                            break;
                        case 'green':
                            ctx.fillStyle = `rgba(46, 204, 113, ${0.7 + this.fireProgress * 0.3})`;
                            break;
                        default:
                            ctx.fillStyle = `rgba(52, 152, 219, ${0.7 + this.fireProgress * 0.3})`;
                    }
                }
                
                // Add glow effect for firing neurons
                ctx.shadowColor = ctx.fillStyle;
                ctx.shadowBlur = 10;
            } else if (this.activationLevel > 0) {
                // Partially activated
                ctx.fillStyle = `rgba(127, 140, 141, ${0.3 + this.activationLevel * 0.7})`;
                ctx.shadowBlur = 0;
            } else {
                // Inactive
                ctx.fillStyle = 'rgba(127, 140, 141, 0.2)';
                ctx.shadowBlur = 0;
            }
            
            ctx.fill();
            ctx.shadowBlur = 0; // Reset shadow blur
        }
    }
    
    // Initialize the network
    function initNetwork() {
        // Clear existing state
        animationState.neurons = [];
        animationState.connections = [];
        animationState.stats.activeNeurons = 0;
        animationState.stats.connectionCount = 0;
        animationState.stats.firingHistory = [];
        
        const { neuronCount, connectionDistance } = animationState.config;
        
        // Create neurons with random positions
        for (let i = 0; i < neuronCount; i++) {
            const x = Math.random() * canvas.width;
            const y = Math.random() * canvas.height;
            animationState.neurons.push(new Neuron(x, y));
        }
        
        // Create connections between nearby neurons
        let connectionCount = 0;
        
        animationState.neurons.forEach(neuron => {
            animationState.neurons.forEach(target => {
                if (neuron !== target) {
                    // Calculate distance between neurons
                    const dx = neuron.x - target.x;
                    const dy = neuron.y - target.y;
                    const distance = Math.sqrt(dx * dx + dy * dy);
                    
                    // Connect if within range (with random chance based on distance)
                    if (distance < connectionDistance) {
                        const probability = 1 - (distance / connectionDistance);
                        if (Math.random() < probability * 0.3) {
                            // Connection strength decreases with distance
                            const strength = 0.2 + (1 - distance / connectionDistance) * 0.6;
                            neuron.addConnection(target, strength);
                            connectionCount++;
                        }
                    }
                }
            });
        });
        
        animationState.stats.connectionCount = connectionCount;
        
        // Randomly activate a few neurons to start
        for (let i = 0; i < 3; i++) {
            const randomIndex = Math.floor(Math.random() * animationState.neurons.length);
            animationState.neurons[randomIndex].firing = true;
            animationState.neurons[randomIndex].lastFireTime = Date.now();
        }
        
        // Update stats display
        updateStatsDisplay();
    }
    
    // Canvas resize functionality
    function resizeCanvas() {
        const container = canvas.parentElement;
        canvas.width = container.clientWidth;
        canvas.height = container.clientHeight;
        
        // Reinitialize the network when the canvas is resized
        if (animationState.neurons.length > 0) {
            initNetwork();
        }
    }
    
    // Update stats display
    function updateStatsDisplay() {
        if (!activeNeuronsCount || !connectionsCount || !firingRateElement) return;
        
        // Count active neurons
        const activeCount = animationState.neurons.filter(n => n.firing || n.activationLevel > 0.2).length;
        animationState.stats.activeNeurons = activeCount;
        
        // Calculate firing rate (fires per second)
        const now = Date.now();
        const recentFirings = animationState.stats.firingHistory.filter(time => now - time < 1000).length;
        animationState.stats.firingRate = recentFirings;
        
        // Clean up old firing history
        animationState.stats.firingHistory = animationState.stats.firingHistory.filter(time => now - time < 1000);
        
        // Update display
        activeNeuronsCount.textContent = activeCount;
        connectionsCount.textContent = animationState.stats.connectionCount;
        firingRateElement.textContent = `${recentFirings} Hz`;
    }
    
    // Animation loop
    function animate(timestamp) {
        if (!animationState.running) return;
        
        // Calculate delta time
        const deltaTime = timestamp - (animationState.lastTimestamp || timestamp);
        animationState.lastTimestamp = timestamp;
        
        // Clear canvas
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        
        // Update and draw neurons
        animationState.neurons.forEach(neuron => {
            neuron.update(deltaTime, animationState.config.firingSpeed);
            neuron.draw(ctx, animationState.config.firingColor);
        });
        
        // Randomly activate neurons occasionally
        if (Math.random() < 0.01 * (animationState.config.firingSpeed / 5)) {
            const randomIndex = Math.floor(Math.random() * animationState.neurons.length);
            const randomNeuron = animationState.neurons[randomIndex];
            
            if (!randomNeuron.firing && Date.now() - randomNeuron.lastFireTime > randomNeuron.refractionPeriod) {
                randomNeuron.activationLevel = randomNeuron.threshold;
            }
        }
        
        // Update stats periodically (every ~500ms)
        if (timestamp % 500 < 20) {
            updateStatsDisplay();
        }
        
        // Continue animation
        animationState.animationFrameId = requestAnimationFrame(animate);
    }
    
    // Start animation
    function startAnimation() {
        if (!animationState.running) {
            animationState.running = true;
            animationState.lastTimestamp = 0;
            animationState.animationFrameId = requestAnimationFrame(animate);
            
            startButton.disabled = true;
            pauseButton.disabled = false;
            resetButton.disabled = false;
        }
    }
    
    // Pause animation
    function pauseAnimation() {
        if (animationState.running) {
            animationState.running = false;
            if (animationState.animationFrameId) {
                cancelAnimationFrame(animationState.animationFrameId);
            }
            
            startButton.disabled = false;
            pauseButton.disabled = true;
            resetButton.disabled = false;
        }
    }
    
    // Reset animation
    function resetAnimation() {
        pauseAnimation();
        initNetwork();
        
        startButton.disabled = false;
        pauseButton.disabled = true;
        resetButton.disabled = false;
    }
    
    // Initialize the visualization
    function initVisualization() {
        if (!canvas) return;
        
        resizeCanvas();
        window.addEventListener('resize', resizeCanvas);
        
        // Set up initial configuration from sliders if they exist
        if (neuronCountSlider) {
            animationState.config.neuronCount = parseInt(neuronCountSlider.value, 10);
            neuronCountValue.textContent = animationState.config.neuronCount;
        }
        
        if (connectionDistanceSlider) {
            animationState.config.connectionDistance = parseInt(connectionDistanceSlider.value, 10);
            connectionDistanceValue.textContent = animationState.config.connectionDistance;
        }
        
        if (firingSpeedSlider) {
            animationState.config.firingSpeed = parseInt(firingSpeedSlider.value, 10);
            firingSpeedValue.textContent = animationState.config.firingSpeed;
        }
        
        if (firingColorSelect) {
            animationState.config.firingColor = firingColorSelect.value;
        }
        
        // Initialize the network
        initNetwork();
        
        // Set button states
        startButton.disabled = false;
        pauseButton.disabled = true;
        resetButton.disabled = true;
    }
    
    // Handle configuration changes
    function setupControlListeners() {
        if (neuronCountSlider) {
            neuronCountSlider.addEventListener('input', () => {
                animationState.config.neuronCount = parseInt(neuronCountSlider.value, 10);
                neuronCountValue.textContent = animationState.config.neuronCount;
            });
            
            neuronCountSlider.addEventListener('change', () => {
                // Only reinitialize network when slider interaction ends
                resetAnimation();
            });
        }
        
        if (connectionDistanceSlider) {
            connectionDistanceSlider.addEventListener('input', () => {
                animationState.config.connectionDistance = parseInt(connectionDistanceSlider.value, 10);
                connectionDistanceValue.textContent = animationState.config.connectionDistance;
            });
            
            connectionDistanceSlider.addEventListener('change', () => {
                resetAnimation();
            });
        }
        
        if (firingSpeedSlider) {
            firingSpeedSlider.addEventListener('input', () => {
                animationState.config.firingSpeed = parseInt(firingSpeedSlider.value, 10);
                firingSpeedValue.textContent = animationState.config.firingSpeed;
            });
        }
        
        if (firingColorSelect) {
            firingColorSelect.addEventListener('change', () => {
                animationState.config.firingColor = firingColorSelect.value;
            });
        }
        
        // Button event listeners
        if (startButton) {
            startButton.addEventListener('click', startAnimation);
        }
        
        if (pauseButton) {
            pauseButton.addEventListener('click', pauseAnimation);
        }
        
        if (resetButton) {
            resetButton.addEventListener('click', resetAnimation);
        }
        
        // Tab switching event from the main tab controller
        document.addEventListener('tabSwitch', (e) => {
            if (e.detail.tab === 'background-animation') {
                // Restart animation when switching to this tab
                if (animationState.neurons.length === 0) {
                    initNetwork();
                }
                
                if (!animationState.running) {
                    startAnimation();
                }
            } else if (animationState.running) {
                // Pause animation when switching away from this tab
                pauseAnimation();
            }
        });
    }
    
    // Initialize everything
    initVisualization();
    setupControlListeners();
});