var mapzen = mapzen || {};
mapzen.whosonfirst = mapzen.whosonfirst || {};

mapzen.whosonfirst.net = (function(){

	var default_cache_ttl = 30000; // ms

	var self = {

		'encode_query': function(query){

			enc = new Array();

			for (var k in query){
				var v = query[k];
				v = encodeURIComponent(v);
				enc.push(k + "=" + v);
			}

			return enc.join("&");
		},

		'fetch': function(url, on_success, on_fail, args){

		    	if (typeof(args) == "undefined") {
			    args = {};
			}

		    	// this is here for backwards compatibility
		    	// (20170113/thisisaaronland)

		    	else if (typeof(args) == "number") {
			    args = { "cache_ttl": args };
			}

		    	else {}

			if (args["cache_ttl"]){
			    args["cache_ttl"] = default_cache_ttl;
			}

		        else {
			     cache_ttl = default_cache_ttl;
			}

			var on_hit = function(data){
				mapzen.whosonfirst.log.debug("[cached] fetch " + url);
				if (on_success){
					on_success(data);
				}
			};

			var on_miss = function(){
				mapzen.whosonfirst.log.debug("[xhr] fetch " + url);
				self.fetch_with_xhr(url, on_success, on_fail, args);
			};

			if (! self.cache_get(url, on_hit, on_miss, cache_ttl)){
				self.fetch_with_xhr(url, on_success, on_fail, args);
			}
		},

		'fetch_with_xhr': function(url, on_success, on_fail, args){

			if (! args){
			    args = {};
			}

			var req = new XMLHttpRequest();

			req.onload = function(){

				try {
					var data = JSON.parse(this.responseText);
				}

				catch (e){
					mapzen.whosonfirst.log.error("failed to parse " + url + ", because " + e);

					if (on_fail){
						on_fail({
							url: url,
							args: args,
							xhr: req
						});
					}

					return false;
				}

				self.cache_set(url, data);

				if (on_success){
					on_success(data);
				}
			};

			try {

			    	if (args["cache-busting"]){

				    var cb = Math.floor(Math.random() * 1000000);

				    var tmp = document.createElement("a");
				    tmp.href = url;

				    if (tmp.search){
					tmp.search += "&cb=" + cb;
				    }

				    else {
					tmp.search = "?cb= " + cb;
				    }

				    url = tmp.href;
				}

			    	// console.log("ARGS " + args);
			    	// console.log("URL " + url);

				req.open("get", url, true);
				req.send();
			}

			catch(e){

				mapzen.whosonfirst.log.error("failed to fetch " + url + ", because ");
				mapzen.whosonfirst.log.debug(e);

				if (on_fail){
					on_fail();
				}
			}
		},

		'cache_get': function(key, on_hit, on_miss, cache_ttl){

			if (typeof(localforage) != 'object'){
				return false;
			}

			var fq_key = self.cache_prep_key(key);

			localforage.getItem(fq_key, function (err, rsp){

				if ((err) || (! rsp)){
					on_miss();
					return;
				}

				var data = rsp['data'];

				if (! data){
					on_miss();
					return;
				}

				var dt = new Date();
				var ts = dt.getTime();

				var then = rsp['created'];
				var diff = ts - then;

				if (diff > cache_ttl){
					self.cache_unset(key);
					on_miss();
					return;
				}

				on_hit(data);
			});

			return true;
		},

		'cache_set': function(key, value){

			if (typeof(localforage) != 'object'){
				return false;
			}

			var dt = new Date();
			var ts = dt.getTime();

			var wrapper = {
				'data': value,
				'created': ts
			};

			key = self.cache_prep_key(key);

			localforage.setItem(key, wrapper);
			return true;
		},

		'cache_unset': function(key){

			if (typeof(localforage) != 'object'){
				return false;
			}

			key = self.cache_prep_key(key);

			localforage.removeItem(key);
			return true;
		},

		'cache_prep_key': function(key){
			return key + '#mapzen.whosonfirst.net';
		}
	};

	return self;

})();

@@ -144,16 +144,48 @@
 0; // ms
+%0A    %09var disable_cache = false;
 %0A%0A%09var s
@@ -3150,33 +3150,90 @@
 = 'object')%7B%0A%09%09%09
-%09
+    return false;%0A%09%09%09%7D%0A%0A%09%09    %09if (disable_cache)%7B%0A%09%09%09    
 return false;%0A%09%09
@@ -3422,17 +3422,239 @@
 wrapper)
-;
+.then(function(v)%7B%0A%09%09%09    // woo woo%0A%09%09%09%7D).catch(function(err)%7B%0A%09%09%09    %0A%09%09%09    // https://github.com/whosonfirst/whosonfirst-www-spelunker/issues/126%0A%0A%09%09%09    if (err%5B'code'%5D == 4)%7B%0A%09%09%09%09disable_cache = true;%0A%09%09%09    %7D%0A%09%09%09%7D);%0A
 %0A%09%09%09retu

disable cache sets in net.js per issue #126; we probably need to update everything that uses localforage to use a generic WOF cache wrapper for disabling things across the board...