GeoTest = window.GeoTest || {}
GeoTest.data = {
	FILE: "output.json",
	countries: {},
	recentWrong: [],
	emptying: false,
	preferences: {},
	cookies: {
		createCookie: function (name,value,days) {
			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
			}
			else var expires = "";
			document.cookie = name+"="+value+expires+"; path=/";
		}, 
		readCookie: function (name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		}
	},
	reload: function (onfinish) {
		$.get(this.FILE, function(data) {
			GeoTest.data.countries = JSON.parse(data);
			onfinish();
		});
	},
	isArray: function (o) {
		return Object.prototype.toString.call(o) === "[object Array]";
	},
	setPref: function (k, v) {
		this.preferences[k] = v;
		
		this.cookies.createCookie("prefs", JSON.stringify(this.preferences), 365 * 10);
	},
	pref: function (k) {
		return this.preferences[k];
	},
	cleanName: function (a) {
		a = a.toLowerCase();
		
		var len = a.length;
		
		if (a.substring(len - 5, len) == " city") {
			a = a.substring(0, len - 5);
		}
		
		a = a.replace(" ", "").replace("í", "i").replace("ã", "a").replace("õ", "o").replace("á", "a")
		
		return a;
	},
	compare: function (a, b) {
		return this.cleanName(a) == this.cleanName(b);
	},
	choose: function () {
		return this.countries[Math.floor(Math.random() * this.countries.length)];
	}
}

GeoTest.data.preferences = JSON.parse(GeoTest.data.cookies.readCookie("prefs") || "{}");  