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

<!-- variables -->

<xsl:variable name="domain">
	<xsl:text>coffeeblack</xsl:text>
</xsl:variable>

<xsl:variable name="site">
	<xsl:value-of select="$domain"/>
	<xsl:text>.org</xsl:text>
</xsl:variable>

<xsl:variable name="root"/>

<!--
<xsl:variable name="root">
	<xsl:text>/prototype/</xsl:text>
	<xsl:value-of select="$site"/>
</xsl:variable>
-->
<xsl:variable name="site-sections">
	<xsl:text>home|profile|work|photos|about|contact</xsl:text>
</xsl:variable>

<xsl:param name="themes">
	<xsl:text>dark|medium|light</xsl:text>
</xsl:param>

<xsl:variable name="separator"> :: </xsl:variable>

<xsl:template name="title-generator">
<xsl:param name="markup" select="true()"/>

	<xsl:if test="/page/@section and not(/page/@section='home')">
		<xsl:choose>
			<xsl:when test="$markup and not(/page/content/*[@name]) and $messageTitle=''">
				<span id="local">
					<xsl:call-template name="capitalizor">
						<xsl:with-param name="text" select="/page/@section"/>
					</xsl:call-template>
				</span>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="capitalizor">
					<xsl:with-param name="text" select="/page/@section"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:if>	
	<xsl:for-each select="/page/content/*[@name]">
		<xsl:if test="/page/@section and not(/page/@section='home' and position()=1)">
			<xsl:choose>
				<xsl:when test="$markup">
					<span class="separator">
						<xsl:value-of select="$separator"/>
					</span>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$separator"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
		
		<xsl:choose>
			<xsl:when test="$markup and position()=last() and $messageTitle=''">
				<span id="local">
					<xsl:call-template name="capitalizor">
						<xsl:with-param name="text" select="@name"/>
					</xsl:call-template>
				</span>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="capitalizor">
					<xsl:with-param name="text" select="@name"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:for-each>

	<xsl:if test="not($messageTitle='')">
		<xsl:choose>
			<xsl:when test="$markup">
				<span class="separator">
					<xsl:value-of select="$separator"/>
				</span>
				<span id="local">
					<xsl:value-of select="$messageTitle"/>
				</span>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$separator"/>
				<xsl:value-of select="$messageTitle"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:if>
</xsl:template>

<xsl:template name="link-generator">
<xsl:param name="rel" select="name()"/>
<xsl:param name="target" select="@target"/>
<xsl:param name="title" select="@title"/>
<xsl:param name="type" select="@type"/>
<xsl:param name="media" select="@media"/>

	<link rel="{$rel}">
		<xsl:attribute name="rel">
			<xsl:value-of select="$rel"/>
			<xsl:if test="$rel='alternate' and $type='text/css'">
				<xsl:text> stylesheet</xsl:text>
			</xsl:if>
		</xsl:attribute>

		<xsl:attribute name="href">
			<xsl:call-template name="target-handler">
				<xsl:with-param name="target" select="$target"/>
			</xsl:call-template>
		</xsl:attribute>

		<xsl:if test="$title and not($title='')">
			<xsl:attribute name="title">
				<xsl:value-of select="$title"/>
			</xsl:attribute>
		</xsl:if>
		
		<xsl:if test="not($type='null')">
			<xsl:attribute name="type">
				<xsl:choose>
					<xsl:when test="not($type) or $type=''">text/html</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$type"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
		</xsl:if>
		
		<xsl:if test="$media">
			<xsl:attribute name="media">
				<xsl:choose>
					<xsl:when test="not($media) or $media=''">all</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$media"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
		</xsl:if>
	</link>
</xsl:template>

<xsl:template name="theme-generator">
<xsl:param name="theme-list" select="$themes"/>
<xsl:param name="mode">link</xsl:param>

	<xsl:if test="not($theme-list='')">
		<xsl:variable name="current-theme">
			<xsl:choose>
				<xsl:when test="contains($theme-list, '|')">
					<xsl:value-of select="substring-before($theme-list, '|')"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$theme-list"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:variable name="current-theme-title">
			<xsl:call-template name="capitalizor">
				<xsl:with-param name="text" select="$current-theme"/>				
			</xsl:call-template>
		</xsl:variable>

		<xsl:choose>
			<xsl:when test="$mode='sidebar'">
				<li>
					<a id="{$current-theme}" href="javascript://">
						<xsl:attribute name="onclick">
							<xsl:text>setActiveStyleSheet(&apos;</xsl:text>
							<xsl:value-of select="$current-theme-title"/>
							<xsl:text>&apos;);</xsl:text>
						</xsl:attribute>
						<xsl:value-of select="$current-theme-title"/>
					</a>
				</li>
			</xsl:when>
			<xsl:when test="$mode='link'">
				<xsl:call-template name="link-generator">
					<xsl:with-param name="rel">
						<xsl:if test="not($current-theme=$theme)">
							<xsl:text>alternate </xsl:text>
						</xsl:if>
						<xsl:text>stylesheet</xsl:text>
					</xsl:with-param>
					<xsl:with-param name="title" select="$current-theme-title"/>
					<xsl:with-param name="type">text/css</xsl:with-param>
					<xsl:with-param name="media">screen</xsl:with-param>
					<xsl:with-param name="target">
						<xsl:text>/styles/themes/</xsl:text>
						<xsl:value-of select="$current-theme"/>
						<xsl:text>/</xsl:text>
						<!--<xsl:choose>
							<xsl:when test="@section='archives'">home</xsl:when>
							<xsl:otherwise>-->
								<xsl:value-of select="@section"/>
							<!--</xsl:otherwise>
						</xsl:choose>-->
						<xsl:text>.css</xsl:text>
					</xsl:with-param>
				</xsl:call-template>
			</xsl:when>
		</xsl:choose>

		<!-- call self recursively with remainder of list, if any -->
		<xsl:if test="contains($theme-list, '|')">
			<xsl:call-template name="theme-generator">
				<xsl:with-param name="theme-list" select="substring-after($theme-list, '|')"/>
				<xsl:with-param name="mode" select="$mode"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:if>
