$(document).ready(function(){ geoLocation.init(); });

var geoLocation = {
	
	prefListUS : [ 'US','CA','AR','BO','BR','CL','CO','EC','GF','GY','PY','PE','SR','UY','VE','AG','BS','BB','BZ','CR','CU','DM','DO','SV','GD','GT','HT','HN','JM','MX','NI','PA','KN','LC','VC','TT' ], // country codes that will see US by preference
	cookieName : 'showSite',    // name of cookie
	cookieDays : 365,           // number of days for cookie to persist
	ipLoc      : null,
	allowQS    : true,			// true/false - allow country code via qs
	
	init: function() {
		var obj = this;
		
		// first determine if user is on the US site (bool)
		obj.patt = /.com|sh-usa-|samuelheath-usa/g; // remove samuelheath-usa
		obj.siteIsUS = obj.patt.test(window.location.href.slice(window.location.href));
		
		if($.cookie('showSite') != null && $.cookie('showSite') != '') {

			if(obj.siteIsUS && $.cookie('showSite') == 'uk') { // user on us site, has uk cookie
				// show message to redirect user to uk site or to overwrite cookie
				obj.proc('non-us', 'us');
			} else if(!obj.siteIsUS && $.cookie('showSite') != 'uk') { // user on uk site, has non-uk cookie
				// show message to redirect user to us site or to overwrite cookie
				obj.proc('us', 'non-us');
			}
		
		} else { // cookie not yet set - need to do IP lookup
			
			if($.cookie('showSite') == null || $.cookie('showSite') == '') {
				
				// check if a querystring country code has been passed in
				if(obj.allowQS) {
					$.each(location.search.substring(1).split('&'), function(s, e) {
						if(this.indexOf('countryCode', 0) != -1) { obj.ipLoc = e.slice(12); }
					});
				}
				
				if(obj.ipLoc == null) { // no querystring
					$.getScript("http://www.maxmind.com/app/country.js", function() {
						try {  obj.ipLoc = geoip_country_code(); } 
						catch(e) {  obj.ipLoc = null; };
						if(obj.ipLoc != null) { obj.lookupCountryInArray(obj.ipLoc); }
					});	
				} else {
					obj.lookupCountryInArray(obj.ipLoc); // pass the querystring value
				}
			}
		}
	},

	lookupCountryInArray : function(cCode) {
		var obj = this;
		var location = jQuery.inArray(obj.ipLoc, obj.prefListUS) != -1 ? 'us' : 'non-us';// user is from a country which should default to the US site
		var site = obj.siteIsUS ? 'us' : 'non-us';
		obj.proc(location, site);
	},
	
	proc: function(location, site) {
		var obj = this;
		if(location == 'non-us' && site == 'us') { 
			obj.showMessageUK(); 
			obj.recordLocaleImpression('US to GB');
		} else if(location == 'us' && site == 'non-us') { 
			obj.showMessageUS(); 
			obj.recordLocaleImpression('GB to US');
		}
		
		if($('.outsideUSA').length > 0) {
			$('#heathYes').click(function(e){
				e.preventDefault();
				$.cookie('showSite', 'uk', {path: '/', expires: 365});
				obj.recordLocaleChange('http://www.samuel-heath.co.uk/', 'Locale Change', 'US to GB', 'yes');
			});
			$('#heathNo').click(function(e){
				e.preventDefault();
				$.cookie('showSite', 'us', {path: '/', expires: 365});
				$('#heathLocation').slideUp(500, function(){ $(this).remove(); });
				obj.recordLocaleChange('#', 'Locale Change', 'US to GB', 'no');
			});			
		} else {
			$('#heathYes').click(function(e){
				e.preventDefault();
				$.cookie('showSite', 'us', {path: '/', expires: 365});
				obj.recordLocaleChange('http://www.samuel-heath.com/', 'Locale Change', 'GB to US', 'yes');
			});
			$('#heathNo').click(function(e){
				e.preventDefault();
				$.cookie('showSite', 'uk', {path: '/', expires: 365});
				$('#heathLocation').slideUp(500, function(){ $(this).remove(); });
				obj.recordLocaleChange('#', 'Locale Change', 'GB to US', 'no');
			});	
		}
	},

	showMessageUS : function() {
		$('body').prepend('<div id="heathLocation"><div id="inner" class="insideUSA">' +
		'<p id="heathTxt">Your visiting from within the <abbr>USA</abbr> / Canada, <br />Please click to view your dedicated market site</p>' + 
		'<div><p id="siteYesBtn"><a href="http://www.samuel-heath.com/" id="heathYes"><span>Visit the <abbr>USA site</span></a></p><span class="hide"> |</span>' +
		'<p id="siteNoBtn"><a href="#" id="heathNo"><span>No thank you</span></a></p></div>' +
		'</div></div>');
	},
	
	showMessageUK : function() {
		$('body').prepend('<div id="heathLocation" class="alternative"><div id="inner" class="outsideUSA">' +
		'<p id="heathTxt">Your visting from outside of the <abbr>USA</abbr>, <br />Please switch to our global site</p>' + 
		'<div><p id="siteYesBtn"><a href="http://www.samuel-heath.co.uk/" id="heathYes"><span>Visit our main website</span></a></p><span class="hide"> |</span>' +
		'<p id="siteNoBtn"><a href="#" id="heathNo"><span>No thank you</span></a></p></div>' +
		'</div></div>');	
	},
	
	recordLocaleChange: function(link, category, action, label) {
		_gat._getTrackerByName()._trackEvent(category, action, label);
		if(label != 'no') { setTimeout(function() { location.href = link; }, 2000); }
	},
	
	recordLocaleImpression: function(action) {
		_gaq.push(['_trackEvent','Locale Change', action,'impression']);
	}

}


