<?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">

<!--
 - Determine the filename for a given node.
-->
<xsl:template name="xhtml.file">
	<xsl:choose>
		<xsl:when test="name() = 'preface'">
			<xsl:text>preface</xsl:text>
			<xsl:number/>
			<xsl:text>.html</xsl:text>
		</xsl:when>
		<xsl:when test="not(title)">
			<xsl:value-of select="name()"/><xsl:text>.html</xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="name()"/>
			<xsl:call-template name="util.secprefix"/>
			<xsl:text>.html</xsl:text>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - Print heading for given section marked up for the navigation bar.
-->
<xsl:template name="xhtml.navbar.heading">

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

	<xsl:if test="$secnumdepth >= $curdepth">
		<xsl:variable name="prefix">
			<xsl:call-template name="util.secprefix"/>
		</xsl:variable>
		<xsl:if test="$prefix != ''">
			<b><xsl:call-template name="i18n.print">
				<xsl:with-param name="key" select="'sectionnumber'"/>
				<xsl:with-param name="number" select="$prefix"/>
			</xsl:call-template></b>
			<xsl:text> </xsl:text>
		</xsl:if>
	</xsl:if>
	<xsl:choose>
		<xsl:when test="not(title)">
			<xsl:call-template name="i18n.print">
				<xsl:with-param name="key" select="name()"/>
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<xsl:apply-templates select="title"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - Print link to given section.
-->
<xsl:template name="xhtml.navbar.link">
	<xsl:param name="direction"/>
	<xsl:variable name="filename">
		<xsl:call-template name="xhtml.file"/>
	</xsl:variable>
	<a href="{$filename}" class="navbar">
		<img src="{$direction}.gif" alt="{$direction}" style="border: 0px;"/>
	</a>
</xsl:template>

<!--
 - Prints the link to the next page.
-->
<xsl:template name="xhtml.nextpage">

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

	<!-- if we can go deeper then first look for children -->
	<xsl:choose>
		<xsl:when test="$secsplitdepth > $curdepth">
			<xsl:choose>
				<xsl:when test="
					child::*[
						name() = 'section' or
						name() = 'subsection' or
						name() = 'subsubsection'
					]">
					<xsl:for-each select="
						child::*[
							name() = 'section' or
							name() = 'subsection' or
							name() = 'subsubsection'
						][1]">
						<xsl:choose>
							<xsl:when test="$mode = 'link'">
								<xsl:call-template name="xhtml.navbar.link">
									<xsl:with-param name="direction" select="'next'"/>
								</xsl:call-template>
							</xsl:when>
							<xsl:otherwise>
								<xsl:call-template name="xhtml.navbar.heading">
									<xsl:with-param name="curdepth" select="$curdepth + 1"/>
									<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
								</xsl:call-template>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:for-each>
				</xsl:when>
				<!-- none eligible found, look in ancestor chain for a following sibling -->
				<xsl:otherwise>
					<xsl:call-template name="xhtml.firstfollowing">
						<xsl:with-param name="curdepth" select="$curdepth"/>
						<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
						<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
						<xsl:with-param name="mode" select="$mode"/>
					</xsl:call-template>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:when>
		<!-- recurse back through ancestor chain and look for a following-sibling -->
		<xsl:otherwise>
			<xsl:call-template name="xhtml.firstfollowing">
				<xsl:with-param name="curdepth" select="$curdepth"/>
				<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
				<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
				<xsl:with-param name="mode" select="$mode"/>
			</xsl:call-template>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
 - Helper template called by 'xhtml.nextpage' to find the first
 - following sibling in the chain of ancestors.
-->
<xsl:template name="xhtml.firstfollowing">

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

	<xsl:choose>
		<xsl:when test="following-sibling::*">
			<xsl:for-each select="following-sibling::*[1]">
				<xsl:choose>
					<xsl:when test="$mode = 'link'">
						<xsl:call-template name="xhtml.navbar.link">
							<xsl:with-param name="direction" select="'next'"/>
						</xsl:call-template>
					</xsl:when>
					<xsl:otherwise>
						<xsl:call-template name="xhtml.navbar.heading">
							<xsl:with-param name="curdepth" select="$curdepth"/>
							<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
						</xsl:call-template>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
		</xsl:when>
		<!-- go back to parent's level and try again -->
		<xsl:otherwise>
			<xsl:choose>
				<xsl:when test="name(parent::*) = name(/*[1])">
					<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:for-each select="parent::*">
						<xsl:call-template name="xhtml.firstfollowing">
							<xsl:with-param name="curdepth" select="$curdepth - 1"/>
							<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
							<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
							<xsl:with-param name="mode" select="$mode"/>
						</xsl:call-template>
					</xsl:for-each>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
 - Helper template called by 'xhtml.prevpage' to find the last child
 - of all descendants of a node where the descendants are at the level
 - of chunking.
