// champ.js !-- coding: utf-8

champ = { ID:-1, id:-1
          ,teams:[]
          ,matches:[]
          ,crossmatches:[]
          ,goals:[]
        };

champ.LoadChamp = function(row)
{
  var pn;
  for (pn in row)
  {
    champ[pn] = row[pn];
  }
  champ.id = champ.ID; // for avoid mistakes...
}

champ.LoadTeams = function(ateams) // array of teams
{
  champ.teams = ateams;
  champ.teamsbyid = {};
  champ.teamcount = 0;
  champ.crossmatches = [];

  for (var n in champ.teams)
  {
    var t = champ.teams[n];
    t.id = t.ID; // for avoid mistakes..
    t.name = t.NAME; // too
    t.idx = n;
    champ.teamsbyid[t.ID] = t;

    t.matchnum  = 0;
    t.wincount  = 0;
    t.drawcount = 0
    t.losecount = 0
    t.points    = 0;
    t.goals1    = 0;
    t.goals2    = 0;
    t.goaldiff  = 0;

    t.members = [];

    champ.crossmatches[t.idx] = [];

    champ.teamcount++;
  }

  champ.totalrounds = champ.teamcount;
  if (champ.teamcount % 2 == 0) champ.totalrounds--;
  if (champ.CHAMPSUBMODE != 2) champ.totalrounds = champ.totalrounds * 2;
}

champ.LoadMatches = function(amatches) // array of matches
{
  var n;
  var lastround = -1;

  champ.matches = amatches;

  champ.matchesbyid = {};
  champ.matchcount = 0;
  champ.roundcount = 0;
  champ.activeround = 0;

  for (n in champ.matches)
  {
    var m = champ.matches[n];
    champ.matchesbyid[m.ID] = m;
    m.team1 = champ.teamsbyid[m.TEAM1];
    m.team2 = champ.teamsbyid[m.TEAM2];
    m.goals = [];
    champ.matchcount++;

    if (m.STATE == 1)
    {
      champ.ProcessMatch(m);
      champ.activeround = m.MATCHROUND;
    }

    if (lastround != m.MATCHROUND)
    {
      champ.roundcount++;
      lastround = m.MATCHROUND;
    }
  }
}

champ.LoadTeamMembers = function(adata)
{
  champ.playersbyid = {};
  for (var ni in adata)
  {
    var tr = adata[ni];
    var t = champ.teamsbyid[tr.TEAMID];
    t.members = tr.members;
    for (var mi in t.members)
    {
      var tm = t.members[mi];
      tm.team = t;
      tm.goals = [];
      champ.playersbyid[tm.ID] = tm;
    }
  }
}

champ.LoadGoals = function(agoals)
{
  champ.goals = agoals;
  for (var gi in champ.goals)
  {
    var g = agoals[gi];
    g.player = champ.playersbyid[g.PLAYERID];
    g.match  = champ.matchesbyid[g.MATCHID];
    if (g.player !== undefined)  g.player.goals.push(g);
    if (g.match != undefined)  g.match.goals.push(g);
  }
}


champ.FormatMatchTime = function(adatestr)
{
  if (StringVal(adatestr) == '') return '';
  var dt = DateFromSql(adatestr);
  return '<span style="font-weight: normal">'+DateFormat(dt,'yyyy-mm-dd')+'</span> '+DateFormat(dt,'HH:MM');
}

champ.FormatTeamName = function(ateam)
{
  if (ateam == 0) return '???';

  var t = ateam;
  if (typeof(t) != 'object')
  {
    t = champ.teamsbyid[t];
  }
  return '<a href="team.php?id='+t.ID+'">'+t.NAME+'</a>';
}

champ.FormatTeamNameNL = function(ateam)
{
  if (ateam == 0) return '???';

  var t = ateam;
  if (typeof(t) != 'object')
  {
    t = champ.teamsbyid[t];
  }
  return t.NAME;
}

champ.FormatGoals = function(goals1,goals2,hlwinner)
{
  var g1 = String(goals1);
  var g2 = String(goals2);
  var sldiff = g1.length - g2.length;
  while (sldiff > 0)
  {
    g2 = g2+'&nbsp;&nbsp;';
    sldiff--;
  }
  while (sldiff < 0)
  {
    g1 = '&nbsp;&nbsp;'+g1;
    sldiff++;
  }
/*
  if (hlwinner)
  {
    if (goals1 > goals2) g1 = '<b>'+g1+'</b>';
    if (goals2 > goals1) g2 = '<b>'+g2+'</b>';
  }
*/
  return g1+'&nbsp;:&nbsp;'+g2;
}

