<?xml version="1.0" encoding="iso-8859-1"?>
<!-- 
    Chess3.xsl: a ChessGML to SVG stylesheet
    Copyright © 2001, 2002 Max Froumentin

     Use and distribution of this code are permitted under the terms of the <a
     href="http://www.w3.org/Consortium/Legal/copyright-software-19980720"
     >W3C Software Notice and License</a>.


    Transforms a standalone ChessGML <game> to SVG

-->

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
               xmlns:xlink="http://www.w3.org/1999/xlink"
               xmlns:svg="http://www.w3.org/2000/svg"
               xmlns="http://www.w3.org/2000/svg"
               xmlns:math="http://www.exslt.org/math"
               version="1.0"
               exclude-result-prefixes="math svg">

  <xsl:import href="text.xsl"/>


  <xsl:output indent="yes"
    doctype-public="-//W3C//DTD SVG 1.0//EN"
    doctype-system="http://www.w3.org/TR/SVG/DTD/svg10.dtd"/>


<!-- ********************************************************************** -->
<!-- PARAMETERS and OPTIONS-->

  <!-- speed of each move in squares per second -->
  <xsl:param name="moveSpeed" select="3"/>

  <!-- time between each move in seconds -->
  <xsl:param name="restDuration" select="2"/>

  <!-- SVG font to use (one of ChessSophia, ChessCase, ChessLeipzig). -->
  <xsl:param name="chessFont" select="'ChessSophia'"/>

  <!-- display moves like "Nf4" or with the Knight Glyph for N? (slower) -->
  <xsl:param name="useChessFontInMoveList" select="true()"/>

<!-- ###################################################################### -->

