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;
  gCssClass = 'os_windows';
} else if (navigator.platform.indexOf("Linux") != -1) {
  gPlatform = PLATFORM_LINUX;
  gCssClass = 'os_linux';
} else if (navigator.userAgent.indexOf("Mac OS X") != -1) {
  gPlatform = PLATFORM_MACOSX;
  gCssClass = 'os_osx';
} else if (navigator.userAgent.indexOf("MSIE 5.2") != -1) {
  gPlatform = PLATFORM_MACOSX;
  gCssClass = 'os_osx';
} else if (navigator.platform.indexOf("Mac") != -1) {
  gPlatform = PLATFORM_MAC;
  gCssClass = 'os_osx';
} else {
  gPlatform = PLATFORM_OTHER;
  gCssClass = '';
}

function getPlatformName(aPlatform)
{
  if (aPlatform == PLATFORM_WINDOWS)
    return "Windows";
  if (aPlatform == PLATFORM_LINUX)
    return "Linux i686";
  if (aPlatform == PLATFORM_MACOSX)
    return "Mac OS X";
  return "Unknown";
}

function getPlatformFileSize(aPlatform, aProduct)
{
  if (aProduct == "fx") {
    if (aPlatform == PLATFORM_WINDOWS)
      return "7.5MB";
    if (aPlatform == PLATFORM_LINUX)
      return "9.2MB";
    if (aPlatform == PLATFORM_MACOSX)
      return "17.0MB";
  } else if (aProduct == "fxold") {
    if (aPlatform == PLATFORM_WINDOWS)
      return "4.9MB";
    if (aPlatform == PLATFORM_LINUX)
      return "8.1MB";
    if (aPlatform == PLATFORM_MACOSX)
      return "16.2MB";
  } else if (aProduct == "tb") {
    if (aPlatform == PLATFORM_WINDOWS)
      return "6.4MB";
    if (aPlatform == PLATFORM_LINUX)
      return "11MB";
    if (aPlatform == PLATFORM_MACOSX)
      return "19MB";
  } else if (aProduct == "tbold") {
    if (aPlatform == PLATFORM_WINDOWS)
      return "6.1MB";
    if (aPlatform == PLATFORM_LINUX)
      return "10.2MB";
    if (aPlatform == PLATFORM_MACOSX)
      return "17.8MB";
  }
  return "Unknown";
}

function getProductName(aProduct)
{
  if (aProduct == "fx") {
    return "firefox";
  } else if (aProduct == "fxold") {
    return "firefox";
  } else if (aProduct == "tb") {
    return "thunderbird";
  } else if (aProduct == "tbold") {
    return "thunderbird";
  }
  return "Unknown";
}

function getDownloadURLForProduct(product, version)
{
  return "http://download.mozilla.org/?product=";
}

// Get a downloadURL given a locale and platform.
// The optional boolean is used when we want to get the download.mozilla.org
// link that points directly to Bouncer.
function getDownloadURLForLanguage(aLangID, aPlatform, directLink)
{
  var abCD = aLangID.abCD;
  var product = getProductName(aLangID.product);
  var version = aLangID[aLangID.product];

  // If we are testing the site locally, or if we explicitly asked for it,  
  // give the direct download URL.
  if (window.location.protocol == "file:" ||
      directLink == true) {
    var url = getDownloadURLForProduct(product, version);
  // Otherwise give the download page URL.
  } else {
    var url = "/download.php?product=";
  }

  url += product + "-" + version + "&amp;os=";

  if (aPlatform == PLATFORM_WINDOWS) {
    url += "win";
  } else if (aPlatform == PLATFORM_LINUX) {
    url += "linux";
  } else if (aPlatform == PLATFORM_MACOSX) {
    url += "osx";
    if (abCD == "ja-JP")
      abCD = "ja-JPM";
    if (abCD == "ja")
      abCD = "ja-JP-mac";
  } else {
    return "http://www.mozilla.com/" + abCD + "/" + product + "/all.html";
  }

  return url + "&amp;lang=" + abCD;
}

