+
+/*
+ * Called to interpolate JavaScript variables in strings using a
+ * similar syntax to rails I18n string interpolation - the only
+ * difference is that [[foo]] is the placeholder syntax instead
+ * of {{foo}} which allows the same string to be processed by both
+ * rails and then later by javascript.
+ */
+function i18n(string, keys) {
+ for (var key in keys) {
+ var re_key = '\\[\\[' + key + '\\]\\]';
+ var re = new RegExp(re_key, "g");
+
+ string = string.replace(re, keys[key]);
+ }
+
+ return string;
+}
+
+function makeShortCode(lat, lon, zoom) {
+ char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@";
+ var x = Math.round((lon + 180.0) * ((1 << 30) / 90.0));
+ var y = Math.round((lat + 90.0) * ((1 << 30) / 45.0));
+ // hack around the fact that JS apparently only allows 53-bit integers?!?
+ // note that, although this reduces the accuracy of the process, it's fine for
+ // z18 so we don't need to care for now.
+ var c1 = 0, c2 = 0;
+ for (var i = 31; i > 16; --i) {
+ c1 = (c1 << 1) | ((x >> i) & 1);
+ c1 = (c1 << 1) | ((y >> i) & 1);
+ }
+ for (var i = 16; i > 1; --i) {
+ c2 = (c2 << 1) | ((x >> i) & 1);
+ c2 = (c2 << 1) | ((y >> i) & 1);
+ }
+ var str = "";
+ for (var i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
+ digit = (c1 >> (24 - 6 * i)) & 0x3f;
+ str += char_array.charAt(digit);
+ }
+ for (var i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
+ digit = (c2 >> (24 - 6 * (i - 5))) & 0x3f;
+ str += char_array.charAt(digit);
+ }
+ for (var i = 0; i < ((zoom + 8) % 3); ++i) {
+ str += "=";
+ }
+ return str;
+}