var gPlatform = PLATFORM_WINDOWS;

var PLATFORM_OTHER    = 0;
var PLATFORM_WINDOWS  = 1;
var PLATFORM_LINUX    = 2;
var PLATFORM_MACOSX   = 3;
var PLATFORM_MAC      = 4;

if (navigator.platform.indexOf("Win32") != -1)
  gPlatform = PLATFORM_WINDOWS;
else if (navigator.platform.indexOf("Win64") != -1)
  gPlatform = PLATFORM_WINDOWS;
else if (navigator.platform.indexOf("Linux") != -1)
  gPlatform = PLATFORM_LINUX;
else if (navigator.userAgent.indexOf("Mac OS X") != -1)
  gPlatform = PLATFORM_MACOSX;
else if (navigator.userAgent.indexOf("MSIE 5.2") != -1)
  gPlatform = PLATFORM_MACOSX;
else if (navigator.platform.indexOf("Mac") != -1)
  gPlatform = PLATFORM_MAC;
else
  gPlatform = PLATFORM_OTHER;

// "" for a version means it should be "Not Yet Available" on all.html,
// null means it should not be listed
// A region code of "-" means that no region code should be used.
// Last JS-based revision on mozilla.com was
// http://viewvc.svn.mozilla.org/vc/projects/mozilla.com/trunk/js/download.js?revision=5420&view=markup
var gLanguages = {
  "af":  { "za": { version: null,      betaversion: null,     name: "Afrikaans",                localName: "Afrikaans" } },
  "ast": { "es": { version: null,      betaversion: null,     name: "Asturian",                 localName: "Asturianu" } },
  "ar":  { "-":  { version: null,      betaversion: null,     name: "Arabic",                   localName: "\u0639\u0631\u0628\u064A" } },
  "be":  { "-":  { version: "2.49.4",   betaversion: "2.48b1",  name: "Belarusian",               localName: "\u0411\u0435\u043B\u0430\u0440\u0443\u0441\u043A\u0430\u044F" } },
  "bg":  { "-":  { version: null,      betaversion: null,     name: "Bulgarian",                localName: "\u0411\u044A\u043B\u0433\u0430\u0440\u0441\u043A\u0438" } },
  "br":  { "fr": { version: null,      betaversion: null,     name: "Breton",                   localName: "Brezhoneg" } },
  "ca":  { "-":  { version: "2.49.4",   betaversion: "2.48b1",  name: "Catalan",                  localName: "Catal\u00E0" } },
  "cs":  { "-":  { version: "2.49.4",   betaversion: "2.48b1",  name: "Czech",                    localName: "\u010Ce\u0161tina" } },
  "cy":  { "gb": { version: null,      betaversion: null,     name: "Welsh",                    localName: "Cymraeg" } },
  "da":  { "-":  { version: null,      betaversion: null,     name: "Danish",                   localName: "Dansk" } },
  "de":  { "-":  { version: "2.49.4",   betaversion: "2.48b1",  name: "German",                   localName: "Deutsch" } },
  "el":  { "-":  { version: null,      betaversion: null,     name: "Greek",                    localName: "\u0395\u03BB\u03BB\u03B7\u03BD\u03B9\u03BA\u03AE" } },
  "en":  { "us": { version: "2.49.4",   betaversion: "2.48b1",  name: "English",                  localName: "English" },
           "gb": { version: "2.49.4",   betaversion: "2.48b1",  name: "English (British)",        localName: "English (British)" } },
  "es":  { "ar": { version: "2.49.4",   betaversion: "2.48b1",  name: "Spanish (Latin American)", localName: "Espa\u00F1ol (de Am\u00E9rica)" },
           "es": { version: "2.49.4",   betaversion: "2.48b1",  name: "Spanish (Spain)",          localName: "Espa\u00F1ol (de Espa\u00F1a)" } },
  "eu":  { "-":  { version: null,      betaversion: null,     name: "Basque",                   localName: "Euskara" } },
  "fi":  { "-":  { version: "2.49.4",   betaversion: "2.48b1",  name: "Finnish",                  localName: "Suomi" } },
  "fr":  { "-":  { version: "2.49.4",   betaversion: "2.48b1",  name: "French",                   localName: "Fran\u00e7ais" } },
  "fy":  { "nl": { version: null,      betaversion: null,     name: "Frisian",                  localName: "Frysk" } },
  "ga":  { "ie": { version: null,      betaversion: null,     name: "Irish",                    localName: "Gaeilge" } },
  "gl":  { "-":  { version: "2.49.4",   betaversion: "2.48b1",  name: "Galician",                 localName: "Galego" } },
  "gu":  { "in": { version: null,      betaversion: null,     name: "Gujarati",                 localName: "\u0A97\u0AC1\u0A9C\u0AB0\u0ABE\u0AA4\u0AC0" } },
  "he":  { "-":  { version: null,      betaversion: null,     name: "Hebrew",                   localName: "\u05E2\u05D1\u05E8\u05D9\u05EA" } },
  "hu":  { "-":  { version: "2.49.4",   betaversion: "2.48b1",  name: "Hungarian",                localName: "Magyar" } },
  "hy":  { "am": { version: null,      betaversion: null,     name: "Armenian",                 localName: "\u0540\u0561\u0575\u0565\u0580\u0565\u0576" } },
  "it":  { "-":  { version: "2.49.4",   betaversion: "2.48b1",  name: "Italian",                  localName: "Italiano" } },
  "ja":  { "-":  { version: "2.49.4",   betaversion: "2.48b1",  name: "Japanese",                 localName: "\u65e5\u672c\u8a9e" } },
  "ka":  { "-":  { version: null,      betaversion: null,     name: "Georgian",                 localName: "\u10e5\u10d0\u10e0\u10d7\u10e3\u10da\u10d8" } },
  "ko":  { "-":  { version: null,      betaversion: null,     name: "Korean",                   localName: "\uD55C\uAD6D\uC5B4" } },
  "lt":  { "-":  { version: "2.49.4",   betaversion: "2.48b1",  name: "Lithuanian",               localName: "Lietuvi\u0173" } },
  "mk":  { "-":  { version: null,      betaversion: null,     name: "Macedonian",               localName: "\u041C\u0430\u043A\u0435\u0434\u043E\u043D\u0441\u043A\u0438" } },
  "mn":  { "-":  { version: null,      betaversion: null,     name: "Mongolian",                localName: "\u041C\u043E\u043D\u0433\u043E\u043B" } },
  "nb":  { "no": { version: "2.49.4",   betaversion: "2.48b1",  name: "Norwegian (Bokm\u00E5l)",  localName: "Norsk bokm\u00E5l" } },
  "nn":  { "no": { version: null,      betaversion: null,     name: "Norwegian (Nynorsk)",      localName: "Norsk nynorsk" } },
  "nl":  { "-":  { version: "2.49.4",   betaversion: "2.48b1",  name: "Dutch",                    localName: "Nederlands" } },
  "pa":  { "in": { version: null,      betaversion: null,     name: "Punjabi",                  localName: "\u0A2A\u0A70\u0A1C\u0A3E\u0A2C\u0A40" } },
  "pl":  { "-":  { version: "2.49.4",   betaversion: "2.48b1",  name: "Polish",                   localName: "Polski" } },
  "pt":  { "br": { version: null,      betaversion: null,     name: "Portuguese (Brazilian)",   localName: "Portugu\u00EAs (do Brasil)" },
           "pt": { version: "2.49.4",   betaversion: "2.48b1",  name: "Portuguese (Portugal)",    localName: "Portugu\u00EAs (Europeu)" } },
  "ro":  { "-":  { version: null,      betaversion: null,     name: "Romanian",                 localName: "Rom\u00E2n\u0103" } },
  "ru":  { "-":  { version: "2.49.4",   betaversion: "2.48b1",  name: "Russian",                  localName: "\u0420\u0443\u0441\u0441\u043A\u0438\u0439" } },
  "sk":  { "-":  { version: "2.49.4",   betaversion: "2.48b1",  name: "Slovak",                   localName: "Slovensk\u00FD" } },
  "sl":  { "-":  { version: null,      betaversion: null,     name: "Slovenian",                localName: "Slovensko" } },
  "sq":  { "-":  { version: null,      betaversion: null,     name: "Albanian",                 localName: "Shqipe" } },
  "sv":  { "se": { version: "2.49.4",   betaversion: "2.48b1",  name: "Swedish",                  localName: "Svenska" } },
  "tr":  { "-":  { version: "2.49.4",   betaversion: "2.48b1",  name: "Turkish",                  localName: "T\u00FCrk\u00E7e" } },
  "uk":  { "-":  { version: "2.49.4",   betaversion: "2.48b1",  name: "Ukrainian",                localName: "\u0423\u043A\u0440\u0430\u0457\u043D\u0441\u044C\u043A\u0430" } },
  "zh":  { "cn": { version: "2.49.4",   betaversion: "2.48b1",  name: "Chinese (Simplified)",     localName: "\u4E2D\u6587 (\u7B80\u4F53)" },
           "tw": { version: "2.49.4",   betaversion: "2.48b1",  name: "Chinese (Traditional)",    localName: "\u6b63\u9ad4\u4e2d\u6587 (\u7E41\u9AD4)" } }
};

