<?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="sort">post</xsl:param>

<xsl:template match="related">
	<h5>Sort</h5>
	<ul class="links">
		<li>
			<xsl:choose>
				<xsl:when test="$sort='title'">
					<xsl:attribute name="id">active</xsl:attribute>
					<xsl:text>By Article Name</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<a>
						<xsl:attribute name="href">
							<xsl:call-template name="target-handler">
								<xsl:with-param name="target">/archives/?sort=title</xsl:with-param>
							</xsl:call-template>
						</xsl:attribute>
						<xsl:text>By Article Name</xsl:text>
					</a>
				</xsl:otherwise>
			</xsl:choose>
		</li>
		<li>
			<xsl:choose>
				<xsl:when test="$sort='post'">
					<xsl:attribute name="id">active</xsl:attribute>
					<xsl:text>By Posting Date</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<a>
						<xsl:attribute name="href">
							<xsl:call-template name="target-handler">
								<xsl:with-param name="target">/archives/?sort=post</xsl:with-param>
							</xsl:call-template>
						</xsl:attribute>
						<xsl:text>By Posting Date</xsl:text>
					</a>
				</xsl:otherwise>
			</xsl:choose>
		</li>
	</ul>
</xsl:template>

	<xsl:template match="content">
		<h3>Articles</h3>
		<ul class="items">
			<xsl:choose>
				<xsl:when test="$sort='title'">
					<xsl:apply-templates select="archive">
						<xsl:sort select="@title" order="ascending"/>
					</xsl:apply-templates>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="archive">
						<xsl:sort select="@year" data-type="number" order="descending"/>
						<xsl:sort select="@month" data-type="number" order="descending"/>
						<xsl:sort select="@day" data-type="number" order="descending"/>
					</xsl:apply-templates>
				</xsl:otherwise>
			</xsl:choose>
		</ul>
	</xsl:template>

<xsl:template match="archive">
	<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>/archives/</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>
			
			<dd>
				<dl class="list-posted mention">
					<dt>Posted </dt>
					<dd>
						<xsl:call-template name="date-generator"/>
					</dd>
				</dl>
			</dd>

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

	<xsl:template match="archive/description">
		<dd>
			<xsl:apply-templates/>
		</dd>
		<dd>
			<xsl:text> (</xsl:text>
			<a>
				<xsl:attribute name="href">
					<xsl:call-template name="target-handler">
						<xsl:with-param name="target">
							<xsl:text>/archives/</xsl:text>
							<xsl:value-of select="parent::archive/@path"/>
							<xsl:text>/</xsl:text>
						</xsl:with-param>
					</xsl:call-template>
				</xsl:attribute>
				<xsl:text>Full article</xsl:text>
			</a>
			<xsl:text>)</xsl:text>
		</dd>
	</xsl:template>

<xsl:template match="archive/image">
	<dl class="framed preview thumb image">
		<dt>
			<a title="{@title}">
				<xsl:attribute name="href">
					<xsl:call-template name="target-handler">
						<xsl:with-param name="target">
							<xsl:text>/archives/</xsl:text>
							<xsl:value-of select="parent::archive/@path"/>
							<xsl:text>/</xsl:text>
						</xsl:with-param>
					</xsl:call-template>
				</xsl:attribute>
				<img alt="{@title}">
					<xsl:attribute name="src">
						<xsl:call-template name="target-handler">
							<xsl:with-param name="target" select="@source"/>
						</xsl:call-template>
					</xsl:attribute>
				</img>
			</a>
		</dt>
		<dd>
			<xsl:value-of select="caption"/>
		</dd>
	</dl>
</xsl:template>

</xsl:stylesheet>
