lang = {
  starts_in: "startet in",
  h: "std",
  m: "min",
  ft: "FT",
  ht: "HT",
  group: "Gruppe",
  wc_sign: "FIFA World Cup 2010",
  round_16: "Achtel-Finale",
  quater: "Viertel-Finale",
  semi: "Halb-Finale",
  match_final: "Finale",
  match_3rd: "Match um den 3. Platz"
};

country = {
 za: "Südafrika", mx: "Mexiko", uy: "Uruguay", fr: "Frankreich", kr: "Südkorea", ar: "Argentinien", ng: "Nigeria", gr: "Griechenland", si: "Slowenien", us: "USA", xe: "England", dz: "Algerien", de: "Deutschland", gh: "Ghana", rs: "Serbia", au: "Australien", nl: "Niederlande", jp: "Japan", cm: "Kamerun", dk: "Dänemark", py: "Paraguay", it: "Italien", nz: "Neuseeland", sk: "Slowakei", br: "Brasilien", kp: "Nordkorea", ci: "Elfenbeinküste", pt: "Portugal", es: "Spanien", ch: "Schweiz", hn: "Honduras", cl: "Chile"
};

function detect_country(name) {
  if(/Afric/i.test(name)) return 'za';
  if(/Mexic/i.test(name)) return 'mx';
  if(/Urug/i.test(name)) return 'uy';
  if(/Franc/i.test(name)) return 'fr';

  if(/Korea/i.test(name) && ! /[DSN]/i.test(name)) return 'kr';
  if(/Argent/i.test(name)) return 'ar';
  if(/Nigeria/i.test(name)) return 'ng';
  if(/Gree/i.test(name)) return 'gr';

  if(/Slovenia/i.test(name)) return 'si';
  if(/USA/i.test(name)) return 'us';
  if(/states/i.test(name)) return 'us';
  if(/Engl/i.test(name)) return 'xe';
  if(/Alger/i.test(name)) return 'dz';

  if(/German/i.test(name)) return 'de';
  if(/Ghan/i.test(name)) return 'gh';
  if(/Serbia/i.test(name)) return 'rs';
  if(/Austral/i.test(name)) return 'au';

  if(/Netherl/i.test(name)) return 'nl';
  if(/Japan/i.test(name)) return 'jp';
  if(/Cameroon/i.test(name)) return 'cm';
  if(/Denma/i.test(name)) return 'dk';

  if(/Parag/i.test(name)) return 'py';
  if(/Ital/i.test(name)) return 'it';
  if(/Zeal/i.test(name)) return 'nz';
  if(/Slovak/i.test(name)) return 'sk';

  if(/Braz/i.test(name)) return 'br';
  if(/Korea/i.test(name)) return 'kp';
  if(/ivoi/i.test(name)) return 'ci';
  if(/Portug/i.test(name)) return 'pt';

  if(/Spain/i.test(name)) return 'es';
  if(/Swi/i.test(name)) return 'ch';
  if(/Hondur/i.test(name)) return 'hn';
  if(/Chil/i.test(name)) return 'cl';
  return 'xx';
}

// Match number to switch tab
var match_number = 'none';

// Live Scores
ls = {
  container_marquee: 'container_marquee',

  content: [],

  update: function() {
    var date = new Date();
    timezone = Math.round(date.getTimezoneOffset() / -60);
    var url = '/ls/ajax/soccer.php?action=matches&mode_date=single_date&ordering=league&sub_ordering=az&live=0&timezone='+timezone+'&result_after=leave&active=' + (date.getDate()<10?'0':'') + date.getDate() + '-' + (date.getMonth()<9?'0':'') + (date.getMonth()+1) + '-' + date.getFullYear(); // %d-%m-%Y
    new Ajax.Request(url, {
      method: 'get',
      onSuccess: function(transport) {
        var content = transport.responseText.evalJSON();
        prev_lid = 0;
        ls.content = [];
        content.each(function(match, index) {
          if(typeof match.lid != 'undefined') {
            prev_lid = match.lid;
          } else {
            match.lid = prev_lid;
          }
          // Delete match if it is not related to WC.
          if(match.lid == 396 || match.lid == 577) {
            ls.content.push(match);
          }
        });
        ls.content.each(function(match, index) {
          // Edit date
          time = match.t.split(':');
          match.hour = parseInt(time[0], 10);
          match.minute = parseInt(time[1], 10);
          match.hours_left = match.hour - date.getHours();
          match.minutes_left = match.minute - date.getMinutes();
          if (match.minutes_left < 0 && match.hours_left > 0) {
            match.minutes_left = match.minutes_left + 60;
            match.hours_left = match.hours_left - 1;
          }
          if (match.hours_left*60 + match.minutes_left < 0) {
            match.minutes_left = -1;
            match.hours_left = -1;
          }
          // detect country
          match.home_country = detect_country(match.th);
          match.away_country = detect_country(match.ta);
          match.home_country_lang = country[match.home_country];
          match.away_country_lang = country[match.away_country];
        });
        ls.render_marquee();
        setTimeout('if(ls.update) ls.update();', 60000);
      }
    });
  },

  render_marquee: function() {
    if(ls.content.length == 0) return;
    var text = '';
    ls.content.each(function(match) {
      text = text + '<span style="margin-left: 30px;"></span>'
                  + '<strong>' + match.home_country_lang + '</strong>'
                  + ' &mdash; '
                  + '<strong>' + match.away_country_lang + '</strong>';
      if(match.minutes_left > 0 || match.hours_left > 0) {
        text = text + ' ' + lang.starts_in+ ' <strong>'
                    + (match.hours_left>0   ? (match.hours_left + lang.h)   : '')  + ' '
                    + (match.minutes_left>0 ? (match.minutes_left + lang.m) : '') + '</strong>';
      } else if (match.s == 'full_time') {
        text = text + ' <strong>' + lang.ft + ' ' + match.ths + ':' + match.tas + '</strong>';
      } else {
        text = text + ' <strong>' + match.ths + ':' + match.tas + '</strong>';
      }
    });
    $(ls.container_marquee).innerHTML = text;
  },

  launch: function() {
    ls.update();
  }
}

//window.onload = function() {
  ls.launch();
//};