-->
<xsl:template name="xhtml.lastvisiblechild">

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

	<!-- check if we contain subsections -->
	<xsl:choose>
		<xsl:when test="($secsplitdepth > $curdepth) and
			child::*[
				name() = 'section' or
				name() = 'subsection' or
				name() = 'subsubsection'
			]">
			<!-- recurse -->
			<xsl:for-each select="
				child::*[
					name() = 'section' or
					name() = 'subsection' or
					name() = 'subsubsection'
				][last()]">
				<xsl:call-template name="xhtml.lastvisiblechild">
					<xsl:with-param name="curdepth" select="$curdepth + 1"/>
					<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
					<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
					<xsl:with-param name="mode" select="$mode"/>
				</xsl:call-template>
			</xsl:for-each>
		</xsl:when>
		<!-- cannot go deeper, so it's us -->
		<xsl:otherwise>
			<xsl:choose>
				<xsl:when test="$mode = 'link'">
					<xsl:call-template name="xhtml.navbar.link">
						<xsl:with-param name="direction" select="'prev'"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="xhtml.navbar.heading">
						<xsl:with-param name="curdepth" select="$curdepth"/>
						<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
					</xsl:call-template>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
 - Prints the link to the previous page.
-->
<xsl:template name="xhtml.prevpage">

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

	<!-- look for a preceding section -->
	<xsl:choose>
		<xsl:when test="
			preceding-sibling::*[
				name() = 'preface' or
				name() = 'chapter' or
				name() = 'section' or
				name() = 'subsection' or
				name() = 'subsubsection' or
				name() = 'appendix' or
				name() = 'glossary'
			]">
			<!-- and search the last visible descendant -->
			<xsl:for-each select="
				preceding-sibling::*[
					name() = 'chapter' or
					name() = 'preface' or
					name() = 'section' or
					name() = 'subsection' or
					name() = 'subsubsection' or
					name() = 'appendix' or
					name() = 'glossary'
				][1]">
				<xsl:call-template name="xhtml.lastvisiblechild">
					<xsl:with-param name="curdepth" select="$curdepth"/>
					<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
					<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
					<xsl:with-param name="mode" select="$mode"/>
				</xsl:call-template>
			</xsl:for-each>
		</xsl:when>
		<!-- if there's no preceding sibling, then our parent is the node we're looking for -->
		<xsl:otherwise>
			<xsl:choose>
				<xsl:when test="parent::*[not(name() = name(/*[1]))]">
					<xsl:for-each select="parent::*">
						<xsl:choose>
							<xsl:when test="$mode = 'link'">
								<xsl:call-template name="xhtml.navbar.link">
									<xsl:with-param name="direction" select="'prev'"/>
								</xsl:call-template>
							</xsl:when>
							<xsl:otherwise>
								<xsl:call-template name="xhtml.navbar.heading">
									<xsl:with-param name="curdepth" select="$curdepth - 1"/>
									<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
								</xsl:call-template>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:for-each>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
 - Print the page head and the navigation bar.
-->
<xsl:template name="xhtml.pagehead">

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

	<xsl:call-template name="xhtml.navbar.headings">
		<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
		<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
		<xsl:with-param name="curdepth" select="$curdepth"/>
	</xsl:call-template>
	<xsl:call-template name="xhtml.navbar.links">
		<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
		<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
		<xsl:with-param name="curdepth" select="$curdepth"/>
	</xsl:call-template>
</xsl:template>

<!--
  - Print navigation bar with text links.
-->
<xsl:template name="xhtml.navbar.headings">

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

	<table border="0" cellspacing="0" cellpadding="2" width="100%">
		<tr class="navbar-headings">
			<td align="left" style="width:33%; font-size: x-small">
				<xsl:call-template name="xhtml.prevpage">
					<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
					<xsl:with-param name="curdepth" select="$curdepth"/>
					<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
					<xsl:with-param name="mode" select="'heading'"/>
				</xsl:call-template>
			</td>
			<td align="center" style="width:34%; font-size: x-small">
				<xsl:call-template name="i18n.print">
					<xsl:with-param name="key" select="'contents'"/>
				</xsl:call-template>
			</td>
			<td align="right" style="width:33%; font-size: x-small">
				<xsl:call-template name="xhtml.nextpage">
					<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
					<xsl:with-param name="curdepth" select="$curdepth"/>
					<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
					<xsl:with-param name="mode" select="'heading'"/>
				</xsl:call-template>
			</td>
		</tr>
	</table>	
</xsl:template>

<!--
  - Print navigation bar with arrow images.
