<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions">
   <xsl:output method="html" indent="no" encoding="utf-8" version="4.0" doctype-system="http://www.w3.org/TR/html4/strict.dtd"
      doctype-public="-//W3C//DTD HTML 4.01//EN"></xsl:output>

<!-- Location of the translation document !-->
<xsl:variable name="translation" select="document('translation.xml')"/>

<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="cssstylesheet.css" />
    <xsl:variable name="filename" select="/TEI/@xml:id"/>
    <xsl:choose>
      <xsl:when test="substring-after($filename,'-') = 'dn'">
        <link rel="stylesheet" type="text/css" href="devnag.css" />
      </xsl:when>
      <xsl:otherwise>
        <link rel="stylesheet" type="text/css" href="roman.css" />
      </xsl:otherwise>
    </xsl:choose>
    <link rel="stylesheet" type="text/css" href="jquery-ui-1.10.3.custom.css" />
    <title><xsl:value-of select="//titleStmt/title"/></title>
  </head>
  <body>
  <div id="container" class="hyphenate">
  <xsl:apply-templates/>
  </div>
  <script type="text/javascript" src="jquery-2.0.3.min.js"></script>
  <script type="text/javascript" src="jquery-ui-1.10.3.custom.js"></script>
  <script>
    $(function(){
        $('.app').hover(function(){
          var b = $(this).find('div').html();
          if(b){
            $('<p class="tooltip"></p>').html(b).appendTo('body').fadeIn('slow');
          }
        }, function() {
          $('.tooltip').remove();
        }).mousemove(function(e) {
          var mousex = e.pageX + 20; //Get X coordinates
          var mousey = e.pageY + 10; //Get Y coordinates
          $('.tooltip')
          .css({ top: mousey, left: mousex })
        });
    });
  </script>
  </body>
</html>
</xsl:template>

<!-- title statement !-->
<xsl:template match="titleStmt">
<div class="titleStmt">
  <h2><span class="title"><xsl:value-of select="title"/></span>
    <xsl:if test="author">
      <xsl:text> by </xsl:text><span class="author"><xsl:value-of select="author"/></span>
    </xsl:if>
    <xsl:if test="editor">
      <xsl:text>, reedited and translated by </xsl:text>
<xsl:for-each select="editor">
        <xsl:apply-templates />
        <xsl:if test="following-sibling::editor[2]">
          <xsl:text>, </xsl:text>
        </xsl:if>
        <xsl:if test="following-sibling::editor">
          <xsl:text> and </xsl:text>
        </xsl:if>
      </xsl:for-each>
    </xsl:if>
  </h2>
  <xsl:apply-templates select="respStmt"/>
  <xsl:variable name="filename" select="/TEI/@xml:id"/>
  <xsl:choose>
    <xsl:when test="substring-after($filename,'-') = 'dn'">
      <xsl:variable name="newfilename" select="concat(substring-before($filename,'-'), '-roman')"/>
      <xsl:text>This file is in devanāgarī. View in </xsl:text><a href="{$newfilename}.html">transliteration</a><xsl:text>.</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="newfilename" select="concat(substring-before($filename,'-'), '-dn')"/>
      <xsl:text>This file is in transliteration. View in </xsl:text><a href="{$newfilename}.html">devanāgarī</a><xsl:text>.</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</div>
</xsl:template>

<xsl:template match="//titleStmt/respStmt">
  <p><xsl:value-of select="resp"/>: <xsl:value-of select="name"/></p>
</xsl:template>

<!-- publication statement !-->
<xsl:template match="//publicationStmt"/>


<xsl:template match="//publicationStmt//p">
  <p><xsl:apply-templates/></p>
</xsl:template>

<!-- source description !-->
<xsl:template match="//sourceDesc"/>

<!-- bibliographic style processing !-->
<xsl:template match="listBibl">
  <ul>
  <xsl:for-each select="bibl">
    <li><xsl:apply-templates select="."/></li>
  </xsl:for-each>
  </ul>
</xsl:template>

