<xsl:stylesheet
  xmlns:tei="http://www.tei-c.org/ns/1.0"
  xmlns:fotex="http://www.tug.org/fotex"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
  xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <xsl:import href="http://www.tei-c.org/Stylesheets/P4/fo/tei.xsl"/>
    
    <xsl:param name="bodyMarginBottom">24pt</xsl:param>
    <xsl:param name="bodyMarginTop">36pt</xsl:param>

    <xsl:param name="numberHeadings" select="''"/>
    <xsl:param name="divRunningheads" select="1"/>
    <xsl:param name="docedSize">18pt</xsl:param>
    <xsl:param name="bodyMaster">14</xsl:param>
    <xsl:param name="runSize">
      <xsl:value-of select="$bodyMaster * 0.8"/><xsl:text>pt</xsl:text>
    </xsl:param>    
    <xsl:param name="bodySize">
      <xsl:value-of select="$bodyMaster"/><xsl:text>pt</xsl:text>
    </xsl:param>
    <xsl:param name="smallSize">
      <xsl:value-of select="$bodyMaster * 0.9"/><xsl:text>pt</xsl:text>
    </xsl:param>

<!-- ======================================================= -->
<!-- twosided region-before page-numbers off  -->

<xsl:template name="headers-footers-twoside">
  <xsl:param name="runodd"/>
  <xsl:param name="runeven"/>
        <fo:static-content flow-name="xsl-region-before-right">
          <fo:block  text-align-last="justify" font-size="{$bodySize}">
          <xsl:choose>	
	   <xsl:when test="$runodd">
             <xsl:value-of select="$runodd"/>
           </xsl:when>
	   <xsl:when test="$sectionHeaders">
            <fo:block text-align-last="justify" 
                border-bottom="0.5pt solid black">
              <xsl:if test="$divRunningheads">
               <fo:inline>
                 <fo:retrieve-marker retrieve-class-name="section0"/>
               </fo:inline>
              </xsl:if>
              <fo:leader leader-pattern="space"/>             
              <fo:inline>
               <fo:page-number/>
              </fo:inline>
            </fo:block>
           </xsl:when>
           <xsl:otherwise>
            <xsl:call-template name="runninghead-title"/>
           </xsl:otherwise>
          </xsl:choose>
          </fo:block>
        </fo:static-content>

        <fo:static-content flow-name="xsl-region-before-left">
          <fo:block text-align-last="justify" font-size="{$bodySize}">
          <xsl:choose>	
	   <xsl:when test="$runeven">
             <xsl:value-of select="$runeven"/>
           </xsl:when>
	   <xsl:when test="$sectionHeaders">
            <fo:block text-align-last="justify" 
                border-bottom="0.5pt solid black">
              <fo:inline>
               <fo:page-number/>
              </fo:inline>
              <fo:leader leader-pattern="space"/>
              <xsl:if test="$divRunningheads">
                <xsl:value-of select="//teiHeader//title"/>
              </xsl:if>
            </fo:block>
           </xsl:when>
           <xsl:otherwise>
            <xsl:call-template name="runninghead-author"/>
           </xsl:otherwise>
          </xsl:choose>
          </fo:block>
        </fo:static-content>

        <fo:static-content flow-name="xsl-region-before-first">
          <fo:block/>
        </fo:static-content>

         <fo:static-content flow-name="xsl-region-after-right">
          <fo:block/>
         </fo:static-content>

        <fo:static-content flow-name="xsl-region-after-left">
          <fo:block/>
        </fo:static-content>

        <fo:static-content flow-name="xsl-region-after-first">
          <fo:block/>
        </fo:static-content>
</xsl:template>


<xsl:template match="div">
 <xsl:choose>

  <xsl:when test="@rend='newdoublepage'">
   <fo:block break-before="odd-page">
    <xsl:variable name="divlevel" select="count(ancestor::div)"/>
    <xsl:call-template name="NumberedHeading">
     <xsl:with-param name="level" select="$divlevel"/>
    </xsl:call-template>
    <xsl:apply-templates/>
   </fo:block>
  </xsl:when>

  <xsl:when test="@type='bibliog'">
    <xsl:apply-templates/>
  </xsl:when>

  <xsl:when test="@type='abstract'">
   <fo:block keep-with-next.within-page="always"
	end-indent="{$exampleMargin}"
	start-indent="{$exampleMargin}">
    <xsl:attribute name="text-align">center</xsl:attribute>
    <xsl:call-template name="setupDiv2"/>
    <fo:inline font-style="italic">Abstract</fo:inline>
   </fo:block>
   <xsl:apply-templates/>
  </xsl:when>
	
 <xsl:when test="@type='ack'">
  <fo:block keep-with-next.within-page="always">
    <xsl:attribute name="text-align">start</xsl:attribute>
    <xsl:call-template name="setupDiv3"/>
    <fo:inline font-style="italic">Acknowledgements</fo:inline>
  </fo:block>
   <xsl:apply-templates/>
 </xsl:when>
	
 <xsl:otherwise>
<!-- behaviour depends on the nesting level of <div> elements -->
  <xsl:variable name="divlevel" select="count(ancestor::div)"/>
  <fo:block>
    <xsl:if test="$divlevel=0">
      <xsl:attribute name="break-before">page</xsl:attribute>
    </xsl:if>
    <xsl:call-template name="NumberedHeading">
      <xsl:with-param name="level" select="$divlevel"/>
    </xsl:call-template>
    <xsl:apply-templates/>
  </fo:block>
 </xsl:otherwise>

 </xsl:choose>
</xsl:template>

<!-- =========================================================== -->
<!-- title page doctitle  -->

</xsl:stylesheet>