function LanguageID(aAB, aCD, aBuild)
{
  if (aCD == "-")
    this.abCD = aAB;
  else
    this.abCD = aAB + "-" + aCD.toUpperCase();
  for (var prop in aBuild)
    this[prop] = aBuild[prop]
}

function buildValidForPlatform(aLangID, aPlatform)
{
  if ((aLangID.abCD == "gu-IN" || aLangID.abCD == "pa-IN") &&
      aPlatform == PLATFORM_MACOSX)
    return false;

  return true;
}

function getLanguageID(betaversion)
{
  var verName = "version";
  if (betaversion)
    verName = "betaversion";

  var language = "";
  if (navigator.language)
    language = navigator.language;
  else if (navigator.userLanguage)
    language = navigator.userLanguage;
  else if (navigator.systemLanguage)
    language = navigator.systemLanguage;

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

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

  language = language.toLowerCase();
  var languageCode = language.split("-")[0];
  var regionCode = language.split("-")[1];

  // String comparison actually works for version numbers.
  var currentVersion = gLanguages["en"]["us"][verName];
  var bestVersion = "";
  var id = null;

  if (gLanguages[languageCode]) {
    var region;
    var build;
    var langid;

    for (region in gLanguages[languageCode]) {
      build = gLanguages[languageCode][region];
      if (build[verName] && regionCode == region) {
        langid = new LanguageID(languageCode, regionCode, build);
        if (buildValidForPlatform(langid, gPlatform)) {
          id = langid;
          bestVersion = build[verName];
        }
      }
    }

    // We have a localized build for this language, but not this region.
    // use the first available region with the highest version

    if (bestVersion != currentVersion) {
      var bestRegionVersion = "";
      for (region in gLanguages[languageCode]) {
        build = gLanguages[languageCode][region];
        if (build[verName] > bestVersion) {
          langid = new LanguageID(languageCode, region, build);
          if (buildValidForPlatform(langid, gPlatform) &&
              (build[verName] > bestRegionVersion)) {
            id = langid;
            bestRegionVersion = build[verName];
          }
        }
      }
      if (bestRegionVersion > bestVersion)
        bestVersion = bestRegionVersion;
    }
  }
  if (bestVersion != currentVersion) {
    id = new LanguageID("en", "us", gLanguages["en"]["us"]);
  }

  return id;
}

