/*
  File: Flags.js
  Object constructor to scroll flags of all countries
*/
function Flags(name, time, base, keys) {

this.name = name; // used in tick() below
this.time = time; // used in tick() below
this.base = base; // used in setup below

/*
Define a country in the current Flags object given its uppercase key code and name
*/
this.img = new Array();
function add(key, cname) {
  this.img[key] = new Object();
  this.img[key].src = this.base + key.toLowerCase() + ".gif";
  this.img[key].alt = "[" + key + "] " + cname;
  this.img[key].width = 34; this.img[key].height = 18;
}
this.add = add;

/*
  This list excludes dependencies of a country already present in this list,
  even if they were historically assigned an ISO3166-1 code, and dead countries.

  However the European Union is present because it has its own concessed territory
  for its institutions, it has an offical flag and provides citizenship for people,
  a government and a parliament, enforced laws, and internationally accepted
  passports and ambassadors at the UNO and in major countries; the EU code has
  been reserved for it in ISO3166-1, but it still hasn't been officially assigned.

  The same applies to the United Nations with the reserved code UN, but its
  instances have specific domain names in INT assigned for Internet by IANA,
  however UNO does not enforce laws and only approves resolutions, and does not
  provide citizenship and passports.
*/
   this.add("AD","Andorre (Principauté d')");
   this.add("AE","Émirats Arabes Unis");
   this.add("AF","Afghanistan");
   this.add("AG","Antigua et Barbuda");
// this.add("AI","Anguilla"); // UK territory
   this.add("AL","Albanie");
   this.add("AM","Arménie");
// this.add("AN","Antilles Néerlandaises"); // NL territory
   this.add("AO","Angola");
   this.add("AQ","Antarctique (Traité International de l')");
   this.add("AR","Argentine");
// this.add("AS","Samoa Américaines (Iles)"); // US territory
   this.add("AT","Autriche");
   this.add("AU","Australie");
// this.add("AW","Aruba"); // Former part of AN, NL territory
   this.add("AZ","Azerbaïdjan");
   this.add("BA","Bosnie Herzégovine");
   this.add("BE","Belgique (Royaume de)");
   this.add("BF","Burkina Faso");
   this.add("BG","Bulgarie");
   this.add("BH","Bahraïn (Émirat de)");
   this.add("BI","Burundi");
   this.add("BJ","Bénin");
// this.add("BM","Bermudes"); // UK territory
   this.add("BN","Bruneï Darussalam (Saltanat de)");
   this.add("BO","Bolivie");
   this.add("BR","Brésil (Fédération du)");
   this.add("BS","Bahamas (Communauté des)");
   this.add("BT","Bouthan (Royaume du)");
// this.add("BU","Birmanie (Royaume de)"); // see now MM
// this.add("BV","Bouvet (Ile)"); // NO territory
   this.add("BW","Botswana");
   this.add("BY","Bélarus");
   this.add("BZ","Bélize");
   this.add("CA","Canada");
// this.add("CC","Cocos (Keeling) (Iles)"); // AU territory
   this.add("CD","Congo (Kinshasa, République Démocratique du)");
   this.add("CF","Centrafrique");
   this.add("CG","Congo (Brazaville)");
   this.add("CH","Suisse (Confédération Helvétique)");
   this.add("CI","Côte d'Ivoire");
   this.add("CL","Chili");
   this.add("CM","Cameroun");
   this.add("CN","Chine (République Populaire de)");
   this.add("CO","Colombie");
   this.add("CR","Costa Rica");
// this.add("CS","Tchécoslovaquie"); // split into CZ and SL
   this.add("CU","Cuba");
   this.add("CV","Cap Vert (Iles du)");
// this.add("CX","Christmas (Ile)"); // AU territory
   this.add("CY","Chypre");
   this.add("CZ","Tchéquie");
// this.add("DD","Allemagne (République Démocratique d')"); // unified with DE
   this.add("DE","Allemagne (République Fédérale d')");
   this.add("DJ","Djibouti");
   this.add("DK","Danemark (Royaume du)");
   this.add("DM","Dominique (Communauté de la)");
   this.add("DO","Dominicaine (République)");
   this.add("DZ","Algérie");
   this.add("EC","Équateur");
   this.add("EE","Estonie");
   this.add("EG","Égypte (République Arabe d')");
   this.add("EH","Sahara Occidental");
   this.add("ER","Érithrée");
   this.add("ES","Espagne (Royaume d')");
   this.add("ET","Éthiopie (République Fédérale Démocratique d')");
   this.add("EU","Union Européenne");
   this.add("FI","Finlande");
   this.add("FJ","Fiji");
// this.add("FK","Falklands (Iles Malouines)"); // UK territory
   this.add("FM","Micronésie (États Fédérés de)");
// this.add("FO","Faroé (Iles)"); // DK territory
   this.add("FR","France");
// this.add("FX","France Métropolitaine"); // FR territory
   this.add("GA","Gambie");
   this.add("GB","Royaume Uni"); // UK territory
   this.add("GD","Grenade");
   this.add("GE","Géorgie");
// this.add("GG","Guernesey (Bailiwick de)"); // UK territory
   this.add("GH","Ghana");
// this.add("GI","Gibraltar"); // UK territory
   this.add("GM","Gambie");
   this.add("GN","Gabon");
// this.add("GP","Guadeloupe et Antilles Françaises"); // FR territory
   this.add("GQ","Guinée Équatoriale");
   this.add("GR","Grèce");
// this.add("GS","Géorgie du Sud et Sandwich (Iles de)"); // UK territory, within FK
   this.add("GT","Guatémala");
// this.add("GU","Guam (Iles de)"); // US territory
   this.add("GW","Guinée Bissau");
   this.add("GY","Guyana (République Coopérative du)");
// this.add("HM","Heard et McDonald (Iles)"); // AU territory
   this.add("HN","Honduras");
   this.add("HR","Croatie");
   this.add("HT","Haïti");
   this.add("HU","Hongrie");
   this.add("ID","Indonésie");
   this.add("IE","Irlande");
   this.add("IL","Israël (État d')");
   this.add("IN","Inde");
// this.add("IO","Océan Indien (Iles Britanniques de l')"); // UK territory
   this.add("IQ","Iraq");
   this.add("IR","Iran (République Islamique d')");
   this.add("IS","Islande");
   this.add("IT","Italie");
// this.add("JE","Jersey (Bailiwick de)"); // UK territory
   this.add("JM","Jamaïque");
   this.add("JO","Jordanie (Royaume Hashémite de)");
   this.add("JP","Japon (État Impérial du)");
   this.add("KE","Kénya");
   this.add("KG","Kirgyzstan");
   this.add("KH","Cambodge (Royaume Khmer du)");
   this.add("KI","Kiribati");
   this.add("KM","Comores");
   this.add("KN","Saint Kitts et Névis");
   this.add("KP","Corée (République Démocratique de)");
   this.add("KR","Corée du Sud");
   this.add("KW","Koweït (Émirat du)");
// this.add("KY","Cayman (Iles)"); // UK territory
   this.add("KZ","Kazakhstan");
   this.add("LA","Laos (République Populaire du)");
   this.add("LB","Liban");
   this.add("LC","Sainte Lucie");
   this.add("LI","Lithuanie");
   this.add("LK","Sri Lanka");
   this.add("LR","Libéria");
   this.add("LS","Lésotho (Royaume du)");
   this.add("LU","Luxembourg (Grand Duché du)");
   this.add("LV","Lettonie");
   this.add("LY","Lybie (Jamahiriya Arabe Socialiste Populaire de)");
   this.add("MA","Maroc (Royaume du)");
   this.add("MC","Monaco (Principauté de)");
   this.add("MD","Moldavie");
   this.add("MG","Madagascar");
   this.add("MH","Marshall (Iles)");
   this.add("MK","Macédoine (ex République Yougoslave de)");
   this.add("ML","Mali");
   this.add("MM","Myanmar (ex Birmanie)");
   this.add("MN","Mongolie");
// this.add("MO","Macao (Région Administrative Spéciale de)"); // CN territory
// this.add("MP","Marianes du Nord (Iles)"); // US territory
// this.add("MQ","Martinique"); // FR territory
   this.add("MR","Mauritanie");
// this.add("MS","Montserrat"); // UK territory
   this.add("MT","Malte");
   this.add("MU","Maurice");
   this.add("MV","Maldives");
   this.add("MW","Malawi");
   this.add("MX","Mexique (Union des États du)");
   this.add("MY","Malaisie (Royaume de)");
   this.add("MZ","Mozambique");
   this.add("NA","Namibie");
// this.add("NC","Nouvelle Calédonie"); // FR territory
   this.add("NE","Niger");
// this.add("NF","Norfolk"); // AU territory
   this.add("NG","Nigéria (République Fédérale du)");
   this.add("NI","Nicaragua");
   this.add("NL","Pays Bas (Royaume des)");
   this.add("NO","Norvège (Royaume de)");
   this.add("NP","Népal (Royaume du)");
   this.add("NR","Nauru");
// this.add("NT","Zone Neutre"); // deleted
   this.add("NU","Niué (État Associé de)");
   this.add("NZ","Nouvelle Zélande");
   this.add("OM","Oman (Sultanat d')");
   this.add("PA","Panama (République Unifiée de)");
// this.add("PC","Paracel (Iles)"); // CN territory
   this.add("PE","Pérou");
// this.add("PF","Polynésie Française"); // FR territory
   this.add("PG","Papouasie Nouvelle Guinée (État Indépendant de)");
   this.add("PH","Philippines");
   this.add("PK","Pakistan");
   this.add("PL","Pologne");
// this.add("PM","Saint-Pierre et Miquelon"); // FR territory
// this.add("PN","Pitcairn (Iles)"); // NZ territory
   this.add("PS","Palestine (Autorité de)");
   this.add("PT","Portugal");
   this.add("PW","Palaü");
   this.add("PY","Paraguay");
// this.add("PZ","Panama (Zone du Canal de)"); // deleted
   this.add("QA","Qatar (Émirat de)");
   this.add("RO","Roumanie");
   this.add("RU","Russie (Fédération de)");
   this.add("RW","Rwanda");
   this.add("SA","Arabie Saoudite (Royaume d')");
   this.add("SB","Salomon (Iles)");
   this.add("SC","Seychelles");
   this.add("SD","Soudan");
   this.add("SE","Suède (Royaume de)");
   this.add("SG","Singapour");
// this.add("SH","Sainte Hélène (Ile de)"); // UK territory
   this.add("SI","Slovénie");
// this.add("SJ","Svalbard et Jan Mayen (Iles)"); // NO territory
   this.add("SK","Slovaquie");
   this.add("SL","Sierra Léone");
   this.add("SM","Saint Marin");
   this.add("SN","Sénégal");
   this.add("SO","Somalie");
   this.add("SR","Suriname");
   this.add("ST","Saõ Tomé et Principe");
// this.add("SU","Union des Républiques Socialistes Soviétique"); // deleted
   this.add("SV","El Salvador");
   this.add("SY","Syrie (République Arabe de)");
   this.add("SZ","Swaziland");
// this.add("TC","Turks et Caïques (Iles)"); // UK territory
   this.add("TD","Tchad");
// this.add("TF","Territoire Français Austral et Antarctique"); // FR territory
   this.add("TG","Togo");
   this.add("TH","Thaïlande (Royaume de)");
   this.add("TJ","Tadjikistan");
// this.add("TK","Tokelau"); // NZ territory
   this.add("TM","Turkménistan");
   this.add("TN","Tunisie");
   this.add("TO","Tonga (Royaume de)");
   this.add("TP","Timor Oriental");
   this.add("TR","Turquie");
   this.add("TT","Trinidad et Tobago");
   this.add("TV","Tuvalu");
   this.add("TW","Taïwan (République de Chine)");
   this.add("TZ","Tanzanie (République Unifiée de)");
   this.add("UA","Ukraine");
   this.add("UG","Ouganda");
// this.add("UK","Royaume Uni"); // See GB
// this.add("UM","Mineures Lointaines Américaines (Iles)"); // US territory
   this.add("UN","Nations Unies (Organisation des)")
   this.add("US","États Unis d'Amérique (Fédération des)");
   this.add("UY","Uruguay");
   this.add("UZ","Ouzbékistan");
   this.add("VA","Saint Siège (Cité du Vatican, État du)");
   this.add("VC","Saint Vincent et les Grenadines");
   this.add("VE","Vénézuéla");
// this.add("VG","Vierges Britanniques (Iles)"); // UK territory
// this.add("VI","Vierges Américaines (Iles)"); // US territory
   this.add("VN","Viet Nam");
   this.add("VU","Vanuatu");
// this.add("WF","Wallis et Futuna (Iles)"); // FR territory
   this.add("WS","Samoa (Occidental, État Indépendant de)");
// this.add("YA","Yémen (République Arabe du)"); // unified with YE
   this.add("YE","Yémen (République Unifiée du)");
// this.add("YT","Mayotte"); // FR territory
   this.add("YU","Yougoslavie (Fédération de)");
   this.add("ZA","Afrique du Sud (Fédération d')");
// this.add("ZR","Zaïre"); // deleted, see CD
   this.add("ZM","Zambie");
   this.add("ZW","Zimbabwé");
   this.add("_F","La Francophonie"); // special code for flag of Francophony
   this.add("__","La Terre"); // special code for the universal flag of Earth

/*
  Build an object of keys by shuffling a string of codes, 2 uppercase chars each
*/
function Shuffle(keys) {
  keys = keys.toUpperCase(); // force upper case before shuffling actual keys
  var count = keys.length / 2;
  this.count = count;
  this.keys = "";
  this.curr = 0;
  while (count > 0) {
    var select = Math.floor(count * Math.random());
    this.keys += keys.substring(select*2, select*2+2);
    keys = keys.substring(0, select*2) + keys.substring(select*2+2, count*2);
    count--;
  }
  return this;
}

/*
  There are four lists of countries here, with an increasing number of countries:
  [0] is a small list of emphasized countries: francophony, european union
      countries, and other major entities.
  [1] is for other important countries: francophony, european countries, other
      important countries.
  [2] are countries with narrower references: small francophony, all other small
      european, other medium ones.
  [3] is the list of the numerous other remaining countries.
  Each list is individually randomly shuffled at time of instanciation, so that
  each country in each list has an equal rate of occurence. The increasing length
  of each list gives different rates of occurence in the flags banner, because
  these flags are taken successively in each list. This fragmentation avoids to
  display consecutively two major countries, but ensures that major countries will
  be displayed more often than others and shuffling ensures that all countries will
  have a chance to be displayed.
*/
this.toggle = 0; // initial value of the selector in countries lists
this.keys = new Array(
  new Shuffle("FrCaBeChLu" + "AtCzDeDkEsGbGrIeItNlNoSe" + "AuCnEuJpRuUs__"),
  new Shuffle("AdCiCmDzEgLbMaMcTnVn_f" + "CyEeFiHrHuIsLiLvMtPlPtRoSiSmTrYu" +
              "BrIdIlInKrMxMyNzPhSgThTwUnZa"),
  new Shuffle("AlBfBgBiBjCdCfCgCvDjDmGaGnGqGwHtJoKhKmLaLcMdMgMkMlMrMuNeRwScSnStTdTgVu" +
              "BaByGeKgKzSkUaUzVa" + "ArBoClCoCrCuFjLkMmPePkPySaVe"),
  new Shuffle("AeAfAgAmAoAqAzBhBnBsBtBwBzDoEcEhErEtFmGdGhGmGtGyHnIqIrJmKeKiKnKpKwLrLsLyMhMn" +
              "MvMwMzNaNgNiNpNrNuOmPaPgPsPwQaSbSdSlSoSrSvSySzTjTmToTpTtTvTzUgUyVcWsYeZmZw"));

this.curr = new Object();
this.curr.keys = keys.toUpperCase(); // initial countries displayed
this.curr.count = keys.length / 2;

for (var i = 0; i < this.curr.count; i++) {
  var key = this.curr.keys.substring(i*2, i*2+2);
  var html = '<img border="0"';
  if (document.all)
    html += ' id="' + this.name + i + '"';
  else if (document.images)
    html += ' name="' + this.name + i + '"';
  html += ' src="' + this.img[key].src +
    '" alt="' + this.img[key].alt +
    '" width="' + ((i == this.curr.count - 1) ? 4 : this.img[key].width) +
    '" height="' + this.img[key].height + '">';
  document.write(html);
}

this.elts = document.all ? document.all : document.images ? document.images : document;

/*
  This function animates the flags at every time tick
*/
function tick() {
  if (this.elts[this.name + 0].width > 4) {
    this.elts[this.name + 0].width -= 4;
    this.elts[this.name + (this.curr.count - 1)].width += 4;
  }
  else {
    this.elts[this.name + 0].width = this.elts[this.name + 1].width;
    this.elts[this.name + (this.curr.count - 1)].width = 4;
    if (--this.toggle < 0)
      this.toggle = this.keys.length - 1;
    var key = this.keys[this.toggle].keys.substring(
      this.keys[this.toggle].curr*2, this.keys[this.toggle].curr*2+2);
    if (--this.keys[this.toggle].curr < 0)
      this.keys[this.toggle].curr = this.keys[this.toggle].count - 1;
    this.curr.keys = this.curr.keys.substring(2, this.curr.keys.length) + key;
    for (var i = this.curr.count; --i >= 0;) {
      key = this.curr.keys.substring(i*2, i*2+2);
      var elt = this.elts[this.name + i];
      elt.src = this.img[key].src;
      elt.alt = this.img[key].alt;
    }
  }
  setTimeout(this.name + ".tick()", this.time);
}
this.tick = tick;
setTimeout(this.name + ".tick()", 4000);

return this;
}

