var Destination = new Array(); // Leave line as is.


Destination["en-au"] = "http://www.mosalingua.com/en";
Destination["en-us"] = "http://www.mosalingua.com/en";
Destination["en"] = "http://www.mosalingua.com/en";
Destination["es"] = "http://www.mosalingua.com/es";
Destination["it"] = "http://www.mosalingua.com/it";
Destination["fr"] = "http://www.mosalingua.com/";


var DefaultDestination = "http://www.mosalingua.com/en";


var lang = navigator.language ? navigator.language :
           navigator.browserlanguage ? navigator.browserlanguage : 
           navigator.systemLanguage ? navigator.systemLanguage : 
           navigator.userLanguage ? navigator.userLanguage : 
           '---';
lang = lang.toLowerCase();
var dest = new String();
for( var t in Destination ) {
   if( t == lang ) {
      dest = Destination[t];
      break;
   }
}
if( dest.length == 0 ) {
   lang = lang.substr(0,2);
   for( var t in Destination ) {
      if( t == lang ) {
         dest = Destination[t];
         break;
         }
      }
   }
if( dest.length == 0 ) { dest = DefaultDestination; }

//saved previous choice
var usr_want_redirect = true;
var already_asked_usr = false;
var support_localStorage = 'localStorage' in window && window['localStorage'] !== null;
if (support_localStorage) {
	if (localStorage["usr_want_redirect"]) {
		usr_want_redirect = (localStorage["usr_want_redirect"] == "true");
	}
	if (localStorage["already_asked_usr"]) {
		already_asked_usr = (localStorage["already_asked_usr"] == "true");
	}
}

if (dest !== location.href && !already_asked_usr) {
	var msg = "Do you want to see the English version of MosaLingua?";
	if (lang.indexOf('fr') === 0) {//=startWith
		msg = 'Voulez-vous voir la version française du site ?';
	}
	else if (lang.indexOf('it') === 0) {
		msg = 'Vuoi vedere la versione italiana di MosaLingua?';
	}
	var r=confirm(msg); 
	if (r == false) {
		dest = "";
	}
	usr_want_redirect = r;
	already_asked_usr = true;
	if (support_localStorage) {
		localStorage["usr_want_redirect"] = r;
		localStorage["already_asked_usr"] = true;
	}
}
//if current location is already good :
if ( location.href.indexOf(dest) === 0 ) {
	//ex. if current url = http://www.mosa.com/en/article1 et dest = http://www.mosa.com/en/, alors on ne redirige pas.
	dest = '';
}
if( dest.length > 0 && dest !== location.href && usr_want_redirect) {
	location.href = dest;
}

