1
|
|
|
/*jslint |
2
|
|
|
indent: 4 |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
/*global |
6
|
|
|
$, google, mytrans, window, Cookies, Coordinates, get_cookie_string, console |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
var Okapi = {}; |
11
|
|
|
Okapi.m_map = null; |
12
|
|
|
Okapi.m_sites = null; |
13
|
|
|
Okapi.m_ready = false; |
14
|
|
|
Okapi.m_showCache = null; |
15
|
|
|
Okapi.m_enabled = false; |
16
|
|
|
Okapi.m_popup = null; |
17
|
|
|
Okapi.m_marker = null; |
18
|
|
|
Okapi.m_icons = null; |
19
|
|
|
Okapi.m_timer = null; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
Okapi.init = function (themap) { |
23
|
|
|
'use strict'; |
24
|
|
|
|
25
|
|
|
this.m_map = themap; |
26
|
|
|
}; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
Okapi.parseLocation = function (s) { |
30
|
|
|
'use strict'; |
31
|
|
|
|
32
|
|
|
var loc = s.split("|"), |
33
|
|
|
lat = parseFloat(loc[0]), |
34
|
|
|
lng = parseFloat(loc[1]); |
35
|
|
|
|
36
|
|
|
if (Coordinates.valid(lat, lng)) { |
37
|
|
|
return new google.maps.LatLng(lat, lng); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return null; |
41
|
|
|
}; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
Okapi.setShowCache = function (code) { |
45
|
|
|
'use strict'; |
46
|
|
|
|
47
|
|
|
this.m_showCache = code; |
48
|
|
|
}; |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
Okapi.setupSites = function () { |
52
|
|
|
'use strict'; |
53
|
|
|
|
54
|
|
|
if (this.m_sites) { |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
var self = this, |
59
|
|
|
main_url = "http://www.opencaching.pl/okapi/services/apisrv/installations", |
60
|
|
|
keys = { |
61
|
|
|
"Opencaching.DE" : "YSqPufH82encfJ67ZxV2", |
62
|
|
|
"Opencaching.PL" : "jhRyc6rGmT6XEvxva29B", |
63
|
|
|
"Opencaching.NL" : "gcwaesuq3REu8RtCgLDj", |
64
|
|
|
"Opencaching.US" : "GvgyCMvwfH42GqJGL494", |
65
|
|
|
"Opencaching.ORG.UK" : "7t7VfpkCd4HuxPabfbHd", |
66
|
|
|
"Opencaching.RO" : "gqSWmVJhZGDwc4sRhyy7" |
67
|
|
|
}; |
68
|
|
|
|
69
|
|
|
this.m_sites = {}; |
70
|
|
|
|
71
|
|
|
$.ajax({ |
72
|
|
|
url: main_url, |
73
|
|
|
dataType: 'json', |
74
|
|
|
success: function (response) { |
75
|
|
|
var siteid, site; |
76
|
|
|
for (siteid in response) { |
77
|
|
|
site = response[siteid]; |
78
|
|
|
if (site.site_name in keys) { |
79
|
|
|
console.log("adding OC site: " + site.site_name); |
|
|
|
|
80
|
|
|
self.m_sites[siteid] = { |
81
|
|
|
siteid: siteid, |
82
|
|
|
name: site.site_name, |
83
|
|
|
site_url: site.site_url, |
84
|
|
|
url: site.okapi_base_url, |
85
|
|
|
key: keys[site.site_name], |
86
|
|
|
ignore_user: null, |
87
|
|
|
markers: {}, |
88
|
|
|
finished: true |
89
|
|
|
}; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
self.m_ready = true; |
94
|
|
|
if (self.m_enabled) { |
95
|
|
|
self.scheduleLoad(true); |
96
|
|
|
} |
97
|
|
|
if (self.m_showCache && self.m_showCache !== "") { |
98
|
|
|
self.centerMap(self.m_showCache); |
99
|
|
|
self.m_showCache = null; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
}); |
103
|
|
|
}; |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
Okapi.setupIcons = function () { |
107
|
|
|
'use strict'; |
108
|
|
|
|
109
|
|
|
if (this.m_icons) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
this.m_icons = {}; |
114
|
|
|
this.m_icons["Other"] = new google.maps.MarkerImage("img/cachetype-1.png"); |
115
|
|
|
this.m_icons["Traditional"] = new google.maps.MarkerImage("img/cachetype-2.png"); |
116
|
|
|
this.m_icons["Multi"] = new google.maps.MarkerImage("img/cachetype-3.png"); |
117
|
|
|
this.m_icons["Virtual"] = new google.maps.MarkerImage("img/cachetype-4.png"); |
118
|
|
|
this.m_icons["Webcam"] = new google.maps.MarkerImage("img/cachetype-5.png"); |
119
|
|
|
this.m_icons["Event"] = new google.maps.MarkerImage("img/cachetype-6.png"); |
120
|
|
|
this.m_icons["Quiz"] = new google.maps.MarkerImage("img/cachetype-7.png"); |
121
|
|
|
this.m_icons["Math/Physics"] = new google.maps.MarkerImage("img/cachetype-8.png"); |
122
|
|
|
this.m_icons["Moving"] = new google.maps.MarkerImage("img/cachetype-9.png"); |
123
|
|
|
this.m_icons["Drive-In"] = new google.maps.MarkerImage("img/cachetype-10.png"); |
124
|
|
|
}; |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
Okapi.icon = function (type) { |
128
|
|
|
'use strict'; |
129
|
|
|
|
130
|
|
|
if (type in this.m_icons) { |
131
|
|
|
return this.m_icons[type]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return this.m_icons["Other"]; |
135
|
|
|
}; |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
Okapi.guessSiteId = function (code) { |
139
|
|
|
'use strict'; |
140
|
|
|
|
141
|
|
|
var site_name = null, |
|
|
|
|
142
|
|
|
siteid = -1; |
|
|
|
|
143
|
|
|
|
144
|
|
|
if (/^OC/i.test(code)) { |
145
|
|
|
site_name = "Opencaching.DE"; |
146
|
|
|
} else if (/^OP/i.test(code)) { |
147
|
|
|
site_name = "Opencaching.PL"; |
148
|
|
|
} else if (/^OB/i.test(code)) { |
149
|
|
|
site_name = "Opencaching.NL"; |
150
|
|
|
} else if (/^OU/i.test(code)) { |
151
|
|
|
site_name = "Opencaching.US"; |
152
|
|
|
} else if (/^OK/i.test(code)) { |
153
|
|
|
site_name = "Opencaching.ORG.UK"; |
154
|
|
|
} else if (/^OR/i.test(code)) { |
155
|
|
|
site_name = "Opencaching.RO"; |
156
|
|
|
} else { |
157
|
|
|
return -1; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
for (siteid in this.m_sites) { |
161
|
|
|
if (this.m_sites[siteid].name === site_name) { |
162
|
|
|
return siteid; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return -1; |
167
|
|
|
}; |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
Okapi.centerMap = function (code) { |
171
|
|
|
'use strict'; |
172
|
|
|
|
173
|
|
|
if (!this.m_ready) { |
174
|
|
|
console.log("okapi not ready"); |
|
|
|
|
175
|
|
|
return; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
var siteid = this.guessSiteId(code); |
179
|
|
|
if (siteid < 0) { |
180
|
|
|
console.log("bad code. cannot determine okapi site"); |
181
|
|
|
return; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
this.showPopup(null, code.toUpperCase(), siteid); |
185
|
|
|
}; |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
Okapi.createPopupContent = function (code, response) { |
189
|
|
|
'use strict'; |
190
|
|
|
|
191
|
|
|
var content = |
192
|
|
|
'<a href="' + response.url + '" target="_blank">' + code + ' <b>' + response.name + '</b></a><br />' |
193
|
|
|
+ '<table>' |
194
|
|
|
+ '<tr><td>' + mytrans("geocache.owner") + '</td><td>' + '<a href="' + response.owner.profile_url + '" target="_blank"><b>' + response.owner.username + '</b></a></td></tr>' |
195
|
|
|
+ '<tr><td>' + mytrans("geocache.type") + '</td><td>' + response.type + '</td></tr>' |
196
|
|
|
+ '<tr><td>' + mytrans("geocache.size") + '</td><td>' + response.size2 + '</td></tr>' |
197
|
|
|
+ '<tr><td>' + mytrans("geocache.status") + '</td><td>' + response.status + '</td></tr>' |
198
|
|
|
+ '<tr><td>' + mytrans("geocache.difficulty") + '</td><td>' + response.difficulty + '/5</td></tr>' |
199
|
|
|
+ '<tr><td>' + mytrans("geocache.terrain") + '</td><td>' + response.terrain + '/5</td></tr>' |
200
|
|
|
+ '<tr><td>' + mytrans("geocache.finds") + '</td><td>' + response.founds + '</td></tr>' |
201
|
|
|
+ '</table>'; |
202
|
|
|
return content; |
203
|
|
|
}; |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
Okapi.showPopup = function (m, code, siteid) { |
207
|
|
|
'use strict'; |
208
|
|
|
|
209
|
|
|
if (!this.m_popup) { |
210
|
|
|
this.m_popup = new google.maps.InfoWindow(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
var self = this, |
214
|
|
|
site = this.m_sites[siteid]; |
215
|
|
|
|
216
|
|
|
$.ajax({ |
217
|
|
|
url: site.url + 'services/caches/geocache', |
218
|
|
|
dataType: 'json', |
219
|
|
|
data: { |
220
|
|
|
'consumer_key': site.key, |
221
|
|
|
'cache_code': code, |
222
|
|
|
'fields' : 'name|type|status|url|owner|founds|size2|difficulty|terrain|location' |
223
|
|
|
}, |
224
|
|
|
success: function (response) { |
225
|
|
|
var coords = self.parseLocation(response.location); |
226
|
|
|
self.m_map.setCenter(coords); |
227
|
|
|
|
228
|
|
|
if (!m) { |
229
|
|
|
m = new google.maps.Marker({ |
230
|
|
|
position: coords, |
231
|
|
|
map: self.m_map, |
232
|
|
|
icon: self.icon(response.type) |
233
|
|
|
}); |
234
|
|
|
if (self.m_maker) { |
235
|
|
|
self.m_marker.setMap(null); |
236
|
|
|
} |
237
|
|
|
self.registerPopup(m, code, siteid); |
238
|
|
|
self.m_marker = m; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
self.m_popup.setContent(self.createPopupContent(code, response)); |
242
|
|
|
self.m_popup.open(self.m_map, m); |
243
|
|
|
} |
244
|
|
|
}); |
245
|
|
|
}; |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
Okapi.registerPopup = function (m, code, siteid) { |
249
|
|
|
'use strict'; |
250
|
|
|
|
251
|
|
|
if (!this.m_ready) { |
252
|
|
|
return; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
var self = this; |
256
|
|
|
|
257
|
|
|
google.maps.event.addListener(m, 'click', function () { |
258
|
|
|
self.showPopup(m, code, siteid); |
259
|
|
|
}); |
260
|
|
|
}; |
261
|
|
|
|
262
|
|
|
|
263
|
|
|
Okapi.removeMarkersSite = function (markers) { |
264
|
|
|
'use strict'; |
265
|
|
|
|
266
|
|
|
if (!this.m_ready) { |
267
|
|
|
return; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
if (markers) { |
271
|
|
|
var m; |
272
|
|
|
for (m in markers) { |
273
|
|
|
markers[m].setMap(null); |
274
|
|
|
delete markers[m]; |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
}; |
278
|
|
|
|
279
|
|
|
|
280
|
|
|
Okapi.removeMarkers = function () { |
281
|
|
|
'use strict'; |
282
|
|
|
|
283
|
|
|
if (!this.m_ready) { |
284
|
|
|
return; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
var siteid; |
288
|
|
|
for (siteid in this.m_sites) { |
289
|
|
|
this.removeMarkersSite(this.m_sites[siteid].markers); |
290
|
|
|
this.m_sites[siteid].markers = {}; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
if (this.m_marker) { |
294
|
|
|
this.m_marker.setMap(null); |
295
|
|
|
delete this.m_marker; |
296
|
|
|
this.m_marker = null; |
297
|
|
|
} |
298
|
|
|
}; |
299
|
|
|
|
300
|
|
|
|
301
|
|
|
Okapi.loadBboxSite = function (siteid) { |
302
|
|
|
'use strict'; |
303
|
|
|
|
304
|
|
|
if (!this.m_ready) { |
305
|
|
|
return; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
var self = this, |
309
|
|
|
site = this.m_sites[siteid], |
310
|
|
|
b, |
311
|
|
|
bbox; |
312
|
|
|
|
313
|
|
|
if (!this.m_enabled) { |
314
|
|
|
site.finished = true; |
315
|
|
|
return; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
if (!site.finished) { |
319
|
|
|
return; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
site.finished = false; |
323
|
|
|
|
324
|
|
|
b = this.m_map.getBounds(); |
325
|
|
|
bbox = b.getSouthWest().lat() + "|" + b.getSouthWest().lng() + "|" + b.getNorthEast().lat() + "|" + b.getNorthEast().lng(); |
326
|
|
|
|
327
|
|
|
$.ajax({ |
328
|
|
|
url: site.url + 'services/caches/shortcuts/search_and_retrieve', |
329
|
|
|
dataType: 'json', |
330
|
|
|
data: { |
331
|
|
|
'consumer_key': site.key, |
332
|
|
|
'search_method': 'services/caches/search/bbox', |
333
|
|
|
'search_params': '{"bbox" : "' + bbox + '", "limit" : "500"}', |
334
|
|
|
'retr_method': 'services/caches/geocaches', |
335
|
|
|
'retr_params': '{"fields": "code|name|location|type|status|url"}', |
336
|
|
|
'wrap': 'false' |
337
|
|
|
}, |
338
|
|
|
success: function (response) { |
339
|
|
|
var addedCaches = {}, |
340
|
|
|
code, |
341
|
|
|
cache, |
342
|
|
|
m; |
343
|
|
|
|
344
|
|
|
for (code in response) { |
345
|
|
|
cache = response[code]; |
346
|
|
|
|
347
|
|
|
if (cache.status !== "Available") { |
348
|
|
|
continue; |
349
|
|
|
} |
350
|
|
|
addedCaches[cache.code] = true; |
351
|
|
|
if (cache.code in site.markers) { |
352
|
|
|
continue; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
m = new google.maps.Marker({ |
356
|
|
|
position: self.parseLocation(cache.location), |
357
|
|
|
map: self.m_map, |
358
|
|
|
icon: self.icon(cache.type) |
359
|
|
|
}); |
360
|
|
|
|
361
|
|
|
site.markers[cache.code] = m; |
362
|
|
|
self.registerPopup(m, cache.code, siteid); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
for (m in site.markers) { |
366
|
|
|
if (!(m in addedCaches)) { |
367
|
|
|
site.markers[m].setMap(null); |
368
|
|
|
delete site.markers[m]; |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
site.finished = true; |
372
|
|
|
}, |
373
|
|
|
error: function () { |
374
|
|
|
console.log("okapi request failed: " + site.name); |
|
|
|
|
375
|
|
|
self.removeMarkersSite(site.markers); |
376
|
|
|
site.markers = {}; |
377
|
|
|
site.finished = true; |
378
|
|
|
} |
379
|
|
|
}); |
380
|
|
|
}; |
381
|
|
|
|
382
|
|
|
|
383
|
|
|
Okapi.loadBbox = function () { |
384
|
|
|
'use strict'; |
385
|
|
|
|
386
|
|
|
if (!this.m_ready) { |
387
|
|
|
return; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
var siteid; |
391
|
|
|
for (siteid in this.m_sites) { |
392
|
|
|
this.loadBboxSite(siteid); |
393
|
|
|
} |
394
|
|
|
}; |
395
|
|
|
|
396
|
|
|
|
397
|
|
|
Okapi.unscheduleLoad = function () { |
398
|
|
|
'use strict'; |
399
|
|
|
|
400
|
|
|
if (!this.m_ready) { |
401
|
|
|
return; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
if (this.m_timer) { |
405
|
|
|
window.clearTimeout(this.m_timer); |
406
|
|
|
this.m_timer = null; |
407
|
|
|
} |
408
|
|
|
}; |
409
|
|
|
|
410
|
|
|
|
411
|
|
|
Okapi.scheduleLoad = function () { |
412
|
|
|
'use strict'; |
413
|
|
|
|
414
|
|
|
if (!this.m_ready) { |
415
|
|
|
return; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
this.unscheduleLoad(); |
419
|
|
|
this.m_timer = window.setTimeout('Okapi.loadBbox()', 500); |
|
|
|
|
420
|
|
|
}; |
421
|
|
|
|
422
|
|
|
|
423
|
|
|
Okapi.toggle = function (t) { |
424
|
|
|
'use strict'; |
425
|
|
|
|
426
|
|
|
Cookies.set('load_caches', t ? "1" : "0", {expires: 30}); |
427
|
|
|
if ($('#geocaches').is(':checked') !== t) { |
428
|
|
|
$('#geocaches').attr('checked', t); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
if (this.m_enabled !== t) { |
432
|
|
|
this.m_enabled = t; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
if (this.m_enabled) { |
436
|
|
|
this.setupIcons(); |
437
|
|
|
this.setupSites(); |
438
|
|
|
this.scheduleLoad(); |
439
|
|
|
} else { |
440
|
|
|
this.unscheduleLoad(); |
441
|
|
|
this.removeMarkers(); |
442
|
|
|
} |
443
|
|
|
}; |
444
|
|
|
|
445
|
|
|
|
446
|
|
|
Okapi.restore = function (defaultValue) { |
447
|
|
|
'use strict'; |
448
|
|
|
|
449
|
|
|
var state = get_cookie_string("load_caches", "invalid"); |
450
|
|
|
|
451
|
|
|
if (state === "0") { |
452
|
|
|
this.toggle(false); |
453
|
|
|
} else if (state === "1") { |
454
|
|
|
this.toggle(true); |
455
|
|
|
} else { |
456
|
|
|
this.toggle(defaultValue); |
457
|
|
|
} |
458
|
|
|
}; |
459
|
|
|
|