<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:import href="main.xsl"/>

<xsl:template match="related" mode="head">
	<xsl:apply-templates select="up|contents|alternate" mode="head"/>
	<xsl:apply-templates select="project[@type='project']" mode="head">
		<xsl:sort select="@year" data-type="number" order="descending"/>
		<xsl:sort select="@month" data-type="number" order="descending"/>
		<xsl:sort select="@name"/>
	</xsl:apply-templates>
	<xsl:apply-templates select="section" mode="head">
		<xsl:sort select="@position" data-type="number"/>
	</xsl:apply-templates>	
</xsl:template>

<xsl:template match="related/section" mode="head">
<xsl:comment>processing section: <xsl:value-of select="@title"/></xsl:comment>
	<xsl:call-template name="link-generator">
		<xsl:with-param name="title">
			<xsl:call-template name="capitalizor">
				<xsl:with-param name="text" select="@title"/>
			</xsl:call-template>
		</xsl:with-param>
		<xsl:with-param name="target">
			<xsl:text>/profile/</xsl:text>
			<xsl:value-of select="@title"/>
			<xsl:text>/</xsl:text>
		</xsl:with-param>
	</xsl:call-template>
</xsl:template>

<xsl:template match="related/project" mode="head">
	<xsl:call-template name="link-generator">
		<xsl:with-param name="rel">appendix</xsl:with-param>
		<xsl:with-param name="title" select="@name"/>
		<xsl:with-param name="target">
			<xsl:text>/work/</xsl:text>
			<xsl:value-of select="@path"/>
			<xsl:text>/</xsl:text>
		</xsl:with-param>
	</xsl:call-template>
</xsl:template>

<xsl:template match="*[profile]">
	<xsl:choose>
		<xsl:when test="profile">
			<xsl:if test="self::related">
				<h5>
					<a>
						<xsl:attribute name="href">
							<xsl:call-template name="target-handler">
								<xsl:with-param name="target">/profile/</xsl:with-param>
							</xsl:call-template>
						</xsl:attribute>

						<xsl:text>Sections</xsl:text>
					</a>
				</h5>
			</xsl:if>
			<dl id="sections" class="links">
				<xsl:apply-templates select="profile">
					<xsl:sort select="@position" data-type="number"/>
				</xsl:apply-templates>
			</dl>
		</xsl:when>
		<xsl:otherwise>
			<xsl:apply-imports/>
		</xsl:otherwise>	
	</xsl:choose>
</xsl:template>

<xsl:template match="profile">
	<xsl:variable name="name">
		<xsl:call-template name="capitalizor">
			<xsl:with-param name="text" select="@title"/>
		</xsl:call-template>
	</xsl:variable>

	<dt>		
		<xsl:choose>
			<xsl:when test="ancestor::related and @title = /page/content/section/@name">
				<xsl:attribute name="id">active</xsl:attribute>
				<xsl:value-of select="$name"/>
			</xsl:when>
			<xsl:otherwise>
				<a>
					<xsl:attribute name="href">
						<xsl:call-template name="target-handler">
							<xsl:with-param name="target">
								<xsl:text>/profile/</xsl:text>
								<xsl:value-of select="@title"/>
								<xsl:text>/</xsl:text>
							</xsl:with-param>
						</xsl:call-template>
					</xsl:attribute>

					<xsl:value-of select="$name"/>
				</a>
			</xsl:otherwise>
		</xsl:choose>
	</dt>
	<dd>
		<xsl:choose>
			<xsl:when test="ancestor::content">
				<xsl:apply-templates mode="toc"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates/>
			</xsl:otherwise>
		</xsl:choose>
	</dd>
</xsl:template>

<!-- suppress these normally -->
<xsl:template match="content//blurb"/>

<xsl:template match="content//blurb" mode="toc">
	<xsl:apply-templates/>
</xsl:template>

<xsl:template match="interest">
	<xsl:apply-templates/>
</xsl:template>

<xsl:template match="content/section/section/title|interest/name">
	<h3>
		<xsl:apply-templates/>
	</h3>
</xsl:template>

<xsl:template match="content/section/section/section/title">
	<h4>
		<xsl:apply-templates/>
	</h4>
</xsl:template>

<xsl:template match="project-list">
	<ul class="links">
		<xsl:apply-templates select="/page/related/project[@type='project']">
			<xsl:sort select="@year" data-type="number" order="descending"/>
			<xsl:sort select="@month" data-type="number" order="descending"/>
			<xsl:sort select="@name"/>
		</xsl:apply-templates>
	</ul>
</xsl:template>

<xsl:template match="/page/related/project">
	<li>
		<a>
			<xsl:attribute name="href">
				<xsl:call-template name="target-handler">
					<xsl:with-param name="target">
						<xsl:text>/work/</xsl:text>
						<xsl:value-of select="@path"/>
						<xsl:text>/</xsl:text>
					</xsl:with-param>
				</xsl:call-template>
			</xsl:attribute>

			<xsl:value-of select="@name"/>
		</a>
	</li>
</xsl:template>

<xsl:template match="position|degree|award|language|event">
	<xsl:apply-templates select="title" mode="heading"/>	
	<dl class="{name()}">
		<xsl:apply-templates/>
	</dl>
</xsl:template>

<xsl:template match="position/title|degree/title|award/title|language/title|event/title"/>

<xsl:template match="position/title|degree/title|award/title|language/title|event/title" mode="heading">
	<h4>
		<xsl:apply-templates/>
	</h4>
</xsl:template>

<xsl:template match="position/*|degree/*|award/*|language/*|event/*">
	<dt class="label">
		<xsl:call-template name="capitalizor">
			<xsl:with-param name="text" select="name()"/>
		</xsl:call-template>
	</dt>
	<dd class="{name()}">
		<xsl:choose>
			<xsl:when test="@web">
				<a href="http://{@web}">
					<xsl:apply-templates/>
				</a>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates/>
			</xsl:otherwise>
		</xsl:choose>
	</dd>
</xsl:template>

</xsl:stylesheet>