<!-- internal variables -->

  <xsl:template match="/">
    <xsl:comment>Generated using ChessGML-to-SVG stylesheet</xsl:comment>
    <xsl:comment>SVG and chess font © 2002 Max Froumentin</xsl:comment>
    <xsl:apply-templates select="//game[1]"/>
  </xsl:template>


  <xsl:template match="game">
    <xsl:variable name="nbHalfMoves" select="count(moves/mp/m)"/>
    <xsl:variable name="nbMoves" select="count(moves/mp)"/>
    <xsl:variable name="initialBoard" select="'ABCDEFGHIJKLMNOP                                ijklmnopabcdefgh'"/>
    
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="-10 -20 150 110">

      <!-- dump SVG font for chess pieces -->
      <xsl:text>
      </xsl:text>
      <xsl:comment> Definition of font for chess pieces </xsl:comment>
      <xsl:copy-of select="document(concat($chessFont,'.svg'))/svg:svg/svg:defs"/>
      <xsl:comment> end </xsl:comment>
      <!-- display board -->
      <xsl:call-template name="printBoardSVG">
        <xsl:with-param name="board" select="$initialBoard"/>
        <xsl:with-param name="event" select="(gameinfo/eventinfo/event | ancestor::tournament/eventinfo/event)[1]"/>
        <xsl:with-param name="site" select="(gameinfo/eventinfo/site | ancestor::tournament/eventinfo/site)[1]"/>
        <xsl:with-param name="date" select="(gameinfo/date | ancestor::round/roundinfo/date)[1]"/>
            <!-- if we're in a standalone SAN file, find the event info -->
            <!-- locally, otherwise get it from the top of the file -->
      </xsl:call-template>

      <!-- put pieces and start animation -->
      <xsl:apply-templates select="moves/mp[1]/m[1]" >
        <xsl:with-param name="board" select="$initialBoard"/>
        <xsl:with-param name="move" select="0"/>
      </xsl:apply-templates>

      <!-- move all pieces back to starting position -->

      <!-- value of begin attribute -->
      <xsl:variable name="beg" select="concat('move',$nbHalfMoves - 1,'.end+5s')"/>

      <xsl:text>&#xA;&#xA;</xsl:text>

      <xsl:comment> revert all pawns to their original shape (before promotion) </xsl:comment>

      <set xlink:href="#IFace" attributeName="xlink:href" to="#whitePawn" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#JFace" attributeName="xlink:href" to="#whitePawn" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#KFace" attributeName="xlink:href" to="#whitePawn" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#LFace" attributeName="xlink:href" to="#whitePawn" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#MFace" attributeName="xlink:href" to="#whitePawn" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#NFace" attributeName="xlink:href" to="#whitePawn" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#OFace" attributeName="xlink:href" to="#whitePawn" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#PFace" attributeName="xlink:href" to="#whitePawn" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#iFace" attributeName="xlink:href" to="#blackPawn" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#jFace" attributeName="xlink:href" to="#blackPawn" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#kFace" attributeName="xlink:href" to="#blackPawn" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#lFace" attributeName="xlink:href" to="#blackPawn" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#mFace" attributeName="xlink:href" to="#blackPawn" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#nFace" attributeName="xlink:href" to="#blackPawn" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#oFace" attributeName="xlink:href" to="#blackPawn" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#pFace" attributeName="xlink:href" to="#blackPawn" begin="{$beg}" fill="freeze"/>


      <xsl:text>&#xA;&#xA;</xsl:text>
      <xsl:comment> move all pieces back to starting position </xsl:comment>
      <xsl:text>&#xA;</xsl:text>
      <set xlink:href="#A" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#B" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#C" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#D" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#E" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#F" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#G" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#H" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#I" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#J" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#K" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#L" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#M" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#N" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#O" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#P" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#a" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#b" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#c" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#d" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#e" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#f" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#g" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#h" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#i" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#j" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#k" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#l" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#m" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#n" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#o" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>
      <set xlink:href="#p" attributeName="visibility" to="visible" attributeType="CSS" begin="{$beg}" fill="freeze"/>

      <animate xlink:href="#A" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="5.5" fill="freeze"/>
      <animate xlink:href="#A" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="79" fill="freeze"/>
      <animate xlink:href="#B" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="15.5" fill="freeze"/>
      <animate xlink:href="#B" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="79" fill="freeze"/>
      <animate xlink:href="#C" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="25.5" fill="freeze"/>
      <animate xlink:href="#C" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="79" fill="freeze"/>
      <animate xlink:href="#D" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="35.5" fill="freeze"/>
      <animate xlink:href="#D" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="79" fill="freeze"/>
      <animate xlink:href="#E" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="45.5" fill="freeze"/>
      <animate xlink:href="#E" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="79" fill="freeze"/>
      <animate xlink:href="#F" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="55.5" fill="freeze"/>
      <animate xlink:href="#F" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="79" fill="freeze"/>
      <animate xlink:href="#G" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="65.5" fill="freeze"/>
      <animate xlink:href="#G" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="79" fill="freeze"/>
      <animate xlink:href="#H" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="75.5" fill="freeze"/>
      <animate xlink:href="#H" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="79" fill="freeze"/>
      <animate xlink:href="#I" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="5.5" fill="freeze"/>
      <animate xlink:href="#I" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="69" fill="freeze"/>
      <animate xlink:href="#J" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="15.5" fill="freeze"/>
      <animate xlink:href="#J" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="69" fill="freeze"/>
      <animate xlink:href="#K" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="25.5" fill="freeze"/>
      <animate xlink:href="#K" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="69" fill="freeze"/>
      <animate xlink:href="#L" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="35.5" fill="freeze"/>
      <animate xlink:href="#L" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="69" fill="freeze"/>
      <animate xlink:href="#M" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="45.5" fill="freeze"/>
      <animate xlink:href="#M" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="69" fill="freeze"/>
      <animate xlink:href="#N" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="55.5" fill="freeze"/>
      <animate xlink:href="#N" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="69" fill="freeze"/>
      <animate xlink:href="#O" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="65.5" fill="freeze"/>
      <animate xlink:href="#O" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="69" fill="freeze"/>
      <animate xlink:href="#P" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="75.5" fill="freeze"/>
      <animate xlink:href="#P" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="69" fill="freeze"/>

      <animate xlink:href="#i" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="5.5" fill="freeze"/>
      <animate xlink:href="#i" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="19" fill="freeze"/>
      <animate xlink:href="#j" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="15.5" fill="freeze"/>
      <animate xlink:href="#j" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="19" fill="freeze"/>
      <animate xlink:href="#k" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="25.5" fill="freeze"/>
      <animate xlink:href="#k" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="19" fill="freeze"/>
      <animate xlink:href="#l" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="35.5" fill="freeze"/>
      <animate xlink:href="#l" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="19" fill="freeze"/>
      <animate xlink:href="#m" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="45.5" fill="freeze"/>
      <animate xlink:href="#m" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="19" fill="freeze"/>
      <animate xlink:href="#n" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="55.5" fill="freeze"/>
      <animate xlink:href="#n" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="19" fill="freeze"/>
      <animate xlink:href="#o" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="65.5" fill="freeze"/>
      <animate xlink:href="#o" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="19" fill="freeze"/>
      <animate xlink:href="#p" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="75.5" fill="freeze"/>
      <animate xlink:href="#p" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="19" fill="freeze"/>
      <animate xlink:href="#a" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="5.5" fill="freeze"/>
      <animate xlink:href="#a" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="9" fill="freeze"/>
      <animate xlink:href="#b" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="15.5" fill="freeze"/>
      <animate xlink:href="#b" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="9" fill="freeze"/>
      <animate xlink:href="#c" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="25.5" fill="freeze"/>
      <animate xlink:href="#c" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="9" fill="freeze"/>
      <animate xlink:href="#d" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="35.5" fill="freeze"/>
      <animate xlink:href="#d" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="9" fill="freeze"/>
      <animate xlink:href="#e" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="45.5" fill="freeze"/>
      <animate xlink:href="#e" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="9" fill="freeze"/>
      <animate xlink:href="#f" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="55.5" fill="freeze"/>
      <animate xlink:href="#f" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="9" fill="freeze"/>
      <animate xlink:href="#g" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="65.5" fill="freeze"/>
      <animate xlink:href="#g" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="9" fill="freeze"/>
      <animate xlink:href="#h" attributeName="x" attributeType="XML" begin="{$beg}" dur="5s" to="75.5" fill="freeze"/>
      <animate id="theEnd" xlink:href="#h" attributeName="y" attributeType="XML" begin="{$beg}" dur="5s" to="9" fill="freeze"/>


      <xsl:text>&#xa;&#xa;</xsl:text><xsl:comment>Reset move list </xsl:comment><xsl:text>&#xa;</xsl:text>

      <animateTransform attributeName="transform" attributeType="XML" type="translate" from="0,{-4.5*$nbMoves}" to="0,0" begin="{$beg}" dur="5s" fill="freeze" xlink:href="#moveList"/>
 
    </svg>
  </xsl:template>
  
