<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" version="1.0">

  <xsl:variable name="league_url" select="'/league_tables.asp#'"/>
  <xsl:variable name="team_url" select="'/player_records.asp?team='"/>
  <xsl:variable name="player_url" select="'/player_records.asp?player='"/>
  <xsl:variable name="ecf_url" select="'http://grading.bcfservices.org.uk/getref.php?ref='"/>
  <xsl:variable name="ecf_club_url" select="'http://grading.bcfservices.org.uk/getclub.php?club='"/>
  <xsl:template name="string-replace-all">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="by"/>
    <xsl:choose>
      <xsl:when test="contains($text,$replace)">
        <xsl:value-of select="substring-before($text,$replace)"/>
        <xsl:value-of select="$by"/>
        <xsl:call-template name="string-replace-all">
          <xsl:with-param name="text" select="substring-after($text,$replace)"/>
          <xsl:with-param name="replace" select="$replace"/>
          <xsl:with-param name="by" select="$by"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="string-format">
    <xsl:param name="text"/>
    <xsl:choose>
      <xsl:when test="contains($text,'0.5')">
        <xsl:call-template name="string-replace-all">
          <xsl:with-param name="text" select="$text"/>
          <xsl:with-param name="replace" select="'0.5'"/>
          <xsl:with-param name="by" select="'½'"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="string-replace-all">
          <xsl:with-param name="text" select="$text"/>
          <xsl:with-param name="replace" select="'.5'"/>
          <xsl:with-param name="by" select="'½'"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="player-format">
    <xsl:param name="ref"/>
    <xsl:param name="league"/>
    <xsl:variable name="name" select="key('players',$ref)"/>
    <xsl:choose>
      <xsl:when test="$name//text()">
        <xsl:element name="a">
          <xsl:attribute name="href">
            <xsl:value-of select="$player_url"/>
            <xsl:value-of select="$ref"/>
            <xsl:if test="$league">&amp;league=<xsl:value-of select="$league"/></xsl:if>
          </xsl:attribute>
          <xsl:attribute name="title">
            <xsl:value-of select="$name/@grade"/>
            <xsl:text> </xsl:text>
            <xsl:value-of select="$name/@team"/>
          </xsl:attribute>
          <xsl:value-of select="$name"/>
        </xsl:element>
      </xsl:when>
      <xsl:when test="$ref = 'Default'">
        <i>
          <xsl:value-of select="$ref"/>
        </i>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$ref"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="grade-format">
    <xsl:param name="ref"/>
    <xsl:variable name="name" select="key('players',$ref)"/>
    <xsl:element name="a">
      <xsl:attribute name="href">
        <xsl:value-of select="$ecf_url"/>
        <xsl:value-of select="$ref"/>
      </xsl:attribute>
      <xsl:choose>
        <xsl:when test="$name//text() and $name/@grade">
          <xsl:attribute name="title">ECF grading page</xsl:attribute>
          <xsl:value-of select="$name/@grade"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:attribute name="title"><xsl:text>Ungraded (</xsl:text><xsl:value-of select="$ref"/>)</xsl:attribute>
          <i>ung</i>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:element>
  </xsl:template>
  <xsl:template name="league-format">
    <xsl:param name="text"/>
    <xsl:param name="league"/>
    <xsl:param name="ref"/>
    <xsl:choose>
      <xsl:when test="$text">
        <xsl:element name="a">
          <xsl:attribute name="href">
            <xsl:value-of select="$league_url"/>
            <xsl:value-of select="$text"/>
          </xsl:attribute>
          <xsl:value-of select="$text"/>
        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:element name="a">
          <xsl:attribute name="href"><xsl:value-of select="$player_url"/><xsl:value-of select="$ref"/>&amp;league=<xsl:value-of select="$league"/></xsl:attribute>
          <xsl:value-of select="$league"/>
        </xsl:element>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="month-name">
    <xsl:param name="num"/>
    <xsl:choose>
      <xsl:when test="$num = '1'">Jan</xsl:when>
      <xsl:when test="$num = '2'">Feb</xsl:when>
      <xsl:when test="$num = '3'">Mar</xsl:when>
      <xsl:when test="$num = '4'">Apr</xsl:when>
      <xsl:when test="$num = '5'">May</xsl:when>
      <xsl:when test="$num = '6'">Jun</xsl:when>
      <xsl:when test="$num = '7'">Jul</xsl:when>
      <xsl:when test="$num = '8'">Aug</xsl:when>
      <xsl:when test="$num = '9'">Sep</xsl:when>
      <xsl:when test="$num = '10'">Oct</xsl:when>
      <xsl:when test="$num = '11'">Nov</xsl:when>
      <xsl:when test="$num = '12'">Dec</xsl:when>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="matchdate-format">
    <xsl:param name="match"/>
    <xsl:variable name="date" select="$match/date"/>
    <xsl:variable name="report" select="$match/report"/>
    <xsl:element name="a"><xsl:choose><xsl:when test="$report!=''"><xsl:attribute name="href"><xsl:value-of select="$report"/></xsl:attribute><xsl:attribute name="title">Match Report <xsl:value-of select="$date/@year"/>-<xsl:value-of select="$date/@month"/>-<xsl:value-of select="$date/@day"/></xsl:attribute></xsl:when><xsl:otherwise><xsl:attribute name="title"><xsl:value-of select="$date/@day"/><xsl:text> </xsl:text><xsl:call-template name="month-name"><xsl:with-param name="num" select="$date/@month"/></xsl:call-template><xsl:text> </xsl:text><xsl:value-of select=" $date/@year"/></xsl:attribute></xsl:otherwise></xsl:choose><xsl:value-of select="$date/@year"/>-<xsl:value-of select="$date/@month"/>-<xsl:value-of select="$date/@day"/></xsl:element>
  </xsl:template>
  <xsl:template name="grading-perf">
  
    <xsl:param name="me"/>
    <xsl:param name="opp"/>
    <xsl:param name="res"/>
    <xsl:param name="colour"/>
    <xsl:if test="$opp//text() and string($opp/@grade)">
      <xsl:variable name="own_grade" select="$me/@grade"/>
      <xsl:variable name="opp_grade">
        <xsl:choose>
          <xsl:when test="($own_grade - 40) &gt; $opp/@grade">
            <xsl:value-of select="$own_grade - 40"/>
          </xsl:when>
          <xsl:when test="($own_grade + 40) &lt; $opp/@grade">
            <xsl:value-of select="$own_grade + 40"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$opp/@grade"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <perf>
        <xsl:choose>
          <xsl:when test="($res='1-0' and $colour='white') or ($res='0-1' and $colour='black')">
            <xsl:value-of select="$opp_grade + 50"/>
          </xsl:when>
          <xsl:when test="$res='0.5-0.5'">
            <xsl:value-of select="$opp_grade"/>
          </xsl:when>
          <xsl:when test="($res='0-1' and $colour='white') or ($res='1-0' and $colour='black')">
            <xsl:value-of select="$opp_grade - 50"/>
          </xsl:when>
        </xsl:choose>
      </perf>
    </xsl:if>
  </xsl:template>
  <!-- Summary table row showing player results -->
  <xsl:template name="player-summary">
    <xsl:param name="type"/>
    <xsl:param name="player"/>
    <xsl:param name="league"/>
    <xsl:param name="team"/>
    <xsl:if test="count(//player-ref[@ref=$player/@ref and                                          (not($league) or ../../../../../@name=$league) and                                                                               (not($team) or $player/@team=$team or ../../../../*/team=$team) and                                                                                                                     not(../../*/player-ref/@ref='Default')])">
      <xsl:variable name="GradePoints">
        <xsl:for-each select="//gameinfo[opponents/white/player-ref/@ref=$player/@ref and (not($league) or ../../@name=$league)]">
          <xsl:call-template name="grading-perf">
            <xsl:with-param name="colour" select="'white'"/>
            <xsl:with-param name="me" select="$player"/>
            <xsl:with-param name="opp" select="key('players',opponents/black/player-ref/@ref)"/>
            <xsl:with-param name="res" select="result/@res"/>
          </xsl:call-template>
        </xsl:for-each>
        <xsl:for-each select="//gameinfo[opponents/black/player-ref/@ref=$player/@ref and (not($league) or ../../@name=$league)]">
          <xsl:call-template name="grading-perf">
            <xsl:with-param name="colour" select="'black'"/>
            <xsl:with-param name="me" select="$player"/>
            <xsl:with-param name="opp" select="key('players',opponents/white/player-ref/@ref)"/>
            <xsl:with-param name="res" select="result/@res"/>
          </xsl:call-template>
        </xsl:for-each>
      </xsl:variable>
      <tr>
        <xsl:choose>
          <xsl:when test="$type = 'filtered'">
            <td>
              <i>
                <xsl:element name="a">
                  <xsl:attribute name="href"><xsl:value-of select="$player_url"/><xsl:value-of select="$player/@ref"/>&amp;league=<xsl:value-of select="$league"/></xsl:attribute>
                  <xsl:attribute name="title">
                    <xsl:value-of select="$league"/>
                    <xsl:text> results for </xsl:text>
                    <xsl:value-of select="$player"/>
                  </xsl:attribute>
                  <xsl:value-of select="$league"/>
                </xsl:element>
              </i>
            </td>
            <td>
              <i>
                <xsl:element name="a">
                  <xsl:attribute name="href"><xsl:value-of select="$team_url"/><xsl:value-of select="$player/@team"/>&amp;league=<xsl:value-of select="$league"/></xsl:attribute>
                  <xsl:attribute name="title">
                    <xsl:text>Show all results for </xsl:text>
                    <xsl:value-of select="$player/@team"/>
                    <xsl:text> in </xsl:text>
                    <xsl:value-of select="$league"/>
                  </xsl:attribute>
                  <xsl:value-of select="$player/@team"/>
                </xsl:element>
              </i>
            </td>
            <td>
            <i>
              <xsl:element name="a">
                <xsl:attribute name="href">
                  <xsl:value-of select="$ecf_club_url"/>
                  <xsl:value-of select="$player/@team"/>
                </xsl:attribute>
                <xsl:attribute name="title">ECF grading page</xsl:attribute>
                all
              </xsl:element>
              </i>
            </td>
          </xsl:when>
          <xsl:otherwise>
            <td>
              <xsl:call-template name="player-format">
                <xsl:with-param name="ref" select="$player/@ref"/>
              </xsl:call-template>
            </td>
            <td>
              <xsl:element name="a">
                <xsl:attribute name="href">
                  <xsl:value-of select="$team_url"/>
                  <xsl:value-of select="$player/@team"/>
                </xsl:attribute>
                <xsl:attribute name="title">
                  <xsl:text>Show all players for </xsl:text>
                  <xsl:value-of select="$player/@team"/>
                </xsl:attribute>
                <xsl:value-of select="$player/@team"/>
              </xsl:element>
            </td>
            <td>
              <xsl:call-template name="grade-format">
                <xsl:with-param name="ref" select="$player/@ref"/>
              </xsl:call-template>
            </td>
          </xsl:otherwise>
        </xsl:choose>
        <td>
          <xsl:value-of select="count(//player-ref[@ref=$player/@ref and (not($league) or ../../../../../@name=$league) and not(../../*/player-ref/@ref='Default')])"/>
        </td>
        <td>
          <xsl:value-of select="count(//gameinfo[opponents/white/player-ref/@ref=$player/@ref and result/@res='1-0' and (not($league) or ../../@name=$league) and not(opponents/black/player-ref/@ref='Default')]) +                                                             count(//gameinfo[opponents/black/player-ref/@ref=$player/@ref and result/@res='0-1' and (not($league) or ../../@name=$league) and not(opponents/white/player-ref/@ref='Default')])"/>
        </td>
        <td>
          <xsl:value-of select="count(//gameinfo[opponents/*/player-ref/@ref=$player/@ref and result/@res='0.5-0.5' and (not($league) or ../../@name=$league)])"/>
        </td>
        <td>
          <xsl:value-of select="count(//gameinfo[opponents/white/player-ref/@ref=$player/@ref and result/@res='0-1' and (not($league) or ../../@name=$league) and not(opponents/black/player-ref/@ref='Default')]) +                                                             count(//gameinfo[opponents/black/player-ref/@ref=$player/@ref and result/@res='1-0' and (not($league) or ../../@name=$league) and not(opponents/white/player-ref/@ref='Default')])"/>
        </td>
        <td>
          <xsl:value-of select="round(sum(msxsl:node-set($GradePoints)/perf) div count(msxsl:node-set($GradePoints)/perf))"/>
        </td>
      </tr>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

