// geotargeting.js

/*
 * This script uses $$
 */
var $$ = jQuery.noConflict();
// set this to true for debug messages
var debug = false;

/*
 * gets the users country either
 * from session cookie (put there by Throttle Filter)
 * or makes AJAX call to SMCountryLookupServlet
 */
function getUserCountry(isDiagnostics) {
	logger('is Diagnostics?' + isDiagnostics);
	var geoParam = findGeoParameter();
	logger('geoParam is: ' + geoParam);
	// if cookies are enabled, get user's country from cookie
	if (navigator.cookieEnabled){ 
		logger('Browser cookies enabled!');
		var smCountry = $$.cookie('SM_COUNTRY');
		if(geoParam == 1) 
			goToHomePage(smCountry);
		else
			highlightUserCountry(smCountry);
	// cookies are not enabled --> make ajax call
	} else {
		logger('Browser cookies not enabled!');
		var url = "../SMCountryLookupServlet";
		logger(geoParam);
		if (geoParam && geoParam.length > 1)
			url += ('?geo=' + geoParam);
		logger(url);
		// make AJAX request to URL & call highlightUserCountry
		if(geoParam == 1)
			$$.get(url, goToHomePage, 'text');
		else
			$$.get(url, highlightUserCountry, 'text');	
	}
}

/*
 * returns the geo parameter value if it is in the query string
 */
function findGeoParameter(){
	// get query string without ?
	var queryString = location.search.substring(1);
	var requestParams = queryString.split("&");
	// look at each parameter if it is geo=
	for (var i in requestParams){
		param = requestParams[i];
		if (/^geo=/.test(param)){
			param = param.split("=")[1];
			return param;
		}
	}
}
		
/*
 * fills <div id="current"> with user's country
 * expects name value pair -> languageId:countryCode
 */
function highlightUserCountry(langCountry){
	// use country code to find right <div> in page by id
	logger(langCountry);
	var usersCountry = $$('#' + langCountry.split(":")[1]).html();
	// transfer content from country <div> to <div id="current">
	if (usersCountry){
			$$('#current').html(usersCountry);
		} else {
			$$('#current').html('');
		}
}

/* 
 * creates home page link for the user's country 
 * and redirects to home page
 * expects name value pair -> languageId:countryCode
 */
function goToHomePage(langCountry){
	var langId = langCountry.split(":")[0];
	// use language Id to build home page link
	var homeUrl = "StoreCatalogDisplay?catalogId=" + langId;
	homeUrl += "&langId="
	homeUrl += langId;
	homeUrl += "&storeId=10001";
	logger('homeURL is: ' + homeUrl);
	redirect_to(homeUrl); // function from sm_javascript.js
}
/*
 * gets the users country either
 * from session cookie (put there by Throttle Filter)
 * or makes AJAX call to SMCountryLookupServlet
 */
function showDisclaimer(isDiagnostics, pageLanguage) {
	logger('is Diagnostics?' + isDiagnostics);
	var geoParam = findGeoParameter();
	logger('geoParam is: ' + geoParam);
	// if cookies are enabled, get user's country from cookie
	if (navigator.cookieEnabled){ 
		logger('Browser cookies enabled!');
		var smCountry = $$.cookie('SM_COUNTRY');
		showDisclaimerPopup(smCountry, pageLanguage);
	// cookies are not enabled --> make ajax call
	} else {
		logger('Browser cookies not enabled!');
		var url = "../SMCountryLookupServlet";
		logger(geoParam);
		if (geoParam && geoParam.length > 1)
			url += ('?geo=' + geoParam);
		logger(url);
		// make AJAX request to URL & call showDisclaimerPopup
		$$.get(url, function(data){showDisclaimerPopup(data, pageLanguage);});

	}
}


function showDisclaimerPopup(langCountry, pageLanguage){
	var usersCountry = langCountry.split(":")[0];
	logger("Users language: " + usersCountry + ", Page language: " + pageLanguage);
	// Disclaimer for US
	if (usersCountry == -1 && !$$.cookie('SM_OPTIN_US')) {
		if (pageLanguage != -1 &&  pageLanguage != -101 &&  pageLanguage != -201){
			$$('#disclaimer_background_US').show();
			$$('#disclaimer_US').bgiframe();
			$$('#disclaimer_US').show();
		}
	}
	// Disclaimer for DX Japan
	if (pageLanguage == -110 && !$$.cookie('SM_OPTIN_DX_JP')){
		$$('#disclaimer_background_DX_JP').show();
		$$('#disclaimer_DX_JP').bgiframe();
		$$('#disclaimer_DX_JP').show();
	;}
		
}

/*
 * sets SM_OPTIN session cookie (if cookies are enabled)
 * and hides the disclaimer
 */
function acceptDisclaimer(extension){
	if (navigator.cookieEnabled){
		var cookieName = 'SM_OPTIN_' + extension;
		$$.cookie(cookieName, 'true', { path: '/'});
	}
		$$('#disclaimer_background_' + extension).hide();
		$$('#disclaimer_' + extension).hide();
}

/*
 * function for logging:
 * Mozilla --> output on console
 * IE      --> alert box
 * en-/disabled via debug variable
 */
function logger(message){
	if (debug){
		if (typeof console != 'undefined'){
			console.log(message);
		}
		else {
			alert(message);
		}
	}
}