</xsl:template>

<xsl:template name="navigation-generator">
<xsl:param name="section-list" select="$site-sections"/>
<xsl:param name="mode">list</xsl:param>

	<xsl:if test="not($section-list='')">
		<xsl:variable name="page-section" select="/page/@section"/>
		<xsl:variable name="current-section">
			<xsl:choose>
				<xsl:when test="contains($section-list, '|')">
					<xsl:value-of select="substring-before($section-list, '|')"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$section-list"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:choose>
			<xsl:when test="$mode='list'">
				<li id="{$current-section}">
					<a>
						<xsl:attribute name="href">
							<xsl:call-template name="target-handler">
								<xsl:with-param name="target">
									<xsl:text>/</xsl:text>
									<xsl:if test="not($current-section='home')">
										<xsl:value-of select="$current-section"/>
										<xsl:text>/</xsl:text>
									</xsl:if>
								</xsl:with-param>
							</xsl:call-template>
						</xsl:attribute>

						<xsl:if test="$current-section=$page-section or ($page-section='archives' and $current-section='home')">
							<xsl:attribute name="id">current</xsl:attribute>
						</xsl:if>
						<xsl:value-of select="$current-section"/>
					</a>
				</li>
			</xsl:when>
			<xsl:when test="$mode='chapters'">
				<xsl:call-template name="link-generator">
					<xsl:with-param name="title">
						<xsl:call-template name="capitalizor">
							<xsl:with-param name="text" select="$current-section"/>
						</xsl:call-template>
					</xsl:with-param>
					<xsl:with-param name="target">
						<xsl:text>/</xsl:text>
						<xsl:if test="not($current-section='home')">
							<xsl:value-of select="$current-section"/>
							<xsl:text>/</xsl:text>
						</xsl:if>
					</xsl:with-param>
					<xsl:with-param name="rel">chapter</xsl:with-param>
					<xsl:with-param name="type">text/html</xsl:with-param>					
				</xsl:call-template>
			</xsl:when>
		</xsl:choose>

		<!-- call self recursively with remainder of list, if any -->
		<xsl:if test="contains($section-list, '|')">
			<xsl:call-template name="navigation-generator">
				<xsl:with-param name="section-list" select="substring-after($section-list, '|')"/>
				<xsl:with-param name="mode" select="$mode"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:if>
</xsl:template>

