[%- wrap = false -%]
var gPlatform = "";

if (navigator.platform.includes("Win32"))
  gPlatform = "win";
else if (navigator.platform.includes("Win64"))
  gPlatform = "win64";
else if (navigator.platform.includes("Linux i"))
  gPlatform = "linux";
else if (navigator.platform.includes("Linux x"))
  gPlatform = "linux64";
else if (navigator.platform.includes("MacIntel"))
  gPlatform = "osx";

var gLanguage = "";

if (navigator.language)
  gLanguage = navigator.language;
else if (navigator.userLanguage)
  gLanguage = navigator.userLanguage;
else if (navigator.systemLanguage)
  gLanguage = navigator.systemLanguage;

// Convert "en" to "en-US" as well since en-US build is the canonical
// translation, and thus better tested.
if (gLanguage == "" || gLanguage == "en")
  gLanguage = "en-US";

// Konqueror uses '_' where other browsers use '-'.
if (gLanguage.indexOf("_") != -1)
  gLanguage = gLanguage.split("_").join("-");

gLanguage = gLanguage.toLowerCase();


function setPlatformSelected(aString)
{
  setOptionSelected("download_platform", aString, "win64");
}

function setLanguageSelected(aString)
{
  setOptionSelected("download_language", aString, "en-US");
}

function setOptionSelected(aSelectID, aString, aDefault)
{
  var options = document.getElementById(aSelectID).options;

  var defaultOption;
  for (var i = 0; i < options.length; i++) {
    if (options[i].value === aString) {
      options[i].selected = "selected";
      return;
    }
    if (options[i].value === aDefault)
      defaultOption = i;
  }
  options[defaultOption].selected = "selected";
}

function getOptionSelected(aSelectID)
{
  var elem = document.getElementById(aSelectID);
  return {
    id : elem.options[elem.selectedIndex].value,
    label : elem.options[elem.selectedIndex].textContent
  };
}

function generateDownloadURL()
{
  var platform = getOptionSelected("download_platform");
  var language = getOptionSelected("download_language");
  var url = document.getElementById(language.id)
                    .querySelector('td > a[data-platform="' + platform.id + '"]');
  if (url) {
    // Update href, title and other attributes of download link.
    var link = document.getElementById("download_link");
    link.href = url.href;
    link.title = link.getAttribute("data-title")
                     .replace("%platform", platform.label)
                     .replace("%language", language.label);
    link.setAttribute("data-platform", platform.id);
    link.setAttribute("data-language", language.id);
    document.getElementById("download_icon").className = url.className;

    // Set platform and language in download_info div.
    document.getElementById("info_platform").textContent = platform.label;
    document.getElementById("info_language").textContent = language.label;
  }
}

function initSelect(aSelectID, aCallback)
{
  var elem = document.getElementById(aSelectID);
  if ("function" == typeof aCallback) {
    elem.addEventListener("change", aCallback, false);
  }
  elem.removeAttribute("disabled");
}

function onPlatformChange(aElem) {
  setPlatformSelected(aElem.target.value);
  generateDownloadURL();
}

function onLanguageChange(aElem) {
  setLanguageSelected(aElem.target.value);
  generateDownloadURL();
}

function init()
{
  setPlatformSelected(gPlatform);
  setLanguageSelected(gLanguage);
  generateDownloadURL();
  initSelect("download_platform", onPlatformChange);
  initSelect("download_language", onLanguageChange);
}

function isSupportedOS(ua) {
  var winVer = ua.match(/Windows( NT)? ([0-9.]+);/);
  if (!winVer) {
   return true; // not Windows, assume supported
  }
  if (winVer[1] == " NT") {
   return winVer[2] >= 6.1; // require Windows 7
  }
  return false; // Vista, XP, Win9x and WinCE are not supported
}

function displayElement(el, show) {
  document.getElementById(el).style.display = show ? "block" : "none";
}