champ.ProcessMatch = function(m)
{
  var t1 = m.team1;
  var t2 = m.team2;

  t1.matchnum++;
  t2.matchnum++;
  if (m.WINNERTEAM == 2)
  {
    t2.wincount++;
    t2.points += 3;
    t1.losecount++;
  }
  else if (m.WINNERTEAM == 1)
  {
    t1.wincount++;
    t1.points += 3;
    t2.losecount++;
  }
  else
  {
    t1.drawcount++;
    t2.drawcount++;
    t1.points += 1;
    t2.points += 1;
  }

  t1.goals1 += m.GOALS1;
  t1.goals2 += m.GOALS2;
  t2.goals1 += m.GOALS2;
  t2.goals2 += m.GOALS1;

  t1.goaldiff = t1.goals1 - t1.goals2;
  t2.goaldiff = t2.goals1 - t2.goals2;

  champ.crossmatches[t1.idx][t2.idx] = m;
}


function SortFuncTopGoalers(p1,p2)
{
  if (p1.sumgoals > p2.sumgoals) return -1;
  if (p1.sumgoals < p2.sumgoals) return 1;
  return 0;
}

champ.CalculateTopGoalers = function()
{
  champ.topgoalers = [];
  for (var mi in champ.playersbyid)
  {
    var tm = champ.playersbyid[mi];
    tm.sumgoals = 0;
    champ.topgoalers.push(tm);
  }

  for (gi in champ.goals)
  {
    var g = champ.goals[gi];
    if (g.player !== undefined)
    {
      g.player.sumgoals += NumberVal(g.VALUE);
    }
  }

  champ.topgoalers.sort(SortFuncTopGoalers);

  var tpos = 1;
  var n = 1;
  var prevtm = undefined;
  for (var gi in champ.topgoalers)
  {
    var tm = champ.topgoalers[gi];
    if ((prevtm == undefined) || (SortFuncTopGoalers(prevtm,tm) != 0))
    {
      tpos = n;
    }
    tm.topgoalerpos = tpos;
    prevtm = tm;
    n++;
  }
}

champ.FormatMemberGoals_table = function(mid)
{
  var s = '';

  s +='<div style="color:white">';
  s +='<table class="datatable" border="0" cellspacing="1" cellpadding="0">';
  s +='<tr>';
  s +='<th>Ford.</th>';
  s +='<th>Ellenfél</th>';
  s +='<th>'+getlang('goals')+'</th>';
  s +='</tr>';

  var tm = champ.playersbyid[mid];
  //s += tm.NAME+'<br>';
  for (gi in tm.goals)
  {
    var g = tm.goals[gi];
    var t = g.match.team1;
    if (tm.team == t) t = g.match.team2;

    s +='<tr '+RowClass(gi+1)+'>';
    s +='<td align="right">'+g.match.MATCHROUND+'.</td>';
    s +='<td>'+t.NAME+'</td>';
    s +='<td align="right" class="strong">'+g.VALUE+'</td>';
    s +='</tr>';
  }
  s += '</table>';
  s += '</div>';
  return s;
}

champ.FormatMemberGoals = function(mid)
{
  var s = '';
  var tm = champ.playersbyid[mid];
  //s += tm.NAME+'<br>';
  for (gi in tm.goals)
  {
    var g = tm.goals[gi];
    var t = g.match.team1;
    if (tm.team == t) t = g.match.team2;
    //s += g.match.MATCHROUND+'. '+getlang('match_round')+': '+g.VALUE+'<br>';
    s += g.VALUE+'<span style="font-weight: normal">&nbsp;&nbsp;-&gt;&nbsp;&nbsp;'+t.NAME+' ('+g.match.MATCHROUND+'.)</span><br>';
  }
  //s += tm.sumgoals;
  return s;
}

