<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:exsl="http://exslt.org/common"
     extension-element-prefixes="exsl">

	<xsl:template match="/">
	<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
	<title><xsl:value-of select="/deck/title"/></title>
    <style type="text/css" >
      <xsl:comment>
      body, p, td, div, pre {
        font-family     : Arial, Helvetica, "MS sans-serif";
        font-size       : x-small;
      }
      table {
        height          : 26cm ;
        width           : 18.9cm ;
      }
      tr {
        height          : 6.5cm ;
        width           : 9.45cm ;
      }
      td {
        height          : 6.5cm ;
        width           : 9.45cm ;
        text-align      : center ;
        vertical-align  : middle ;
        padding         : 30px;
      }
      .caption {
        font-size       : 6pt;
      }
      .front {
        font-size       : 22pt;
      }
      .back {
        font-size       : 11pt;
           border-color : #666 ;
           border-width : 1px 1px 1px 1px ;
           border-style : solid ;
      }
      @media print {
        .pagestart {
           page-break-before  : always ;
        }
      }

      </xsl:comment>
    </style >
	</head><body><xsl:apply-templates select="deck"/></body></html>
	</xsl:template>
 <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->
	<xsl:template match="deck">
	    <h1><xsl:value-of select="title"/></h1>
	    <p><xsl:value-of select="comment"/></p>
	    <p>When printing to cut flashcards you do not need to print this page</p>
	    <xsl:call-template name="page">
		<xsl:with-param name="cards" select="//card[position() > 0]"/>
	    </xsl:call-template>
	</xsl:template>
 <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->
	<xsl:template name="page">
	  <xsl:param name="cards"/>
	  <xsl:choose>
	    <xsl:when test="exsl:node-set($cards)[position() > 0]">
	      <table class='pagestart'>
	      <xsl:for-each select="exsl:node-set($cards)[9>position()]">
		<xsl:choose>
		   <xsl:when test="position() mod 2 = 0">
			<xsl:variable name="pos2"><xsl:value-of select="position()"/></xsl:variable>
			<xsl:variable name="pos1"><xsl:value-of select="$pos2 - 1"/></xsl:variable>
			<tr><td class="back">
			 <span class="caption">(<xsl:value-of select="/deck/title"/>)</span><br/><br/>
			 <xsl:value-of select="exsl:node-set($cards)[position() = $pos1]/back"/>
			</td><td class="back">
			 <span class="caption">(<xsl:value-of select="/deck/title"/>)</span><br/><br/>
			 <xsl:value-of select="back"/>
			</td></tr>
		    </xsl:when>
		    <xsl:otherwise>
			 <xsl:variable name="pos3"><xsl:value-of select="position()"/></xsl:variable>
			 <xsl:choose>
			 <xsl:when test="exsl:node-set($cards)[position() = $pos3 + 1 and front='']">
			  <tr><td class="back">
			   <span class="caption">(<xsl:value-of select="/deck/title"/>)</span><br/><br/>
			   <xsl:value-of select="back"/>
			  </td><td> &#160;
			  </td></tr>
			 </xsl:when><xsl:otherwise></xsl:otherwise></xsl:choose>
		    </xsl:otherwise>
		</xsl:choose>
	      </xsl:for-each>
              </table>
	      <table class='pagestart'>
	      <xsl:for-each select="exsl:node-set($cards)[9>position()]">
		<xsl:choose>
		   <xsl:when test="position() mod 2 = 0">
			<xsl:variable name="pos2"><xsl:value-of select="position()"/></xsl:variable>
			<xsl:variable name="pos1"><xsl:value-of select="$pos2 - 1"/></xsl:variable>
			<tr><td class="front">
			 <xsl:value-of select="front"/>
			</td><td class="front">
			 <xsl:value-of select="exsl:node-set($cards)[position() = $pos1]/front"/>
			</td></tr>
		   </xsl:when>
		   <xsl:otherwise>
			<xsl:variable name="pos3"><xsl:value-of select="position()"/></xsl:variable>
			<xsl:choose>
			 <xsl:when test="exsl:node-set($cards)[position() = $pos3 + 1 and front='']">
			  <tr><td> &#160;
			  </td><td class="front">
			   <xsl:value-of select="front"/>
			  </td></tr>
			 </xsl:when>
			 <xsl:otherwise></xsl:otherwise>
			</xsl:choose>
		    </xsl:otherwise>
		</xsl:choose>
	      </xsl:for-each>
              </table>
<!--next-->   <xsl:call-template name="page">
		<xsl:with-param name="cards" select="exsl:node-set($cards)[position() > 8]"/>
	      </xsl:call-template>
	    </xsl:when>
	   <xsl:otherwise>
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:template>

</xsl:stylesheet>


