
function CountrySelectorController() {
	var countryCode = "GB";	//default country
	var langCode;
	
	/**
	 * Called when a country has been selected.
	 * @param newLanguageCode - A language code on the format sv, en, da
	 * @param newCountryCode - A country code on the format SE, GB, DK
	 */
	
	this.onCountrySelected = function(newCountryCode) {
		countryCode = newCountryCode;
		if (newCountryCode) {
			showLangChoices();
		} else {
			removeLangChoices();
		}
	}
	
	this.onLanguageSelected = function(newLanguageCode) {
		langCode = newLanguageCode;
	}
	
	this.onfocusCountrySelector = function() {
		removeLangChoices();
	}
	
	this.goShopping = function() {
		
		if (countryCode != null && langCode != null) {
			setLocale(langCode, countryCode);
			var returnUrl = makeReturnUrl(langCode, countryCode);
			navigateAway(returnUrl);	
		}
	}
	
	
	function fixLC(currentDropDownId) {
		var setCode = function(lc){
			langCode = lc;
		};

		$("select.langSelector_" + countryCode, theContext).show();
		
		var langOpts = $("select." + currentDropDownId + " > option", theContext),
			len = langOpts.length;
		
		while ( len-- ) {
			var opt = langOpts[len];
			//console.dir(opt);
			if ( opt.selected == true ) {
				setCode(opt.value);
				break;
			}			
		}


		//lock the dropdown if there's only one language available.
		if ( langOpts.size() == 1 ){
			$("select.langSelector_" + countryCode, theContext).attr("disabled", true);
		}
		
		delete setCode;
	}
	
	function init(){
	}

	function selectCountry(newCountryCode){
		var $options = $('select.countrySelector option', theContext),
			len = $options.length;
		
		while ( len-- ) {
			var opt = $options[len];
			opt.selected = (opt.value == newCountryCode);	
		}
	}
	
	function showLangChoices() {
		var currentDropDownId = "langSelector_"+countryCode;
		$('select.'+currentDropDownId, theContext).attr("disabled", false);
		
		// hide any old drop downs
		$("select.lyCountrySelectorLanguageDropDown:not(select." + currentDropDownId + ")", theContext).hide();
		$("select." + currentDropDownId, theContext).show();
		
		fixLC(currentDropDownId);
	}

	function removeLangChoices() {
		//$("#langSelector", theContext).attr("disabled", true);
	}

	
	function showLocaleChanged() {
		$("#countrySelectorTitle").hide();
		$("#countrySelectorMain").hide();
		$("#countrySelectorChangedCountryTitle").css('visibility','visible');
		$("#countrySelectorChangedCountryMain").show();
	}
	this.showLocaleChanged = showLocaleChanged;
	
	function setLocale(newLanguageCode, newCountryCode) {
		var locale = newLanguageCode + "_" + newCountryCode;
	
		setCookie(locale);
		putLocaleIntoHttpSession(newLanguageCode, newCountryCode);
	}
	this.setLocale = setLocale;
	
	function setCookie(locale) {
		cookie.setCountryCookie(locale);
	}
	
	function makeReturnUrl(userLanguage, userCountry) {
		var cookiesEnabled = testGetSession();
		var tobeUrl = "";
		
		if(cookiesEnabled) {
			queryString = StripQueryStringKey("cc");
			queryString = StripQueryStringKey("lc", queryString);
			
			tobeUrl = window.location.protocol + "//" 
			+ window.location.host 
			+ window.location.pathname.replace("/countrySelector", "/index") 
			+ ((queryString.length != 0 ) ? ("?" + queryString) : "")
			+ window.location.hash;
		} else {
			var newSearchString = "?wlc=" + userLanguage + "&wcc=" + userCountry;
			 
			tobeUrl = window.location.protocol + "//" 
			+ window.location.host 
			+ window.location.pathname.replace("/countrySelector", "/index")
			+ newSearchString
			+ window.location.hash;
		}	
		return tobeUrl;
	}
	this.makeReturnUrl = makeReturnUrl;
		
	 function putLocaleIntoHttpSession(userLanguage, userCountry) {
		sendDWRCallAsPost(function() {
			userContext.putLocaleIntoSession( userLanguage, userCountry, {
				async:false,
				timeout: 5000,
				errorHandler: function(data) { /* NOP */ },
				callback: function(result) { /* NOP */ }
			});
		});
	}
	this.putLocaleIntoHttpSession = putLocaleIntoHttpSession;	
	
	function testGetSession() {
		var gotSession = true;
		sendDWRCallAsPost(function() {
			userContext.testGetSession( {
				async:false,
				timeout: 5000,
				errorHandler: function(data) { /* NOP */ },
				callback: function(result) { gotSession = result; }
			});
		});
		
		return gotSession;
	}
	
	// As in base, but countrySelector is standalone.
	function sendDWRCallAsPost(functionParam) {
		try {
			dwr.engine.setVerb('POST');
			functionParam.call();
		} finally {
			dwr.engine.setVerb('GET');
		}
	}
	
	function navigateAway(returnUrl) {
		if (window.location.toString() == encodeURI(returnUrl) || window.location.toString() == returnUrl) {
			window.location.reload();
		} else {
			window.location.assign(decodeURI(returnUrl));
		}
	}
	
	init();
	this.navigateAway = navigateAway;
}
///////////////////////////////////////////////////////////////////////////////////
// DO NOT remove the next newline and DO NOT add code after this comment.
