var helps_routing = {};

helps_routing.host = "http://" + window.location.host + "/";

helps_routing.init = function() {
	var lc = helps_routing.getCookie();
	var langList = document.getElementById("Language")
	for (var i=0; i<langList.length; i++) {
		if (langList.options[i].value == lc) {
			langList.options[i].selected = "selected"
		}
	}
}

helps_routing.redirect = function() {
	var path = window.location.pathname.toString();

  var si = path.indexOf("/intl")
	var page = "";
	if (si != -1) {
		page = path.split("/")[3];
	}
	else {
		page = path.split("/")[2];
	}
	//alert(path + " yields page name: " + page);

	var lc = helps_routing.getCookie();
	var ulc = helps_routing.getLangFromURL();
	//alert("lc = " + lc + ", ulc = " + ulc);
	if (ulc == "zh_cn" || lc == "zh_cn") {
		ulc = "cn";
		helps_routing.setCookie(ulc, 30);
		helps_routing.route(ulc, page);
	}
	if (lc == null && ulc == null) {
		//alert("No language prefs in cookie or URL, defaulting to 'en'.");
		helps_routing.setCookie("en", 30);
	}
	if (lc != null && ulc == null) {
		if (lc.toLowerCase() == "en") {
			return;
		}
		helps_routing.route(lc, page);
	}
	if (lc == null && ulc != null) {
		helps_routing.setCookie(ulc, 30);
		helps_routing.route(ulc, page);
	}
}

// Route visitor based on language code
// Must handle null
helps_routing.route = function(lc, page) {
	
	if (!page) {
		page = "index.html";
	}
	
  if (lc == null || lc == "SI") {
    return;
  }
	lc = lc.toLowerCase();
  helps_routing.setCookie(lc, 30);
	//location.reload(true);

	//return;
  // English
  if (lc == "en") {
    window.location = helps_routing.host + page;
  }
  else if (lc == "en_gb") {
    window.location = helps_routing.host + "intl/EN_GB/" + page;
  }
  // Simplified Chinese
  else if (lc == "cn") {
    window.location = helps_routing.host + "intl/CN/" + page;
  }
  // Traditional Chinese
  else if (lc == "b5") {
    window.location = helps_routing.host + "intl/B5/" + page;
  }
  // HK Chinese
  else if (lc == "cn_hk") {
    window.location = helps_routing.host + "intl/CN_HK/" + page;
  }
  // German
  else if (lc == "de") {
    window.location = helps_routing.host + "intl/DE/" + page;
  }
  // Spanish
  else if (lc == "es") {
    window.location = helps_routing.host + "intl/ES/" + page;
  }
  // French
  else if (lc == "fr") {
    window.location = helps_routing.host + "intl/FR/" + page;
  }
  // Italian
  else if (lc == "it") {
    window.location = helps_routing.host + "intl/IT/" + page;
  }
  // Japanese
  else if (lc == "ja") {
    window.location = helps_routing.host + "intl/JA/" + page;
  }
  // Korean
  else if (lc == "ko") {
    window.location = helps_routing.host + "intl/KO/" + page;
  }
  // Dutch
  else if (lc == "nl") {
    window.location = helps_routing.host + "intl/NL/" + page;
  }
  // Polish
  else if (lc == "pl") {
    window.location = helps_routing.host + "intl/PL/" + page;
  }
  // Portuguese
  else if (lc == "pt_br") {
    window.location = helps_routing.host + "intl/PT_BR/" + page;
  }
  // Russian
  else if (lc == "ru") {
    window.location = helps_routing.host + "intl/RU/" + page;
  }
  // Thai
  else if (lc == "th") {
    window.location = helps_routing.host + "intl/TH/" + page;
  }
  // Turkish
  else if (lc == "tr") {
    window.location = helps_routing.host + "intl/TR/" + page;
  }
}

helps_routing.getLocale = function(cc) {
  try {
    if (helps_routing.locales[cc] != undefined) {
      return helps_routing.locales[cc];
    } else {
      return "en";
    }
  }
  catch (e) {
  }
}

