<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:tei="http://www.tei-c.org/ns/1.0"
    xmlns:mets="http://www.loc.gov/METS/"
    xmlns:mods="http://www.loc.gov/mods/v3"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exist="http://exist.sourceforge.net/NS/exist"
    exclude-result-prefixes="tei" 
    version="1.0">
  
    <!-- Einbindung der Standard-Templates und Variablen -->
    <xsl:import href="http://diglib.hab.de/rules/styles/param.xsl"/>
    <xsl:import href="http://diglib.hab.de/rules/styles/tei-phraselevel.xsl"/>
    

    <xsl:output encoding="UTF-8" indent="yes" method="html"/>
    
    <xsl:strip-space elements="*"/>
   
    <xsl:param name="dir"/>
    <xsl:param name="footerXML"/>
    <xsl:param name="footerXSL"/>
    <xsl:param name="metsID"/>
    
    <xsl:variable name="metsfile"><xsl:value-of select="concat('http://diglib.hab.de/',$dir,'/mets.xml')"/></xsl:variable>

    <xsl:template match="/">
        <html>
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title><xsl:value-of select="/tei:TEI/tei:teiHeader/tei:fileDesc/tei:titleStmt/tei:title"/></title>
                <link rel="stylesheet" type="text/css" href="http://diglib.hab.de/navigator2.css"/>
                <script src="http://diglib.hab.de/navigator.js" type="text/javascript"><noscript>please activate javascript to enable wdb functions</noscript></script>
            </head>
            <body style="text-align:justify;">
                <!--  Dokumentkopf -->
                <div id="doc_header">
                    <div style="margin-bottom:0.2em;padding:0;letter-spacing:0.3em;">Artikel<div style="letter-spacing:0;font-size:small;float:right;"><xsl:text>Zitieren: http://diglib.hab.de/</xsl:text><xsl:value-of select="$dir"/><xsl:text>/id/</xsl:text><xsl:value-of select="$metsID"/><xsl:text>/start.htm</xsl:text></div></div>
                    <hr style="margin:0;padding:0;height:1px;"/>
                    <!-- weitere Funktionen rechtsbuendig -->
                </div>
                <!-- Titel -->
                <div id="caption">
                    <div>
                        <xsl:value-of select="/tei:TEI/tei:teiHeader/tei:fileDesc/tei:titleStmt/tei:title"/>
                    </div>
                    <div style="font-size:0.8em;font-weight:300;margin:0.3em 0 1em 0;">
                        <xsl:value-of select="/tei:TEI/tei:teiHeader/tei:fileDesc/tei:titleStmt/tei:author//tei:forename"/><xsl:text> </xsl:text><xsl:value-of select="/tei:TEI/tei:teiHeader/tei:fileDesc/tei:titleStmt/tei:author//tei:surname"/>
                    </div>
                </div>
                <!-- Inhaltsuebersicht -->
                <div id="headings">
                    <ul>
                        <xsl:for-each select="/tei:TEI/tei:text/tei:body//tei:div/tei:head[not(parent::tei:figure)]">
                            <li>
                                <xsl:number level="multiple" count="tei:div" format="1.1. "/>
                                <a>
                                    <xsl:attribute name="href"><xsl:text>#hd</xsl:text><xsl:number level="any"/></xsl:attribute>
                                    <xsl:apply-templates>
                                        <xsl:with-param name="caption">true</xsl:with-param>
                                    </xsl:apply-templates>
                                </a>
                                <xsl:apply-templates select="../tei:argument/tei:p">
                                    <xsl:with-param name="caption">true</xsl:with-param>
                                </xsl:apply-templates>
                            </li>
                        </xsl:for-each>
                    </ul>
                </div>
                <!-- Haupttext -->
                <div class="content">
                    <xsl:apply-templates select="tei:TEI/tei:text/tei:body/tei:div[not(@type='footnotes')]"/>
                </div>
                <!-- Fussnoten -->
                <div id="doc_footnotes">
                    <xsl:choose>
                        <xsl:when test="tei:TEI/tei:text/tei:body/tei:div[@type='footnotes']">
                            <a name="footnotes">
                                <hr style="width:50%;height:1px;margin:1em 0 1em 0;"/>
                            </a>
                            <!--  Wenn es einen Fussnotenbereich gibt -->
                            <xsl:apply-templates select="tei:TEI/tei:text/tei:body/tei:div[@type='footnotes']"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:if test="tei:TEI/tei:text/tei:body//tei:note[@type='footnote']">
                                <a name="footnotes">
                                    <hr style="width:50%;height:1px;margin:1em 0 1em 0;"/>
                                </a>
                            </xsl:if>
                            <!--  andernfalls notes auswerten -->
                            <xsl:for-each select="tei:TEI/tei:text/tei:body//tei:note[@type='footnote']">
                                <div class="footnotes">
                                    <xsl:element name="a">
                                        <xsl:attribute name="name">
                                            <xsl:text>fn</xsl:text>
                                            <xsl:number level="any" count="tei:note[@type ='footnote']"/>
                                        </xsl:attribute>
                                        <a>
                                            <xsl:attribute name="href">
                                                <xsl:text>#fna</xsl:text><xsl:number level="any" count="tei:note[@type ='footnote']"/>
                                            </xsl:attribute>
                                            <span style="font-size:0.7em;vertical-align:super;color:blue;margin-right:0.3em;"><xsl:number level="any" count="tei:note[@type ='footnote']"/></span>
                                        </a>
                                        <xsl:apply-templates/>
                                    </xsl:element>
                                </div>
                            </xsl:for-each>
                        </xsl:otherwise>
                    </xsl:choose>
                </div>
                <!-- footer -->
                <div id="doc_footer">
                    <hr/>
                    <b><xsl:text>© </xsl:text><xsl:value-of select="/tei:TEI/tei:teiHeader/tei:fileDesc/tei:titleStmt/tei:author//tei:forename"/><xsl:text> </xsl:text><xsl:value-of select="/tei:TEI/tei:teiHeader/tei:fileDesc/tei:titleStmt/tei:author//tei:surname"/></b>
                    <xsl:choose>  
                        <xsl:when test="contains(document($metsfile)//mets:div[current()/tei:TEI/@xml:id = mets:fptr/@FILEID]/@ADMID,'_CC')">
                            <a>
                                <xsl:attribute name="href">
                                    <xsl:value-of select="document($metsfile)//mets:rightsMD[@ID=document($metsfile)//mets:div/@ADMID]/mets:mdRef/@xlink:href"/>
                                </xsl:attribute>
                                <img src="http://diglib.hab.de/images/cc-by-sa.png" alt="image CC BY-SA licence" width="50px" align="right" hspace="10px"/>
                            </a>
                        </xsl:when>
                      </xsl:choose>
                </div>
                <xsl:call-template name="footer">
                    <xsl:with-param name="footerXML">
                        <xsl:value-of select="$footerXML"/>
                    </xsl:with-param>
                    <xsl:with-param name="footerXSL">
                        <xsl:value-of select="$footerXSL"/>
                    </xsl:with-param>
                </xsl:call-template>
            </body>
        </html>
    </xsl:template>
    
   
    <!--header-->
    <!-- editor, contributer etc. -->
    <xsl:template match="tei:respStmt">
        <xsl:if test="position() != 1">
            <xsl:text>,</xsl:text>
        </xsl:if>
        <xsl:text> </xsl:text>
        <xsl:value-of select="tei:resp"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="tei:name"/>
    </xsl:template>

    <!-- Front / Header-->
    <xsl:template match="tei:front">
        <xsl:apply-templates select="tei:titlePage"/>
    </xsl:template>

    <xsl:template match="tei:titlePage">
        <p class="content">
            <xsl:apply-templates/>
        </p>
    </xsl:template>

    <xsl:template match="tei:docTitle">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="tei:byline">
        <p class="content">
            <xsl:apply-templates/>
        </p>
    </xsl:template>

    <xsl:template match="tei:imprimatur">
        <p class="content">
            <xsl:apply-templates/>
        </p>
    </xsl:template>

    <xsl:template match="tei:docDate">
        <p class="content">
            <xsl:apply-templates/>
        </p>
    </xsl:template>

    <xsl:template match="tei:docAuthor">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="tei:epigraph">
        <p class="legende">
            <xsl:apply-templates/>
        </p>
    </xsl:template>

    <xsl:template match="tei:titlePart">
        <p class="content">
            <xsl:apply-templates/>
        </p>
    </xsl:template>


    <!--body-->
    <!-- main distribution-->
    <xsl:template match="tei:div">
        <a>
            <xsl:attribute name="name">
                <xsl:text>div</xsl:text>
                <xsl:number level="any"/>
            </xsl:attribute>
        </a>
        <div>
            <xsl:if test="@type = 'abstract'"><xsl:attribute name="style">font-size:smaller;</xsl:attribute></xsl:if>
            <xsl:apply-templates/>
        </div>
    </xsl:template>
    
    <xsl:template match="tei:p">
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template>

