var livescoringInitialized = false;
var livescoringPlayerNames = {};
var livescoringResultCache = {};

function livescoringHandleResultUpdates(r,e)
{
  $.each(r, function(m,d)
  {
    var s = '.'+m;

    $.each(d, function(k,v)
    {
          switch(k)
          {
            case 'p1': case 'p2':
              // otteluohjelman pelaajien päivitys
              $(s+' .'+k).text(livescoringPlayerNames[v]);
              if (d.court > 0 && d.ended == 0)
                $('.livescoringTable'+d.court+' .'+k).text(livescoringPlayerNames[v]);
              break;

            case 'g1': case 'g2':
              // jos tulos muuttunut -> animoi
              if (livescoringResultCache[m][k] != v) {
                $(s+' .'+k).text(v);
                // animaatio liveseurantaan
                $('.livescoringTable'+d.court+' .'+k)
                  .queue (function () {
                    $(this).animate({backgroundColor:"#ff0",color:"#000"},1000);
                    $(this).dequeue();
                  })
                  .queue (function () {
                    $(this).animate({color:"#ff0"},500);
                    $(this).dequeue();
                  });
                $('.livescoringTable'+d.court+' .'+k).text(v);
                $('.livescoringTable'+d.court+' .'+k)
                  .queue (function () {
                    $(this).animate({color:"#000"},500);
                    $(this).dequeue();
                  })
                $('.livescoringTable'+d.court+' .'+k)
                  .queue(function () {
                    $(this).animate({backgroundColor:"#000",color:"#fff"},1000);
                    $(this).dequeue();
                  });
                $('.livescoringTable'+d.court+' .'+k)
                  .css({backgroundColor:"#000",color:"#fff"});
              } else if (livescoringResultCache[m][k] == (d.bestof+1)/2) {
                // ottelu päättynyt
                setTimeout('livescoringClearTable('+d.court+')', 10000);
              }
              break;

            case 'mnbr': case 'bestof':
                $(s+' .'+k).html(v);
                if (d.ended == 0)
                  $('.livescoringTable'+d.court+' .'+k).text(v);
                break;

/*
            case 'court':
              // liveseurannan pelien päivitys animaatioilla
              var raceto = (d.bestof+1) / 2;
              if (d.court && d.g1 <= raceto && d.g2 <= raceto) {
                $('.livescoringTable'+d.court+' .p1').html(livescoringPlayerNames[d.p1]);
                $('.livescoringTable'+d.court+' .p2').html(livescoringPlayerNames[d.p2]);
                $('.livescoringTable'+d.court+' .g1').html(d.g1);
                $('.livescoringTable'+d.court+' .g2').html(d.g2);
                $('.livescoringTable'+d.court+' .bestof').html('('+d.bestof+')');
                if (d.g1 == raceto) {
                  $('.livescoringTable'+d.court+' .p1').addClass('winner');
                  $('.livescoringTable'+d.court+' .g1').addClass('winner');
                } else if (d.g2 == raceto) {
                  $('.livescoringTable'+d.court+' .p2').addClass('winner');
                  $('.livescoringTable'+d.court+' .g2').addClass('winner');
                } else {
                  $('.livescoringTable'+d.court+' .p1').removeClass('winner');
                  $('.livescoringTable'+d.court+' .g1').removeClass('winner');
                  $('.livescoringTable'+d.court+' .p2').removeClass('winner');
                  $('.livescoringTable'+d.court+' .g2').removeClass('winner');
                }
              }
              break;
*/
            default:
          }
    });
    // tallenna otteluvälimuisti
    livescoringResultCache[m] = d;
    // jos ottelu päättynyt -> tyhjennä liveottelu pikku viiveellä
    if (d.ended == 1) {
      // setTimeout('livescoringClearTable('+d.court+')', 10000);
    }
  });
}
/*
                .animate({backgroundColor:"#ff0"}, 500)
                .animate({backgroundColor:"#fff"}, 500)
                .queue(function(){
                  $(this).css({backgroundColor:""});
                  $(this).dequeue();
                });
*/
function livescoringPollUpdates() {
  $.ajax ({
    type: "POST",
    url: 'ajax/livescoring.php',
    data: 'axn=pollupdates',
    cache: false,
    async: true,
    success: function (json) {
      if (json && json.results) {
        livescoringHandleResultUpdates(json.results);
      } else {}
    },
    complete: function(p1,p2){
      setTimeout('livescoringPollUpdates()',8000);
    },
    dataType: "json"
  });
}

function livescoringInitTables() {
  $.each(livescoringResultCache, function (m, d) {
    if (d.court && d.ended == 0) {
      $('.livescoringTable'+d.court+' .mnbr').text(d.mnbr);
      $('.livescoringTable'+d.court+' .p1').text(livescoringPlayerNames[d.p1]);
      $('.livescoringTable'+d.court+' .p2').text(livescoringPlayerNames[d.p2]);
      $('.livescoringTable'+d.court+' .g1').text(d.g1);
      $('.livescoringTable'+d.court+' .g2').text(d.g2);
    }
  });
}

function livescoringClearTable(t) {
  if (t) {
    $('.livescoringTable'+t+' .mnbr').text('');
    $('.livescoringTable'+t+' .p1').text('');
    $('.livescoringTable'+t+' .p2').text('');
    $('.livescoringTable'+t+' .g1').text('');
    $('.livescoringTable'+t+' .g2').text('');
  }
}

function livescoringInit() {
  $.ajax ({
    type: "POST",
    url: 'ajax/livescoring.php',
    data: 'axn=init',
    cache: false,
    async: true,
    success: function (json) {
      if (json && json.players && json.results && json.html){
        livescoringPlayerNames = json.players;
        livescoringResultCache = json.results;
        $("div.livescoringSchedule").html (json.html);
        livescoringInitTables();
/*
        livescoringHandleResultUpdates(json.results);
*/
      }
    },
    complete: function(p1,p2){
      livescoringPollUpdates();
    },
    dataType: "json"
  });
}

$(function (){
  livescoringInit();
});

