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

<!-- defaults to 1 if nothing specified -->
<xsl:param name="photo">1</xsl:param>
<xsl:param name="max-related">5</xsl:param>
<xsl:param name="totalImages"/>
<xsl:param name="rangeStart"/>
<xsl:param name="rangeEnd"/>
<xsl:param name="rangeSize"/>

<xsl:template match="content">
	<xsl:choose>
		<xsl:when test="not($error) or $error=''">
			<xsl:apply-templates select="collection"/>
		</xsl:when>
		<xsl:otherwise>
			<p>
				<xsl:text>There are only </xsl:text>
				<xsl:value-of select="$totalImages"/>
				<xsl:text> photos in this collection.</xsl:text>
			</p>
			<ul class="links">
				<li>
					<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="collection/@path"/>
									<xsl:text>/</xsl:text>
								</xsl:with-param>
							</xsl:call-template>
						</xsl:attribute>
						<xsl:text>Back to the collection</xsl:text>
					</a>
				</li>
				<li>
					<a>
						<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>View available collections</xsl:text>
					</a>
				</li>
			</ul>
		</xsl:otherwise>
	</xsl:choose>	
</xsl:template>

<xsl:template match="content/collection">
	<xsl:variable name="size" select="number($rangeEnd) - (number($rangeStart) - 1)"/>
	<xsl:variable name="prevRangeEnabled" select="$rangeStart &gt; $rangeSize"/>
	<xsl:variable name="nextRangeEnabled" select="$rangeEnd &lt; $totalImages"/>
	<xsl:variable name="showRanges" select="$prevRangeEnabled or $nextRangeEnabled"/>
	<xsl:variable name="prevEnabled" select="$prevRangeEnabled or ($size &gt; 1 and $photo &gt; 1)"/>
	<xsl:variable name="nextEnabled" select="$nextRangeEnabled or ($size &gt; 1 and $photo &lt; $totalImages)"/>

	<ol id="photoNavigation" class="sequence">
		<xsl:if test="$showRanges">
			<li class="arrow" id="prevRange">
				<a>
					<xsl:attribute name="id">first</xsl:attribute>

					<xsl:attribute name="class">
						<!-- grey out when could be visible -->
						<xsl:if test="not($prevRangeEnabled)">disabled </xsl:if>
						<xsl:text>button</xsl:text>
					</xsl:attribute>

					<xsl:if test="$prevRangeEnabled">
						<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>/?photo=</xsl:text>
									<xsl:value-of select="number($rangeStart - 1)"/>
								</xsl:with-param>
							</xsl:call-template>
						</xsl:attribute>
						<xsl:attribute name="title">
							<xsl:text>previous set (</xsl:text>
							<xsl:value-of select="number($rangeStart - $rangeSize)"/>
							<xsl:text>-</xsl:text>
							<xsl:value-of select="number($rangeStart - 1)"/>
							<xsl:text> of </xsl:text>
							<xsl:value-of select="$totalImages"/>
							<xsl:text>)</xsl:text>
						</xsl:attribute>
					</xsl:if>

					<xsl:text>&lt;&lt;</xsl:text>
				</a>
			</li>
		</xsl:if>

		<li class="arrow" id="previous">			
			<a>
				<xsl:attribute name="class">
					<xsl:if test="not($prevEnabled)">
						<xsl:text>disabled </xsl:text>
					</xsl:if>
					<xsl:text>button</xsl:text>
				</xsl:attribute>

				<xsl:if test="not($showRanges)">
					<xsl:attribute name="id">first</xsl:attribute>
				</xsl:if>

				<xsl:if test="$prevEnabled">
					<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>/?photo=</xsl:text>
								<xsl:value-of select="number($photo - 1)"/>
							</xsl:with-param>
						</xsl:call-template>
					</xsl:attribute>

					<xsl:attribute name="title">
						<xsl:variable name="prevImageTitle" select="image[position() = $photo - 1]/@title"/>
						
						<xsl:text>previous </xsl:text>
						<xsl:choose>
							<xsl:when test="$prevImageTitle and not($prevImageTitle='')">
								<xsl:text>(</xsl:text>
								<xsl:value-of select="$prevImageTitle"/>
								<xsl:text>)</xsl:text>
							</xsl:when>
							<xsl:otherwise>image</xsl:otherwise>
						</xsl:choose>
					</xsl:attribute>
				</xsl:if>	
				<xsl:text>&lt;</xsl:text>
			</a>
		</li>

		<xsl:apply-templates select="image[position() &gt;= $rangeStart and position() &lt;= $rangeEnd]"/>

		<li class="arrow" id="next">
			<a>
				<xsl:attribute name="class">
					<xsl:if test="not($nextEnabled)">
						<xsl:text>disabled </xsl:text>
					</xsl:if>
					<xsl:text>button</xsl:text>
				</xsl:attribute>

				<xsl:if test="not($showRanges)">
					<xsl:attribute name="id">last</xsl:attribute>
				</xsl:if>

				<xsl:if test="$nextEnabled">
					<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>/?photo=</xsl:text>
								<xsl:value-of select="number($photo + 1)"/>
							</xsl:with-param>
						</xsl:call-template>
					</xsl:attribute>

					<xsl:attribute name="title">
						<xsl:variable name="nextImageTitle" select="image[position() = $photo + 1]/@title"/>
						
						<xsl:text>next </xsl:text>
						<xsl:choose>
							<xsl:when test="$nextImageTitle and not($nextImageTitle='')">
								<xsl:text>(</xsl:text>
								<xsl:value-of select="$nextImageTitle"/>
								<xsl:text>)</xsl:text>
							</xsl:when>
							<xsl:otherwise>image</xsl:otherwise>
						</xsl:choose>
					</xsl:attribute>
				</xsl:if>

				<xsl:text>&gt;</xsl:text>
			</a>
		</li>

		<xsl:if test="$showRanges">
			<li class="arrow" id="nextRange">
				<a>
					<xsl:attribute name="id">last</xsl:attribute>

					<xsl:attribute name="class">
						<!-- grey out when could be visible -->
						<xsl:if test="not($nextRangeEnabled)">disabled </xsl:if>
						<xsl:text>button</xsl:text>
					</xsl:attribute>

					<xsl:if test="$nextRangeEnabled">
						<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>/?photo=</xsl:text>
									<xsl:value-of select="number($rangeEnd + 1)"/>
								</xsl:with-param>
							</xsl:call-template>
						</xsl:attribute>
						<xsl:attribute name="title">
							<xsl:text>next set (</xsl:text>
							<xsl:value-of select="number($rangeEnd + 1)"/>
							<xsl:text>-</xsl:text>
							<xsl:choose>
								<xsl:when test="($rangeEnd + $rangeSize) &gt; $totalImages">
									<xsl:value-of select="$totalImages"/>
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="$rangeEnd + $rangeSize"/>					
								</xsl:otherwise>
							</xsl:choose>
							<xsl:text> of </xsl:text>
							<xsl:value-of select="$totalImages"/>
							<xsl:text>)</xsl:text>
						</xsl:attribute>
					</xsl:if>
	
					<xsl:text>&gt;&gt;</xsl:text>
				</a>
			</li>
		</xsl:if>

	</ol>
	
	<xsl:apply-templates select="image[position()=$photo]" mode="feature"/>
