<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		version="1.0">

<!-- strip leading and trailing whitespace from all elements ... -->
<xsl:strip-space elements="*"/>

<xsl:param name="displayID">empty</xsl:param>
<xsl:param name="targetPerson">foo</xsl:param>


<xsl:output method="html" media-type="text/html"/>

<!-- ******************************************************* -->
<!-- ******************************************************* -->

<!-- root template: controls overall page layout --> 

<xsl:template match="/">
 <xsl:variable name="actualString" select="descendant::name[@type='person' and @persref=$targetPerson]/text()"/>
 <xsl:variable name="hitCount" select="count(descendant::name[@type='person' and @persref=$targetPerson])"/>
  <tr>
   <xsl:choose>
    <xsl:when test="$hitCount!=0">
     <td width="20%" align="left" valign="top">
      <div>
       <h3 align="left"><span class="hilit"><xsl:value-of select="$actualString"/></span><br/>occurs in:
       </h3>
       <xsl:call-template name="hitList"/>
       <h3>Total = <xsl:value-of select="$hitCount"/></h3>
      </div>
     </td>
     <td width="80%" align="left" valign="top">
      <xsl:choose>
       <xsl:when test="$displayID='none'">
        <xsl:apply-templates select="//div2[descendant::name/@persref=$targetPerson]" mode="firstDisplay"/>
       </xsl:when>
       <xsl:otherwise>
        <xsl:apply-templates select="//div2[@id=$displayID]" mode="display"/>
       </xsl:otherwise>
      </xsl:choose>
     </td>
    </xsl:when>
    <xsl:otherwise>
     <td align="left" valign="top"><!-- cell for main content -->
      <h3>
         <span class="hilit"><xsl:value-of select="$targetPerson"/></span> not found
      </h3>
     </td>
    </xsl:otherwise>
   </xsl:choose>
  </tr>
</xsl:template>

<!-- ******************************************************* -->
<!-- ******************************************************* -->


<xsl:template name="hitList">
  <xsl:for-each select="//div2[descendant::name/@persref=$targetPerson]">
   <xsl:variable name="div2ID" select="@id"/>
   <xsl:variable name="div2Head" select="child::head"/>
   <xsl:variable name="div1Head" select="parent::div1/head"/>
   <xsl:variable name="hitsNum" select="count(descendant::name[@persref=$targetPerson])"/>
   <xsl:choose>
    <xsl:when test="position()=1 and $displayID='none'">
     <p><xsl:value-of select="$div1Head"/>,<br/><xsl:text>  </xsl:text><span class="current"><a href="itDecSrchChr.php?person={$targetPerson}&amp;display={$div2ID}"><xsl:value-of select="$div2Head"/></a> (<xsl:value-of select="$hitsNum"/>)</span>
     </p>
    </xsl:when>
    <xsl:when test="$displayID=$div2ID">
     <p><xsl:value-of select="$div1Head"/>,<br/><xsl:text>  </xsl:text><span class="current"><a href="itDecSrchChr.php?person={$targetPerson}&amp;display={$div2ID}"><xsl:value-of select="$div2Head"/></a> (<xsl:value-of select="$hitsNum"/>)</span>
     </p>
    </xsl:when>
    <xsl:otherwise>
     <p><xsl:value-of select="$div1Head"/>,<br/><xsl:text>  </xsl:text><a href="itDecSrchChr.php?person={$targetPerson}&amp;display={$div2ID}"><xsl:value-of select="$div2Head"/></a> (<xsl:value-of select="$hitsNum"/>)
     </p>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:for-each>
</xsl:template>


<!-- ******************************************************* -->
<!-- ******************************************************* -->


<xsl:template match="div2" mode="firstDisplay">
 <xsl:variable name="div1Head" select="parent::div1/head"/>
  <xsl:choose>
   <xsl:when test="position()=1">
    <div>
     <h2>
      <xsl:value-of select="$div1Head"/>
     </h2>
     <xsl:apply-templates mode="display"/>
    </div>
   </xsl:when>
   <xsl:otherwise>
   </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="div2" mode="display">
 <xsl:variable name="div1Head" select="parent::div1/head"/>
  <div>
   <h2>
    <xsl:value-of select="$div1Head"/>
   </h2>
   <xsl:apply-templates mode="display"/>
  </div>
</xsl:template>


<xsl:template match="div3" mode="display">
 <div>
  <xsl:apply-templates mode="display"/>
 </div>
</xsl:template>


<xsl:template match="argument" mode="display">
 <xsl:apply-templates mode="display"/>
</xsl:template>


<xsl:template match="head" mode="display">
 <h2>
  <span class="current">
   <xsl:apply-templates mode="display"/>
  </span>
 </h2>
</xsl:template>


<xsl:template match="p" mode="display">
 <xsl:variable name="voice" select="ancestor::*[@who][position()= 1]/@who"/>
 <p>
  <span class="framing">[Voice: <xsl:value-of select="$voice"/>]</span><br/>
    <xsl:apply-templates mode="display"/>
 </p>
</xsl:template>


<xsl:template match="lg" mode="display">
 <p>
  <xsl:if test="position()= 1">
   <span class="framing">[Voice: <xsl:value-of select="parent::div3/@who"/>]</span><br/>
  </xsl:if>
  <xsl:apply-templates mode="display"/>
 </p>
</xsl:template>


<xsl:template match="l" mode="display">
 <xsl:apply-templates mode="display"/>
 <br/>
</xsl:template>

<xsl:template match="name" mode="display">
 <xsl:choose>
  <xsl:when test="@persref=$targetPerson">
   <span class="hilit">
    <xsl:apply-templates mode="display"/>
   </span>
  </xsl:when>
  <xsl:otherwise>
   <xsl:apply-templates mode="display"/>
  </xsl:otherwise>
 </xsl:choose>
</xsl:template>

<xsl:template match="q" mode="display">
 <xsl:text>"</xsl:text><xsl:apply-templates mode="display"/><xsl:text>"</xsl:text>
</xsl:template>

<xsl:template match="title" mode="display">
 <em>
  <xsl:apply-templates mode="display"/>
 </em>
</xsl:template>

<xsl:template match="milestone" mode="display">
 <xsl:variable name="selfID" select="@id"/>
  [<xsl:value-of select="substring($selfID, 7, 3)"/>]
</xsl:template>


<!-- ******************************************************* -->
<!-- ******************************************************* -->



<!-- COMMENTED OUT






 -->

</xsl:stylesheet>