<!-- ###################################################################### -->

  <xsl:template match="mp/m" >
    <xsl:param name="board" select="'error'"/>
    <xsl:param name="move" select="'error'"/>

<xsl:text>

</xsl:text>
        <xsl:comment>Move <xsl:value-of select="$move"/></xsl:comment>
<xsl:text>
</xsl:text>

    <!-- compute the value of the begin attribute -->
    <xsl:variable name="begin">
      <xsl:choose>
        <xsl:when test="$move=0">2s;theEnd.end+2s</xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat('move',$move - 1,'.end+',$restDuration, 's')"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <!-- scroll the text -->
    <xsl:if test="$move mod 2 = 0">
      <animateTransform
        attributeName="transform" attributeType="XML"
        type="translate" from="0,0" to="0,-4.5" 
        accumulate="sum" additive="sum"
        begin="{$begin}" dur="1s"  
        fill="freeze" xlink:href="#moveList"/>
      
    </xsl:if>

    <xsl:choose>
      <xsl:when test="@spec='0-0' and @c='w'">
        <!-- White king's side castling -->
        <!-- 1. generate king's move -->
        <!-- we have to use format number here because the Adobe SVG player chokes on too many -->
        <!-- decimals, which xsltproc can generate, such as 0.666666667 which Adobe reads a 0 -->

        <animate id="move{$move}"
          xlink:href="#E" attributeName="x" attributeType="XML"
          begin="{$begin}" dur="{format-number(2 div $moveSpeed,'0.00')}" 
          from="45" to="65" fill="freeze" />
        <!-- 2. generate rook's move -->
        <animate xlink:href="#H" attributeName="x" attributeType="XML"
          begin="{$begin}" dur="{format-number(2 div $moveSpeed,'0.00')}" 
          from="75" to="55" fill="freeze" />
        <!-- do next move using updated board -->

	<xsl:apply-templates select="(following-sibling::m | ../following-sibling::mp[1]/m)[1]">
	  <xsl:with-param name="board" 
	    select="concat(substring($board,1,4),' HE ',substring($board,9))"/>
	  <xsl:with-param name="move" select="$move + 1"/>
	</xsl:apply-templates>
      </xsl:when>

      <xsl:when test="@spec='0-0-0' and @c='w'">
	<!-- White queen's side castling -->
	<!-- 1. generate king's move -->
	<animate id="move{$move}" 
          xlink:href="#E" attributeName="x" attributeType="XML"
	  begin="{$begin}" dur="{format-number(3 div $moveSpeed,'0.00')}" 
	  from="45" to="25" fill="freeze"  />
	<!-- 2. generate rook's move -->
	<animate xlink:href="#A" attributeName="x" attributeType="XML"
	  begin="{$begin}" dur="{format-number(3 div $moveSpeed,'0.00')}" 
	  from="5" to="35" fill="freeze"  />
	<!-- do next move using updated board -->
	<xsl:apply-templates select="(following-sibling::m | ../following-sibling::mp[1]/m)[1]" >
	  <xsl:with-param name="board" 
	    select="concat('  EA ',substring($board,6))"/>
	  <xsl:with-param name="move" select="$move + 1"/>
	</xsl:apply-templates>
      </xsl:when>


      <xsl:when test="@spec='0-0-0' and @c='b'">
	<!-- Black queen's side castling -->
	<!-- 1. generate king's move -->
	<animate id="move{$move}"
          xlink:href="#e" attributeName="x" attributeType="XML"
	  begin="{$begin}" dur="{format-number(3 div $moveSpeed,'0.00')}" 
	  from="45" to="25" fill="freeze" />
	<!-- 2. generate rook's move -->
	<animate id="move{$move}"
          xlink:href="#a" attributeName="x" attributeType="XML"
	  begin="{$begin}" dur="{format-number(3 div $moveSpeed,'0.00')}" 
	  from="5" to="35" fill="freeze" />
	<!-- do next move using updated board -->
	<xsl:apply-templates select="(following-sibling::m | ../following-sibling::mp[1]/m)[1]" >
	  <xsl:with-param name="board" 
	    select="concat(substring($board,1,56),'  ea ',substring($board,62))"/>
	  <xsl:with-param name="move" select="$move + 1"/>
	</xsl:apply-templates>
      </xsl:when>


      <xsl:when test="@spec='0-0' and @c='b'">
	<!-- Black king's side castling -->
	<!-- 1. generate king's move -->
	<animate id="move{$move}"
          xlink:href="#e" attributeName="x" attributeType="XML"
	  begin="{$begin}" dur="{format-number(2 div $moveSpeed,'0.00')}" 
	  from="45" to="65" fill="freeze" />
	<!-- 2. generate rook's move -->
	<animate xlink:href="#h" attributeName="x" attributeType="XML"
	  begin="{$begin}" dur="{format-number(2 div $moveSpeed,'0.00')}" 
	  from="75" to="55" fill="freeze" />
	<!-- do next move using updated board -->
	<xsl:apply-templates select="(following-sibling::m | ../following-sibling::mp[1]/m)[1]">
	  <xsl:with-param name="board" 
	    select="concat(substring($board,1,60),' he ')"/>
	  <xsl:with-param name="move" select="$move + 1"/>
	</xsl:apply-templates>	
      </xsl:when>


      <xsl:otherwise>
	<!-- standard move -->
	<xsl:variable name="startCol">
	  <xsl:choose>
	    <xsl:when test="*[2][self::sq]">
              <xsl:value-of select="translate(substring(sq[1]/@n,1,1),'abcdefgh','12345678')"/>
            </xsl:when>
	    <xsl:otherwise>
              <xsl:value-of select="translate(substring(local-name(*[2]),1,1),'abcdefgh','12345678')"/>
            </xsl:otherwise>
          </xsl:choose>
	</xsl:variable>

	<xsl:variable name="startRow">
	  <xsl:choose>
	    <xsl:when test="*[2][self::sq]">
	      <xsl:value-of select="substring(sq[1]/@n,2)"/>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="substring(local-name(*[2]),2)"/>
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:variable>

	<xsl:variable name="endCol">
	  <xsl:choose>
	    <xsl:when test="*[3][self::sq]">
	      <xsl:value-of select="translate(substring(sq[last()]/@n,1,1),'abcdefgh','12345678')"/>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="translate(substring(local-name(*[3]),1,1),'abcdefgh','12345678')"/>
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:variable>

	<xsl:variable name="endRow">
	  <xsl:choose>
	    <xsl:when test="*[3][self::sq]">
	      <xsl:value-of select="substring(sq[last()]/@n,2)"/>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="substring(local-name(*[3]),2)"/>
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:variable>

        <xsl:variable name="moveLength">
          <xsl:call-template name="math:sqrt">
            <xsl:with-param name="number" select="(($endCol - $startCol)*($endCol - $startCol) + ($endRow - $startRow)*($endRow - $startRow))"/>
          </xsl:call-template>
        </xsl:variable>

        <xsl:variable name="moveDuration" select="$moveLength div $moveSpeed"/>

	<xsl:variable name="startPos" select="($startRow - 1)*8+$startCol"/>
	<xsl:variable name="endPos" select="($endRow - 1)*8+$endCol"/>

	<xsl:variable name="piece" select="substring($board,$startPos,1)"/>

	<xsl:variable name="startX" select="$startCol*10 - 5"/>
	<xsl:variable name="endX" select="$endCol*10 - 5"/>
	<xsl:variable name="startY" select="80 - ($startRow*10 - 8.5)"/>
	<xsl:variable name="endY" select="80 - ($endRow*10 - 8.5)"/>


	<xsl:if test="$startX != $endX">
	  <animate id="move{$move}" begin="{$begin}" 
	    xlink:href="#{$piece}" attributeName="x" attributeType="XML"
            dur="{format-number($moveDuration,'0.00')}s" from="{$startX}" to="{$endX}" fill="freeze"/>
	</xsl:if>
	<xsl:if test="$startY != $endY">
          <animate xlink:href="#{$piece}" attributeName="y" attributeType="XML"
            begin="{$begin}" dur="{format-number($moveDuration,'0.00')}s"
            from="{$startY}" to="{$endY}" fill="freeze">

	    <xsl:if test="$startX = $endX">
	      <!-- no movement in X, so put an id here, for the following moves to chain after -->
	      <xsl:attribute name="id">move<xsl:value-of select="$move"/></xsl:attribute>
	    </xsl:if>
	  </animate>
	</xsl:if>


	<!-- if capture, hide captured piece -->
	<xsl:choose>
	  <xsl:when test="substring($board,$endPos,1)!=' '">
	    <!-- normal capture -->
	    <set xlink:href="#{substring($board,$endPos,1)}" 
	      begin="{concat('move',$move - 1,'.end+',format-number($restDuration+$moveDuration,'0.00'), 's')}"  
	      attributeType="CSS" attributeName="visibility" to="hidden" 
              /> <!-- can't use begin="{$begin}" here as "move1.end+2s+4s" is not allowed in SVG -->
	  </xsl:when>
	  <xsl:when test="@spec='ep' and @c='b'">
	    <!-- black capture, en passant -->
	    <set xlink:href="#{substring($board,$endPos + 8,1)}" 
	      begin="{concat('move',$move - 1,'.end+',format-number($restDuration+$moveDuration,'0.00'), 's')}"  
	      attributeType="CSS" attributeName="visibility" to="hidden" 
	      />
	  </xsl:when>
	  <xsl:when test="@spec='ep' and @c='w'">
	    <!-- white capture, en passant -->
	    <set xlink:href="#{substring($board,$endPos - 8,1)}" 
	      begin="{concat('move',$move - 1,'.end+',format-number($restDuration+$moveDuration,'0.00'), 's')}"  
	      attributeType="CSS" attributeName="visibility" to="hidden" 
	      />
	  </xsl:when>
	</xsl:choose>

	<!-- if promotion, change piece -->
	<xsl:if test="@prpi">
	  <xsl:comment>Promotion</xsl:comment>
	  <set xlink:href="#{substring($board,$startPos,1)}Face" 
	    begin="{concat('move',$move - 1,'.end+',format-number($restDuration+$moveDuration,'0.00'), 's')}"  
	     attributeType="XML" attributeName="xlink:href">
	    <xsl:attribute name="to">
	      <xsl:choose>
		<xsl:when test="@prpi='q' and @c='b'">#blackQueen</xsl:when>
		<xsl:when test="@prpi='q' and @c='w'">#whiteQueen</xsl:when>
		<xsl:when test="@prpi='b' and @c='w'">#whiteBishop</xsl:when>
		<xsl:when test="@prpi='r' and @c='w'">#whiteRook</xsl:when>
		<xsl:when test="@prpi='n' and @c='w'">#whiteKnight</xsl:when>
		<xsl:when test="@prpi='n' and @c='b'">#blackKnight</xsl:when>
		<xsl:when test="@prpi='b' and @c='b'">#blackBishop</xsl:when>
		<xsl:when test="@prpi='r' and @c='b'">#blackRook</xsl:when>
		<xsl:otherwise>Promotion error</xsl:otherwise>
	      </xsl:choose>
	    </xsl:attribute>
	  </set>
	</xsl:if>


	<xsl:variable name="newBoard1" select="concat(substring($board,1,$startPos - 1),' ',substring($board,$startPos+1))"/>


	<xsl:apply-templates select="(following-sibling::m | ../following-sibling::mp[1]/m)[1]" >
	  <xsl:with-param name="board" select="concat(substring($newBoard1,1,$endPos - 1),$piece,substring($newBoard1,$endPos+1))"/>
	  <xsl:with-param name="move" select="$move + 1"/>
	</xsl:apply-templates>
	
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