</xsl:template>

<xsl:template match="collection/image">
<xsl:variable name="number" select="count(preceding-sibling::image) + 1"/>

	<li>
		<a>
			<xsl:choose>
				<xsl:when test="$number=$photo">
					<xsl:attribute name="id">selected</xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<xsl:attribute name="href">
						<xsl:call-template name="target-handler">
							<xsl:with-param name="target">
								<xsl:text>/photos/</xsl:text>
								<xsl:value-of select="parent::collection/@path"/>
								<xsl:text>/?photo=</xsl:text>
								<xsl:value-of select="$number"/>
							</xsl:with-param>
						</xsl:call-template>
					</xsl:attribute>
				</xsl:otherwise>
			</xsl:choose>
			
			<xsl:attribute name="class">
				<xsl:if test="not($number=$photo)">normal </xsl:if>
				<xsl:text>button</xsl:text>
			</xsl:attribute>

			<xsl:attribute name="title">
				<xsl:choose>
					<xsl:when test="not(@title) or @title=''">
						<xsl:text>Image </xsl:text>
						<xsl:value-of select="$number"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="@title"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:value-of select="$number"/>
		</a>
	</li>
</xsl:template>

<xsl:template match="image" mode="feature">
	<xsl:variable name="enlarge-target">
		<xsl:text>setActiveStyleSheet(&apos;</xsl:text>
		<xsl:value-of select="//related/alternate[contains(@target, 'full-image')]/@title"/>
		<xsl:text>&apos;);</xsl:text>
	</xsl:variable>
	<xsl:variable name="enlarge-title">Toggle full-size image</xsl:variable>
	
	<dl id="feature">
		<dt id="photoTitle">
			<a href="javascript://" onclick="{$enlarge-target}" title="{$enlarge-title}">
				<xsl:choose>
					<xsl:when test="@title and not(@title='')">
						<xsl:value-of select="@title"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:attribute name="class">hidden</xsl:attribute>
						<span>Untitled</span>
					</xsl:otherwise>
				</xsl:choose>
			</a>
		</dt>
		<dd>
			<xsl:call-template name="image-generator">
				<xsl:with-param name="type">
					<xsl:text>full</xsl:text>
					<xsl:if test="parent::collection/@type='project'">
						<xsl:text> framed</xsl:text>
					</xsl:if>
				</xsl:with-param>
				<xsl:with-param name="collection" select="parent::collection/@path"/>
				<xsl:with-param name="number" select="$photo"/>
				<xsl:with-param name="text" select="@title"/>
				<xsl:with-param name="link">javascript://</xsl:with-param>
				<xsl:with-param name="linkTitle" select="$enlarge-title"/>
				<xsl:with-param name="onclick" select="$enlarge-target"/>
				<xsl:with-param name="extension" select="parent::collection/@extension"/>
			</xsl:call-template>
		</dd>
	</dl>
