// redirect to regional site if region-default cookie is true and we can find a URL for the region in our regions object
if(document.jsAPI.getCookie('region-default') != null && document.jsAPI.getCookie('region-default') == 'true') {
	var region = document.jsAPI.getCookie('region');
	if(region && regions[region]){
		if(debugMode) alert('region redirect');
		window.location = regions[region];
	}
}

//Redirect if cookied region is set to something other than what was geo'd
if(document.jsAPI.getCookie('region')) {
	var cookiedRegionCombo = document.jsAPI.getCookie('region');
	var currentRegionCombo = countryCode + '|' + language;
	
	if(cookiedRegionCombo != currentRegionCombo) {
		rRegion = cookiedRegionCombo.split('|')[0];
		rLanguage = cookiedRegionCombo.split('|')[1];
		window.location = appBase + rRegion + '/' + rLanguage + '/index' + getQueryString();
	}
}

// detect browser language
var browserLang = null;
if(window.navigator.language) { // Firefox and Opera
	browserLang = window.navigator.language;
} else if(navigator.browserLanguage) { // Internet Explorer
	browserLang = navigator.browserLanguage;
}

// ensure 2 char length
if(browserLang != null && browserLang.length >= 2) {
	browserLang = browserLang.substring(0, 2).toLowerCase();
	// redirect if different from current and we find a match in the regions object
	if(browserLang != language && regions[countryCode + '|' + browserLang] && document.jsAPI.getCookie('region') == null) {
		if(debugMode) alert('language redirect');
		window.location = appBase + countryCode + "/" + browserLang + "/index" + getQueryString();
	}
}

if(document.jsAPI.getCookie('region') == null)
	document.jsAPI.setCookie('region', countryCode+'|'+language);

function genkanFlashEmbedCallback(e) {
	if(e.success) {
		swffit.fit("flashcontent", 960, 600);
	} else {
		var bodyEl = $('body');

		bodyEl.removeClass('flashenabled');
		bodyEl.addClass('noflash');
		
		document.jsAPI.trackPage('index', 'HTML'); //if/when deeplink support required, window.location.hash.substring(1)
		document.jsAPI.setCookie('declinedExpressInstall', 'true', 0); //Flash ExpressInstall declined or failed
		
		setGenkanClicks();
	}
}

function setGenkanClicks() {
	$('#flash-install-link a').click(function(e) {
		document.jsAPI.trackLink('exit', 'Adobe Flash install', this.href);
	});
	
	$('#region-default-yes').click(function(e) {
		document.jsAPI.setCookie('region', $('#region-selector').attr('region'));
		document.jsAPI.setCookie('region-default', 'true');
		document.jsAPI.trackLink('region-default-prompt', 'Genkan region default clicked yes');
		
		var targetUrl = $('#region-selector').attr('currentURL');
		document.jsAPI.trackLink('exit', targetUrl, targetUrl);
		var redirect = function(){
			document.location = targetUrl;
		}
		self.setTimeout(redirect,500); // wait 500 ms to allow time for Omniture request

		e.preventDefault();
	});
	
	$('#region-default-no').click(function(e) {
		document.jsAPI.trackLink('region-default-prompt', 'Genkan region default clicked no');
		
		var targetUrl = $('#region-selector').attr('currentURL');
		document.jsAPI.trackLink('exit', targetUrl, targetUrl);
		var redirect = function(){
			document.location = targetUrl;
		}
		self.setTimeout(redirect,500); // wait 500 ms to allow time for Omniture request

		e.preventDefault();
	});
	
	$('#region-selector table td a').click(function(e) {
		var link = this;
		$.getJSON('/scripts/region-localization.js?linkCountry='+$(link).attr('rel')+'&linkLanguage='+$(link).attr('lang'), function(data) {
			if($(link).attr('lang') == 'he' || $(link).attr('lang') == 'ar') {
				$('#region-overlay-content').addClass('rtl');
			} else {
				$('#region-overlay-content').removeClass('rtl');
			}
			
			$('#region-default-message').html(data.prompt.replace(/<[^>]*>/g, ""));
			$('#region-default-yes').html(data.yesLabel.replace(/<[^>]*>/g, ""));
			$('#region-default-no').html(data.noLabel.replace(/<[^>]*>/g, ""));
			$('#region-default-warning').html(data.defaultWarning.replace(/<[^>]*>/g, ""));
			
			$('#region-selector').attr('currentURL', link.href);
			$('#region-selector').attr('region', $(link).attr('rel')+'|'+$(link).attr('lang'));
			
			$('#region-overlay').css('display', 'block');
			
			document.jsAPI.trackLink('region-default-prompt-view', 'Genkan region default message displayed');
		});
		
		e.preventDefault();
	});
	
	$('#region-selector .close-button').click(function(e) {
		$('#region-overlay').css('display', 'none');
		document.jsAPI.trackLink('region-default-prompt', 'Genkan region default clicked close');
		e.preventDefault();
	});

	$('#flash-install-link').click(function() {
		//TODO Omniture
	});
}