<xsl:template match="bibl">
  <xsl:choose> <!-- if the text has an editor, put that first !-->
    <xsl:when test="editor">
      <xsl:for-each select="editor">
        <xsl:apply-templates />
        <xsl:if test="following-sibling::editor[2]">
          <xsl:text>, </xsl:text>
        </xsl:if>
        <xsl:if test="following-sibling::editor">
          <xsl:text> and </xsl:text>
        </xsl:if>
        <xsl:if test="not(following-sibling::editor)">
          <xsl:choose>
            <xsl:when test="preceding-sibling::editor | following-sibling::editor">
              <xsl:text> (eds.). </xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text> (ed.). </xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:if>
      </xsl:for-each>
    </xsl:when>
    <xsl:otherwise> <!-- otherwise, put in the author information !-->
      <xsl:for-each select="author">
        <xsl:apply-templates />
        <xsl:if test="following-sibling::author[2]">
          <xsl:text>, </xsl:text>
        </xsl:if>
        <xsl:if test="following-sibling::author">
          <xsl:text> and </xsl:text>
        </xsl:if>
        <xsl:text>. </xsl:text>
      </xsl:for-each>
    </xsl:otherwise>
  </xsl:choose>
  <em><xsl:choose>
    <xsl:when test="title[@type='sub']">
      <xsl:value-of select="title[@type='main']"/>
      <xsl:text>: </xsl:text><xsl:value-of select="title[@type='sub']"/>
    </xsl:when>
    <xsl:otherwise><xsl:value-of select="title"/></xsl:otherwise>
    </xsl:choose>
    </em><xsl:text>. </xsl:text> 
  <xsl:value-of select=".//pubPlace"/><xsl:text>: </xsl:text><xsl:value-of select=".//publisher"/><xsl:text>, </xsl:text><xsl:value-of select=".//date"/><xsl:text>. </xsl:text>
  <xsl:if test="series">
    <xsl:value-of select="series/title[@level='s']"/><xsl:text> </xsl:text><xsl:value-of select="series/biblScope"/><xsl:text>. </xsl:text>
  </xsl:if>
</xsl:template>

<xsl:template match="bibl//persName">
  <xsl:choose>
    <xsl:when test="position() = 1">
      <xsl:value-of select="surname"/><xsl:text>, </xsl:text>
      <xsl:for-each select="forename">
        <xsl:value-of select="."/>
        <xsl:if test="following-sibling::forename"><xsl:text> </xsl:text></xsl:if>
      </xsl:for-each>
    </xsl:when>
    <xsl:otherwise>
      <xsl:for-each select="forename">
        <xsl:value-of select="."/>
        <xsl:if test="following-sibling::forename"><xsl:text> </xsl:text></xsl:if>
      </xsl:for-each>
      <xsl:value-of select="surname"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
<!-- including mss. descriptions !-->

<xsl:template match="//sourceDesc/listWit">
<div class="//listWit">
  <p>Manuscript sources:</p>
  <xsl:apply-templates select="p"/>
  <ul>
  <xsl:for-each select="witness">
    <li><xsl:value-of select="p"/></li>
  </xsl:for-each>
  </ul>
</div>
</xsl:template>

<!-- encoding description !-->
<xsl:template match="//encodingDesc"/>

<!-- revision description !-->
<xsl:template match="//revisionDesc"/>

<!-- front matter !-->
<xsl:template match="//text/front"> 
  <a class="linebefore" name="front"/>
  <h2><xsl:value-of select="head" /></h2>
  <div class="front">
    <xsl:apply-templates/>
  </div>
</xsl:template>