</xsl:template>

<xsl:template match="related">
	<xsl:if test="collection and (not($error) or $error='')">
		<xsl:if test="collection[@type='collection']">
			<h5>
				<a>
					<xsl:attribute name="href">
						<xsl:call-template name="target-handler">
							<xsl:with-param name="target">/photos/?type=collection</xsl:with-param>
						</xsl:call-template>
					</xsl:attribute>

					<xsl:text>Collections</xsl:text>
				</a>
			</h5>
			<ul class="links">
				<xsl:apply-templates select="collection[@type='collection']">
					<xsl:sort select="@title"/>
				</xsl:apply-templates>
			</ul>
		</xsl:if>
		<xsl:if test="collection[@type='project']">
			<h5>
				<a>
					<xsl:attribute name="href">
						<xsl:call-template name="target-handler">
							<xsl:with-param name="target">/photos/?type=project</xsl:with-param>
						</xsl:call-template>
					</xsl:attribute>

					<xsl:text>Project Screen Shots</xsl:text>
				</a>
			</h5>
			<ul class="links">
				<xsl:apply-templates select="collection[@type='project']">
					<xsl:sort select="@title"/>
				</xsl:apply-templates>
			</ul>
		</xsl:if>
	</xsl:if>	
</xsl:template>

<xsl:template match="related" mode="head">
	<xsl:apply-templates select="section" mode="head">
		<xsl:sort select="@type"/>
		<xsl:sort select="@title"/>
	</xsl:apply-templates>
	<xsl:apply-templates select="alternate|index|contents|first|prev|next|last|up|bookmark|version|chapter|subsection|appendix" mode="head"/>
</xsl:template>

<!-- suppress private -->
<xsl:template match="related/section" mode="head"/>

<xsl:template match="related/section[not(@type='private')]" mode="head">
	<!-- section elements have collection type embedded -->
	<xsl:call-template name="link-generator">
		<xsl:with-param name="type">text/html</xsl:with-param>
	</xsl:call-template>
</xsl:template>

<xsl:template match="first|prev|next|last" mode="head">
	<xsl:if test="not($error) or $error=''">
		<xsl:apply-imports/>
	</xsl:if>
</xsl:template>

<xsl:template match="related/collection">
<xsl:variable name="collType" select="@type"/>
<xsl:variable name="number" select="count(preceding-sibling::collection[@type=$collType])"/>
	<li>
	<xsl:choose>
		<xsl:when test="$number &lt; $max-related">
			<xsl:choose>
				<xsl:when test="@path=/page/content/collection/@path">
					<xsl:attribute name="id">active</xsl:attribute>
					<xsl:value-of select="@title"/>
				</xsl:when>
				<xsl:otherwise>
					<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>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:when>
		<xsl:when test="$number=$max-related">
			<a>
				<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="$collType"/>
						</xsl:with-param>
					</xsl:call-template>
				</xsl:attribute>
				<xsl:text>More (</xsl:text>
				<xsl:value-of select="count(following-sibling::collection[@type=$collType]) + 1"/>
				<xsl:text>)</xsl:text>
			</a>
		</xsl:when>
		<xsl:otherwise/>
	</xsl:choose>
	</li>
</xsl:template>

</xsl:stylesheet>

