<?xml version="1.0" encoding="UTF-8"?>
<!--
 - Desc:    This file is part of the eCromedos document preparation system
 - Date:    2006/03/09
 - Author:  Tobias Koch (tkoch@ecromedos.net)
 - License: GNU General Public License, version 2
 - URL:     http://www.ecromedos.net
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!--
  - Puts an identifier superscript number in place of the footnote.
-->
<xsl:template match="footnote">
	<xsl:call-template name="footnote.mark">
		<xsl:with-param name="footnote" select="."/>
	</xsl:call-template>
</xsl:template>

<!--
  - Invoked from 'footnote.text'.
-->
<xsl:template match="footnote" mode="settext">
	<xsl:apply-templates/>
</xsl:template>

<!--
  - Leave a mark for a footnote.
--> 
<xsl:template name="footnote.mark">
	<xsl:param name="footnote"/>
	<xsl:choose>
		<!-- are we a sectioning node? -->
		<xsl:when test="
			name() = 'preface' or
			name() = 'chapter' or
			name() = 'appendix' or
			name() = 'section' or
			name() = 'subsection' or
			name() = 'subsubsection' or
			name() = 'biblio'">

			<xsl:for-each select="
				child::*[not(
					name() = 'section' or
					name() = 'subsection' or
					name() = 'subsubsection'
				)]//footnote">
				<xsl:if test="generate-id(self::*) = generate-id($footnote)">
					<sup class="footnote"><a href="#{generate-id()}"><xsl:value-of select="position()"/></a></sup>
				</xsl:if>
			</xsl:for-each>
		</xsl:when>
		<!-- no, then try our parent -->
		<xsl:otherwise>
			<xsl:for-each select="parent::*">
				<xsl:call-template name="footnote.mark">
					<xsl:with-param name="footnote" select="$footnote"/>
				</xsl:call-template>
			</xsl:for-each>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - Prints a section's footnotes between two horz. rules.
-->
<xsl:template name="footnote.text">
	<xsl:if test="
		child::*[not(
			name() = 'section' or
			name() = 'subsection' or
			name() = 'subsubsection'
		)]//footnote">
		<hr class="footnote"/>
		<xsl:for-each select="
			child::*[not(
				name() = 'section' or
				name() = 'subsection' or
				name() = 'subsubsection'
			)]//footnote">
			<div class="footnote">
				<sup><a id="{generate-id()}" name="{generate-id()}"><xsl:value-of select="position()"/></a></sup>
				<xsl:text> </xsl:text>
				<xsl:apply-templates select="." mode="settext"/>
			</div>
		</xsl:for-each>
		<hr class="footnote"/>
	</xsl:if>
</xsl:template>

<!--
  - Places a link in the page that serves as a reference point when
  - adjusting the actual marginal with javascript.
-->
<xsl:template match="marginal">
	<a id="a:{generate-id()}" name="a:{generate-id()}"></a>
</xsl:template>

<!--
  - Invoked from 'marginal.text'.
-->
<xsl:template match="marginal" mode="settext">
	<xsl:apply-templates/>
</xsl:template>


<!--
  - Inserts the javascript that aligns marginals along the vertical
  - position of the corresponding anchor in the document.
-->
<xsl:template name="marginal.script">

	<xsl:param name="curdepth"/>
	<xsl:param name="secsplitdepth"/>

	<script type="text/javascript">
		<xsl:comment>
			<xsl:variable name="num_marginals">
				<xsl:choose>
					<xsl:when test="$secsplitdepth > $curdepth">
						<xsl:value-of select="
							count(child::*[not(
								name() = 'section' or
								name() = 'subsection' or
								name() = 'subsubsection'
							)]//marginal)"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="count(.//marginal)"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>
			var mnotes = new Array(<xsl:value-of select="$num_marginals"/>);	
			<xsl:choose>
				<xsl:when test="$secsplitdepth > $curdepth">
					<xsl:for-each select="
						child::*[not(
							name() = 'section' or
							name() = 'subsection' or
							name() = 'subsubsection'
						)]//marginal">
						mnotes[<xsl:value-of select="position() - 1"/>] = "<xsl:value-of select="generate-id()"/>";
					</xsl:for-each>
				</xsl:when>
				<xsl:otherwise test="$secsplitdepth > $curdepth">
					<xsl:for-each select=".//marginal">
						mnotes[<xsl:value-of select="position() - 1"/>] = "<xsl:value-of select="generate-id()"/>";
					</xsl:for-each>
				</xsl:otherwise>
			</xsl:choose>

			function update_margin() {

				var i, id, top, obj, anchor, div;

				for(i = 0; i &lt; <xsl:value-of select="$num_marginals"/>; i++) {

					id = mnotes[i];
					obj = document.getElementById("a:" + id);
					div = document.getElementById("div:" + id);
					top = 0;

					while(obj.offsetParent) {
						top += obj.offsetTop;
						obj = obj.offsetParent;
					}

					div.style.top = top + "px";
				}
			}
			
			window.onload = update_margin;
			window.onresize = update_margin;
		</xsl:comment>
	</script>
</xsl:template>

<!--
  - Place the marginals in the side column.
-->
<xsl:template name="marginal.text">
	<xsl:choose>
		<xsl:when test="$secsplitdepth > $curdepth">
			<xsl:for-each select="
				child::*[not(
					name() = 'section' or
					name() = 'subsection' or
					name() = 'subsubsection'
				)]//marginal">
				<div id="div:{generate-id()}" style="position: absolute; margin-left: 15px; width: 150px;">
					<xsl:apply-templates mode="settext" select="."/>
				</div>
			</xsl:for-each>		
		</xsl:when>
		<xsl:otherwise>
			<xsl:for-each select=".//marginal">
				<div id="div:{generate-id()}" style="position: absolute; margin-left: 15px; width: 150px;">
					<xsl:apply-templates mode="settext" select="."/>
				</div>
			</xsl:for-each>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>
