<?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">
	
    <!-- imports -->
	<xsl:import href="tmpl/default-layout.xsl"/>
	
	<!-- layout variables -->
	<xsl:variable name="layout.global.nav.page" select="'index'"/>
	
	
	<xsl:template match="xweb:x-data" mode="page-title">
		<xsl:value-of select="key('i18n','recipe-index.title')"/>
	</xsl:template>
	
	<xsl:template match="xweb:x-data" mode="page-head-content">
		<link rel="stylesheet" type="text/css" href="{$web-context}/css/recipe-forms.css" media="screen"/>
		<script type="text/javascript" xml:space="preserve">
			<xsl:comment>
				var W3CDOM = (document.getElementById &amp;&amp; true);
				
				function handlePaginationChange(event) {
					if ( event.target.form) {
						event.target.form.submit();
					}
				}
				
				function init() {
					if ( !W3CDOM ) {
						alert("Your browser does not support DOM");
						return;
					}
					var pageSelect = document.getElementById("pagination-select");
					if ( pageSelect ) {
						pageSelect.onchange = handlePaginationChange;
					}
				}
				
				window.onload = init;
			//</xsl:comment>
		</script>
	</xsl:template>
	
	<xsl:template match="xweb:x-data" mode="page-body">
		<xsl:apply-templates select="xweb:x-model/eat:search-results"/>
	</xsl:template>
	
	<xsl:template match="eat:search-results">
		<xsl:call-template name="render-ui-index">
			<xsl:with-param name="ui-index" select="eat:ui-index"/>
			<xsl:with-param name="link" select="'/index.do'"/>
		</xsl:call-template>
		<xsl:variable name="total-count" select="eat:ui-index/eat:index-section[@selected]/@count"/>
		<xsl:if test="@pageSize &gt; 0 and $total-count &gt; @pageSize">
			<div class="page-navigation">
				<xsl:value-of select="key('i18n','recipe-index.pages.displayName')"/>
				<xsl:text>: </xsl:text>
				<xsl:call-template name="render-index-navigation">
					<xsl:with-param name="search-results" select="."/>
					<xsl:with-param name="max-page" select="ceiling($total-count div @pageSize)"/>
					<xsl:with-param name="link" select="'/index.do'"/>
				</xsl:call-template>
			</div>
		</xsl:if>
		
		<!-- Render <select> menu for pagination support -->
		<xsl:apply-templates select="." mode="render-index-pagination-menu"/>
		
		<ol class="recipe-index" start="{(@pageStart * @pageSize) + 1}">
			<xsl:apply-templates select="eat:recipe"/>
		</ol>
	</xsl:template>
	
	<xsl:template match="eat:search-results/eat:recipe">
		<li>
			<span class="recipe-title">
				<a class="go">
					<xsl:attribute name="href">
						<xsl:value-of select="$web-context"/>
						<xsl:text>/recipe.do?recipeId=</xsl:text>
						<xsl:value-of select="@recipeId"/>
						<xsl:text>&amp;index.displaySection=</xsl:text>
						<xsl:value-of select="../eat:ui-index/eat:index-section[@selected='true']/@index-key"/>
						<xsl:if test="../@pageSize">
							<xsl:text>&amp;index.pageSize=</xsl:text>
							<xsl:value-of select="../@pageSize"/>
						</xsl:if>
						<xsl:if test="../@pageStart">
							<xsl:text>&amp;index.pageStart=</xsl:text>
							<xsl:value-of select="../@pageStart"/>
						</xsl:if>
					</xsl:attribute>
					<xsl:value-of select="eat:name"/>
				</a>
			</span>
			<p class="excerpt">
				<xsl:value-of select="eat:excerpt"/>
			</p>
		</li>
	</xsl:template>
    
</xsl:stylesheet>
