-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1174 lines (997 loc) · 41.2 KB
/
script.js
File metadata and controls
1174 lines (997 loc) · 41.2 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
const ALL_STATES = [
// US States
"alabama","alaska","arizona","arkansas","california","colorado","connecticut","delaware",
"florida","georgia","hawaii","idaho","illinois","indiana","iowa","kansas","kentucky",
"louisiana","maine","maryland","massachusetts","michigan","minnesota","mississippi",
"missouri","montana","nebraska","nevada","new_hampshire","new_jersey","new_mexico",
"new_york","north_carolina","north_dakota","ohio","oklahoma","oregon","pennsylvania",
"rhode_island","south_carolina","south_dakota","tennessee","texas","utah","vermont",
"virginia","washington","west_virginia","wisconsin","wyoming", "district_of_columbia",
// US Territories
"american_samoa", "commonwealth_of_the_northern_mariana_islands", "guam",
"puerto_rico", "united_states_virgin_islands",
// Canadian Provinces/Territories
"alberta", "british_columbia", "manitoba", "new_brunswick", "newfoundland_and_labrador",
"northwest_territories", "nova_scotia", "nunavut", "ontario", "prince_edward_island",
"quebec", "saskatchewan", "yukon"
];
const MAIN_MAP_STATES = [
"alabama","alaska","arizona","arkansas","california","colorado","connecticut","delaware",
"florida","georgia","hawaii","idaho","illinois","indiana","iowa","kansas","kentucky",
"louisiana","maine","maryland","massachusetts","michigan","minnesota","mississippi",
"missouri","montana","nebraska","nevada","new_hampshire","new_jersey","new_mexico",
"new_york","north_carolina","north_dakota","ohio","oklahoma","oregon","pennsylvania",
"rhode_island","south_carolina","south_dakota","tennessee","texas","utah","vermont",
"virginia","washington","west_virginia","wisconsin","wyoming", "district_of_columbia",
"alberta", "british_columbia", "manitoba", "new_brunswick", "newfoundland_and_labrador",
"northwest_territories", "nova_scotia", "nunavut", "ontario", "prince_edward_island",
"quebec", "saskatchewan", "yukon"
];
const REMOTE_TERRITORIES = [
"american_samoa", "commonwealth_of_the_northern_mariana_islands",
"guam", "puerto_rico", "united_states_virgin_islands"
];
const CANADIAN_REGIONS = [
"alberta", "british_columbia", "manitoba", "new_brunswick", "newfoundland_and_labrador",
"northwest_territories", "nova_scotia", "nunavut", "ontario", "prince_edward_island",
"quebec", "saskatchewan", "yukon"
];
let animationEnabled = true;
let isAnimating = false;
const MAP_CENTER = [50.0, -100.0];
const MAP_ZOOM = 3;
const THEMES = {
classic: {
name: "Classic",
loggedColor: "#28a745",
unloggedColor: "#6a6a6a",
showLegend: true
},
random: {
name: "Random Colors",
colors: ["#007bff", "#dc3545", "#28a745", "#ffc107", "#6f42c1", "#fd7e14"],
unloggedColor: "#6a6a6a",
showLegend: false
},
flag: {
name: "Flag Colors",
us_patterns: {
red: "#dc3545",
white: "#ffffff",
blue: "#1e3a8a",
red_stripes: "red_white_stripes",
blue_stars: "blue_with_stars"
},
canada_patterns: {
red: "#ff0000",
white: "#ffffff",
red_white: "canada_red_white"
},
unloggedColor: "#6a6a6a",
showLegend: false
}
};
// Global variables
let randomColorAssignments = {};
let stateMapLayers = {};
let currentMap = null;
let currentTheme = 'classic';
const stateCache = {};
function getFlagAssignment(regionName) {
const seed = regionName.split('').reduce((a, b) => a + b.charCodeAt(0), 0);
if (REMOTE_TERRITORIES.includes(regionName)) {
return 'blue';
} else if (CANADIAN_REGIONS.includes(regionName)) {
const canadaOptions = ['red', 'white'];
return canadaOptions[seed % canadaOptions.length];
} else {
const usOptions = ['red', 'white', 'blue'];
return usOptions[seed % usOptions.length];
}
}
function recenterMap() {
if (currentMap) {
currentMap.setView(MAP_CENTER, MAP_ZOOM);
}
}
async function animateToState(stateName) {
if (!currentMap || isAnimating) return;
isAnimating = true;
try {
// Get the state layer
const stateLayer = stateMapLayers[stateName];
if (stateLayer) {
// Get bounds of the state
const bounds = stateLayer.getBounds();
// Zoom to the state (1 second)
currentMap.fitBounds(bounds, {
padding: [50, 50],
duration: 1.0
});
// Wait 1 second, then zoom back out
setTimeout(() => {
currentMap.setView(MAP_CENTER, MAP_ZOOM, {
duration: 1.0
});
isAnimating = false;
}, 1000);
} else {
// Fallback if no layer found
setTimeout(recenterMap, 500);
isAnimating = false;
}
} catch (error) {
console.error("Animation error:", error);
setTimeout(recenterMap, 500);
isAnimating = false;
}
}
function extractPoints(data) {
if (!data.geojson || !data.geojson.coordinates) return [];
const coordinates = data.geojson.coordinates;
const points = [];
if (data.geojson.type === "Polygon") {
for (const coord of coordinates[0]) {
points.push([coord[1], coord[0]]);
}
} else if (data.geojson.type === "MultiPolygon") {
for (const polygon of coordinates) {
for (const coord of polygon[0]) {
points.push([coord[1], coord[0]]);
}
}
}
return points;
}
function getClosestDistance(points, userLat, userLon) {
if (isNaN(userLat) || isNaN(userLon)) return null;
if (!Array.isArray(points) || points.length === 0) return null;
let minDistance = Infinity;
for (const [lat, lon] of points) {
const dist = haversine(userLat, userLon, lat, lon);
if (dist < minDistance) minDistance = dist;
}
return minDistance * 0.621371; // Convert km to miles
}
function haversine(lat1, lon1, lat2, lon2) {
const toRad = deg => deg * Math.PI / 180;
const R = 6371;
const dLat = toRad(lat2 - lat1);
const dLon = toRad(lon2 - lon1);
const a = Math.sin(dLat / 2) ** 2 + Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon / 2) ** 2;
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
}
async function checkProximity(stateName, userLat, userLon) {
try {
if (!stateCache[stateName]) {
const filePath = `state_jsons/${stateName.toLowerCase()}.json`;
const response = await fetch(filePath);
if (!response.ok) {
throw new Error(`Failed to load ${filePath}: ${response.status}`);
}
stateCache[stateName] = await response.json();
}
const points = extractPoints(stateCache[stateName]);
if (points.length === 0) {
throw new Error(`No geographic data available for ${stateName}`);
}
return getClosestDistance(points, userLat, userLon);
} catch (error) {
console.error(`Error in checkProximity for ${stateName}:`, error);
throw error;
}
}
function loadPlateLog() {
const raw = localStorage.getItem("plateLog");
return raw ? JSON.parse(raw) : {};
}
function savePlateLog(log) {
localStorage.setItem("plateLog", JSON.stringify(log));
}
function updatePlateLog(stateName, locationLabel, miles) {
const log = loadPlateLog();
const prev = log[stateName];
if (!prev || miles > prev.miles) {
log[stateName] = { location: locationLabel, miles };
savePlateLog(log);
}
return log;
}
function formatNumber(num) {
return Math.round(num).toLocaleString();
}
function getTotalScore(log) {
return Object.values(log).reduce((sum, entry) => sum + entry.miles, 0);
}
// Share Card Functions
// REPLACE the generateShareCard function with this better version
function generateShareCard() {
const log = loadPlateLog();
const entries = Object.entries(log);
// Count by category
let usStates = 0, territories = 0, canada = 0;
let totalMiles = 0, maxMiles = 0, farthestState = '';
// Track unique locations where plates were spotted
const spottingLocations = new Set();
entries.forEach(([state, data]) => {
const miles = data.miles || 0;
totalMiles += miles;
if (miles > maxMiles) {
maxMiles = miles;
farthestState = state.replace(/_/g, ' ').replace(/\b\w/g, c => c.toUpperCase());
}
// Track where you spotted plates
spottingLocations.add(data.location);
if (REMOTE_TERRITORIES.includes(state)) {
territories++;
} else if (CANADIAN_REGIONS.includes(state)) {
canada++;
} else {
usStates++;
}
});
// Better estimated travel calculation
const uniqueLocations = spottingLocations.size;
let estimatedTravel;
if (uniqueLocations <= 1) {
// All plates spotted from one location (like your case)
estimatedTravel = Math.floor(entries.length * 5); // ~5 miles per plate hunt
} else if (uniqueLocations <= 3) {
// A few different locations (local area)
estimatedTravel = Math.floor(uniqueLocations * 25 + entries.length * 3);
} else {
// Multiple locations (road trip)
estimatedTravel = Math.floor(uniqueLocations * 100 + entries.length * 10);
}
// Realistic days calculation
const days = Math.max(1, Math.floor(entries.length / 3) + Math.floor(uniqueLocations * 0.5));
return {
usStates,
territories,
canada,
totalMiles,
farthestState: farthestState.substring(0, 3).toUpperCase() || 'N/A',
maxMiles,
days,
estimatedTravel,
uniqueLocations // for debugging
};
}
function generateShareText() {
const data = generateShareCard();
const totalLogged = data.usStates + data.territories + data.canada;
const completionPercent = Math.round((totalLogged / 69) * 100);
return `🚗 My So Close So Far Progress! 🚗
${completionPercent}% Complete (${totalLogged}/69 regions)
📍 US States: ${data.usStates}/51
🏝️ US Territories: ${data.territories}/5
🍁 Canada: ${data.canada}/13
🎯 Total Distance: ${data.totalMiles.toLocaleString()} miles
🏁 Farthest Plate: ${data.farthestState}
Join the challenge: ${window.location.href}`;
}
async function shareToFacebook() {
const text = generateShareText();
await copyToClipboard(text);
const url = `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(window.location.href)}`;
window.open(url, '_blank', 'width=600,height=400');
alert('Caption copied! Paste it when sharing on Facebook.');
}
async function shareToInstagram() {
const text = generateShareText() + '\n\n#SoClosesoFar #LicensePlateGame #RoadTrip';
await copyToClipboard(text);
alert('Caption copied! Take a screenshot of the share card above and post to Instagram with the copied caption.');
window.open('https://www.instagram.com/', '_blank');
}
async function shareToX() {
const data = generateShareCard();
const text = `🚗 My So Close So Far progress: ${Math.round(((data.usStates + data.territories + data.canada) / 69) * 100)}% complete!\n\nFarthest plate: ${data.farthestState} (${data.maxMiles.toLocaleString()} miles!)\n\n${window.location.href}\n\n#SoClosesoFar`;
await copyToClipboard(text);
const url = `https://twitter.com/intent/tweet`;
window.open(url, '_blank', 'width=600,height=400');
alert('Tweet copied! Paste it when creating your tweet.');
}
async function shareToThreads() {
const text = generateShareText();
await copyToClipboard(text);
window.open('https://www.threads.net/', '_blank');
alert('Caption copied! Paste it when creating your Threads post.');
}
async function shareToReddit() {
const data = generateShareCard();
const totalLogged = data.usStates + data.territories + data.canada;
const completionPercent = Math.round((totalLogged / 69) * 100);
const redditPost = `## My So Close So Far Challenge Progress! 🚗
${completionPercent}% completion on this license plate spotting game!
| Category | Progress |
|----------|----------|
| 🇺🇸 US States | ${data.usStates}/51 |
| 🏝️ US Territories | ${data.territories}/5 |
| 🍁 Canada | ${data.canada}/13 |
**Farthest plate:** ${data.farthestState} (${data.maxMiles.toLocaleString()} miles away!)
[Try the game here!](${window.location.href})`;
await copyToClipboard(redditPost);
window.open('https://www.reddit.com/submit', '_blank');
alert('Reddit post copied! Paste it when creating your post.');
}
async function copyShareText() {
const text = generateShareText();
await copyToClipboard(text);
alert('Share text copied to clipboard!');
document.querySelector('.share-modal')?.remove();
}
async function copyToClipboard(text) {
if (navigator.clipboard) {
await navigator.clipboard.writeText(text);
} else {
const textArea = document.createElement('textarea');
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
}
}
// REPLACE the createShareCardHTML function with this fixed version
function createShareCardHTML(data) {
const totalLogged = data.usStates + data.territories + data.canada;
const completionPercent = Math.round((totalLogged / 69) * 100);
const usPercent = Math.round((data.usStates / 51) * 100);
const territoryPercent = Math.round((data.territories / 5) * 100);
const canadaPercent = Math.round((data.canada / 13) * 100);
// Format all numbers properly
const formattedTotalMiles = Math.round(data.totalMiles).toLocaleString();
const formattedMaxMiles = Math.round(data.maxMiles).toLocaleString();
const formattedEstTravel = Math.round(data.estimatedTravel).toLocaleString();
return `
<div class="share-card" style="width: 400px; background: white; border-radius: 16px; box-shadow: 0 8px 32px rgba(0,0,0,0.12); overflow: hidden; margin: 20px auto; font-family: 'Segoe UI', Roboto, Arial, sans-serif;">
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px; text-align: center; position: relative;">
<h2 style="margin: 0 0 8px 0; font-size: 24px; font-weight: 600;">So Close, So Far</h2>
<p style="margin: 0; opacity: 0.9; font-size: 16px;">${completionPercent}% Complete • ${formattedTotalMiles} Miles</p>
<div style="position: absolute; bottom: 5px; right: 10px; font-size: 10px; color: rgba(255,255,255,0.7);">LicensePlateGame.com</div>
</div>
<div style="padding: 20px;">
<div style="margin-bottom: 15px;">
<div style="display: flex; justify-content: space-between; margin-bottom: 6px; font-size: 14px; font-weight: 500;">
<span>US States</span><span>${data.usStates}/51</span>
</div>
<div style="height: 8px; background: #e9ecef; border-radius: 4px; overflow: hidden;">
<div style="height: 100%; background: #28a745; border-radius: 4px; width: ${usPercent}%; transition: width 0.3s ease;"></div>
</div>
</div>
<div style="margin-bottom: 15px;">
<div style="display: flex; justify-content: space-between; margin-bottom: 6px; font-size: 14px; font-weight: 500;">
<span>US Territories</span><span>${data.territories}/5</span>
</div>
<div style="height: 8px; background: #e9ecef; border-radius: 4px; overflow: hidden;">
<div style="height: 100%; background: #007bff; border-radius: 4px; width: ${territoryPercent}%; transition: width 0.3s ease;"></div>
</div>
</div>
<div style="margin-bottom: 15px;">
<div style="display: flex; justify-content: space-between; margin-bottom: 6px; font-size: 14px; font-weight: 500;">
<span>Canada</span><span>${data.canada}/13</span>
</div>
<div style="height: 8px; background: #e9ecef; border-radius: 4px; overflow: hidden;">
<div style="height: 100%; background: #dc3545; border-radius: 4px; width: ${canadaPercent}%; transition: width 0.3s ease;"></div>
</div>
</div>
</div>
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px; padding: 0 20px 20px 20px;">
<div style="text-align: center; padding: 15px 10px; background: #f8f9fa; border-radius: 8px;">
<div style="font-size: 24px; font-weight: 700; color: #2c3e50; margin-bottom: 4px;">${data.farthestState}</div>
<div style="font-size: 12px; color: #666; font-weight: 500;">Farthest</div>
</div>
<div style="text-align: center; padding: 15px 10px; background: #f8f9fa; border-radius: 8px;">
<div style="font-size: 24px; font-weight: 700; color: #2c3e50; margin-bottom: 4px;">${formattedMaxMiles}</div>
<div style="font-size: 12px; color: #666; font-weight: 500;">Max Miles</div>
</div>
<div style="text-align: center; padding: 15px 10px; background: #f8f9fa; border-radius: 8px;">
<div style="font-size: 24px; font-weight: 700; color: #2c3e50; margin-bottom: 4px;">${data.days}</div>
<div style="font-size: 12px; color: #666; font-weight: 500;">Days</div>
</div>
<div style="text-align: center; padding: 15px 10px; background: #f8f9fa; border-radius: 8px;">
<div style="font-size: 24px; font-weight: 700; color: #2c3e50; margin-bottom: 4px;">${formattedEstTravel}</div>
<div style="font-size: 12px; color: #666; font-weight: 500;">Est. Traveled</div>
</div>
</div>
<div style="padding: 15px 20px; background: #f8f9fa; text-align: center; border-top: 1px solid #e9ecef;">
<a href="#" style="color: #667eea; text-decoration: none; font-weight: 600; font-size: 14px;">Start Your Journey →</a>
</div>
</div>
`;
}
function showShareModal() {
const data = generateShareCard();
const shareCardHTML = createShareCardHTML(data);
const modal = document.createElement('div');
modal.style.cssText = `
position: fixed; top: 0; left: 0; width: 100%; height: 100%;
background: rgba(0,0,0,0.5); display: flex; align-items: center;
justify-content: center; z-index: 10000;
font-family: 'Segoe UI', Roboto, Arial, sans-serif;
`;
modal.innerHTML = `
<div style="background: white; border-radius: 16px; max-width: 500px; max-height: 90vh; overflow-y: auto; position: relative;">
<div style="padding: 20px; text-align: center; border-bottom: 1px solid #e9ecef;">
<h3 style="margin: 0; color: #2c3e50;">Share Your Progress</h3>
<button onclick="this.closest('.share-modal').remove()" style="position: absolute; top: 15px; right: 15px; background: none; border: none; font-size: 24px; cursor: pointer; color: #666;">×</button>
</div>
<div style="padding: 20px;">
${shareCardHTML}
<div style="margin: 20px 0; text-align: center;">
<h4 style="margin-bottom: 15px; color: #2c3e50;">Choose Platform:</h4>
<div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; max-width: 240px; margin: 0 auto;">
<button onclick="shareToFacebook()" title="Post to Facebook" style="
background: #1877f2; color: white; border: none;
width: 50px; height: 50px; border-radius: 8px; cursor: pointer;
display: flex; align-items: center; justify-content: center;
transition: background 0.2s ease;
" onmouseover="this.style.background='#166fe5'" onmouseout="this.style.background='#1877f2'">
<i class="fab fa-facebook-f" style="font-size: 20px;"></i>
</button>
<button onclick="shareToInstagram()" title="Share to Instagram" style="
background: linear-gradient(45deg, #f09433 0%,#e6683c 25%,#dc2743 50%,#cc2366 75%,#bc1888 100%);
color: white; border: none;
width: 50px; height: 50px; border-radius: 8px; cursor: pointer;
display: flex; align-items: center; justify-content: center;
transition: opacity 0.2s ease;
" onmouseover="this.style.opacity='0.9'" onmouseout="this.style.opacity='1'">
<i class="fab fa-instagram" style="font-size: 20px;"></i>
</button>
<button onclick="shareToX()" title="Tweet" style="
background: #000000; color: white; border: none;
width: 50px; height: 50px; border-radius: 8px; cursor: pointer;
display: flex; align-items: center; justify-content: center;
transition: background 0.2s ease;
" onmouseover="this.style.background='#333'" onmouseout="this.style.background='#000'">
<i class="fab fa-x-twitter" style="font-size: 20px;"></i>
</button>
<button onclick="shareToThreads()" title="Post to Threads" style="
background: #000000; color: white; border: none;
width: 50px; height: 50px; border-radius: 8px; cursor: pointer;
display: flex; align-items: center; justify-content: center;
transition: background 0.2s ease;
" onmouseover="this.style.background='#333'" onmouseout="this.style.background='#000'">
<i class="fab fa-threads" style="font-size: 20px;"></i>
</button>
<button onclick="shareToReddit()" title="Post to Reddit" style="
background: #ff4500; color: white; border: none;
width: 50px; height: 50px; border-radius: 8px; cursor: pointer;
display: flex; align-items: center; justify-content: center;
transition: background 0.2s ease;
" onmouseover="this.style.background='#e63e00'" onmouseout="this.style.background='#ff4500'">
<i class="fab fa-reddit-alien" style="font-size: 20px;"></i>
</button>
<button onclick="copyShareText()" title="Copy text" style="
background: #6c757d; color: white; border: none;
width: 50px; height: 50px; border-radius: 8px; cursor: pointer;
display: flex; align-items: center; justify-content: center;
transition: background 0.2s ease;
" onmouseover="this.style.background='#5a6268'" onmouseout="this.style.background='#6c757d'">
<i class="fas fa-clipboard" style="font-size: 20px;"></i>
</button>
</div>
</div>
</div>
</div>
`;
modal.className = 'share-modal';
document.body.appendChild(modal);
modal.addEventListener('click', (e) => {
if (e.target === modal) modal.remove();
});
}
// Social Media Sharing Functions
function fallbackCopyText(text) {
const textArea = document.createElement('textarea');
textArea.value = text;
textArea.style.position = 'fixed';
textArea.style.left = '-999999px';
textArea.style.top = '-999999px';
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
document.execCommand('copy');
alert('Share text copied to clipboard!');
document.querySelector('.share-modal')?.remove();
} catch (err) {
alert('Copy failed. Here\'s your share text:\n\n' + text);
}
document.body.removeChild(textArea);
}
// ADD THESE FUNCTIONS AFTER COMBINING PARTS 1 AND 2
// Theme and Map Functions
function initializeRandomColors() {
const saved = localStorage.getItem('randomColorAssignments');
if (saved) {
randomColorAssignments = JSON.parse(saved);
const colors = THEMES.random.colors;
let needsUpdate = false;
for (const state of ALL_STATES) {
if (!randomColorAssignments[state]) {
randomColorAssignments[state] = colors[Math.floor(Math.random() * colors.length)];
needsUpdate = true;
}
}
if (needsUpdate) {
localStorage.setItem('randomColorAssignments', JSON.stringify(randomColorAssignments));
}
} else {
const colors = THEMES.random.colors;
for (const state of ALL_STATES) {
randomColorAssignments[state] = colors[Math.floor(Math.random() * colors.length)];
}
localStorage.setItem('randomColorAssignments', JSON.stringify(randomColorAssignments));
}
}
function getStateStyle(stateName, isLogged) {
const theme = THEMES[currentTheme];
if (!isLogged) {
return {
fillColor: theme.unloggedColor,
fillOpacity: 0.7,
color: "#666",
weight: 1
};
}
let fillColor;
switch (currentTheme) {
case 'classic':
fillColor = theme.loggedColor;
break;
case 'random':
fillColor = randomColorAssignments[stateName] || theme.colors[0];
break;
case 'flag':
const assignment = getFlagAssignment(stateName);
if (CANADIAN_REGIONS.includes(stateName)) {
fillColor = theme.canada_patterns[assignment] || theme.canada_patterns.red;
} else {
fillColor = theme.us_patterns[assignment] || theme.us_patterns.blue;
}
break;
default:
fillColor = theme.loggedColor || "#28a745";
}
return {
fillColor: fillColor,
fillOpacity: 0.8,
color: "#333",
weight: 2
};
}
function createSVGPatterns() {
if (document.getElementById('mapPatterns')) return;
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.id = 'mapPatterns';
svg.style.position = 'absolute';
svg.style.width = '0';
svg.style.height = '0';
document.body.appendChild(svg);
const defs = document.createElementNS("http://www.w3.org/2000/svg", "defs");
svg.appendChild(defs);
const stripesPattern = document.createElementNS("http://www.w3.org/2000/svg", "pattern");
stripesPattern.id = 'red_white_stripes';
stripesPattern.setAttribute('patternUnits', 'userSpaceOnUse');
stripesPattern.setAttribute('width', '10');
stripesPattern.setAttribute('height', '10');
const rect1 = document.createElementNS("http://www.w3.org/2000/svg", "rect");
rect1.setAttribute('width', '10');
rect1.setAttribute('height', '5');
rect1.setAttribute('fill', '#dc3545');
stripesPattern.appendChild(rect1);
const rect2 = document.createElementNS("http://www.w3.org/2000/svg", "rect");
rect2.setAttribute('y', '5');
rect2.setAttribute('width', '10');
rect2.setAttribute('height', '5');
rect2.setAttribute('fill', '#ffffff');
stripesPattern.appendChild(rect2);
defs.appendChild(stripesPattern);
}
async function loadAllStates() {
const log = loadPlateLog();
const loggedStates = new Set(Object.keys(log));
const statesToLoad = MAIN_MAP_STATES.slice();
const batchSize = 8;
for (let i = 0; i < statesToLoad.length; i += batchSize) {
const batch = statesToLoad.slice(i, i + batchSize);
await Promise.all(batch.map(async (stateName) => {
try {
if (!stateCache[stateName]) {
const response = await fetch(`state_jsons/${stateName}.json`);
if (!response.ok) throw new Error(`Failed to load ${stateName}`);
stateCache[stateName] = await response.json();
}
const geoJsonData = stateCache[stateName];
if (geoJsonData && geoJsonData.geojson) {
const isLogged = loggedStates.has(stateName);
const style = getStateStyle(stateName, isLogged);
const layer = L.geoJSON(geoJsonData.geojson, {
style: style,
onEachFeature: (feature, layer) => {
const displayName = stateName.replace(/_/g, " ").replace(/\b\w/g, c => c.toUpperCase());
layer.bindPopup(`<strong>${displayName}</strong><br>${isLogged ? 'Found!' : 'Not found yet'}`);
}
});
if (currentMap) {
layer.addTo(currentMap);
stateMapLayers[stateName] = layer;
}
}
} catch (error) {
console.error(`Failed to load state ${stateName}:`, error);
}
}));
if (i + batchSize < statesToLoad.length) {
await new Promise(resolve => setTimeout(resolve, 150));
}
}
}
function updateMapColors(log) {
const loggedStates = new Set(Object.keys(log));
for (const [stateName, layer] of Object.entries(stateMapLayers)) {
if (layer && currentMap && currentMap.hasLayer(layer)) {
const isLogged = loggedStates.has(stateName);
const style = getStateStyle(stateName, isLogged);
layer.setStyle(style);
// SAFE popup update - check if popup exists first
const displayName = stateName.replace(/_/g, " ").replace(/\b\w/g, c => c.toUpperCase());
const popupContent = `<strong>${displayName}</strong><br>${isLogged ? 'Found!' : 'Not found yet'}`;
if (layer.getPopup()) {
layer.getPopup().setContent(popupContent);
} else {
// Create popup if it doesn't exist
layer.bindPopup(popupContent);
}
}
}
}
function updateLegendVisibility() {
const legend = document.getElementById('mapLegend');
if (legend) {
legend.style.display = THEMES[currentTheme].showLegend ? 'block' : 'none';
}
}
function updateTerritoriesSidebar(log) {
const territoriesList = document.getElementById("territoriesList");
if (!territoriesList) return;
const loggedStates = new Set(Object.keys(log));
let html = "";
for (const territory of REMOTE_TERRITORIES) {
const isLogged = loggedStates.has(territory);
const displayName = territory.replace(/_/g, " ").replace(/\b\w/g, c => c.toUpperCase());
const className = isLogged ? "logged" : "not-logged";
let style = "";
if (isLogged) {
switch (currentTheme) {
case 'classic':
style = 'style="background-color: #d4edda; border-color: #28a745;"';
break;
case 'random':
const color = randomColorAssignments[territory] || THEMES.random.colors[0];
style = `style="background-color: ${color}; border-color: ${color}; color: white;"`;
break;
case 'flag':
const assignment = getFlagAssignment(territory);
let bgColor = THEMES.flag.us_patterns[assignment] || THEMES.flag.us_patterns.blue;
style = `style="background-color: ${bgColor}; border-color: ${bgColor}; color: white;"`;
break;
}
}
html += `<div class="territory-item ${className}" ${style}>${displayName}</div>`;
}
territoriesList.innerHTML = html;
}
async function initializeMap() {
const mapContainer = document.getElementById('map');
if (currentMap) {
try {
currentMap.remove();
currentMap = null;
stateMapLayers = {};
} catch (e) {
console.log("Error removing existing map:", e);
}
}
mapContainer.innerHTML = '';
try {
currentMap = L.map('map').setView(MAP_CENTER, MAP_ZOOM);
L.tileLayer('https://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}{r}.png', {
attribution: '© OpenStreetMap contributors © CARTO',
subdomains: 'abcd',
maxZoom: 19
}).addTo(currentMap);
await loadAllStates();
} catch (error) {
console.error("Error creating map:", error);
mapContainer.innerHTML = '<p style="padding: 20px; text-align: center; color: red;">Map could not be loaded. Please refresh the page.</p>';
}
}
async function processLocation(latitude, longitude, stateName) {
try {
const { label, stateName: currentState } = await getLocationLabel(latitude, longitude);
const miles = (stateName === currentState)
? 0
: await checkProximity(stateName, latitude, longitude);
if (miles === null) {
throw new Error(`Could not calculate distance to ${stateName}`);
}
const log = updatePlateLog(stateName, label, miles);
renderTable(log);
updateMapColors(log);
updateTerritoriesSidebar(log);
if (animationEnabled && !isAnimating) {
animateToState(stateName);
} else {
setTimeout(recenterMap, 500);
}
const displayName = stateName.replace(/_/g, " ").replace(/\b\w/g, c => c.toUpperCase());
alert(`${displayName} logged! Distance: ${Math.round(miles)} miles from ${label}`);
} catch (error) {
console.error("Error processing location:", error);
alert(`Error: ${error.message}`);
}
document.getElementById("stateSelect").value = "";
}
async function handleManualLocation(stateName) {
const locationInput = prompt(
"Enter your location:\n\n" +
"• City, State (e.g., 'New York, NY')\n" +
"• ZIP code (e.g., '10001')\n" +
"• Latitude, Longitude (e.g., '40.7128, -74.0060')"
);
if (!locationInput) return;
try {
const coordMatch = locationInput.match(/^(-?\d+\.?\d*),\s*(-?\d+\.?\d*)$/);
if (coordMatch) {
const lat = parseFloat(coordMatch[1]);
const lng = parseFloat(coordMatch[2]);
if (Math.abs(lat) <= 90 && Math.abs(lng) <= 180) {
await processLocation(lat, lng, stateName);
return;
}
}
const coords = await geocodeAddress(locationInput);
await processLocation(coords.lat, coords.lng, stateName);
} catch (error) {
alert("Could not find that location. Please try again with a different format.");
console.error("Manual location error:", error);
}
}
async function geocodeAddress(address) {
const url = `https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(address)}&limit=1`;
try {
const response = await fetch(url);
if (!response.ok) throw new Error(`Geocoding failed: ${response.status}`);
const data = await response.json();
if (data.length === 0) {
throw new Error("Location not found");
}
return {
lat: parseFloat(data[0].lat),
lng: parseFloat(data[0].lon)
};
} catch (error) {
throw new Error(`Failed to geocode address: ${error.message}`);
}
}
async function getLocationLabel(lat, lon) {
const url = `https://nominatim.openstreetmap.org/reverse?format=json&lat=${lat}&lon=${lon}`;
try {
const res = await fetch(url);
if (!res.ok) throw new Error(`Geocoding failed: ${res.status}`);
const data = await res.json();
// Enhanced fallback strategy for location naming
const city = data.address?.city || data.address?.town || data.address?.village || data.address?.hamlet || "";
const county = data.address?.county || "";
const state = data.address?.state || data.address?.region || "";
// Build location label with fallbacks
let locationLabel;
if (city && state) {
locationLabel = `${city}, ${state}`;
} else if (county && state) {
locationLabel = `${county} County, ${state}`;
} else if (state) {
locationLabel = state;
} else {
locationLabel = `${lat.toFixed(3)}°, ${lon.toFixed(3)}°`;
}
return { label: locationLabel, stateName: state.toLowerCase().replace(/\s+/g, "_") };
} catch (err) {
console.warn("Reverse geocoding failed:", err.message);
return { label: "Unknown location", stateName: "" };
}
}
function renderTable(log) {
const result = document.getElementById("result");
const entries = Object.entries(log).sort(([a], [b]) => a.localeCompare(b));
const progressBar = document.getElementById("progressBar");
const progressText = document.getElementById("progressText");
const loggedCount = Object.keys(log).length;
const totalCount = ALL_STATES.length;
if (progressBar) progressBar.value = loggedCount;
if (progressText) progressText.textContent = `${loggedCount} of ${totalCount} plates logged`;
let html = `
<table>
<thead>
<tr>
<th>License Plate</th>
<th>Your Location</th>
<th style="text-align: right;">Miles</th>
<th> </th>
</tr>
</thead>
<tbody>
`;
for (const [state, { location, miles }] of entries) {
const label = state.replace(/_/g, " ").replace(/\b\w/g, c => c.toUpperCase());
html += `
<tr>
<td>${label}</td>
<td>${location}</td>
<td style="text-align: right;">${formatNumber(miles)}</td>
<td><button class="removeBtn" data-state="${state}">X</button></td>
</tr>
`;
}
const loggedStates = new Set(Object.keys(log));
const missingStates = ALL_STATES.filter(s => !loggedStates.has(s));
const missingLabels = missingStates
.map(s => s.replace(/_/g, " ").replace(/\b\w/g, c => c.toUpperCase()))
.join(", ");
html += `
<tr><td colspan="4"><strong>Plates not yet logged:</strong> ${missingLabels}</td></tr>
<tr><td colspan="4"><strong>Total Score:</strong> ${formatNumber(getTotalScore(log))} miles</td></tr>
</tbody>
</table>