<?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:param name="type"/>

<xsl:template match="related" mode="head">
	<xsl:apply-templates select="filter" mode="head"/>
	<xsl:apply-imports/>
</xsl:template>

<xsl:template match="related">
	<h5>Filter</h5>
	<ul class="links">
		<xsl:apply-templates select="filter"/>

		<li>
			<xsl:choose>
				<xsl:when test="not($type) or $type=''">
					<xsl:attribute name="id">active</xsl:attribute>
					<xsl:text>All</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<a title="Show all">
						<xsl:attribute name="href">
							<xsl:call-template name="target-handler">
								<xsl:with-param name="target">/photos/</xsl:with-param>
							</xsl:call-template>
						</xsl:attribute>
					
						<xsl:text>All</xsl:text>
					</a>
				</xsl:otherwise>
			</xsl:choose>
		</li>
	</ul>
</xsl:template>

<xsl:template match="related/filter" mode="head">
	<xsl:call-template name="link-generator">
		<xsl:with-param name="rel">bookmark</xsl:with-param>
		<!-- essential, otherwise will be 'collection' -->
		<xsl:with-param name="type">text/html</xsl:with-param>
		<xsl:with-param name="target">
			<xsl:text>/photos/?type=</xsl:text>
			<xsl:value-of select="@type"/>
		</xsl:with-param>
	</xsl:call-template>
</xsl:template>

<xsl:template match="related/filter">
	<li>
		<xsl:choose>
			<xsl:when test="@type=$type">
				<xsl:attribute name="id">active</xsl:attribute>
				<xsl:value-of select="@title"/>
			</xsl:when>
			<xsl:otherwise>
				<a title="Only show {@title}">
					<xsl:attribute name="href">
						<xsl:call-template name="target-handler">
							<xsl:with-param name="target">
								<xsl:text>/photos/?type=</xsl:text>
								<xsl:value-of select="@type"/>
							</xsl:with-param>
						</xsl:call-template>
					</xsl:attribute>
					
					<xsl:value-of select="@title"/>
				</a>
			</xsl:otherwise>
		</xsl:choose>
	</li>
</xsl:template>

<xsl:template match="related/filter" mode="sorted">
<xsl:variable name="filterType" select="@type"/>

	<h3>
		<xsl:value-of select="@title"/>
	</h3>
	<ul class="items">
		<xsl:apply-templates select="/page/content/collection[@type=$filterType]">
			<xsl:sort select="@title"/>
		</xsl:apply-templates>
	</ul>
</xsl:template>

<xsl:template match="content">
	<xsl:choose>
		<xsl:when test="$type='private'"/>
		<xsl:otherwise>
			<xsl:choose>
				<xsl:when test="not($type) or $type=''">
					<xsl:apply-templates select="../related/filter" mode="sorted"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="../related/filter[@type=$type]" mode="sorted"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:otherwise>		
	</xsl:choose>
</xsl:template>

<!-- suppress -->
<xsl:template match="collection[@type='private']"/>

<xsl:template match="collection[not(@type='private')]">
	<li>
		<xsl:if test="position()=1">
			<xsl:attribute name="class">first</xsl:attribute>		
		</xsl:if>

		<xsl:apply-templates select="image"/>

		<dl class="info">
			<dt>
				<a>
					<xsl:attribute name="href">
						<xsl:call-template name="target-handler">
							<xsl:with-param name="target">
								<xsl:text>/photos/</xsl:text>
								<xsl:value-of select="@path"/>
								<xsl:text>/</xsl:text>
							</xsl:with-param>
						</xsl:call-template>
					</xsl:attribute>
	
					<xsl:value-of select="@title"/>
				</a>
			</dt>

			<xsl:apply-templates select="description"/>

			<dd>
				<xsl:text> (</xsl:text>
				<a>
					<xsl:attribute name="href">
						<xsl:call-template name="target-handler">
							<xsl:with-param name="target">
								<xsl:text>/photos/</xsl:text>
								<xsl:value-of select="@path"/>
								<xsl:text>/</xsl:text>
							</xsl:with-param>
						</xsl:call-template>
					</xsl:attribute>

					<xsl:value-of select="@size"/>
					<xsl:text> image</xsl:text>
					<xsl:if test="@size &gt; 1">
						<xsl:text>s</xsl:text>
					</xsl:if>		
				</a>
				<xsl:text>)</xsl:text>
			</dd>
		</dl>
	</li>
</xsl:template>

<xsl:template match="collection/description">
	<dd>
		<xsl:apply-templates/>
	</dd>
</xsl:template>

<xsl:template match="collection/image">
	<xsl:call-template name="image-generator">
		<xsl:with-param name="type">
			<xsl:if test="parent::collection/@type='project'">
				<xsl:text>framed </xsl:text>
			</xsl:if>
			<xsl:text>preview thumb</xsl:text>
		</xsl:with-param>
		<xsl:with-param name="collection" select="parent::collection/@path"/>
		<xsl:with-param name="extension" select="parent::collection/@extension"/>
	</xsl:call-template>
</xsl:template>

</xsl:stylesheet>
