// Script to store maxmind geop data
// Requires jquery and jquery.cookie plugins

var geoCookieKey = 'geo_location';
var geoLocationInfo = {};

$(document).ready(function() {

	function fetchGeoInfo() {
		
		try {
			
			// var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
			var host = (('https:' === document.location.protocol) ? 'https://' : 'http://'),
				handle = STORE_HANDLE;
			
			maxmindScriptLoader = $('<script language="javascript" src="' + host + 'enstoresecured.appspot.com/'+ handle + '/data/geoip.js"></script>')
			maxmindScriptLoader.ready(function() {
				setTimeout(function() {
					$.cookie(geoCookieKey, geoip_country_code() + "," + geoip_city() + "," + geoip_region(), {expires: 30, path: '/'});
				
				}, 1000)
			});
	
			$("body").append(maxmindScriptLoader);
		} catch (e) {
			// Fail gracefully
		}
	}

	function loadGeoInfo() {
		
		try {
			info = $.cookie(geoCookieKey).split(',');
		
			geoLocationInfo = {
				'countryCode': info[0],
				'cityName': info[1],
				'regionCode': info[2]
			}
		
		} catch(err) {
			$.cookie(geoCookieKey, null);
		};
		
	}

	if (!$.cookie(geoCookieKey)) {
		fetchGeoInfo();
	} else {
		loadGeoInfo();
	}
	
});