champ.FormatTopGoalers = function()
{
  var s = '';

  s +='<table class="datatable" width="96%" border="0" cellspacing="1" cellpadding="0">';
  s +='<tr>';
  s +='<th width="30px">'+getlang('position_short')+'</th>';
  s +='<th width="150px">'+getlang('name')+'</th>';
  s +='<th width="30px">'+getlang('goals')+'</th>';
  s +='<th width="200px">'+getlang('team')+'</th>';
  s +='</tr>';

  for (var n in champ.topgoalers)
  {
    var tm = champ.topgoalers[n];

    if (tm.sumgoals < 1) break;

    s +='<tr '+RowClass(NumberVal(n))+'>';
    s +='<td align="right">'+tm.topgoalerpos+'.</td>';
    s +='<td class="strong">'+tm.NAME+'</td>';
    s +='<td align="right" class="strong" style="cursor: default" onmouseover="overlib(champ.FormatMemberGoals('+tm.ID+'))" onmouseout="nd()">'+tm.sumgoals+'</td>';
    s +='<td>'+tm.team.NAME+'</td>';
    s +='</tr>';
  }

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

champ.FormatCrossTable = function(tableparams)
{
  var s = '';

  var tds = '';
  //if (champ.teamcount > 12) tds = 'style="font-size: 65%"';
  if (champ.teamcount > 8) tds = 'style="font-size: 75%"';

  s += '<div '+tds+'><table class="datatable" border="0" cellspacing="1" cellpadding="0" '+tableparams+'>';
  var htitle = '';
  var r,c;

  var thpar = ' width="'+Math.round(100 / (champ.teamcount+2))+'%"';

  for (r in champ.teams)
  {
    htitle += '<th'+thpar+'>'+champ.teams[r].SHORTNAME+'</th>';
  }
  htitle = '<tr><th'+thpar+'>&nbsp;</th>'+htitle+'<th'+thpar+'>&nbsp;</th></tr>';

  s += htitle;

  for (r in champ.teams)
  {
    s +='<tr>';
    s +='<th>'+champ.teams[r].SHORTNAME+'</th>';
    for (c in champ.teams)
    {
      if (c == r)
      {
        s += '<td class="diagonal">&nbsp;</td>';
      }
      else
      {
        m = champ.crossmatches[r][c];
        if (m && (m.STATE == 1))
          s += '<td align="center">'
             //'<span onmouseover="overlib(\'aaa\');" onmouseout="nd();">'
             + champ.FormatGoals(m.GOALS1, m.GOALS2, false)
             //+'</span>'
             +'</td>';
        else
          s +='<td>&nbsp;</td>';
      }
    }
    s +='<th>'+champ.teams[r].SHORTNAME+'</th>';
    s +='</tr>';
  }

  s += htitle;
  s += '</table></div>';
  return s;
}



champ.ShowMatchGoalers = function(mid)
{
  var m = champ.matchesbyid[mid];
  var data_ok = false;

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

  if (m && (m.STATE == 1))
  {
    var tg1 = '<table border="0">';
    var tg2 = tg1;

    for (var gi in m.goals)
    {
      var g = m.goals[gi];
      var tgs = '<tr><td style="font-weight: normal">'+g.player.NAME+':</td><td align="right">'+g.VALUE+'</td></tr>';

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

      data_ok = true;
    }

    tg1 += '</table>';
    tg2 += '</table>';

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

  s += '</table>';

  if (data_ok) overlib(s,CENTER,WIDTH,280);
}

champ.FormatTeamMatches = function(tid, tableparams)
{
  var s;
  var result = '';
  result += '<table class="datatable" border="0" cellspacing="1" cellpadding="0" '+tableparams+'>';
  var nextround = 1;
  for (mi in champ.matches)
  {
    var m = champ.matches[mi];

    if ( (m.TEAM1 != tid) && (m.TEAM2 != tid) ) continue;

    while (nextround < m.MATCHROUND)
    {
      result += '<tr>';
      result += '<td width="10px" align="center">'+nextround+'</td>';
      result += '<td class="strong" align="center">'+getlang('not_playing')+'</td>';
      result += '<td>&nbsp;</td>';
      result += '<td>&nbsp;</td>';
      result += '<td>&nbsp;</td>';
      result += '<td>&nbsp;</td>';
      result += '</tr>';
      nextround++;
    }

    nextround = m.MATCHROUND+1;

    result += '<tr>';

    result += '<td width="10px" align="center">'+m.MATCHROUND+'</td>';

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

    tn = champ.FormatTeamName(m.team1);
    //if (m.winnerteam == 1) tn = '<b>'+tn+'</b>';
    s = '';
    if ((m.STATE == 1) && (m.TEAM1 == tid))
    {
      if (m.WINNERTEAM == 1) s = ' class="win"';
      else if (m.WINNERTEAM == 2) s = ' class="lose"';
      else s = ' class="draw"';
    }
    result += '<td width="100px" align="right" '+s+'>'+tn+'</td>';

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

    tn = champ.FormatTeamName(m.team2);
    //if (m.winnerteam == 2) tn = '<b>'+tn+'</b>';
    s = '';
    if ((m.STATE == 1) && (m.TEAM2 == tid))
    {
      if (m.WINNERTEAM == 2) s = ' class="win"';
      else if (m.WINNERTEAM == 1) s = ' class="lose"';
      else s = ' class="draw"';
    }
    result += '<td width="100" '+s+'>'+tn+'</td>';

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

    result += '</tr>';
  } // for

  result += '</table>';
  return result;
}

champ.CalculateTabella = function(toround)
{
  champ.tabella = [];
  // init
  var n;
  for (n=1;n<=champ.teamcount;n++)
  {
    var t = champ.teams[n-1];
    t.matchnum  = 0;
    t.wincount  = 0;
    t.losecount = 0;
    t.drawcount = 0;
    t.goals1 = 0;
    t.goals2 = 0;
    t.points = 0;
    t.goaldiff = 0;
    t.tabellapos = 0;
    t.tposhist = [];
    champ.tabella[n-1] = t;
  }
  // processing matches
  var lastround = -1;
  for (n in champ.matches)
  {
    var m = champ.matches[n];
    if ((m === undefined) || (m.MATCHROUND > toround))
    {
      break; //-->
    }

    if (m.MATCHROUND != lastround)
    {
      if (lastround > 0)
      {
        champ.SortTabella();
        champ.SaveTabellaPosToHist(lastround);
      }
      lastround = m.MATCHROUND;
    }

    if (m.STATE == 1)
    {
      champ.ProcessMatch(m);
    }
  }

  if (lastround > 0)
  {
    champ.SortTabella();
    champ.SaveTabellaPosToHist(lastround);
  }
}

function SortFuncTabella(t1,t2)
{
  if (t1.points > t2.points) return -1;
  if (t1.points < t2.points) return 1;
  if (t1.goaldiff > t2.goaldiff) return -1;
  if (t1.goaldiff < t2.goaldiff) return 1;
  if (t1.goals1 > t2.goals1) return -1;
  if (t1.goals1 < t2.goals1) return 1;
  return 0;
}

champ.SortTabella = function()
{
  champ.tabella.sort(SortFuncTabella);

  var tpos = 1;
  var n;
  var prevt = undefined;
  for (n=1;n<=champ.teamcount;n++)
  {
    var t = champ.tabella[n-1];
    if ((prevt == undefined) || (SortFuncTabella(prevt,t) != 0))
    {
      tpos = n;
    }
    t.tabellapos = tpos;
    prevt = t;
  }
}

champ.SaveTabellaPosToHist = function(around)
{
  var n;
  for (n=1;n<=champ.teamcount;n++)
  {
    var t = champ.tabella[n-1];
    t.tposhist[around] = t.tabellapos;
  }
}

champ.FormatTabella = function(tableparams, hlteam)
{
  var s = '';
  var hc = 'class="gold"';
  s += '<table id="TABELLA" class="datatable" border="0" cellspacing="1" cellpadding="0" '+tableparams+'>';
  s += '<tr>';
  s += '<th '+hc+' width="20px">'+getlang('position_short')+'</th>';
  s += '<th '+hc+' width="100px">'+getlang('name')+'</th>';
  s += '<th '+hc+' width="20px">Pt.</th>';
  s += '<th '+hc+' width="20px">P. %</th>';
  s += '<th '+hc+' width="20px">&nbsp;</th>';
  s += '<th '+hc+' width="20px">M</th>';
  s += '<th '+hc+' width="20px">Gy</th>';
  s += '<th '+hc+' width="20px">D</th>';
  s += '<th '+hc+' width="20px">V</th>';
  s += '<th '+hc+' width="50px">'+getlang('goals')+'</th>';
  s += '<th '+hc+' width="20px">+/-</th>';
  //s += '<th '+hc+' width="40px">'+getlang('name')+'</th>';
  s += '</th>';

  var n;
  for (n=1;n<=champ.teamcount;n++)
  {
    var t = champ.tabella[n-1];

    if (hlteam == t.ID)
      var hls = 'class="selected"';
    else
      var hls = RowClass(n+1);

    if (t.matchnum > 0)
      tpercent = Math.round(100*t.points/(t.matchnum*3))+'&nbsp;%';
    else
      tpercent = '&nbsp;';

    s += '<tr '+hls+'>';
    s += '<td align="right">'+t.tabellapos+'.</td>';
    s += '<td>'+champ.FormatTeamName(t)+'</td>';
    //s += '<td>'+t.shortname+'</td>';
    s += '<td align="right" class="strong">'+t.points+'</td>';
    s += '<td align="right">'+tpercent+'</td>';
    s += '<td>&nbsp;</td>';
    s += '<td align="right">'+t.matchnum+'</td>';
    s += '<td align="right">'+t.wincount+'</td>';
    s += '<td align="right">'+t.drawcount+'</td>';
    s += '<td align="right">'+t.losecount+'</td>';
    s += '<td align="center">'+champ.FormatGoals(t.goals1,t.goals2,false)+'</td>';
    s += '<td align="right">'+t.goaldiff+'</td>';
    s += '</tr>';
  }

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

// general functions:

function RowClass(rownum)
{
  if (rownum % 2 == 0)
    return '';
  else
    return ' class="line2"';
}

