<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

  <xsl:variable name='interactive' select='0'/>

  <xsl:output method="html"/>

  <xsl:template match="/">
    <xsl:if test="$interactive">
      <form method='post' action='http://erwin-web/misc/update-votes.cgi'>
        <xsl:apply-templates/>
        <p/>
        <input type='submit' value='Update votes'/>
      </form>
    </xsl:if>
    <xsl:if test="not($interactive)">
      <xsl:apply-templates/>
    </xsl:if>
    
  </xsl:template>

  <xsl:template match="shelf">
    <tr><th style='background: yellow' colspan='4' align='center'>Shelf <xsl:value-of select='@name'/></th></tr>

    <tr>
      <td colspan='4'>
        <table>
          <tr>
            <xsl:apply-templates select='book' mode='featured'/>
          </tr>
        </table>
      </td>
    </tr>
    
    <tr>
      <th>Title</th>
      <th>Authors</th>
      <th>Comments</th>
    </tr>
    <xsl:apply-templates mode="table">
      <xsl:sort select="title" order="ascending"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match='book' mode='featured'>
    <xsl:if test="@featured = 1">
      <td style='font-size: xx-small; text-align: center' valign='top'>
        <a href='{url}'>
          <img border='0' src='{image}'/>
          <br/>
          <xsl:value-of select="title/@abbrev"/>
          <br/>
          by <xsl:value-of select="authors/@abbrev"/>
        </a>
      </td>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="books">
    <table class='books' style='empty-cells: show' border='0' cellpadding='1' cellspacing='0'>
      <xsl:apply-templates/>
    </table>
  </xsl:template>

  <xsl:template match="book" mode="table">
    <tr>
      <td>
        <a href="{url}" title="See '{title/.}' at amazon.com">
          <xsl:value-of select="title/@abbrev"/>
        </a>
      </td>
      <td>
        <xsl:if test="authors = authors/@abbrev">
          <xsl:value-of select="authors/."/>
        </xsl:if>
        
        <xsl:if test="authors != authors/@abbrev">
          <abbr title='{authors/.}'>
            <xsl:value-of select="authors/@abbrev"/>
          </abbr>
        </xsl:if>
        
      </td>
      <td>
        <!-- apply rating here -->
        <xsl:if test="$interactive">

          <input type='radio' name='{@isbn}-vote' value='0'>
            <xsl:if test="@rating = 0">
              <xsl:attribute name="checked">1</xsl:attribute>
            </xsl:if>
          </input>
          <input type='radio' name='{@isbn}-vote' value='1'>
            <xsl:if test="@rating = 1">
              <xsl:attribute name="checked">1</xsl:attribute>
            </xsl:if>
          </input>
          <input type='radio' name='{@isbn}-vote' value='2'>
            <xsl:if test="@rating = 2">
              <xsl:attribute name="checked">1</xsl:attribute>
            </xsl:if>
          </input>
          <input type='radio' name='{@isbn}-vote' value='3'>
            <xsl:if test="@rating = 3">
              <xsl:attribute name="checked">1</xsl:attribute>
            </xsl:if>
          </input>
          <input type='radio' name='{@isbn}-vote' value='4'>
            <xsl:if test="@rating = 4">
              <xsl:attribute name="checked">1</xsl:attribute>
            </xsl:if>
          </input>
          
        </xsl:if>
        <xsl:if test="not($interactive)">
          <xsl:if test="@rating = 4">****</xsl:if>
          <xsl:if test="@rating = 3">***</xsl:if>
          <xsl:if test="@rating = 2">**</xsl:if>
          <xsl:if test="@rating = 1">*</xsl:if>
          <xsl:if test="@rating = 0"></xsl:if>
          <xsl:value-of select="comments"/>
        </xsl:if>
      </td>
    </tr>
  </xsl:template>

</xsl:stylesheet>
