<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:dyn="http://exslt.org/dynamic" extension-element-prefixes="dyn">

  <xsl:template name="chapterDetailView">
    <xsl:param name="path" />
    <fieldset id="chapterView">
      <legend>Chapter</legend>
      	
        <table class="textChart">
          <tbody>
            <tr class="row1">
              <td><b>Ordinal:</b>&#160;<xsl:value-of select="dyn:evaluate($path)/Ordinal" /> </td>
              <td> <b>Title:</b>&#160;<xsl:value-of select="dyn:evaluate($path)/Title" /> </td>
            </tr>
            
            <tr class="row1">
              <td><b>Page Count:</b>&#160;<xsl:value-of select="dyn:evaluate($path)/NumPages" /></td>
              <td><b>Language:</b>&#160;<xsl:value-of select="dyn:evaluate($path)/LanguageCode" /></td>
            </tr>

            <!--- put dynamic call to build Contributor and Subject Code lists here -->
            <xsl:call-template name="buildChapterContributorRows">
                <xsl:with-param name="path"><xsl:value-of select="$path" /></xsl:with-param>
            </xsl:call-template>

          </tbody>
        </table>
    </fieldset>
  </xsl:template>

    <!-- JPK - I think we may end up with chapter subjects, so I am going to leave the subject logic in here -->
    <xsl:template name="buildChapterContributorRows">
        <xsl:param name="path" />
        <xsl:variable name="numContributors" select="count(dyn:evaluate($path)/ContributorHash/Contributor)" />
        <xsl:variable name="numSubjects" select="count(dyn:evaluate($path)/SubjectHash/Subject)" />
        <xsl:variable name="numRows">
            <xsl:choose>
                <xsl:when test="$numContributors &gt; $numSubjects"><xsl:value-of select="$numContributors" /></xsl:when>
                <xsl:when test="$numContributors &lt; $numSubjects"><xsl:value-of select="$numSubjects" /></xsl:when>
                <xsl:otherwise><xsl:value-of select="$numSubjects" /></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:call-template name="addChapterContributorAndSubjectCodeRow">
            <xsl:with-param name="numRows"><xsl:number value="number($numRows)" /></xsl:with-param> 
            <xsl:with-param name="count"><xsl:number value="0" /></xsl:with-param> 
            <xsl:with-param name="numContributors"><xsl:value-of select="$numContributors" /></xsl:with-param> 
            <xsl:with-param name="numSubjects"><xsl:value-of select="$numSubjects" /></xsl:with-param> 
            <xsl:with-param name="path" select="$path" />
        </xsl:call-template>

    </xsl:template>

    <xsl:template name="addChapterContributorAndSubjectCodeRow">
  	    <xsl:param name="path" />
  	    <xsl:param name="count" />
  	    <xsl:param name="numRows" />
  	    <xsl:param name="numContributors" />
  	    <xsl:param name="numSubjects" />
        <xsl:if test="$count &lt; $numRows">
            <tr class="row1">
              <td>
                <xsl:choose>
                    <xsl:when test="$count &lt; $numContributors"><b>Contributor:</b>&#160;<xsl:value-of select="dyn:evaluate($path)/ContributorHash/Contributor[@i = $count]/Name" /></xsl:when>
                    <xsl:otherwise></xsl:otherwise>
                </xsl:choose>
              </td>
              <td>
                <xsl:choose>
                    <xsl:when test="$count &lt; $numSubjects"><b>Subject Code:</b>&#160;<xsl:value-of select="dyn:evaluate($path)/SubjectHash/Subject[@i = $count]/SubjectCode" /></xsl:when>
                    <xsl:otherwise></xsl:otherwise>
                </xsl:choose>
              </td>
            </tr>
            <xsl:call-template name="addChapterContributorAndSubjectCodeRow">
                <xsl:with-param name="count"><xsl:number value="number($count)+1" /></xsl:with-param>
                <xsl:with-param name="numRows"><xsl:number value="number($numRows)" /></xsl:with-param> 
                <xsl:with-param name="numContributors"><xsl:value-of select="$numContributors" /></xsl:with-param> 
                <xsl:with-param name="numSubjects"><xsl:value-of select="$numSubjects" /></xsl:with-param> 
                <xsl:with-param name="path" select="$path" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

	<xsl:template name="lastModifiedCell">
		<b>Last Modified:</b>&#160;<xsl:value-of select="Chapter/ModifiedDate" /><xsl:if test="normalize-space(Chapter/ModifiedBy)"> by <xsl:value-of select="Chapter/ModifiedBy" /></xsl:if>	
	</xsl:template>


</xsl:stylesheet>
