<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:eat="http://msqr.us/xsd/ieat"
	xmlns:xweb="http://msqr.us/xsd/jaxb-web"
	xmlns:date="http://exslt.org/dates-and-times"
	exclude-result-prefixes="eat xweb">
	
    <!-- imports -->
	<xsl:import href="tmpl/default-layout.xsl"/>
	<xsl:import href="tmpl/display-recipe.xsl"/>
	
	<!-- layout variables -->
	<xsl:variable name="layout.global.nav.page" select="'vew-recipe'"/>
	
	<!-- helper vars -->
	<xsl:variable name="index-section" 
			select="$aux/eat:ui/eat:search-results/eat:ui-index/eat:index-section[@selected = 'true']"/>
	<xsl:variable name="recipe-criteria" select="$aux/eat:ui/eat:search-results/eat:recipe-criteria"/>
	<xsl:variable name="recipe" select="$aux/eat:ui/eat:recipe"/>
	<xsl:variable name="comments" select="$recipe/eat:comment"/>
	
	<!-- create key of users for easy lookup -->
	<xsl:key name="comment-users" match="xweb:x-data/xweb:x-auxillary/eat:ui/eat:search-results/eat:user" use="@userId"/>

	<xsl:template match="xweb:x-data" mode="page-title">
		<xsl:value-of select="$recipe/eat:name"/>
		<xsl:text> (</xsl:text>
		<xsl:value-of select="key('i18n','comments')"/>
		<xsl:text>)</xsl:text>
	</xsl:template>
	
	<xsl:template match="eat:recipe" mode="return-nav">
		<a class="back">
			<xsl:attribute name="href">
				<xsl:value-of select="$web-context"/>
				<xsl:text>/recipe.do?recipeId=</xsl:text>
				<xsl:value-of select="@recipeId"/>
				<xsl:apply-templates select="$index-section" mode="href"/>
				<xsl:apply-templates select="$recipe-criteria" mode="href"/>
			</xsl:attribute>
			<xsl:value-of select="key('i18n','back.to')"/>
			<xsl:text> </xsl:text>
			<xsl:value-of select="eat:name"/>
		</a>
	</xsl:template>
    
	<xsl:template match="xweb:x-data" mode="page-head-content">
		<script type="text/javascript" src="{$web-context}/js/jquery-1.3.2.js" xml:space="preserve"> </script>
		<script type="text/javascript" src="{$web-context}/js/jquery.ajaxQueue.js" xml:space="preserve"> </script>
		<script type="text/javascript" src="{$web-context}/js/xweb-locale.js" xml:space="preserve"> </script>
		<script type="text/javascript" src="{$web-context}/js/ieat-util.js" xml:space="preserve"> </script>
		<script type="text/javascript" xml:space="preserve">
			<xsl:comment>
			function init() {
				installTextAreaFocusHandler();
				document.forms[0].elements['comment'].focus();
			}
			
			window.onload = init;
			//</xsl:comment>
		</script>
	</xsl:template>
	
	<xsl:template match="xweb:x-data" mode="page-body">
		<div class="return-nav">
			<xsl:apply-templates select="$recipe" mode="return-nav"/>
		</div>
		
		<xsl:if test="$acting-user">
	        <div class="intro">
				<xsl:value-of select="key('i18n','recipe-comments.intro')"/>
			</div>
			
			<form method="post" action="{$web-context}{$ctx/xweb:path}">
				<table class="form">
					<tr>
						<th>
							<xsl:value-of select="key('i18n','recipe.comment.displayName')"/>
						</th>
						<td>
							<textarea name="comment" class="small">
								<xsl:value-of select="$req/eat:param[@key='comment']"/>
								<!-- the following space prevents browser rendering mishaps -->
								<xsl:text> </xsl:text>
							</textarea>
						</td>
					</tr>
					<tr>
						<th/>
						<td>
							<input type="submit" value="{key('i18n','save.displayName')}"/>
						</td>
					</tr>
				</table>
				<input type="hidden" name="recipeId" value="{$recipe/@recipeId}"/>
				<!-- handle contextual navigation -->
				<xsl:apply-templates select="$aux/eat:ui/eat:search-results" 
					mode="contextual-navigation"/>
			</form>
			
			<hr />
		</xsl:if>
		
		<xsl:choose>
			<xsl:when test="$comments">
				<ol>
					<xsl:apply-templates select="$comments"/>
				</ol>
			</xsl:when>
			<xsl:otherwise>
				<p>
					<xsl:value-of select="key('i18n','recipe-comments.none')"/>
				</p>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="eat:comment">
		<li>
			<div class="comment-body">
				<xsl:value-of select="."/>
			</div>
			<div>
				<xsl:attribute name="class">
					<xsl:choose>
						<xsl:when test="@userId = $acting-user/@userId">
							<xsl:text>comment-credits-self</xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>comment-credits</xsl:text>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:attribute>
				<xsl:value-of select="key('comment-users',@userId)/@login"/>
				<xsl:text> </xsl:text>
				<xsl:value-of select="key('i18n','on')"/>
				<xsl:text> </xsl:text>
				<xsl:value-of select="date:format-date(string(@createdDate),'d MMM yyyy')"/>
			</div>
		</li>
	</xsl:template>

</xsl:stylesheet>