// <!-- coding: utf-8 -->

function ResetNotPlayed()
{
  for (var tn in champ.teams)
  {
    champ.teams[tn].played = false;
  }
}


function FormatNotPlayingRow(cnt)
{
  var s = '';

  for (var tn in champ.teams)
  {
    var t = champ.teams[tn];
    if (! t.played)
    {
      if (s != '') s += ', ';
      s += champ.FormatTeamName(t);
    }
  }

  if (s != '')
  {
    s = '<tr '+RowClass(cnt)+'><td colspan="5">'+getlang('not_playing')+': '+s+'</td></tr>';
  }

  return s;
}


function FormatRoundMatches(around, tableparams)
{
  ResetNotPlayed();

  var s = '';

  var result = '';
  result += '<table class="datatable" border="0" cellspacing="1" cellpadding="0" '+tableparams+'>';
  var cnt = 0;
  for (var mn in champ.matches)
  {
    var m = champ.matches[mn];
    if (m.MATCHROUND != around) continue;

    cnt++;

    result += '<tr '+RowClass(cnt)+'>';

    result += '<td align="center" width="80px">'+champ.FormatMatchTime(m.DUETIME)+'</td>';

    m.team1.played = true;
    m.team2.played = true;

    var tn = champ.FormatTeamName(m.team1);
    //if (m.winnerteam == 1) tn = '<b>'+tn+'</b>';
/*
    if (m.state != 1) s = '';
    else if (m.winnerteam == 1) s = 'win';
    else if (m.winnerteam == 2) s = 'lose';
    else s = 'draw';
*/
    result += '<td class="'+s+'" width="120px" align="right">'+tn+'</td>';

    var gs = '<b>&nbsp;-&nbsp;:&nbsp;-&nbsp;</b>';
    var mocode = '';
    if (m.STATE == 1)
    {
      gs = '<b>'+champ.FormatGoals(m.GOALS1, m.GOALS2, true)+'</b>';
      mocode = 'onmouseover="champ.ShowMatchGoalers('+m.ID+')" onmouseout="nd();"';
    }
    result += '<td width="80px" align="center" style="font-size: 180%; cursor:default;" '+mocode+'>'+gs+'</td>';

    var tn = champ.FormatTeamName(m.team2);
    //if (m.winnerteam == 2) tn = '<b>'+tn+'</b>';
/*
    if (m.state != 1) s = '';
    else if (m.winnerteam == 2) s = 'win';
    else if (m.winnerteam == 1) s = 'lose';
    else s = 'draw';
*/
    result += '<td class="'+s+'" width="120px">'+tn+'</td>';

    if (accesslevel >= 9)
      result += '<td width="15px"><a href="matchresult.php?id='+m.ID+'">&lt;&lt;</a></td>';
    else
      result += '<td width="15px">&nbsp;</td>';

    result += '</tr>';
  }

  result += FormatNotPlayingRow(cnt+1);
  result += '</table>';

  return result;
}


function ctcellinfo(t1,t2)
{
  var s = '';

  var tn1 = champ.teams[t1-1].NAME;
  var tn2 = champ.teams[t2-1].NAME;

  var m = champ.crossmatches[t1-1][t2-1];
  var g1 = '', g2 = '';

  s = '<table border="0" cellspacing="0" cellpadding="4">';

  if (m && (m.STATE == 1))
  {
    g1  = m.GOALS1;
    g2  = m.GOALS2;
/*
    if (g1 > g2) tn1 = '<b>'+tn1+'</b>';
    else if (g1 < g2) tn2 = '<b>'+tn2+'</b>';
*/
  }

  s += '<tr><td width="120px" align="right">'+tn1+'</td><td width="20px" align="center">-</td><td width="120px">'+tn2+'</td></tr>';

  if (m && (m.STATE == 1))
  {
    s += '<tr style="font-size: 250%"><td align="right">'+g1+'</td><td align="center">:</td><td>'+g2+'</td></tr>';

    var tg1 = '';
    var tg2 = '';

    for (var gi in m.goals)
    {
      var g = m.goals[gi];
      var tgs = g.player.NAME+': '+g.VALUE+'<br>';

      if (g.player.team == m.team1)
        tg1 += tgs;
      else
        tg2 += tgs;
    }

    if (tg1+tg2 != '')
    {
      s += '<tr style="font-weight: normal"><td valign="top">'+tg1+'</td><td>&nbsp;</td><td valign="top">'+tg2+'</td></tr>';
    }
  }

  s += '</table>';

  overlib(s,CENTER,WIDTH,280,ABOVE);

  var t = document.getElementById('crosstable');
  t.rows[t1].cells[t2].className = 'hl';
}

function ctcellinfo_out(t1,t2)
{
  nd();
  var t = document.getElementById('crosstable');
  t.rows[t1].cells[t2].className = '';
}

