<?xml version="1.0"?>
<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"
	exclude-result-prefixes="eat xweb">
	
	<xsl:import href="global-variables.xsl"/>
	<xsl:import href="util.xsl"/>
	
    <xsl:output method="html" omit-xml-declaration="yes"/>

	<xsl:template name="error-intro">
		<xsl:param name="errors-node"/>
		<xsl:if test="$errors-node/xweb:error">
			<div class="error-intro">
				<xsl:if test="$errors-node/xweb:error[not(@field)]">
					<!--<xsl:value-of select="$messages[@key='global.error.intro']"/>
					<xsl:text> </xsl:text>-->
					<xsl:apply-templates select="$errors-node/xweb:error[not(@field)]"/>
				</xsl:if>
				<xsl:if test="$errors-node/xweb:error[@field]">
					<xsl:value-of select="key('i18n','field.error.intro')"/>
					<ul>
						<xsl:for-each select="$errors-node/xweb:error[@field]">
							<li><xsl:value-of select="."/></li>
						</xsl:for-each>
					</ul>
				</xsl:if>
			</div>
		</xsl:if>
	</xsl:template>
	
	<!--
		Global Nav template
		
		Global vars:
			acting-user: the logged in user, if logged in
			web-context: the web context
	 -->
	<xsl:template name="global-nav">
		<xsl:param name="page"/>
		<div class="nav-banner">
			<xsl:if test="$acting-user or key('appenv','feature.anonymous') = 'true'">
				<xsl:choose>
					<xsl:when test="$page = 'home'">
						<xsl:value-of select="key('i18n','link.home')"/>
					</xsl:when>
					<xsl:otherwise>
						<a href="{$web-context}/home.do" title="{key('i18n','link.home.title')}">
							<xsl:value-of select="key('i18n','link.home')"/>
						</a>
					</xsl:otherwise>
				</xsl:choose>
				<xsl:text> - </xsl:text>
				<xsl:choose>
					<xsl:when test="$page = 'index'">
						<xsl:value-of select="key('i18n','link.recipe.index')"/>
					</xsl:when>
					<xsl:otherwise>
						<a href="{$web-context}/index.do" title="{key('i18n','link.recipe.index.title')}">
							<xsl:value-of select="key('i18n','link.recipe.index')"/>
						</a>
					</xsl:otherwise>
				</xsl:choose>
				<xsl:text> - </xsl:text>
				<xsl:choose>
					<xsl:when test="$page = 'search'">
						<xsl:value-of select="key('i18n','link.recipe.search')"/>
					</xsl:when>
					<xsl:otherwise>
						<a href="{$web-context}/search.do" title="{key('i18n','link.recipe.search.title')}">
							<xsl:value-of select="key('i18n','link.recipe.search')"/>
						</a>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:if>
			<xsl:if test="$acting-user">
				<xsl:text> - </xsl:text>
				<xsl:choose>
					<xsl:when test="$page = 'add-recipe'">
						<xsl:value-of select="key('i18n','link.recipe.add')"/>
					</xsl:when>
					<xsl:otherwise>
						<a href="{$web-context}/addRecipe.do" title="{key('i18n','link.recipe.add.title')}">
							<xsl:value-of select="key('i18n','link.recipe.add')"/>
						</a>
					</xsl:otherwise>
				</xsl:choose>
				<xsl:text> - </xsl:text>
				<xsl:choose>
					<xsl:when test="$page = 'meals'">
						<xsl:value-of select="key('i18n','link.meals')"/>
					</xsl:when>
					<xsl:otherwise>
						<a href="{$web-context}/meals.do" title="{key('i18n','link.meals.title')}">
							<xsl:value-of select="key('i18n','link.meals')"/>
						</a>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:if>
			<xsl:if test="$acting-user or key('appenv','feature.anonymous') = 'true'">
				<xsl:text> - </xsl:text>
			</xsl:if>
			<xsl:choose>
				<xsl:when test="$acting-user">
					<xsl:if test="$acting-user/../@admin = 'true'">
						<xsl:choose>
							<xsl:when test="$page = 'admin-home'">
								<xsl:value-of select="key('i18n','link.admin')"/>
							</xsl:when>
							<xsl:otherwise>
								<a href="{$web-context}/admin.do" title="{key('i18n','link.admin.title')}">
									<xsl:value-of select="key('i18n','link.admin')"/>
								</a>
							</xsl:otherwise>
						</xsl:choose>
						<xsl:text> - </xsl:text>
					</xsl:if>
					<a href="{$web-context}/logoff.do" title="{key('i18n','link.logout.title')}">
						<xsl:value-of select="key('i18n','link.logout')"/>
					</a>
					<xsl:text> (</xsl:text>
						<xsl:value-of select="$acting-user/eat:name"/>
					<xsl:text>)</xsl:text>
				</xsl:when>
				<xsl:when test="$page = 'logon'">
					<xsl:value-of select="key('i18n','link.logon')"/>
				</xsl:when>
				<xsl:otherwise>
					<a href="{$web-context}/logon.do" title="{key('i18n','link.logon.title')}">
						<xsl:value-of select="key('i18n','link.logon')"/>
					</a>
				</xsl:otherwise>
			</xsl:choose>
		</div>
	</xsl:template>

	<!-- 
		Search results hidden form element templates.
	-->
	<xsl:template match="eat:search-results" mode="search-results-contextual-navigation">
		<xsl:if test="@pageSize &gt; 0">
			<input type="hidden" name="searchResults.pageSize" value="{@pageSize}"/>
		</xsl:if>
		<xsl:if test="@pageStart &gt; 0">
			<input type="hidden" name="searchResults.pageStart" value="{@pageStart}"/>
		</xsl:if>
		<xsl:apply-templates select="eat:ui-index/eat:index-section[@selected='true']"
			 mode="search-results-contextual-navigation"/>
		<xsl:apply-templates select="eat:recipe-criteria" 
			mode="search-results-contextual-navigation"/>
	</xsl:template>
	<xsl:template match="eat:ui-index/eat:index-section" mode="search-results-contextual-navigation">
		<input type="hidden" name="searchResults.uiIndex.indexSection[{position() - 1}].indexKey" 
				value="{@index-key}"/>
	</xsl:template>
	<xsl:template match="eat:recipe-criteria" mode="search-results-contextual-navigation">
		<input type="hidden" name="searchResults.criteria.mode" value="{@mode}"/>
		<xsl:if test="eat:simpleQuery">
			<input type="hidden" name="searchResults.criteria.simpleQuery" 
				value="{eat:simpleQuery}"/>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="eat:search-results" mode="contextual-navigation">
		<xsl:if test="@pageSize &gt; 0">
			<input type="hidden" name="index.pageSize" value="{@pageSize}"/>
		</xsl:if>
		<xsl:if test="@pageStart &gt; 0">
			<input type="hidden" name="index.pageStart" value="{@pageStart}"/>
		</xsl:if>
		<xsl:apply-templates select="eat:ui-index/eat:index-section[@selected='true']"
			 mode="contextual-navigation"/>
		<xsl:apply-templates select="eat:recipe-criteria" mode="contextual-navigation"/>
	</xsl:template>
	<xsl:template match="eat:ui-index/eat:index-section" mode="contextual-navigation">
		<input type="hidden" name="index.displaySection" value="{@index-key}"/>
	</xsl:template>
	<xsl:template match="eat:recipe-criteria" mode="contextual-navigation">
		<input type="hidden" name="criteria.mode" value="{@mode}"/>
		<xsl:if test="eat:simpleQuery">
			<input type="hidden" name="criteria.simpleQuery" value="{eat:simpleQuery}"/>
		</xsl:if>
	</xsl:template>

	
	<!-- 
		Render index page navigation
	-->
	<xsl:template name="render-ui-index">
		<xsl:param name="ui-index"/>
		<xsl:param name="link"/>
		<table class="recipe-index">
			<tbody>
				<tr>
					<xsl:for-each select="$ui-index/eat:index-section">
						<xsl:call-template name="render-index-section">
							<xsl:with-param name="index-section" select="."/>
							<xsl:with-param name="link" select="$link"/>
						</xsl:call-template>
					</xsl:for-each>
				</tr>
			</tbody>
		</table>
	</xsl:template>
	<xsl:template name="render-index-section">
		<xsl:param name="index-section"/>
		<xsl:param name="link"/>
		<td>
			<xsl:if test="$index-section/@selected">
				<xsl:attribute name="class">selected</xsl:attribute>
			</xsl:if>
			<xsl:choose>
				<xsl:when test="$index-section/@selected">
					<xsl:value-of select="$index-section/@index-key"/>
				</xsl:when>
				<xsl:otherwise>
					<a>
						<xsl:attribute name="href">
							<xsl:value-of select="$web-context"/>
							<xsl:value-of select="$link"/>
							<xsl:text>?displaySection=</xsl:text>
							<xsl:value-of select="$index-section/@index-key"/>
							<xsl:if test="not($index-section/../../@pageSize = $index-section/../../eat:pagination/@defaultPageSize)">
								<xsl:text>&amp;pageSize=</xsl:text>
								<xsl:value-of select="$index-section/../../@pageSize"/>
							</xsl:if>
						</xsl:attribute>
						<xsl:value-of select="$index-section/@index-key"/>
					</a>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:text> (</xsl:text>
			<xsl:value-of select="$index-section/@count"/>
			<xsl:text>)</xsl:text>
		</td>
	</xsl:template>
	<xsl:template name="render-index-navigation">
		<xsl:param name="search-results"/>
		<xsl:param name="curr-page" select="0"/>
		<xsl:param name="max-page"/>
		<xsl:param name="link"/>
		<xsl:if test="$curr-page &gt; 0">
			<xsl:text> - </xsl:text>
		</xsl:if>
		<xsl:choose>
			<xsl:when test="$search-results/@pageStart = $curr-page">
				<span class="selected-page">
					<xsl:value-of select="$curr-page + 1"/>
				</span>
			</xsl:when>
			<xsl:otherwise>
				<a>
					<xsl:attribute name="href">
						<xsl:value-of select="$web-context"/>
						<xsl:value-of select="$link"/>
						<xsl:text>?displaySection=</xsl:text>
						<xsl:value-of select="$search-results/eat:ui-index/eat:index-section[@selected]/@index-key"/>
						<xsl:text>&amp;pageSize=</xsl:text>
						<xsl:value-of select="$search-results/@pageSize"/>
						<xsl:if test="$curr-page &gt; 0">
							<xsl:text>&amp;pageStart=</xsl:text>
							<xsl:value-of select="$curr-page"/>
						</xsl:if>
					</xsl:attribute>
					<xsl:value-of select="$curr-page + 1"/>
				</a>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:if test="$curr-page &lt; ($max-page - 1)">
			<xsl:call-template name="render-index-navigation">
				<xsl:with-param name="search-results" select="$search-results"/>
				<xsl:with-param name="curr-page" select="$curr-page + 1"/>
				<xsl:with-param name="max-page" select="$max-page"/>
				<xsl:with-param name="link" select="$link"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="eat:search-results" mode="render-index-pagination-menu">
		<xsl:if test="eat:pagination/eat:allowablePageSize">
			<div class="pagination">
				<form action="{$web-context}{$ctx/xweb:path}" method="post">
					<xsl:value-of select="key('i18n','pagination.size.displayName')"/>
					<xsl:text>: </xsl:text>
					<select name="pageSize" id="pagination-select">
						<xsl:for-each select="eat:pagination/eat:allowablePageSize">
							<option value="{.}">
								<xsl:if test="string(.) = ../../@pageSize">
									<xsl:attribute name="selected">selected</xsl:attribute>
								</xsl:if>
								<xsl:value-of select="."/>
							</option>
						</xsl:for-each>
						<xsl:if test="eat:pagination/@allowUnlimited = 'true'">
							<option value="-1">
								<xsl:if test="'-1' = ../../@pageSize">
									<xsl:attribute name="selected">selected</xsl:attribute>
								</xsl:if>
								<xsl:value-of select="key('i18n','unlimited')"/>
							</option>
						</xsl:if>
						<xsl:apply-templates select="eat:allowablePageSize"></xsl:apply-templates>
					</select>
					<input type="hidden" name="pageStart" value="0"/>
					<input type="hidden" name="displaySection" 
						value="{eat:ui-index/eat:index-section[@selected='true']/@index-key}"/>
				</form>
			</div>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="eat:index-section" mode="href">
		<xsl:text>&amp;index.displaySection=</xsl:text>
		<xsl:value-of select="@index-key"/>
		<xsl:if test="../../@pageSize">
			<xsl:text>&amp;index.pageSize=</xsl:text>
			<xsl:value-of select="../../@pageSize"/>
		</xsl:if>
		<xsl:if test="../../@pageStart &gt; 0">
			<xsl:text>&amp;index.pageStart=</xsl:text>
			<xsl:value-of select="../../@pageStart"/>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="eat:index-section" mode="href-direct">
		<xsl:text>&amp;displaySection=</xsl:text>
		<xsl:value-of select="@index-key"/>
		<xsl:if test="../../@pageSize">
			<xsl:text>&amp;pageSize=</xsl:text>
			<xsl:value-of select="../../@pageSize"/>
		</xsl:if>
		<xsl:if test="../../@pageStart &gt; 0">
			<xsl:text>&amp;pageStart=</xsl:text>
			<xsl:value-of select="../../@pageStart"/>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="eat:recipe-criteria" mode="href">
		<xsl:text>&amp;criteria.mode=</xsl:text>
		<xsl:value-of select="@mode"/>
	</xsl:template>
	
	<!-- 
		Search for recipes form template
	-->
	<xsl:template match="xweb:x-data" mode="search-for-recipes">
		<xsl:variable name="rCriteria" select="xweb:x-model/eat:search-results/eat:recipe-criteria"/>
		<form method="post" action="{$web-context}{$ctx/xweb:path}">
			<table class="form">
				<tr>
					<th>
						<xsl:value-of select="key('i18n','name.displayName')"/>
					</th>
					<td colspan="3">
						<input type="text" name="criteria.name" value="{$rCriteria/eat:name}"/>
					</td>
				</tr>
				<tr>
					<th/>
					<td>
						<input type="submit" value="{key('i18n','search.displayName')}"/>
						<xsl:text> </xsl:text>
						<input type="submit" name="_cancel" value="{key('i18n','cancel.displayName')}"/>
					</td>
				</tr>
			</table>
			<input type="hidden" name="mode" value="{xweb:x-model/eat:search-results/@mode}"/>
		</form>
		<script type="text/javascript" xml:space="preserve">
			document.forms[0].elements['criteria.name'].focus();
		</script>
		<xsl:if test="xweb:x-model/eat:search-results/eat:recipe-criteria">
			<xsl:apply-templates select="xweb:x-model/eat:search-results" mode="search-for-recipes"/>
		</xsl:if>
	</xsl:template>
    <xsl:template match="eat:search-results" mode="search-for-recipes">
    		<hr />
 		<xsl:choose>
			<xsl:when test="@totalResults &gt; 0">
				<table class="recipe-index">
					<tbody>
						<tr>
							<th>
								<xsl:value-of select="key('i18n','search.results.results')"/>
								<xsl:text>: </xsl:text>
								<xsl:value-of select="@totalResults"/>
								<xsl:text> </xsl:text>
								<xsl:choose>
									<xsl:when test="@totalResults &gt; 1">
										<xsl:value-of select="key('i18n','search.results.match.plural')"/>
									</xsl:when>
									<xsl:otherwise>
										<xsl:value-of select="key('i18n','search.results.match')"/>
									</xsl:otherwise>
								</xsl:choose>
							</th>
						</tr>
					</tbody>
				</table>
				<ol>
					<xsl:apply-templates select="eat:recipe" mode="search-for-recipes"/>
				</ol>
			</xsl:when>
			<xsl:otherwise>
				<p>
					<xsl:value-of select="key('i18n','search.noresults')"/>
				</p>
			</xsl:otherwise>
		</xsl:choose>
    </xsl:template>
    <xsl:template match="eat:recipe" mode="search-for-recipes">
    		<li>
    			<a>
    				<xsl:attribute name="href">
    					<xsl:text>javascript:chooseRecipe(</xsl:text>
    					<xsl:value-of select="@recipeId"/>
    					<xsl:text>,'</xsl:text>
    					<xsl:call-template name="single-quote-string">
						<xsl:with-param name="output-string" select="eat:name"/>
					</xsl:call-template>
					<xsl:text>')</xsl:text>
    				</xsl:attribute>
    				<xsl:value-of select="eat:name"/>
    			</a>
    			<p>
    				<xsl:call-template name="truncate-at-word">
    					<xsl:with-param name="text" select="eat:excerpt"/>
    					<xsl:with-param name="max-length" select="100"/>
    				</xsl:call-template>
    			</p>
    		</li>
    </xsl:template>
    
	
</xsl:stylesheet>
