// Genkan does not use URL hash
var BASE_PATH = appBase + 'global/'+ (language == null ? 'en' : language);
var CHINA_HOSTNAME = "global-cn.infiniti.com";

var hash = window.location.hash.substring(1); // eg "/main/landing"
var path = window.location.pathname.substring(BASE_PATH.length); 

if(hash != '' && hash.match(/^\//) == null) {
	hash = '/' + hash;
	window.location = '#' + hash;
}

if(path.charAt(path.length-1) == '/') // strip trailing slash
	path = path.substring(0, path.length-1);

if(hash != '' && hash != path) {
	var newURL = BASE_PATH + hash;
	
	if(debugMode)alert('hash: '+hash+', path: '+path+', redirecting to: '+newURL);
	window.location = newURL;
} else if(hash == '' && window.location.pathname.match(BASE_PATH)) {
	if((document.jsAPI.getCookie('region') == 'world|en' && !document.jsAPI.cookiesEnabled) ||
       (document.jsAPI.getCookie('region') == null && document.jsAPI.cookiesEnabled)) {
		checkGeoRegion();
	}
	
	window.location = '#'+path;
	SWFAddress.setValue(path);
}

// set the region cookie if it's empty
if((document.jsAPI.getCookie('region') == 'world|en' && !document.jsAPI.cookiesEnabled) ||
   (document.jsAPI.getCookie('region') == null && document.jsAPI.cookiesEnabled)) {
    checkGeoRegion();
}

function checkGeoRegion() {
	var newRegion = getQueryParam("r").toLowerCase();
	newRegion = (newRegion == 'gb') ? 'uk': newRegion; //If user is geolocated to 'gb', point region to 'uk'
	
    var defaultLang = regionLangDefault[newRegion];

    if (newRegion != "" && defaultLang != null) {
        document.jsAPI.setCookie('region', newRegion + '|' + defaultLang);
    } else if (document.location.hostname == CHINA_HOSTNAME) {
        document.jsAPI.setCookie('region', 'cn|zh');
    } else {
        document.jsAPI.setCookie('region', 'world|en');
    }
}

function globalFlashEmbedCallback(e) {
	if(e.success) {
		//swffit.fit("flashcontent", 960, 600);
	} else {
		document.jsAPI.trackPage(path, 'HTML');
		document.jsAPI.setCookie('declinedExpressInstall', 'true', 0); //Flash ExpressInstall declined or failed
		injectFlashUpgradeNotice();
	}
}