helps_routing.getLangFromURL = function() {
  if (window && window.location) {
    var url = window.location.toString();
    if (url.indexOf("intl") == -1) {
			var param = window.location.search;
			if (param != "" && param.indexOf("hl=") != -1) {
				var startIndex = param.indexOf("hl=")+3;
				var endIndex = param.indexOf("&", startIndex);
				if (endIndex == -1) {
					endIndex = param.length;
				}
				//alert(startIndex + ", " + endIndex);
				return param.substring(startIndex, endIndex).toLowerCase();
			}
			else {
				return null;
			}
      //return null;
    }
    else {
      urlArray = url.split("/");
      //return urlArray[4].toUpperCase();
			//return urlArray[4].toLowerCase();
			// Use 5th element while URL is protected.
			return urlArray[4].toLowerCase();
    }
  } else {
    return null;
  }
}

helps_routing.getLangFromQuery = function() {
  if (window && window.location) {
    var param = window.location.search;
    return param.substring(param.indexOf("hl=")+3, param.length).toUpperCase();
  } else {
    return null;
  }
}

helps_routing.getLangPath = function() {
  if (window && window.location) {
    return new String(window.location);
  } else {
    return null;
  }
}

helps_routing.detectCookies = function() {
  var cookie = document.cookie;
  
  if (!cookie) {
    return false;
  } else {
    return true;
  }
}

