<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xhtml="http://www.w3.org/1999/xhtml"
	xmlns="http://www.w3.org/1999/xhtml"
	exclude-result-prefixes="xhtml"
>

	<xsl:output method="xml" encoding="ISO-8859-1" indent="no"
		doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" 
		doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
		/>
		
	<!-- Parameters -->

	<xsl:param name="base" select="''"/>
	<xsl:param name="css" select="''"/>
	<xsl:param name="email" select="''"/>
	
	<!-- Head -->

	<xsl:template match="xhtml:head">
		<xsl:copy>
			<xsl:for-each select="@*">
				<xsl:copy-of select="."/>
			</xsl:for-each>
			
			<xsl:if test="$css">
				<link href="{concat($base,$css)}" type="text/css" rel="stylesheet"/>
			</xsl:if>
			
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

	<!-- Body -->

	<xsl:template match="xhtml:body">
		<xsl:copy>
			<xsl:for-each select="@*">
				<xsl:copy-of select="."/>
			</xsl:for-each>
			
			<xsl:variable name="title" select="normalize-space(//xhtml:title)"/>
			<xsl:if test="$title">
				<div id="header"><xsl:value-of select="$title"/></div>
			</xsl:if>

			<xsl:variable name="sitemap" select="document(concat($base,'sitemap.xml'), /*)"/>
			<xsl:variable name="pageid" select="/xhtml:html/@id"/>
			<xsl:if test="$pageid">
				<div class="menubar">
					<xsl:apply-templates select="$sitemap" mode="menubar">
						<xsl:with-param name="pageid" select="$pageid"/>
					</xsl:apply-templates>
				</div>
				<div class="sidebar">
					<div>Site navigation</div>
					<xsl:apply-templates select="$sitemap" mode="sidebar">
						<xsl:with-param name="pageid" select="$pageid"/>
					</xsl:apply-templates>
				</div>
			</xsl:if>

			<xsl:if test=".//xhtml:h1">
				<div id="toc" class="sidebar">
					<div>Page navigation</div>
					<xsl:apply-templates select=".//xhtml:h1" mode="toc"/>
				</div>
			</xsl:if>

			<div id="body">
				<xsl:apply-templates/>
			</div>

			<div id="footer"><xsl:value-of select="$email"/></div>

			<script src='http://www.google-analytics.com/ga.js' type='text/javascript'/>
			<script type="text/javascript">
			<![CDATA[
				var pageTracker = _gat._getTracker("UA-3224074-3");
				pageTracker._initData();
				pageTracker._trackPageview();
			]]>
			</script>
		</xsl:copy>
	</xsl:template>

	<xsl:template match="xhtml:h1//xhtml:a">
		<xsl:choose>
			<xsl:when test="@*[name()!='id' and name()!='name']">
				<xsl:copy>
					<xsl:for-each select="@*[name()!='id' and name()!='name']">
						<xsl:copy-of select="."/>
					</xsl:for-each>

					<xsl:apply-templates/>
				</xsl:copy>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="xhtml:h1">
		<xsl:copy>
			<xsl:for-each select="@*">
				<xsl:copy-of select="."/>
			</xsl:for-each>

			<xsl:if test="not (@id)">
				<xsl:attribute name="id">
					<xsl:call-template name="anchor" select="."/>
				</xsl:attribute> 
			</xsl:if>
			
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

	<xsl:template match="xhtml:*">
		<xsl:copy>
			<xsl:for-each select="@*">
				<xsl:copy-of select="."/>
			</xsl:for-each>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

	<!-- Menu navigation bar -->

	<xsl:template match="sitemap" mode="menubar">
		<xsl:param name="pageid" select="@id"/>
		
		<xsl:variable name="page" select=".//page[@id=$pageid]"/>
		<xsl:variable name="pagehref" select="$page/@href"/>

		<xsl:apply-templates mode="menubar" select="$page/ancestor-or-self::page">
			<xsl:with-param name="pageid" select="$pageid"/>
			<xsl:with-param name="pagehref" select="$pagehref"/>
		</xsl:apply-templates>
	</xsl:template>

	<xsl:template match="page" mode="menubar">
		<xsl:param name="pageid" select="@id"/>
		<xsl:param name="pagehref" select="@href"/>

		<xsl:variable name="href">
			<xsl:call-template name="relative-path">
				<xsl:with-param name="src" select="$pagehref"/>
				<xsl:with-param name="dst" select="@href"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:choose>
			<xsl:when test="@id=$pageid">
				<span class="highlight">
					<xsl:value-of select="normalize-space(@title)"/>
				</span>
			</xsl:when>
			<xsl:otherwise>
				<a href="{normalize-space($href)}">
					<xsl:value-of select="normalize-space(@title)"/>
				</a>
				<xsl:text>&gt;</xsl:text>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!-- Side navigation bar -->

	<xsl:template match="sitemap" mode="sidebar">
		<xsl:param name="pageid" select="@id"/>
		
		<xsl:variable name="page" select=".//page[@id=$pageid]"/>
		<xsl:variable name="pagehref" select="$page/@href"/>

		<xsl:choose>
			<xsl:when test="$page/page">
				<xsl:apply-templates select="$page/page" mode="sidebar">
					<xsl:with-param name="pageid" select="$pageid"/>
					<xsl:with-param name="pagehref" select="$pagehref"/>
				</xsl:apply-templates>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="$page/../page" mode="sidebar">
					<xsl:with-param name="pageid" select="$pageid"/>
					<xsl:with-param name="pagehref" select="$pagehref"/>
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="page" mode="sidebar">
		<xsl:param name="pageid" select="@id"/>
		<xsl:param name="pagehref" select="@href"/>

		<xsl:choose>
			<xsl:when test="@id=$pageid">
				<span class="this">
					<!--<xsl:text>raquo </xsl:text>-->
					<xsl:value-of select="normalize-space(@title)"/>
				</span>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="href">
					<xsl:call-template name="relative-path">
						<xsl:with-param name="src" select="$pagehref"/>
						<xsl:with-param name="dst" select="@href"/>
					</xsl:call-template>
				</xsl:variable>
				<a href="{normalize-space($href)}">
					<!--<xsl:text>rsaquo </xsl:text>-->
					<xsl:value-of select="normalize-space(@title)"/>
				</a>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<!-- Table of contents -->
	
	<xsl:template name="anchor">
		<xsl:choose>
			<xsl:when test="@id">
				<xsl:value-of select="@id"/>
			</xsl:when>
			<xsl:when test=".//xhtml:a/@id">
				<xsl:value-of select=".//xhtml:a/@id"/>
			</xsl:when>
			<xsl:when test=".//xhtml:a/@name">
				<xsl:value-of select=".//xhtml:a/@name"/>
			</xsl:when>
			<xsl:when test=".//xhtml:a/@name">
				<xsl:value-of select=".//xhtml:a/@name"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="generate-id(.)"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="xhtml:h1" mode="toc">
		<a>
			<xsl:attribute name="href">
				<xsl:text>#</xsl:text>
				<xsl:call-template name="anchor" select="."/>
			</xsl:attribute>
			<xsl:value-of select="normalize-space(.//text())"/>
		</a>
	</xsl:template>

	<xsl:template match="text()" mode="toc"/>

	<!-- File path manipulation -->

	<xsl:template name="revert-path">
		<xsl:param name="path" select="''"/>
		<xsl:if test="contains($path,'/')">
			<xsl:text>../</xsl:text>
			<xsl:call-template name="revert-path">
				<xsl:with-param name="path" select="substring-after($path,'/')"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
	
	<xsl:template name="relative-path">
		<xsl:param name="src" select="''"/>
		<xsl:param name="dst" select="''"/>
		<xsl:choose>
			<xsl:when test="contains($src,'/') and contains($dst,'/') and substring-before($src,'/')=substring-before($dst,'/')">
				<xsl:call-template name="relative-path">
					<xsl:with-param name="src" select="substring-after($src,'/')"/>
					<xsl:with-param name="dst" select="substring-after($dst,'/')"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="revert-path">
					<xsl:with-param name="path" select="$src"/>
				</xsl:call-template>
				<xsl:value-of select="$dst"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
</xsl:stylesheet>