<!--    Ueberschriften-->
    <xsl:template match="tei:head">
        <div style="clear:both;"/>
        <xsl:choose>
            <xsl:when test="parent::tei:list"> 
                <hr style="margin-top:2em;"/><h2><xsl:apply-templates/></h2>
            </xsl:when>
            <!-- keine Bildueberschriften verarbeiten -->
            <xsl:when test="parent::tei:figure">
                <xsl:text>YYYYYY</xsl:text>
            </xsl:when>
             <xsl:otherwise>
                <a>
                    <xsl:attribute name="name"><xsl:text>hd</xsl:text><xsl:number level="any"/></xsl:attribute>
                    <xsl:text> </xsl:text>
                </a>
                <h2 style="background-color:#EEE;padding:0.3em;margin-top:1em;position:relative;">
                    <span><xsl:number level="multiple" count="tei:div" format="1.1. "/><xsl:apply-templates/></span>
                    <div style="position:absolute;left:95%;top:33%;z-index:1;"><a href="#" title="up"><img src="http://diglib.hab.de/images/arrowup.png" alt="[arrow up]"/></a></div>
                </h2>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- dictonary entries -->
    <xsl:template match="tei:orth">
        <br/>
        <span class="lemma">
            <a>
                <xsl:attribute name="name">
                    <xsl:text>orth</xsl:text>
                    <xsl:number level="any"/>
                </xsl:attribute>
            </a>
            <xsl:apply-templates/>
        </span>
    </xsl:template>

    <!-- pysical pages     -->
    <xsl:template match="tei:pb">
        <div class="pagebreak">
            <xsl:text> || </xsl:text>
            <a>
                <xsl:attribute name="name">
                    <xsl:value-of select="substring(@facs,2)"/>
                </xsl:attribute>
            </a>
            <xsl:text> [</xsl:text>
            <xsl:if test="@ed">
                <xsl:value-of select="substring(@ed,4)"/>
                <xsl:text>: </xsl:text>
            </xsl:if>
            <xsl:choose>
                <xsl:when test="@facs">
                    <xsl:element name="a">
                        <xsl:attribute name="href">
                            <xsl:value-of select="$displayurl"/>
                            <!-- convert identifier z.B. drucke_qun-59-9-1_00006 -->
                            <!-- select type of resource e.g. drucke -->
                            <xsl:value-of select="substring-before(substring(@facs,2),'_')"/>
                            <xsl:text>/</xsl:text>
                            <!-- select shelf mark e.g.qun-59-9-1 -->
                            <xsl:value-of
                                select="substring-before(substring-after(substring(@facs,2),'_'),'_')"/>
                            <!-- select Image-No , e.g. 00006  -->
                            <xsl:text>&amp;image=</xsl:text>
                            <xsl:value-of
                                select="substring-after(substring-after(substring(@facs,2),'_'),'_')"
                            />
                        </xsl:attribute>
                        <xsl:attribute name="target">
                            <xsl:text>display2</xsl:text>
                        </xsl:attribute>
                        <xsl:choose>
                            <xsl:when test="@n">
                                <xsl:value-of select="@n"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of
                                    select="substring-after(substring-after(substring(@facs,2),'_'),'_')"
                                />
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:element>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="@n"/>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:text>] </xsl:text>
        </div>
    </xsl:template>
    
    <xsl:template match="tei:term">
        <span style="font-style: italic;">
            <xsl:apply-templates/>
        </span>
    </xsl:template>
    