<xsl:template match="script" name="script-generator">
<xsl:param name="src" select="@src"/>
<xsl:param name="code"/>
<xsl:param name="type">text/javascript</xsl:param>

	<xsl:if test="not($src='' and $code='')">
		<script type="{$type}">

			<xsl:choose>
				<xsl:when test="not($src='')">
					<xsl:attribute name="src">
						<xsl:call-template name="target-handler">
							<xsl:with-param name="target" select="$src"/>
						</xsl:call-template>
					</xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<xsl:comment>
						<xsl:text> &lt;![CDATA[
						</xsl:text>
						<xsl:value-of select="$code"/>
						<xsl:text>
						// ]]&gt; </xsl:text>
					</xsl:comment>
				</xsl:otherwise>
			</xsl:choose>

		</script>
	</xsl:if>
</xsl:template>

<xsl:template name="image-generator">
<xsl:param name="type">full</xsl:param>
<xsl:param name="collection" select="@collection"/>
<xsl:param name="number" select="@number"/>
<xsl:param name="text" select="@title"/>
<xsl:param name="source" select="@source"/>
<xsl:param name="extension">jpg</xsl:param>
<xsl:param name="caption"/>
<xsl:param name="link"/>
<xsl:param name="linkTitle"/>
<xsl:param name="onclick"/>

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

	<dl class="{$type} image">		
		<dt>
			<xsl:choose>
				<xsl:when test="contains($type, 'preview')">
					<a title="{$title}">
						<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"/>
									<xsl:text>/?photo=</xsl:text>
									<xsl:value-of select="$number"/>
								</xsl:with-param>
							</xsl:call-template>
						</xsl:attribute>

						<xsl:call-template name="img-markup">
							<xsl:with-param name="title" select="$title"/>
							<xsl:with-param name="collection" select="$collection"/>
							<xsl:with-param name="source" select="$source"/>
							<xsl:with-param name="number" select="$number"/>
							<xsl:with-param name="extension" select="$extension"/>
						</xsl:call-template>
					</a>
				</xsl:when>
				<xsl:otherwise>
					<xsl:choose>
						<xsl:when test="$link and not($link='')">
							<a>
								<xsl:attribute name="href">
									<xsl:call-template name="target-handler">
										<xsl:with-param name="target" select="$link"/>
									</xsl:call-template>
								</xsl:attribute>

								<xsl:if test="$onclick and not($onclick='')">
									<xsl:attribute name="onclick">
										<xsl:value-of disable-output-escaping="yes" select="$onclick"/>
									</xsl:attribute>
								</xsl:if>

								<xsl:if test="$linkTitle and not($linkTitle='')">
									<xsl:attribute name="title">
										<xsl:value-of select="$linkTitle"/>
									</xsl:attribute>
								</xsl:if>

								<xsl:call-template name="img-markup">
									<xsl:with-param name="title" select="$title"/>
									<xsl:with-param name="collection" select="$collection"/>
									<xsl:with-param name="source" select="$source"/>
									<xsl:with-param name="number" select="$number"/>
									<xsl:with-param name="extension" select="$extension"/>
								</xsl:call-template>
							</a>	
						</xsl:when>
						<xsl:otherwise>
							<xsl:call-template name="img-markup">
								<xsl:with-param name="title" select="$title"/>
								<xsl:with-param name="collection" select="$collection"/>
								<xsl:with-param name="source" select="$source"/>
								<xsl:with-param name="number" select="$number"/>
								<xsl:with-param name="extension" select="$extension"/>
							</xsl:call-template>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:otherwise>
			</xsl:choose>
		</dt>

		<xsl:choose>
			<xsl:when test="not($caption) or $caption=''">
				<xsl:apply-templates/>
			</xsl:when>
			<xsl:otherwise>
				<dd>
					<xsl:value-of select="$caption"/>
				</dd>
			</xsl:otherwise>
		</xsl:choose>
	</dl>
</xsl:template>

<xsl:template name="img-markup">
<xsl:param name="source"/>
<xsl:param name="title"/>
<xsl:param name="collection"/>
<xsl:param name="number"/>
<xsl:param name="extension"/>

	<img alt="{$title}">
		<xsl:attribute name="src">
			<xsl:choose>
				<xsl:when test="not($source) or $source=''">
					<xsl:call-template name="target-handler">
						<xsl:with-param name="target">
							<xsl:text>/photos/</xsl:text>
							<xsl:value-of select="$collection"/>
							<xsl:text>/</xsl:text>
							<xsl:value-of select="$number"/>
							<xsl:text>.</xsl:text>
							<xsl:value-of select="$extension"/>
						</xsl:with-param>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$source"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:attribute>
	</img>
</xsl:template>

<xsl:template name="capitalizor">
	<xsl:param name="text"/>

	<xsl:if test="not($text='')">
		<xsl:value-of select="translate(substring($text, 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
		<xsl:value-of select="substring($text, 2)"/>
	</xsl:if>

</xsl:template>

<xsl:template name="target-handler">
<xsl:param name="target"/>

	<xsl:if test="$target and not($target='')">
		<xsl:if test="not(contains($target, ':') or starts-with($target, '#') or starts-with($target, '?') or starts-with($target, '.'))">
			<xsl:value-of select="$root"/>
		</xsl:if>
		<xsl:value-of disable-output-escaping="yes" select="$target"/>
		<xsl:if test="starts-with($target, '?') and not($queryString='')">
			<xsl:text>&amp;</xsl:text>
			<xsl:value-of select="$queryString"/>
		</xsl:if>			
	</xsl:if>
</xsl:template>

	<xsl:template name="date-generator">
		<xsl:param name="year" select="@year"/>
		<xsl:param name="month" select="@month"/>
		<xsl:param name="day" select="@day"/>
	
		<xsl:choose>
			<xsl:when test="@month='1'">January</xsl:when>
			<xsl:when test="@month='2'">February</xsl:when>
			<xsl:when test="@month='3'">March</xsl:when>
			<xsl:when test="@month='4'">April</xsl:when>
			<xsl:when test="@month='5'">May</xsl:when>
			<xsl:when test="@month='6'">June</xsl:when>
			<xsl:when test="@month='7'">July</xsl:when>
			<xsl:when test="@month='8'">August</xsl:when>
			<xsl:when test="@month='9'">September</xsl:when>
			<xsl:when test="@month='10'">October</xsl:when>
			<xsl:when test="@month='11'">November</xsl:when>
			<xsl:when test="@month='12'">December</xsl:when>
		</xsl:choose>
		<xsl:if test="@day">
			<xsl:text> </xsl:text>
			<xsl:value-of select="@day"/>
			<xsl:text>,</xsl:text>
		</xsl:if>
		<xsl:text> </xsl:text>
		<xsl:value-of select="@year"/>
	</xsl:template>

</xsl:stylesheet>
