<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns:bridge="http://www.thomaso.com/xmlns/bridge"
                exclude-result-prefixes="bridge xsl"
		xmlns="http://www.w3.org/1999/xhtml">

<xsl:param name="serverSide">no</xsl:param>
<xsl:variable name="columns">3</xsl:variable>
<xsl:variable name="rows">6</xsl:variable>
<xsl:variable name="honors">AKQJ</xsl:variable>
<xsl:variable name="ten">10</xsl:variable>
<xsl:variable name="percentage">33.33%</xsl:variable>

<xsl:variable name="void">&#x2014;&#x2014;</xsl:variable> <!-- 2 emdashes -->

<xsl:variable name="spade"><span class="blacksuit">&#x2660;</span></xsl:variable>
<xsl:variable name="heart"><span class="redsuit">&#x2665;</span></xsl:variable>
<xsl:variable name="diamond"><span class="redsuit">&#x2666;</span></xsl:variable>
<xsl:variable name="club"><span class="blacksuit">&#x2663;</span></xsl:variable>

<xsl:variable name="bgcolor">#66FF66</xsl:variable>

<xsl:template match="/">
<html>
<head>
<LINK href="../favicon.ico" rel="SHORTCUT ICON"/>
<style type="text/css">
table.page {
   border: thin groove  ;
   width: 100% ;
   background-color: #66FF66 
}

table.deal {
   border: thin groove  ;
   width: 100% ;
   padding: 1em
}

table.hand {
   width: 100% ;
}

td.pagecell {
   width: <xsl:value-of select="$percentage"/>
   border:  thin groove
}

td.holding { text-align: left ; width: auto }
td.suitsym { 
font-family: "Courier New", Courier, monospace;
width: 10pt ; text-align: right 
}

.redsuit { color: red; }
</style>

<title>Dealset Generated with XSL</title>
</head>
<body>
<p>
<em>This page is written as HTML by an XSL script. 
<xsl:if test="'no'=$serverSide">To view the input XML,
use the browser menu item, "View Source."</xsl:if></em>
</p>
<xsl:apply-templates select="/bridge:dealset"/>
</body>
</html>
</xsl:template>

<xsl:template match="bridge:dealset">
<!-- for each deal which should begin a "page" -->
<xsl:for-each select="bridge:deal[position() mod ($columns*$rows)=1]">

<table class="page">
<tr class="pagerow"><th colspan="{$rows}">Page <xsl:value-of select="position()"/></th></tr>

<!-- For each deal on this page which should begin a row -->
<xsl:for-each select=".|following-sibling::bridge:deal[position() mod $columns=0][position()&lt;$rows]">
<!-- Create a table row -->

<tr class="pagecell" valign="center">
<!-- For each deal on that row -->
<xsl:for-each select=".|following-sibling::bridge:deal[$columns>position()]">

<td class="pagecell"><xsl:apply-templates select="."/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
<xsl:if test="position()!=last()"><br/></xsl:if>
</xsl:for-each>
</xsl:template>

<xsl:template match="bridge:title">
<h1><xsl:apply-templates/></h1>
</xsl:template>

<xsl:template name="holding">
<xsl:param name="attr"/>
<xsl:choose>
<xsl:when test="contains($attr,'T')">
<xsl:value-of select="translate(substring-before($attr,'T'),'AKQJ',$honors)"/><xsl:copy-of select="$ten"/><xsl:value-of select="translate(substring-after($attr,'T'),'AKQJ',$honors)"/>
</xsl:when>
<xsl:when test="string($attr)"><xsl:value-of select="translate($attr,'AKQJ',$honors)"/></xsl:when>
<xsl:otherwise><xsl:copy-of select="$void"/></xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="card">
<xsl:param name="card"/>
<xsl:choose>
<xsl:when test="$card='T'">
<xsl:copy-of select="$ten"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="translate($card,'AKQJ',$honors)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="writehand">
<table class="hand">
<xsl:if test="@sp">
<tr><td class="suitsym"><xsl:copy-of select="$spade"/></td>
   <td class="holding" align="left"><xsl:call-template name="holding">
	<xsl:with-param name="attr" select="@sp"/>
   </xsl:call-template></td></tr>
</xsl:if>

<xsl:if test="@he">
<tr><td class="suitsym"><xsl:copy-of select="$heart"/></td>
    <td class="holding"><xsl:call-template name="holding">
	<xsl:with-param name="attr" select="@he"/>
   </xsl:call-template></td></tr>
</xsl:if>

<xsl:if test="@di">
<tr><td class="suitsym"><xsl:copy-of select="$diamond"/></td>
   <td class="holding"><xsl:call-template name="holding">
	<xsl:with-param name="attr" select="@di"/>
   </xsl:call-template></td></tr>
</xsl:if>

<xsl:if test="@cl">
<tr><td class="suitsym"><xsl:copy-of select="$club"/></td>
   <td class="holding"><xsl:call-template name="holding">
	<xsl:with-param name="attr" select="@cl"/>
   </xsl:call-template></td></tr>
</xsl:if>
</table>
</xsl:template>

<xsl:template match="bridge:author"></xsl:template>

<xsl:template match="bridge:programdata">
</xsl:template>

<xsl:template match="bridge:north|bridge:east|bridge:south|bridge:west|bridge:hand">
	<xsl:call-template name="writehand"/>
</xsl:template>

<xsl:template match="bridge:deal">
<table class="deal">
<tr>
<td width="10%"/><td width="10%"/>
<td width="10%"/><td width="10%"/>
<td width="10%"/><td width="10%"/>
<td width="10%"/><td width="10%"/>
<td width="10%"/><td width="10%"/>
</tr>
<tr valign="bottom">
<td colspan="4" width="40%" valign="top"><xsl:apply-templates select="bridge:header"/></td>
<td colspan="4" width="40%"><xsl:apply-templates select="bridge:hand[@seat='north']"/></td>
<td colspan="2" width="20%"/>
</tr><tr>
<td colspan="2" width="10%"/>
<td colspan="4" width="40%"><xsl:apply-templates select="bridge:hand[@seat='west']"/></td>
<!--<td colspan="1" width="10%"/>-->
<td colspan="4" width="40%"><xsl:apply-templates select="bridge:hand[@seat='east']"/></td>
</tr><tr valign="top">
<td colspan="4"/>
<td colspan="4"><xsl:apply-templates select="bridge:hand[@seat='south']"/></td>
<td colspan="2"/>
</tr>
</table>

</xsl:template>

<xsl:template match="bridge:a[@name]">
<a><xsl:attribute name="name">
<xsl:value-of select="@name"/>
</xsl:attribute><xsl:apply-templates/></a>
</xsl:template>

<xsl:template match="bridge:a[@href]">
<a target="_top"><xsl:attribute name="href">
<xsl:value-of select="@href"/>
</xsl:attribute><xsl:apply-templates/></a>
</xsl:template>

<xsl:template match="bridge:chapter">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="bridge:h2">
<h3><xsl:apply-templates/></h3>
</xsl:template>

<xsl:template match="bridge:h3">
<h4><xsl:apply-templates/></h4>
</xsl:template>

<xsl:template match="*">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>

</xsl:stylesheet>