<!--    Links -->
    <xsl:template match="tei:ref">
        <xsl:choose>
            <xsl:when test="@type='footnote'">
                <xsl:element name="a">
                    <xsl:attribute name="name">
                        <xsl:text>fna</xsl:text>
                        <xsl:value-of select="."/>
                    </xsl:attribute>
                    <a>
                        <xsl:attribute name="href">
                            <xsl:value-of select="@target"/>
                        </xsl:attribute>
                        <span style="font-size:10pt;vertical-align:super;color:blue;"><xsl:value-of select="."/></span>
                    </a>
                </xsl:element>
            </xsl:when>
            <xsl:when test="@type='bibliography'">
                <a>
                    <xsl:if test="starts-with(@target,'#')">
                        <xsl:attribute name="title">
                            <xsl:value-of select="normalize-space(id(substring(@target,2)))"/>
                        </xsl:attribute>
                    </xsl:if>  
                    <xsl:attribute name="href"> <xsl:value-of select="@target"/></xsl:attribute>
                    <xsl:apply-templates/>
                </a>
            </xsl:when>
            <xsl:when test="starts-with(@target,'http://')">
                <a>
                    <xsl:attribute name="href"><xsl:value-of select="@target"/></xsl:attribute>
                    <xsl:attribute name="target">_blank</xsl:attribute>
                    <xsl:apply-templates/>
                </a>
            </xsl:when>
            <xsl:when test="starts-with(@target,'#')">
                <a>
                    <xsl:attribute name="href"><xsl:value-of select="@target"/></xsl:attribute>
                    <xsl:apply-templates/>
                </a>
            </xsl:when>
            <xsl:when test="@cRef">
                <a>
                    <xsl:attribute name="href">
                        <xsl:call-template name="referencesREF">
                            <xsl:with-param name="cRefValue">
                                <xsl:value-of select="@cRef"/>
                            </xsl:with-param>
                            <xsl:with-param name="refType">
                                <xsl:value-of select="@type"/>
                            </xsl:with-param>
                            <xsl:with-param name="refXSL">
                                <xsl:value-of select="footerXSL"/>
                            </xsl:with-param>
                            <xsl:with-param name="refXML">
                                <xsl:value-of select="$footerXML"/>
                            </xsl:with-param>
                        </xsl:call-template>
                    </xsl:attribute>
                    <xsl:apply-templates/>
                </a>
             </xsl:when>
        </xsl:choose>
     </xsl:template>
    
 
   <xsl:template match="tei:ptr">
        <xsl:apply-templates/>
        <xsl:choose>
            <xsl:when test="starts-with(@target,'http://')">
                [<a>
                    <xsl:attribute name="href"><xsl:value-of select="@target"/></xsl:attribute>
                    <xsl:attribute name="target">_blank</xsl:attribute>
                    <xsl:text>Link</xsl:text>
                </a>]
            </xsl:when>
            <xsl:when test="starts-with(@target,'#')">
                [<a>
                    <xsl:attribute name="href"><xsl:value-of select="@target"/></xsl:attribute>
                    <xsl:text>Link</xsl:text>
                </a>]
            </xsl:when>
            <xsl:when test="@cRef">
                <xsl:call-template name="referencesPTR">
                    <xsl:with-param name="refType">
                        <xsl:value-of select="@type"/>
                    </xsl:with-param>
                    <xsl:with-param name="refValue">
                        <xsl:value-of select="@cRef"/>
                    </xsl:with-param>
                 </xsl:call-template>
            </xsl:when>
         </xsl:choose>
     </xsl:template>
 
 
 
    <xsl:template match="tei:listBibl">
        <ul>
            <xsl:apply-templates/>
        </ul>
    </xsl:template>
    
    
    <xsl:template match="tei:bibl">
        <xsl:choose>
            <xsl:when test="parent::tei:listBibl">
                <li>
                    <xsl:element name="a">
                        <xsl:attribute name="name">
                            <xsl:value-of select="@xml:id"/>
                        </xsl:attribute>
                        <xsl:text> </xsl:text>
                    </xsl:element>
                    <xsl:apply-templates/>
                </li>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template match="tei:title">
        <i>
            <xsl:apply-templates/>
        </i>
    </xsl:template>
    
    <xsl:template match="tei:quote">
        <xsl:text>„</xsl:text>
        <xsl:apply-templates/>
        <xsl:text>“</xsl:text>
    </xsl:template>
    
    <!-- footnotes, annotations -->

    <xsl:template match="tei:note">
        <xsl:choose>
            <xsl:when test="parent::tei:div[@type='footnotes' ]">
                <div class="footnotes">
                <xsl:element name="a">
                    <xsl:attribute name="name">
                        <xsl:text>fn</xsl:text>
                         <xsl:value-of select="@n"/>
                    </xsl:attribute>
                    <a>
                        <xsl:attribute name="href">
                            <xsl:text>#fna</xsl:text><xsl:value-of select="@n"/>
                        </xsl:attribute>
                        <span style="font-size:10pt;vertical-align:super;color:blue;margin-right:0.3em;">
                            <xsl:value-of select="@n"/>
                        </span>
                    </a>
                </xsl:element>
                    <xsl:apply-templates/>
                </div>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates/>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:text> </xsl:text>
    </xsl:template>
    
    <!-- Grafiken -->
    <xsl:template match="tei:figure">
        <xsl:apply-templates select="tei:graphic"/>
    </xsl:template>
    
    <xsl:template match="tei:graphic">
        <xsl:variable name="ill">
            <xsl:number level="any"/>
        </xsl:variable>
        <div class="illustration">
            <xsl:choose>
                <xsl:when test="($ill mod 2) = 1">
                    <xsl:attribute name="style">
                        float:right;margin:1em 0 1em 0.7em;
                    </xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:attribute name="style">
                        margin:1em 0.7em 1em 0;
                    </xsl:attribute>
                </xsl:otherwise>
            </xsl:choose>
          <a>
            <xsl:attribute name="href">
                <xsl:text>javascript:show_annotation_html('</xsl:text><xsl:value-of select="$dir"/><xsl:text>','</xsl:text><xsl:value-of select="@url"/><xsl:text>',700,700)</xsl:text>
            </xsl:attribute>
            <img src="{@url}" alt="Abbildung" >
            <xsl:choose>
                <xsl:when test="@rend ='horizontal'">
                    <xsl:attribute name="heigth">200</xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:attribute name="width">200</xsl:attribute>
                </xsl:otherwise>
            </xsl:choose>
        </img>
        </a>
          <div class="illustration_desc">  
              <xsl:choose>
                  <xsl:when test="@rend ='horizontal'">
                      <xsl:attribute name="style">position:relative; height:200;font-size:smaller;text-align:left;</xsl:attribute>
                  </xsl:when>
                  <xsl:otherwise>
                      <xsl:attribute name="style">position:relative; width:200;font-size:smaller;text-align:left;</xsl:attribute>
                  </xsl:otherwise>
              </xsl:choose>
              <xsl:if test="preceding-sibling::tei:head">
                    <xsl:value-of select="preceding-sibling::tei:head"/>
                  <xsl:if test="following-sibling::tei:figDesc">
                      <xsl:text>: </xsl:text>
                  </xsl:if>
              </xsl:if>
             <xsl:value-of select="following-sibling::tei:figDesc"/>
          </div>
        </div>
    </xsl:template>

</xsl:stylesheet>