helps_routing.getCookie = function() {
    var nameEQ = "django_language=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

helps_routing.setCookie = function(value, days) {
  // Default to 30 day expiry
  // Default name is helps_lang
  var date = new Date();
  date.setTime(date.getTime()+(days*24*60*60*1000));
  var expires = "; expires="+date.toGMTString();
  //document.cookie = "helps_lang="+value.toUpperCase()+expires+"; path=/";
  document.cookie = "django_language="+value.toLowerCase()+expires+"; path=/";
}

helps_routing.clearCookie = function(name) {
  helps_routing.setCookie("", -1);
}

helps_routing.locales = {};
helps_routing.locales["HK"]="CN_HK"; //Hong Kong
helps_routing.locales["TW"]="B5"; //Taiwan
helps_routing.locales["VN"]="B5"; //Vietnam
helps_routing.locales["BN"]="CN"; //Brunei Darussalam
helps_routing.locales["CN"]="CN"; //China
helps_routing.locales["SG"]="CN"; //Singapore
helps_routing.locales["BE"]="NL"; //Belgium
helps_routing.locales["ID"]="NL"; //Indonesia
helps_routing.locales["NL"]="NL"; //Netherlands
helps_routing.locales["SR "]="NL"; //Suriname
helps_routing.locales["BE"]="FR"; //Belgium
helps_routing.locales["CI"]="FR"; //Cote d'Ivoire
helps_routing.locales["CM"]="FR"; //Cameroon
helps_routing.locales["DE"]="FR"; //DEy
helps_routing.locales["DJ"]="FR"; //Djibouti
helps_routing.locales["FR"]="FR"; //France
helps_routing.locales["GG"]="FR"; //Guernsey
helps_routing.locales["GP"]="FR"; //Guadeloupe
helps_routing.locales["HT"]="FR"; //Haiti
helps_routing.locales["JE"]="FR"; //Jersey
helps_routing.locales["LU"]="FR"; //Luxembourg
helps_routing.locales["MA"]="FR"; //Morocco
helps_routing.locales["MU"]="FR"; //Mauritius
helps_routing.locales["RW"]="FR"; //Rwanda
helps_routing.locales["SC"]="FR"; //Seychelles
helps_routing.locales["SN"]="FR"; //Senegal
helps_routing.locales["TG"]="FR"; //Togo
helps_routing.locales["TN"]="FR"; //Tunisia
helps_routing.locales["VN"]="FR"; //Vietnam
helps_routing.locales["VU"]="FR"; //Vanuatu
helps_routing.locales["AT"]="DE"; //Austria
helps_routing.locales["BE"]="DE"; //Belgium
helps_routing.locales["DE"]="DE"; //Germany
helps_routing.locales["LU"]="DE"; //Luxembourg
helps_routing.locales["RO"]="DE"; //Romania
helps_routing.locales["CH"]="DE"; //Switzerland
helps_routing.locales["GI"]="DE"; //Gibraltar
helps_routing.locales["IT"]="IT"; //Italy
helps_routing.locales["LY"]="IT"; //Libyan Arab Jamahiriya
helps_routing.locales["SM"]="IT"; //San Marino
helps_routing.locales["JP"]="JA"; //Japan
helps_routing.locales["KR"]="KO"; //Republic of Korea
helps_routing.locales["PL"]="PL"; //Poland
helps_routing.locales["BR"]="PT_BR"; //Brazil
helps_routing.locales["AM"]="RU"; //Armenia
helps_routing.locales["AZ"]="RU"; //Azerbaijan
helps_routing.locales["BY"]="RU"; //Belarus
helps_routing.locales["EE"]="RU"; //Estonia
helps_routing.locales["KG"]="RU"; //Kyrgyzstan
helps_routing.locales["KZ"]="RU"; //Kazakhstan
helps_routing.locales["LV"]="RU"; //Latvia
helps_routing.locales["MD"]="RU"; //Republic of Moldova
helps_routing.locales["RU"]="RU"; //RU Federation
helps_routing.locales["TJ"]="RU"; //Tajikistan
helps_routing.locales["TM"]="RU"; //Turkmenistan
helps_routing.locales["UA"]="RU"; //Ukraine
helps_routing.locales["UZ"]="RU"; //Uzbekistan
helps_routing.locales["AR"]="ES"; //Argentina
helps_routing.locales["BO"]="ES"; //Bolivia
helps_routing.locales["BZ"]="ES"; //Belize
helps_routing.locales["CL"]="ES"; //Chile
helps_routing.locales["CO"]="ES"; //Colombia
helps_routing.locales["CR"]="ES"; //Costa Rica
helps_routing.locales["CU"]="ES"; //Cuba
helps_routing.locales["DO"]="ES"; //Dominican Republic
helps_routing.locales["EC"]="ES"; //Ecuador
helps_routing.locales["ES"]="ES"; //Spain
helps_routing.locales["GI"]="ES"; //Gibraltar
helps_routing.locales["GT"]="ES"; //Guatemala
helps_routing.locales["HN"]="ES"; //Honduras
helps_routing.locales["MX"]="ES"; //Mexico
helps_routing.locales["NI"]="ES"; //Nicaragua
helps_routing.locales["PA"]="ES"; //Panama
helps_routing.locales["PE"]="ES"; //Peru
helps_routing.locales["PR"]="ES"; //Puerto Rico
helps_routing.locales["PY"]="ES"; //Paraguay
helps_routing.locales["SV"]="ES"; //El Salvador
helps_routing.locales["TT"]="ES"; //Trinidad and Tobago
helps_routing.locales["UY"]="ES"; //Uruguay
helps_routing.locales["VE"]="ES"; //Venezuela
helps_routing.locales["TH"]="TH"; //Thailand
helps_routing.locales["TR"]="TR"; //Turkey

helps_routing.detectLang = function() {
  if (!helps_routing.detectCookies()) {
    return;
  }
  else {
    var cookie = helps_routing.getCookie();
  
    // Check cookie, route accordingly if it is set
    if (cookie != "" && cookie != null) {
    
      var path = helps_routing.getLangPath();
      if (path.indexOf("intl") == -1) {
    
        if (cookie != "en") {
          helps_routing.route(cookie);
        }
      }
      else if(path.indexOf(cookie) == -1) {
        helps_routing.route(cookie);
      }
    }
    // Check URL for Google's referral string, route accordingly if set
    else {
      var langCode = helps_routing.getLangFromQuery();
    
      if (langCode != "" && langCode != null) {
        helps_routing.route(langCode);
      }
      // Check the Google API for locale
      else {
        var cc = google.loader.ClientLocation.address.country_code;
    
        if (cc != "" && cc != null) {
          var lc = helps_routing.getLocale(cc);
          helps_routing.route(lc);
        }
      }
    }
  }
}

//helps_routing.detectLang();
helps_routing.redirect();