<!-- ###################################################################### -->

  <!-- display board -->
  <xsl:template name="printBoardSVG">
    <xsl:param name="board" select="'error'"/>
    <xsl:param name="event" select="'error'"/>
    <xsl:param name="site" select="'error'"/>

    <!-- and chess board -->
    <xsl:copy-of select="document('board.svg')/svg:svg/*"/>

    <!-- captions -->
    <g style="font-family: Times New Roman; font-size: 4">
      <text x="40" y="-10" text-anchor="middle">
	<xsl:value-of select="$event"/>
      </text>

      <text x="40" y="-5" text-anchor="middle">
        <xsl:value-of select="$site"/>
        <xsl:text> </xsl:text>
        <xsl:call-template name="print-date">
          <xsl:with-param name="day" select="(ancestor-or-self::game/gameinfo|ancestor-or-self::chess/tournament/rounds/round/roundinfo)[1]/date/@day"/>
          <xsl:with-param name="month" select="(ancestor-or-self::game/gameinfo|ancestor-or-self::chess/tournament/rounds/round/roundinfo)[1]/date/@month"/>
          <xsl:with-param name="year" select="(ancestor-or-self::game/gameinfo|ancestor-or-self::chess/tournament/rounds/round/roundinfo)[1]/date/@year"/>
        </xsl:call-template>
      </text>

      <!-- who's white -->
      <text x="82" y="0">
	<xsl:call-template name="print-colour">
	  <xsl:with-param name="colour" select="'white'"/>
	</xsl:call-template>
	<xsl:text>: </xsl:text>
	<xsl:apply-templates select="gameinfo/opponents/white/player | ancestor::tournament/players/player[@id = current()/gameinfo/opponents/white/player-ref/@ref]"/>
      </text>

      <!-- who's black -->
      <text x="82" y="7">
	<xsl:call-template name="print-colour">
	  <xsl:with-param name="colour" select="'black'"/>
	</xsl:call-template>
	<xsl:text>: </xsl:text>
	<xsl:apply-templates select="gameinfo/opponents/black/player | ancestor::tournament/players/player[@id = current()/gameinfo/opponents/black/player-ref/@ref]"/>
      </text>
      
      <!-- move list -->
      <xsl:comment>Move list</xsl:comment>
      <g transform="translate(90,30)">
	<rect x="-5" y="-5" width="40" height="55" fill="#ccc"/>
	<polygon fill="black" points="-3.5,-3.5    -1,-1.5   -3.5,.5"/>

	<clipPath id="MyClip">
	  <rect x="-5" y="-5" width="40" height="55"/>
	</clipPath>
	<g clip-path="url(#MyClip)">
	  <g id="moveList">
	    <xsl:apply-templates select="moves/mp" mode="text">
	      <xsl:with-param name="baselineDist" select="4.5"/>
	    </xsl:apply-templates>
	  </g>
	</g>
      </g>
      <xsl:comment>End move list</xsl:comment>
    </g>

    
    <!-- pieces -->
    <g style="font-family: {$chessFont}; font-size: 10; fill: black" text-anchor="middle">
      <xsl:call-template name="placePieces">
	<xsl:with-param name="board" select="$board"/>
	<xsl:with-param name="pos" select="0"/>
      </xsl:call-template>
    </g>


  </xsl:template>