<!-- let's have a table of contents too !-->
<xsl:template match="//text/body"> 
  <a class="linebefore" name="body"/>
  <h2><xsl:text>Text</xsl:text></h2>
  <div class="toc">
  <h3><xsl:text>Table of contents</xsl:text></h3>
  <xsl:for-each select="$translation/TEI/div">
    <p class="div1head"><a href="#{head}">
      <xsl:if test="@n">
        <xsl:value-of select="@n"/><xsl:text>: </xsl:text>
      </xsl:if>
    <xsl:value-of select="head"/></a></p>
    <xsl:for-each select="./div">
      <p class="div2head"><a href="#{head}">
        <xsl:if test="../@n">
          <xsl:value-of select="../@n"/>
          <xsl:if test="@n"><xsl:text>.</xsl:text><xsl:value-of select="@n"/></xsl:if>
          <xsl:text>: </xsl:text>
        </xsl:if><xsl:value-of select="head"/></a></p>
      <xsl:for-each select="./div">
        <p class="div3head"><a href="#{head}">
        <xsl:if test="../../@n">
          <xsl:value-of select="../../@n"/>
          <xsl:if test="../@n">
            <xsl:text>.</xsl:text><xsl:value-of select="../@n"/>
            <xsl:if test="@n"><xsl:text>.</xsl:text><xsl:value-of select="@n"/></xsl:if>
          </xsl:if><xsl:text>: </xsl:text>
        </xsl:if><xsl:value-of select="head"/></a></p>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:for-each>
  </div>
  <xsl:variable name="bodyvar">
    <xsl:apply-templates select="linkGrp[@type='section']"/>
  </xsl:variable>
  <xsl:apply-templates select="$bodyvar" mode="roundtwo"/>
  <a class="linebefore" name="body"/>
  <div type="notes">
    <a id="Notes.Text"/>
    <h3><xsl:text>Notes to the text</xsl:text></h3>
    <xsl:for-each select="//note[@type='totext']">
      <xsl:variable name="notenumber">
        <xsl:number count="note[@type='totext']" level="any"/>
      </xsl:variable>
      <xsl:variable name="notelabel" select="concat('t',$notenumber)"/>
      <xsl:variable name="notereference" select="concat('T',$notenumber)"/>
      <p>
        <a id="{$notelabel}" href="#{$notereference}"><sup><xsl:value-of select="$notenumber"/></sup></a>
        <xsl:apply-templates select="." mode="footnote"/>
      </p>
    </xsl:for-each>
  </div>
  <a class="linebefore" name="body"/>
  <div type="notes">
    <a id="Notes.Translation"/>
    <h3><xsl:text>Notes to the translation</xsl:text></h3>
    <xsl:for-each select="$translation//note[@type='totrans']">
      <xsl:variable name="notenumber">
        <xsl:number count="note[@type='totrans']" level="any"/>
      </xsl:variable>
      <xsl:variable name="notelabel" select="concat('e',$notenumber)"/>
      <xsl:variable name="notereference" select="concat('E',$notenumber)"/>
      <p>
        <a id="{$notelabel}" href="#{$notereference}"><sup><xsl:value-of select="$notenumber"/></sup></a>
        <xsl:apply-templates select="." mode="footnote"/>
      </p>
    </xsl:for-each>
  </div>
</xsl:template>

<!-- We will have THREE layers of linkGrp:
  (1) @type="section"
    This is the section-level layer, which controls
    the top-level alignment and with headings that 
    are printed in the table of contents. 
  (2) @type="block"
    These are typographic blocks: the Indic text
    will be aligned with the English text, and the
    formatting of each will be preserved.
  (3) @type="verse"
    This links individual verses with verses, with
    no formatting instructions. It is for indexing,
    searching, etc.  !-->

<!-- SECTION-LEVEL ALIGNMENT !-->
<!-- This assumes that the headings will only
  be labelled in the ENGLISH text. !-->
<xsl:template match="linkGrp[@type='section']">
  <!-- do all of the sections at once !-->
  <xsl:for-each select="link">
    <xsl:variable name="indicid" select="substring-after(substring-before(@target,' '),'#')"/>
    <xsl:variable name="indicnode">
      <xsl:copy-of select="//*[@xml:id=$indicid]"/>
    </xsl:variable>
    <xsl:variable name="englishid" select="substring-after(substring-after(@target,' '),'#')"/>
    <xsl:variable name="englishnode">
      <xsl:copy-of select="document('translation.xml')//*[@xml:id=$englishid]"/>
    </xsl:variable>
    <div class="section">
    <!-- If the English text has a heading,
          print it. !-->
    <xsl:if test="$englishnode//head">
      <xsl:variable name="aid">
        <xsl:value-of select="$englishnode//head"/>
      </xsl:variable>
      <a id="{$aid}"/>
      <h3><xsl:value-of select="$englishnode//head"/></h3>
    </xsl:if>
    <!-- Now call the routine for BLOCK-LEVEL ALIGNMENT
      within each section. !-->
    <xsl:call-template name="blockAlign">
      <xsl:with-param name="indic" select="$indicnode"/>
      <xsl:with-param name="english" select="$englishnode"/>
    </xsl:call-template>
    <p><span class="navigator">
      <!-- navigation block will go here !-->
    </span></p><br/>
    </div>
  </xsl:for-each>