function ctteamhl(tid)
{
  var cw = 'lawngreen';
  var cl = 'tomato';
  var cd = 'gold';

  var tcnt = champ.teamcount;
  var t = document.getElementById('crosstable');
  var rows = t.rows;
  var cells;
  var r,c;

  var bgc;
  var m;

  for (n=1;n<=tcnt;n++)
  {
    if (n == tid) continue;

    r = n;
    c = tid;
    bgc = 'ctrowhl';
    m = champ.crossmatches[r-1][c-1];
    if (m && (m.STATE == 1))
    {
      if (m.GOALS1 < m.GOALS2) bgc = 'win';
      else if (m.GOALS1 > m.GOALS2) bgc = 'lose';
      else bgc = 'draw';
    }
    rows[r].cells[c].className = bgc;

    r = tid;
    c = n;
    bgc = 'ctrowhl';
    m = champ.crossmatches[r-1][c-1];
    if (m && (m.STATE == 1))
    {
      if (m.GOALS1 > m.GOALS2) bgc = 'win';
      else if (m.GOALS1 < m.GOALS2) bgc = 'lose';
      else bgc = 'draw';
    }
    rows[r].cells[c].className = bgc;
  }

  rows[tid].cells[0].className = 'gold';
  rows[tid].cells[tcnt+1].className = 'gold';
  rows[0].cells[tid].className = 'gold';
  rows[tcnt+1].cells[tid].className = 'gold';
}

function ctteamhl_out(tid)
{
  var tcnt = champ.teamcount;
  var t = document.getElementById('crosstable');
  var rows = t.rows;
  var n;

  for (n=1;n<=tcnt;n++)
  {
    if (n == tid) continue;
    rows[n].cells[tid].className = '';
    rows[tid].cells[n].className = '';
  }

  rows[tid].cells[0].className = '';
  rows[tid].cells[tcnt+1].className = '';
  rows[0].cells[tid].className = '';
  rows[tcnt+1].cells[tid].className = '';
}

function SetupCrossTable(tableid)
{
  var tcnt = champ.teamcount;

  var t = document.getElementById(tableid);
  var rows = t.rows;
  var cells;
  var r,c;
  for (r=1;r<=tcnt;r++)
  {
    cells = rows[r].cells;
    for (c=1;c<=tcnt;c++)
    {
      if (r == c) continue;
      var cx = cells[c];
      cx.onmouseover = new Function('ctcellinfo('+r+','+c+');');
      cx.onmouseout  = new Function('ctcellinfo_out('+r+','+c+');');
    }
  }

  for (c=1;c<=tcnt;c++)
  {
    var cx = rows[0].cells[c];
    cx.onmouseover = new Function('ctteamhl('+c+');');
    cx.onmouseout  = new Function('ctteamhl_out('+c+');');
    var cx = rows[tcnt+1].cells[c];
    cx.onmouseover = new Function('ctteamhl('+c+');');
    cx.onmouseout  = new Function('ctteamhl_out('+c+');');
  }

  for (r=1;r<=tcnt;r++)
  {
    var cx = rows[r].cells[0];
    cx.onmouseover = new Function('ctteamhl('+r+');');
    cx.onmouseout  = new Function('ctteamhl_out('+r+');');
    var cx = rows[r].cells[tcnt+1];
    cx.onmouseover = new Function('ctteamhl('+r+');');
    cx.onmouseout  = new Function('ctteamhl_out('+r+');');
  }
}

function hlround(r)
{
  if (r == selectedround) return;
  var cx = document.getElementById('ROUNDCELL'+r);
  cx.className = 'mousehl';
}

function hlround_out(r)
{
  if (r == selectedround) return;

  var cx = document.getElementById('ROUNDCELL'+r);
  cx.className = '';
}

function SelectRound(r)
{
  selectedround = r;

  var rcnt = champ.roundcount;
  var c;
  for (c=1;c<=rcnt;c++)
  {
    var cx = document.getElementById('ROUNDCELL'+c); //cells[c-1];
    if (c == r)
    {
      cx.className = 'hl strong'; //style.backgroundColor = 'gold';
    }
    else
    {
      cx.className = ''; //style.backgroundColor = '';
    }
  }

  var d = document.getElementById('ROUNDMATCHES');
  d.innerHTML = FormatRoundMatches(r,'width="100%" height="100%"');
}


function FormatRoundSelector()
{
  var s = '';
  var rcols = NumberVal(champ.roundcount);
  if (rcols == 30) rcols = 15;
  else if (rcols > 16) rcols = 16;

  s += '<table id="tbl_rounds" class="datatable" width="100%" border="0" cellspacing="1" cellpadding="0">';
  s += '<tr><th colspan="'+rcols+'">'+getlang('match_round')+'</th></tr>';

  if (champ.roundcount <= 0)
  {
    s += '</table>';
    return s;
  }

  var r;
  for (r=1;r<=champ.roundcount;r++)
  {
    var rs = r;
    if (r <= champ.activeround) rs = '<b>'+rs+'</b>';

    s += '<td id="ROUNDCELL'+r+'" align="center" style="cursor: pointer;" width="40px"'
       +'onmousedown="SelectRound('+r+');" onmouseover="hlround('+r+');" onmouseout="hlround_out('+r+');"'
       +'>'+rs+'</td>';

    if ((r % rcols) == 0)
    {
      s += '</tr>';
      s += '<tr>';
    }
  }

  while (((r-1) % rcols) != 0)
  {
    r++;
    s += '<td></td>';
  }

  s += '</tr></table>';
  return s;
}

function FormatMatchRounds()
{
  var s = '';
  s += '<table border="0" cellspacing="1" cellpadding="0" height="100%" width="100%">';
  s += '<tr><td>' + FormatRoundSelector() + '</td></tr>';
  // spacing row
  s += '<tr height="8px"><td></td></tr>';
  // round matches
  s += '<tr height="100%"><td id="ROUNDMATCHES">';
  s += '</td></tr>';
  s += '</table>';
  return s;
}