<!-- ###################################################################### -->

  <xsl:template name="placePieces">
    <xsl:param name="board" select="'error'"/>
    <xsl:param name="pos" select="'error'"/>
    <xsl:variable name="piece" select="substring($board,1,1)"/>
    <xsl:if test="$piece!=' '">
      <xsl:variable name="pieceName">
        <xsl:choose>
          <xsl:when test="$piece='a' or $piece='h'">blackRook</xsl:when>
	  <xsl:when test="$piece='b' or $piece='g'">blackKnight</xsl:when>
	  <xsl:when test="$piece='c' or $piece='f'">blackBishop</xsl:when>
	  <xsl:when test="$piece='d'">blackQueen</xsl:when>
	  <xsl:when test="$piece='e'">blackKing</xsl:when>
	  <xsl:when test="$piece='i' or $piece='j' or $piece='k' or
	        $piece='l' or $piece='m' or $piece='n' or
	        $piece='o' or $piece='p'">blackPawn</xsl:when>
	  <xsl:when test="$piece='A' or $piece='H'">whiteRook</xsl:when>
	  <xsl:when test="$piece='B' or $piece='G'">whiteKnight</xsl:when>
	  <xsl:when test="$piece='C' or $piece='F'">whiteBishop</xsl:when>
	  <xsl:when test="$piece='D'">whiteQueen</xsl:when>
	  <xsl:when test="$piece='E'">whiteKing</xsl:when>
	  <xsl:when test="$piece='I' or $piece='J' or $piece='K' or
	   $piece='L' or $piece='M' or $piece='N' or $piece='O' or 
	    $piece='P'">whitePawn</xsl:when>
  	 </xsl:choose>
      </xsl:variable>
      <text 
	id="{$piece}"
	x="{($pos mod 8)*10+5.5}" 
	y="{80 - (($pos - $pos mod 8) div 8)*10 - 1}">
	<tref id="{$piece}Face" xlink:href="#{$pieceName}"/>
      </text>
    </xsl:if>
    <xsl:if test="$pos &lt; 63">
      <xsl:call-template name="placePieces">
	<xsl:with-param name="board" select="substring($board,2)"/>
	<xsl:with-param name="pos" select="$pos+1"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>



  <!-- Move List Generation -->
  <xsl:template match="mp" mode="text">
    <xsl:param name="baselineDist" select="1"/>
    <text y="{$baselineDist * position()}" x="0">
      <xsl:number format="1"/><xsl:text>. </xsl:text>
      <xsl:apply-templates select="m" mode="text"/>
    </text>

    <text y="{$baselineDist * (position()+1)}">
      <xsl:if test="position()=last()">
	<xsl:choose>
	  <xsl:when test="../../gameinfo/result/@res = 'draw'">½-½</xsl:when>
	  <xsl:otherwise>
	    <xsl:value-of select="../../gameinfo/result/@res"/>
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:if>
</text>
  </xsl:template>

  <xsl:template match="m" mode="text">
    <xsl:choose>

      <!-- castling -->
      <xsl:when test="@spec='0-0'"><xsl:text>0-0</xsl:text></xsl:when>
      <xsl:when test="@spec='0-0-0'"><xsl:text>0-0-0</xsl:text></xsl:when>

      <xsl:otherwise>
        <xsl:choose>
          <!-- pawn movement: print starting column only when capture -->
          <xsl:when test="p/@n='p'">
  	    <xsl:choose>
	      <xsl:when test="@capt='1'">
		<xsl:choose>
		  <xsl:when test="*[2][self::sq]">
		    <xsl:value-of select="substring(sq[1]/@n,1,1)"/>
		  </xsl:when>
		  <xsl:otherwise>
		    <xsl:value-of select="substring(local-name(*[2]),1,1)"/>
		  </xsl:otherwise>
		</xsl:choose>
	      </xsl:when>
	      <xsl:otherwise> </xsl:otherwise> 
	    </xsl:choose>
          </xsl:when>

          <xsl:otherwise>
          <!-- not a pawn nor a castling -->
            <xsl:choose>
              <xsl:when test="$useChessFontInMoveList">
  	        <xsl:choose>
	      <xsl:when test="p/@c='w' and p/@n='n'"><tspan style="font-family: {$chessFont}; font-size: 5; fill: black">&#x2658;</tspan></xsl:when>
	      <xsl:when test="p/@c='w' and p/@n='q'"><tspan style="font-family: {$chessFont}; font-size: 5; fill: black">&#x2655;</tspan></xsl:when>
	      <xsl:when test="p/@c='w' and p/@n='r'"><tspan style="font-family: {$chessFont}; font-size: 5; fill: black">&#x2656;</tspan></xsl:when>
	      <xsl:when test="p/@c='w' and p/@n='b'"><tspan style="font-family: {$chessFont}; font-size: 5; fill: black">&#x2657;</tspan></xsl:when>
	      <xsl:when test="p/@c='w' and p/@n='k'"><tspan style="font-family: {$chessFont}; font-size: 5; fill: black">&#x2654;</tspan></xsl:when>
	      <xsl:when test="p/@c='b' and p/@n='n'"><tspan style="font-family: {$chessFont}; font-size: 5; fill: black">&#x265e;</tspan></xsl:when>
	      <xsl:when test="p/@c='b' and p/@n='q'"><tspan style="font-family: {$chessFont}; font-size: 5; fill: black">&#x265b;</tspan></xsl:when>
	      <xsl:when test="p/@c='b' and p/@n='r'"><tspan style="font-family: {$chessFont}; font-size: 5; fill: black">&#x265c;</tspan></xsl:when>
	      <xsl:when test="p/@c='b' and p/@n='b'"><tspan style="font-family: {$chessFont}; font-size: 5; fill: black">&#x2657;</tspan></xsl:when>
	      <xsl:when test="p/@c='b' and p/@n='k'"><tspan style="font-family: {$chessFont}; font-size: 5; fill: black">&#x265a;</tspan></xsl:when>
                </xsl:choose>
              </xsl:when>
              <xsl:otherwise>
	        <xsl:choose>
	          <xsl:when test="p/@n='r'">R</xsl:when>
	          <xsl:when test="p/@n='n'">N</xsl:when>
	          <xsl:when test="p/@n='b'">B</xsl:when>
	          <xsl:when test="p/@n='q'">Q</xsl:when>
	          <xsl:when test="p/@n='k'">K</xsl:when>
                </xsl:choose>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
 	<xsl:if test="@capt='1'">x</xsl:if>

	<!-- coordinates of ending square -->
	<xsl:choose>
	  <xsl:when test="*[3][self::sq]">
	    <xsl:value-of select="sq[last()]/@n"/>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:value-of select="local-name(*[position()=last()])"/>
	  </xsl:otherwise>
	</xsl:choose>

	<!-- en passant -->
	<xsl:if test="@spec='ep'"> e.p. </xsl:if>

	<!-- promotion -->
	<xsl:if test="@prpi">
	  <xsl:text>=</xsl:text>
	  <xsl:value-of select="translate(@prpi,'qnbr','QNBR')"/>
	</xsl:if>

	<!-- check -->
	<xsl:if test="@chk='1'">
	  <xsl:text>+</xsl:text>
	</xsl:if>

	<!-- checkmate -->
	<xsl:if test="@mate='1'">
	  <xsl:text>++</xsl:text>
	</xsl:if>

      </xsl:otherwise>
    </xsl:choose>

    <xsl:if test="position()=1">
      <!-- just after white's move, add a space before black's -->
      <xsl:text> </xsl:text>
    </xsl:if>
  </xsl:template>


  <!-- ############################################################ -->
  <!-- square root template (for computing the speed of each move) -->
  <!-- from the EXSLT function library http://exslt.org -->
  <!-- written by Nate Austin -->

<xsl:template name="math:sqrt">
  <!-- The number you want to find the square root of -->
  <xsl:param name="number" select="0" />  
  <!-- The current 'try'.  This is used internally. -->
  <xsl:param name="try" select="1" />
  <!-- The current iteration, checked against maxiter to limit loop count -->  
  <xsl:param name="iter" select="1" />
  <!-- Set this up to ensure against infinite loops -->
  <xsl:param name="maxiter" select="10" />

  <!-- This template was written by Nate Austin using Sir Isaac Newton's
       method of finding roots -->

  <xsl:choose>
    <xsl:when test="$try * $try = $number or $iter > $maxiter">
      <xsl:value-of select="$try"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="math:sqrt">
        <xsl:with-param name="number" select="$number"/>

        <xsl:with-param name="try" 
                        select="$try - (($try * $try - $number) div (2 * $try))"/>
        <xsl:with-param name="iter" select="$iter + 1"/>
        <xsl:with-param name="maxiter" select="$maxiter"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


</xsl:transform>