function highlightDownload(aIdBase, betaversion)
{
  if (gPlatform == PLATFORM_WINDOWS) {
    document.getElementById(aIdBase + "-win").className = "main";
    document.getElementById(aIdBase + "-linux").style.display = "none";
    document.getElementById(aIdBase + "-mac").style.display = "none";
  }
  else if (gPlatform == PLATFORM_LINUX) {
    document.getElementById(aIdBase + "-win").style.display = "none";
    document.getElementById(aIdBase + "-linux").className = "main";
    document.getElementById(aIdBase + "-mac").style.display = "none";
  }
  else if (gPlatform == PLATFORM_MACOSX) {
    document.getElementById(aIdBase + "-win").style.display = "none";
    document.getElementById(aIdBase + "-linux").style.display = "none";
    document.getElementById(aIdBase + "-mac").className = "main";
  }
  else {
    document.getElementById(aIdBase + "-win").style.display = "none";
    document.getElementById(aIdBase + "-linux").style.display = "none";
    document.getElementById(aIdBase + "-mac").style.display = "none";
    document.getElementById(aIdBase + "-other").className = "main";
  }

  // see if we need to change download box for localized version
  var languageID = getLanguageID(betaversion);
  if (languageID.abCD != "en-US") {
    // Japanese for Mac needs special rules
    var mac_abCD = languageID.abCD;
    if (mac_abCD == "ja-JP")
        mac_abCD = "ja-JPM";
    if (mac_abCD == "ja")
        mac_abCD = "ja-JP-mac";

    // download URLs - we only support bouncer URLs here!
    document.getElementById(aIdBase + "-win-link").href =
      document.getElementById(aIdBase + "-win-link").href.replace(/lang=en-US/, "lang=" + languageID.abCD);
    document.getElementById(aIdBase + "-linux-link").href =
      document.getElementById(aIdBase + "-linux-link").href.replace(/lang=en-US/, "lang=" + languageID.abCD);
    document.getElementById(aIdBase + "-mac-link").href =
      document.getElementById(aIdBase + "-mac-link").href.replace(/lang=en-US/, "lang=" + languageID.abCD);

    // download language name
    document.getElementById(aIdBase + "-win-lang").firstChild.data = languageID.name;
    document.getElementById(aIdBase + "-linux-lang").firstChild.data = languageID.name;
    document.getElementById(aIdBase + "-mac-lang").firstChild.data = languageID.name;
  }
}

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] >= 5.1; // require WinXP (assume SP2 or better)
  }
  return false; // Win9x and WinCE are not supported
}

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