<?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="work-common.xsl"/>

<xsl:template match="content" mode="head">
	<xsl:apply-imports mode="head"/>
	<xsl:apply-templates select="project/script" mode="head"/>	
</xsl:template>

<xsl:template match="content/project">
	<xsl:variable name="link" select="not(@screenshots) or @screenshots='yes'"/>
	<xsl:variable name="download" select="resources/appendix[@type='application/x-gzip']"/>
	<xsl:variable name="previewPath">
		<xsl:call-template name="target-handler">
			<xsl:with-param name="target">
				<xsl:text>/work/</xsl:text>
				<xsl:value-of select="@path"/>
				<xsl:text>/preview.gif</xsl:text>
			</xsl:with-param>
		</xsl:call-template>
	</xsl:variable>

	<xsl:if test="not(@preview) or @preview='yes'">
		<xsl:variable name="screenShots">
			<xsl:if test="$link">
				<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:if>
		</xsl:variable>
		
		<xsl:variable name="archive">
			<xsl:if test="$download">
				<xsl:call-template name="target-handler">
					<xsl:with-param name="target">
						<xsl:value-of select="resources/appendix[@type='application/x-gzip']/@target"/>
					</xsl:with-param>
				</xsl:call-template>
			</xsl:if>
		</xsl:variable>

		<dl>
			<xsl:attribute name="class">
				<xsl:if test="$link or $download">preview </xsl:if>
				<xsl:text>framed image thumb</xsl:text>
			</xsl:attribute>
			<dt>
				<xsl:choose>
					<xsl:when test="$link">
						<a title="View Screen Shots" href="{$screenShots}">
							<img alt="{@name} screen shot preview" src="{$previewPath}"/>
						</a>
					</xsl:when>
					<xsl:when test="$download">
						<a title="Download {@name}" href="{$archive}">
							<img alt="{@name} image" src="{$previewPath}"/>
						</a>
					</xsl:when>
					<xsl:otherwise>
						<img alt="{@name} image" src="{$previewPath}"/>
					</xsl:otherwise>
				</xsl:choose>
			</dt>
			<xsl:choose>
				<xsl:when test="/page/content/project/preview">
					<dd>
						<xsl:apply-templates select="/page/content/project/preview" mode="image"/>
						<xsl:if test="$download">
							<br />
							<a title="Download {@name}" href="{$archive}">Download</a>
						</xsl:if>
					</dd>
				</xsl:when>
				<xsl:when test="$link">
					<dd>
						<a href="{$screenShots}">View Screen Shots</a>
					</dd>
				</xsl:when>
				<xsl:otherwise/>
			</xsl:choose>			
		</dl>
	</xsl:if>

	<xsl:if test="@year">
		<dl class="mention" id="completion">
			<dt>Completed </dt>
			<dd>
				<xsl:call-template name="date-generator"/>
			</dd>
		</dl>
	</xsl:if>

	<xsl:apply-templates/>
</xsl:template>

<xsl:template match="project/script"/>

<xsl:template match="project/script" mode="head">
	<xsl:call-template name="script-generator"/>
</xsl:template>

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

<xsl:template match="project/description/need">
	<h3 class="project">Need</h3>
	<xsl:apply-templates/>
</xsl:template>

<xsl:template match="project/description/solution">
	<h3 class="project">Solution</h3>
	<xsl:apply-templates/>
</xsl:template>

<xsl:template match="project/preview"/>

<xsl:template match="project/preview" mode="image">
	<xsl:apply-templates/>
</xsl:template>

<xsl:template match="contribution|language">
	<dl class="mention" id="{name()}">
		<dt>
			<xsl:call-template name="capitalizor">
				<xsl:with-param name="text" select="name()"/>
			</xsl:call-template>
			<xsl:if test="count(child::*) &gt; 1 or count(child::*/item) &gt; 1">
				<xsl:text>s</xsl:text>
			</xsl:if>
			<xsl:text>: </xsl:text>
		</dt>
		<xsl:apply-templates/>
	</dl>
</xsl:template>

<xsl:template match="contribution/*">
	<dd>
		<xsl:for-each select="item">
			<xsl:call-template name="item-handler"/>
		</xsl:for-each>
		<xsl:text> </xsl:text>
		<xsl:value-of select="name()"/>
		<xsl:text>. </xsl:text>
	</dd>
</xsl:template>

<xsl:template match="contribution/item">
	<xsl:call-template name="item-handler"/>
</xsl:template>

<xsl:template match="language/item">
	<dd>
		<xsl:call-template name="item-handler"/>
	</dd>
</xsl:template>

<xsl:template match="example">
	<dd>
		<xsl:apply-templates select="paragraph[1]"/>
		<form action="">
			<div>
				<xsl:apply-templates select="field"/>
			</div>
		</form>
		<xsl:apply-templates select="paragraph[@id]"/>
	</dd>
</xsl:template>

<xsl:template match="example/paragraph[1]">
	<p>
		<strong>Example: </strong>
		<xsl:apply-templates/>
	</p>
</xsl:template>

<xsl:template match="example/field">
	<input type="text" name="{@title}" onkeyup="{@onkeyup}" maxlength="{@maxlen}" value="{@value}"/>
</xsl:template>

<xsl:template match="example/paragraph[@id]">
	<p id="{@id}" class="results">
		<xsl:apply-templates/>
	</p>
</xsl:template>

<xsl:template name="item-handler">
	<xsl:if test="preceding-sibling::item and not(following-sibling::item)">
		<xsl:text> and </xsl:text>
	</xsl:if>
	<xsl:apply-templates/>
	<xsl:if test="(not(preceding-sibling::item) and count(following-sibling::item) &gt; 1)
		or (preceding-sibling::item and following-sibling::item)">
		<xsl:text>, </xsl:text>
	</xsl:if>
</xsl:template>

</xsl:stylesheet>
