<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"
		doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
		doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"/>

	<xsl:template match="/doc">
		<html lang="en" xml:lang="en">
			<head>
				<title>Dir Listing</title>
				<xsl:element name="link">
					<xsl:attribute name="rel">stylesheet</xsl:attribute>
					<xsl:attribute name="type">text/css</xsl:attribute>
					<xsl:attribute name="href"><xsl:value-of
						select="supportDir"/>/list.css</xsl:attribute>
				</xsl:element>
			</head>
			<body>
				<xsl:call-template name="header"/>
				<xsl:call-template name="buttons"/>
				<xsl:apply-templates select="files"/>
				<xsl:call-template name="footer"/>
				<xsl:apply-templates select="diagnostic"/>
			</body>
		</html>
	</xsl:template>

	<xsl:template match="files">
		<div class="dircontents">
			<xsl:apply-templates/>
		</div>
	</xsl:template>

	<xsl:template match="dir">
		<div>
			<div class="dir">
				<xsl:element name="a">
					<xsl:attribute name="href"><xsl:value-of
						select="@href"/></xsl:attribute>
					<xsl:element name="img">
						<xsl:attribute name="width">15</xsl:attribute>
						<xsl:attribute name="height">13</xsl:attribute>
						<xsl:attribute name="src"><xsl:value-of
							select="/doc/iconDir"/>/folder.gif</xsl:attribute>
					</xsl:element>
					<xsl:text> </xsl:text>
					<xsl:value-of select="@name"/>
					(Last visited: <xsl:value-of select="@lastVisit"/>)
				</xsl:element>
			</div>
			<xsl:apply-templates select="files"/>
		</div>
	</xsl:template>

	<xsl:template match="file">
		<xsl:element name="div">
			<xsl:attribute name="class">
				<xsl:choose>
					<xsl:when test="newer=1">newerItem</xsl:when>
					<xsl:otherwise>file</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:element name="a">
				<xsl:attribute name="target">doc</xsl:attribute>
				<xsl:attribute
					name="href"><xsl:value-of select="href"/></xsl:attribute>
				<xsl:element name="img">
					<xsl:attribute name="width">16</xsl:attribute>
					<xsl:attribute name="height">16</xsl:attribute>
					<xsl:attribute name="src"><xsl:value-of
						select="/doc/iconDir"/>/<xsl:value-of
						select="icon"/>.gif</xsl:attribute>
				</xsl:element>
				<xsl:text> </xsl:text>
				<xsl:value-of select="name"/> (<xsl:value-of select="size"/>)
			</xsl:element>
		</xsl:element>
	</xsl:template>

	<!--
		Function: header
			params: none

		Generate web page header (first section of the body element).
	-->
	<xsl:template name="header">
		<h2>Tree View of <xsl:value-of
			select="/doc/project"/>/<xsl:value-of select="/doc/target"/></h2>
		<xsl:if test="/doc/header">
			<div class="desc">
				<xsl:value-of select="/doc/header"/>
			</div>
		</xsl:if>
	</xsl:template>

	<!--
		Function: footer
			params: none

		Generate web page footer (last section of the body element).
	-->
	<xsl:template name="footer">
		<xsl:if test="/doc/footer">
			<div class="desc">
				<xsl:value-of select="/doc/footer"/>
			</div>
		</xsl:if>
	</xsl:template>

	<xsl:template match="diagnostic">
		<xsl:apply-templates />
	</xsl:template>

	<!--
		Function: buttons
			params: none

		Generate web page buttons (below the header).
	-->
	<xsl:template name="buttons">
		<div style="text-align:right">

			<xsl:call-template name="button">
				<xsl:with-param name="label">LIST</xsl:with-param>
				<xsl:with-param name="active"><xsl:value-of
					select="/doc/listActive" /></xsl:with-param>
				<xsl:with-param name="href"><xsl:value-of
					select="/doc/listHref" /></xsl:with-param>
				<xsl:with-param name="current"><xsl:value-of
					select="/doc/listCurrent" /></xsl:with-param>
				<xsl:with-param name="title"
					>General list format</xsl:with-param>
			</xsl:call-template>

			<xsl:call-template name="button">
				<xsl:with-param name="label">TREE</xsl:with-param>
				<xsl:with-param name="active"><xsl:value-of
					select="/doc/treeActive" /></xsl:with-param>
				<xsl:with-param name="href"><xsl:value-of
					select="/doc/treeHref" /></xsl:with-param>
				<xsl:with-param name="current"><xsl:value-of
					select="/doc/treeCurrent" /></xsl:with-param>
				<xsl:with-param name="title"
					>Displays entire tree from current point</xsl:with-param>
			</xsl:call-template>

			<xsl:call-template name="button">
				<xsl:with-param name="label">ALL</xsl:with-param>
				<xsl:with-param name="active"><xsl:value-of
					select="/doc/listflatActive" /></xsl:with-param>
				<xsl:with-param name="href"><xsl:value-of
					select="/doc/listflatHref" /></xsl:with-param>
				<xsl:with-param name="current"><xsl:value-of
					select="/doc/listflatCurrent" /></xsl:with-param>
				<xsl:with-param name="title"
					>General list with directories as regular files</xsl:with-param>
			</xsl:call-template>

			<xsl:call-template name="button">
				<xsl:with-param name="label">IMAGE</xsl:with-param>
				<xsl:with-param name="active"><xsl:value-of
					select="/doc/imglistActive" /></xsl:with-param>
				<xsl:with-param name="href"><xsl:value-of
					select="/doc/imglistHref" /></xsl:with-param>
				<xsl:with-param name="current"><xsl:value-of
					select="/doc/imglistCurrent" /></xsl:with-param>
				<xsl:with-param name="title"
					>General list with image previews</xsl:with-param>
			</xsl:call-template>

			<!-- Won't work in xml environment
			<a href="javascript:fullscreen()">
				<img alt="Switches file display to the entire window"
					width="70" height="18"
					src="/images/buttons/btn-fullscreen.gif"/></a>
					-->

			<xsl:element name="a">
				<xsl:attribute name="href"><xsl:value-of
					select="/doc/help"/></xsl:attribute>
				<xsl:element name="img">
					<xsl:attribute name="alt"
						>Frequently asked questions</xsl:attribute>
					<xsl:attribute name="width">70</xsl:attribute>
					<xsl:attribute name="height">18</xsl:attribute>
					<xsl:attribute name="src"><xsl:value-of
						select="/doc/buttonDir"
						/>/btn-help.gif</xsl:attribute>
				</xsl:element>
			</xsl:element>

			<xsl:element name="a">
				<xsl:attribute name="href">mailto:<xsl:value-of
					select="/doc/admin"/></xsl:attribute>
				<xsl:element name="img">
					<xsl:attribute name="alt"
						>Email for support</xsl:attribute>
					<xsl:attribute name="width">70</xsl:attribute>
					<xsl:attribute name="height">18</xsl:attribute>
					<xsl:attribute name="src"><xsl:value-of
						select="/doc/buttonDir"
						/>/btn-contact.gif</xsl:attribute>
				</xsl:element>
			</xsl:element>
		</div>
	</xsl:template>

	<!--
		Function: button
			params:
			label; string; label for mode button
			active; boolean; indicates mode is available from this context
			href; string; URL to activate mode
			current; boolean; indicates this is the current mode
			title; string; alt text for mouse-hover

		Generate a single mode button.
	-->
	<xsl:template name="button">
		<xsl:param name="label"/>
		<xsl:param name="active"/>
		<xsl:param name="href"/>
		<xsl:param name="current"/>
		<xsl:param name="title"/>
		<xsl:choose>
		<xsl:when test="$active=1">
			<xsl:element name="a">
				<xsl:attribute name="href"><xsl:value-of
					select="$href"/></xsl:attribute>
				<span class="btnOn" title="{$title}"
					><xsl:value-of select="$label"/></span>
			</xsl:element>
		</xsl:when>
		<xsl:otherwise>
			<xsl:if test="$current=1">
				<span class="btnOff" title="{$title}"
					style="border:1pt solid blue"
					><xsl:value-of select="$label"/></span>
			</xsl:if>
			<xsl:if test="not($current=1)">
				<span class="btnOff" title="{$title}"><xsl:value-of
					select="$label"/></span>
			</xsl:if>
		</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

<!-- reproduce all elements unchanged, including arbitrary nesting -->
<xsl:template match="*">
	<xsl:element name="{name()}">
		<xsl:apply-templates select="@*" />
		<xsl:apply-templates/>
	</xsl:element>
</xsl:template>

<!-- attribute enumerator -->
<xsl:template match="@*">
	<xsl:attribute name="{name()}"><xsl:value-of select="." /></xsl:attribute>
</xsl:template>

<!-- all text - superfluous as this is a default rule -->
<xsl:template match="text()">
	<xsl:value-of select="."/>
</xsl:template>

</xsl:stylesheet>