-->
<xsl:template name="xhtml.navbar.links">

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

	<table border="0" cellspacing="0" cellpadding="2" width="100%">
		<tr class="navbar-links">
			<td align="left" style="width:33%; font-size: 0px">
				<xsl:call-template name="xhtml.prevpage">
					<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
					<xsl:with-param name="curdepth" select="$curdepth"/>
					<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
					<xsl:with-param name="mode" select="'link'"/>
				</xsl:call-template>			
			</td>
			<td align="center" style="width:34%; font-size: 0px">
				<a href="index.html#{generate-id()}" class="navbar">
					<img src="toc.gif" alt="home" style="border: 0px;"/>
				</a>
			</td>
			<td align="right" style="width:33%; font-size: 0px">
				<xsl:call-template name="xhtml.nextpage">
					<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
					<xsl:with-param name="curdepth" select="$curdepth"/>
					<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
					<xsl:with-param name="mode" select="'link'"/>
				</xsl:call-template>
			</td>
		</tr>
	</table>
</xsl:template>


<!--
 - Print the page foot and navigation bar.
-->
<xsl:template name="xhtml.pagefoot">

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

	<xsl:call-template name="xhtml.navbar.links">
		<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
		<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
		<xsl:with-param name="curdepth" select="$curdepth"/>
	</xsl:call-template>
	<xsl:call-template name="xhtml.navbar.headings">
		<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
		<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
		<xsl:with-param name="curdepth" select="$curdepth"/>
	</xsl:call-template>
</xsl:template>

<!--
  - Print CSS definitions
-->
<xsl:template name="css.definitions">
	<xsl:text>html, body {
		margin: 0;
		padding: 0;
		height: 100%;
		font-size: 100.01%;
		font-family: arial,verdana,sans-serif;
		color: #000000;
}&#x0a;&#x0a;</xsl:text>

	<xsl:text>a { text-decoration: none; }&#x0a;</xsl:text>
	<xsl:text>a:link, a:hover, a:visited { color: #1a1184; }&#x0a;</xsl:text>
	<xsl:text>a.navbar, a.navbar:link, a.navbar:hover, </xsl:text>
	<xsl:text>a.navbar:visited { color: #ffffff; }&#x0a;&#x0a;</xsl:text>

	<xsl:text>h1 { font-size: x-large; clear: both; margin-top: 2ex; margin-bottom: 1ex;}&#x0a;</xsl:text>
	<xsl:text>h2 { font-size: large; clear: both; margin-top: 2ex; margin-bottom: 1ex;}&#x0a;</xsl:text>
	<xsl:text>h3 { font-size: medium; clear: both; margin-top: 2ex; margin-bottom: 1ex;}&#x0a;</xsl:text>
	<xsl:text>h4 { font-size: medium; clear: both; margin-top: 1.5ex; margin-bottom: 1ex;}&#x0a;&#x0a;</xsl:text>
	<xsl:text>span.p { font-weight: bold; padding-right: 1.5ex; }</xsl:text>

	<xsl:text>tr.navbar-headings { color: #000000; }&#x0a;</xsl:text>
	<xsl:text>tr.navbar-links { color: #ffffff; background-color: #5577aa; }&#x0a;&#x0a;</xsl:text>

	<xsl:text>p { margin-top: 1ex; margin-bottom: 1ex;}&#x0a;&#x0a;</xsl:text>

	<xsl:text>td.margin {
		width: 180px;
		background-color: #eeeeee;
		border-left: 1px dashed black;
		vertical-align: top;
}&#x0a;&#x0a;</xsl:text>

	<xsl:text>hr.footnote {
		width: 40%;
		margin-left: 0px;
		color: black;
		background-color: black;
		height: 1px;
		border: 0px;
		text-align: left;
}&#x0a;&#x0a;</xsl:text>

	<xsl:text>sup.footnote {
		font-weight: normal;
		font-style: normal;
		text-decoration: none;
}&#x0a;&#x0a;</xsl:text>

	<xsl:text>div.footnote { font-size: x-small; }&#x0a;&#x0a;</xsl:text>
	
	<xsl:text>pre.listing {
		font-family: "courier new", courier, fixed;
		font-size: small;
		margin: 1px;
}&#x0a;&#x0a;</xsl:text>

	<xsl:text>pre.verbatim {
		font-family: "courier new", courier, fixed;
}&#x0a;&#x0a;</xsl:text>

	<xsl:text>.block-element {
		margin-top: 1ex;
		margin-bottom: 1ex;
}&#x0a;&#x0a;</xsl:text>
	
</xsl:template>

<!--
  - Print CSS style defintions to external file
-->
<xsl:template name="css.stylesheet">
	<xsl:document href="style.css" method="text" indent="no" encoding="UTF-8">
		<xsl:call-template name="css.definitions"/>
	</xsl:document>
</xsl:template>

<!--
  - Print CSS style definitions inline
-->
<xsl:template name="css.inline">
	<style type="text/css">
		<xsl:comment>
			<xsl:call-template name="css.definitions"/>
		</xsl:comment>
	</style>
</xsl:template>

</xsl:stylesheet>