</xsl:template>

<!-- BLOCK-LEVEL ALIGNMENT !-->
<xsl:template name="blockAlign">
  <!-- these are passed on from the SECTION template,
    so they should contain all of the material in the
    Indic and English sections. !-->
  <xsl:param name="indic"/>
  <xsl:param name="english"/>
    <table class="blocks">
    <!-- do all blocks in a section at once !-->
    <xsl:for-each select="$english//div[@type='block']">
    <tr>
      <td class="sa">
        <xsl:for-each select=".//ab">
          <xsl:variable name="xmlid" select="@cRef"/>
          <xsl:apply-templates select="$indic//*[@xml:id=$xmlid]"/>
        </xsl:for-each>
      </td>
      <td class="en">
        <xsl:apply-templates select="."/>
      </td>
    </tr>
    </xsl:for-each>
    </table>
</xsl:template>

<xsl:template match="lg">
  <xsl:variable name="xmlid" select="@xml:id"/>
  <xsl:variable name="type" select="@type"/>
  <a name="{$xmlid}"/>
  <div class="lg sanskrit" type="{$type}">
    <xsl:apply-templates/>
  </div>
</xsl:template>

<xsl:template match="p">
  <xsl:variable name="xmlid" select="@xml:id"/>
  <a name="{$xmlid}"/>
  <!-- HTML can't handle nested paragraphs. In order
    for the apparatus to work, paragraphs need to be
    divs. Too bad. !-->
  <div class="para sanskrit"><xsl:apply-templates/></div>
</xsl:template>

<!-- NOTES:
      These are put in as markers, and the
      full notes are called separately. !-->
<xsl:template match="note" mode="footnote">
  <xsl:apply-templates/>
</xsl:template>

<!-- Notes !-->
<!-- On the first pass, simply copy the note element !-->
<xsl:template match="note">
  <xsl:copy-of select="."/>
</xsl:template>
<!-- On the second pass, replace it with a footnote marker !-->
<xsl:template match="@*|node()" mode="roundtwo">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()" mode="roundtwo"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="note" mode="roundtwo">
  <xsl:choose>
    <xsl:when test="self::note[@type='totrans']">
      <xsl:variable name="totransnum" select="count(preceding::note[@type='totrans'])+1"/>
      <xsl:variable name="notelabel" select="concat('e',$totransnum)"/>
      <xsl:variable name="notereference" select="concat('E',$totransnum)"/>
      <a id="{$notereference}" href="#{$notelabel}"><sup><xsl:value-of select="$totransnum"/></sup></a>
    </xsl:when>
    <xsl:when test="self::note[@type='footnote']">
      <a href="#" class="app">*<div><xsl:apply-templates/></div></a>
    </xsl:when>
    <xsl:otherwise> <!-- when note[@type='totext'] !-->
      <xsl:variable name="totextnum" select="count(preceding::note[@type='totext'])+1"/>
      <xsl:variable name="notelabel" select="concat('t',$totextnum)"/>
      <xsl:variable name="notereference" select="concat('T',$totextnum)"/>
      <a id="{$notereference}" href="#{$notelabel}"><sup><xsl:value-of select="$totextnum"/></sup></a>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- The following bits will
     style the verse side of the page,
     including the apparatus. !-->

<!-- style lines depending on metrical form !-->
<!-- not presently doing anything with this, since CSS will work !-->
<xsl:template match="l">
  <xsl:if test="position()!=last()">
    <span class="line"><xsl:apply-templates/></span>
  </xsl:if>
  <xsl:if test="position()=last()">
    <span class="line"><xsl:apply-templates/></span>
  </xsl:if>
</xsl:template>
<!--<xsl:template match="l/seg[@type='pada']">
  <xsl:variable name="letter" select="substring(@xml:id,string-length(@xml:id))"/>
  <span class="{$letter}"><xsl:apply-templates/></span>