// "" 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.
var gLanguages = {
"da": { 	"-": { fx: "3.6",	fxold: "1.5.0.12",	tb: "2.0.0.6",	tbold: "1.5.0.13",	name: "Danese",	localName: "Dansk" } },
"de": { 	"-": { fx: "3.6",	fxold: "1.5.0.12",	tb: "2.0.0.6",	tbold: "1.5.0.13",	name: "Tedesco",	localName: "Deutsch" } },
"en": { 
 	"gb": { fx: "3.6",	fxold: "1.5.0.12",	tb: "2.0.0.6",	tbold: "1.5.0.13",	name: "Inglese (UK)",	localName: "English (British)" },
 	"us": { fx: "3.6",	fxold: "1.5.0.12",	tb: "2.0.0.6",	tbold: "1.5.0.13",	name: "Inglese (USA)",	localName: "English" } },
"eo": { 	"-": { fx: null,	fxold: null,	tb: null,	tbold: null,	name: "Esperanto",	localName: "Esperanto" } },
"fi": { 	"-": { fx: "3.6",	fxold: "1.5.0.12",	tb: "2.0.0.6",	tbold: "1.5.0.13",	name: "Finlandese",	localName: "suomi" } },
"fr": { 	"-": { fx: "3.6",	fxold: "1.5.0.12",	tb: "2.0.0.6",	tbold: "1.5.0.13",	name: "Francese",	localName: "Francais" } },
"ga": { 	"ie": { fx: "3.6",	fxold: "1.5.0.12",	tb: "2.0.0.6",	tbold: "1.5.0.13",	name: "Irlandese",	localName: "Gaeilge" } },
"it": { 	"-": { fx: "3.6",	fxold: "1.5.0.12",	tb: "2.0.0.6",	tbold: "1.5.0.13",	name: "Italiano",	localName: "Italiano" } },
"nn": { 	"no": { fx: "3.6",	fxold: null,	tb: "2.0.0.6",	tbold: null,	name: "Norvegese",	localName: "Norsk nynorsk" } },
"pl": { 	"-": { fx: "3.6",	fxold: "1.5.0.12",	tb: "2.0.0.6",	tbold: "1.5.0.13",	name: "Polacco",	localName: "polski" } },
"sl": { 	"-": { fx: "3.6",	fxold: "1.5.0.12",	tb: "2.0.0.6",	tbold: "1.5.0.13",	name: "Sloveno",	localName: "Slovensko" } },
"sv": { 	"se": { fx: "3.6",	fxold: "1.5.0.12",	tb: "2.0.0.6",	tbold: "1.5.0.13",	name: "Svedese",	localName: "Svenska" } }
};
function LanguageID(aAB, aCD, aProduct, aBuild)
{
  if (aCD == "-")
    this.abCD = aAB;
  else
    this.abCD = aAB + "-" + aCD.toUpperCase();
  this.product = aProduct;
  for (var prop in aBuild)
    this[prop] = aBuild[prop];
}

function buildValidForPlatform(aLangID, aPlatform)
{
  var product = getProductName(aLangID.product);
  var version = aLangID[aLangID.product];
  if ((aLangID.abCD == "gu-IN" ||
       (aLangID.abCD == "pa-IN" &&
        ((product == "firefox" && version < "2.0.0.1") ||
         (product == "thunderbird" && version < "2.0.0.0")))) &&
      (aPlatform == PLATFORM_MACOSX))
    return false;

  return true;
}

function getLanguageIDs(aProduct)
{
  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"][aProduct];
  var bestVersion = "";
  var ids = [];

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

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

    // We have a localized build for this language, but not this region. 
    // Show all available regions and let the user pick. 

    if (bestVersion != currentVersion) {
      var bestRegionVersion = "";
      for (region in gLanguages[languageCode]) {
        build = gLanguages[languageCode][region];
        if (build[aProduct] > bestVersion) {
          langid = new LanguageID(languageCode, region, aProduct, build);
          if (buildValidForPlatform(langid, gPlatform)) {
            ids[ids.length] = langid;
            if (build[aProduct] > bestRegionVersion)
              bestRegionVersion = build[aProduct];
          }
        }
      }
      if (bestRegionVersion > bestVersion)
        bestVersion = bestRegionVersion;
    }
  }


  // Offer the en-US version if it has a higher version than the locale
  if (bestVersion != currentVersion) {
    ids[ids.length] = new LanguageID("en", "us", aProduct, gLanguages["en"]["us"]);
  }

  return ids;
}

function writeDownloadItem(aLanguageID)
{
  var item = gDownloadItemTemplate;
  item = item.replace(/%DOWNLOAD_URL%/g,  getDownloadURLForLanguage(aLanguageID, gPlatform));
  item = item.replace(/%BOUNCER_URL%/g,   getDownloadURLForLanguage(aLanguageID, gPlatform, true));
  item = item.replace(/%VERSION%/g,       aLanguageID[aLanguageID.product]);
  item = item.replace(/%PLATFORM_NAME%/g, getPlatformName(gPlatform));
  item = item.replace(/%LANGUAGE_NAME%/g, aLanguageID.name);
  item = item.replace(/%FILE_SIZE%/g,     getPlatformFileSize(gPlatform, aLanguageID.product));
  item = item.replace(/%CSS_CLASS%/g,     gCssClass);
  document.writeln(item);
}

function writeDownloadItems(aProduct)
{
  // Show the dynamic links
  if (gPlatform == PLATFORM_MAC) {
    document.writeln(gDownloadItemMacOS9);
  } else if (gPlatform == PLATFORM_OTHER) {
    document.writeln(gDownloadItemOtherPlatform);
  } else {
    var languageIDs = getLanguageIDs(aProduct);
    for (var i = 0; i < languageIDs.length; ++i)
      writeDownloadItem(languageIDs[i]);
  }
}

function do_download(link)
{
  // If we have IE, use a new window to push the download.
  // We have to do this because other methods did not work in IE.
  if (navigator.appVersion.indexOf('MSIE') != -1) {
    window.open(link, 'download_window', 'toolbar=0,location=no,directories=0,status=0,scrollbars=0,resizeable=0,width=1,height=1,top=0,left=0');
    window.focus();
  }
}