</xsl:template>!-->
<xsl:template match="l/seg">
  <xsl:variable name="letter">
    <xsl:choose>
      <xsl:when test="../following-sibling::l">
        <xsl:choose>
          <xsl:when test="./following-sibling::seg">a</xsl:when>
          <xsl:otherwise>b</xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="./following-sibling::seg">c</xsl:when>
          <xsl:otherwise>d</xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <span class="{$letter}"><xsl:apply-templates/></span>
</xsl:template>

<!-- italicize foreign-language inline text !-->
<xsl:template match="foreign">
<span class="foreign"><xsl:apply-templates /></span>
</xsl:template>
<!-- for lemmas in commentaries, etc !-->
<xsl:template match="mentioned">
<span class="mentioned"><xsl:apply-templates/></span>
</xsl:template>
<xsl:template match="ref[@type='pratīka']">
<xsl:variable name="xmlid" select="@cRef"/>
<xsl:variable name="filename" select="/TEI/@xml:id"/>
<xsl:variable name="linkedfile" select="concat(substring-before($filename,'comm'),'text-',substring-after($filename,'-'),'.html')"/>
<span class="mentioned"><a href="{$linkedfile}#{$xmlid}" target="_blank"><xsl:apply-templates/></a></span>
</xsl:template>

<!-- links !-->
<xsl:template match="ref[@target]">
  <xsl:variable name="target" select="@target"/>
  <a href="{$target}"><xsl:apply-templates/></a>
</xsl:template>

<!-- bold labels !-->
<xsl:template match="label">
<span class="label"><xsl:apply-templates /></span>
</xsl:template>

<xsl:template match="emph">
<em><xsl:apply-templates /></em>
</xsl:template>

<!-- format lists !-->
<xsl:template match="list">
  <xsl:choose>
  <xsl:when test="label">
    <ul class="labelled"><xsl:apply-templates/></ul>
  </xsl:when>
  <xsl:otherwise>
    <xsl:choose>
      <xsl:when test="@type='ordered'">
        <ol><xsl:apply-templates/></ol>
      </xsl:when>
      <xsl:otherwise>
        <ul><xsl:apply-templates/></ul>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:otherwise>
  </xsl:choose>
</xsl:template>
<xsl:template match="item">
<li>
  <xsl:if test="../label">
    <span class="label"><xsl:value-of select="preceding-sibling::label[1]"/></span><xsl:text> </xsl:text>
  </xsl:if>
  <xsl:apply-templates/></li>
</xsl:template>
<xsl:template match="list/label"/>

<!-- format tables !-->
<xsl:template match="table">
<table><xsl:apply-templates/></table>
</xsl:template>
<xsl:template match="row">
<tr><xsl:apply-templates/></tr>
</xsl:template>
<xsl:template match="cell">
<td><xsl:apply-templates/></td>
</xsl:template>

<!-- only reads lemmas in the text !-->
<xsl:template match="app">
  <xsl:variable name="apparatus">
      <span class="sanskrit"><xsl:value-of select="lem"/></span>
      <xsl:if test="lem/@wit">
        <xsl:text> </xsl:text><span style="font-weight:800"><xsl:value-of select="translate(translate(lem/@wit,' ',''),'#','')"/></span>
        </xsl:if>
      <xsl:text>] </xsl:text>
      <xsl:for-each select=".//rdg">
        <xsl:if test="@type='gloss'">
          <xsl:text> = </xsl:text>
        </xsl:if>
        <span class="sanskrit"><xsl:apply-templates /></span>
        <xsl:if test="note">
          <span class="appnote"><xsl:value-of select="./note"/></span>
        </xsl:if>
        <xsl:if test="@wit">
          <xsl:text> </xsl:text><span style="font-weight:800"><xsl:value-of select="translate(translate(@wit,' ',''),'#','')"/></span>
        </xsl:if>
        <xsl:if test="@type='conj'">
          <xsl:text> conj. </xsl:text><span style="font-weight:800"><xsl:value-of select="translate(translate(@resp,' ',''),'#','')"/></span>
        </xsl:if>
      <xsl:if test="position()!=last()">
        <xsl:text>, </xsl:text>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>
  <a href="#" class="app"><xsl:value-of select="lem"/><div><xsl:copy-of select="$apparatus"/></div></a>
</xsl:template>

<xsl:template match="rdg/note"/>
<xsl:template match="head" />

</xsl:stylesheet>
