<?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"/>
	<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="xweb:x-data/xweb:x-model/eat:search-results/eat:ui-index/eat:index-section[@selected = 'true']"/>
	<xsl:variable name="recipe-criteria" select="xweb:x-data/xweb:x-model/eat:search-results/eat:recipe-criteria"/>
	<xsl:variable name="recipe" select="xweb:x-data/xweb:x-model/eat:search-results/eat:recipe"/>

	<xsl:template match="xweb:x-data" mode="page-title">
		<xsl:value-of select="$recipe/eat:name"/>
	</xsl:template>
	
	<xsl:template match="xweb:x-data" mode="page-head-content">
		<xsl:if test="$acting-user">
			<!-- include the rating JavaScript -->
			<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" src="{$web-context}/js/ratings.js"><xsl:text> </xsl:text></script>
			<script type="text/javascript" xml:space="preserve">
				<xsl:comment>
					var W3CDOM = (document.createElement &amp;&amp; document.getElementsByTagName);
					var STAR_SETS = new Array();
					
					function init() {
						if ( !W3CDOM ) {
							alert("Your browser does not support DOM: some features will not be available.");
							return;
						}

						popupWindows();

						STAR_SETS[0] = new StarSet('<xsl:value-of select="$web-context"/>/img/','<xsl:value-of 
							select="$web-context"/>/setRating.do?recipeId=<xsl:value-of 
							select="$recipe/@recipeId"/>&amp;rating=',0,<xsl:choose xml:space="default">
								<xsl:when test="$recipe/eat:rating[@userId=$acting-user/@userId]">
									<xsl:value-of select="$recipe/eat:rating[@userId=$acting-user/@userId]/@rating"/>
								</xsl:when>
								<xsl:otherwise>
									<xsl:text>0</xsl:text>
								</xsl:otherwise>
							</xsl:choose>,<xsl:value-of 
							select="$recipe/@recipeId"/>);
					}

					window.onload = init;
			
				//</xsl:comment>
			</script>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="xweb:x-data" mode="action-links">
		<a title="{key('i18n','link.recipe.convertSystem.title')}">
			<xsl:attribute name="href">
				<xsl:value-of select="$web-context"/>
				<xsl:text>/convertRecipe.do?recipeId=</xsl:text>
				<xsl:value-of select="$recipe/@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','link.recipe.convertSystem')"/>
		</a>
		<xsl:text> - </xsl:text>
		<a title="{key('i18n','link.recipe.convertSize.title')}">
			<xsl:attribute name="href">
				<xsl:value-of select="$web-context"/>
				<xsl:text>/alterRecipe.do?recipeId=</xsl:text>
				<xsl:value-of select="$recipe/@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','link.recipe.convertSize')"/>
		</a>
		<xsl:text> - </xsl:text>
		<a title="{key('i18n','link.export.title')}">
			<xsl:attribute name="href">
				<xsl:value-of select="$web-context"/>
				<xsl:text>/exportRecipe.do?recipeId=</xsl:text>
				<xsl:value-of select="$recipe/@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','link.export')"/>
		</a>
		<xsl:if test="$acting-user">
			<xsl:text> - </xsl:text>
			<a title="{key('i18n','link.email.title')}">
				<xsl:attribute name="href">
					<xsl:value-of select="$web-context"/>
					<xsl:text>/emailRecipe.do?recipeId=</xsl:text>
					<xsl:value-of select="$recipe/@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','link.email')"/>
			</a>
			<xsl:text> - </xsl:text>
			<a href="{$web-context}/addRecipeToMeal.do?recipeId={$recipe/@recipeId}" 
					title="{key('i18n','link.recipe.addToMeal.title')}">
				<xsl:value-of select="key('i18n','link.recipe.addToMeal')"/>
			</a>
			<xsl:if test="$acting-user/@userId = $recipe/eat:owner/@userId or 
					$acting-user/../@admin = 'true'">
				<xsl:text> - </xsl:text>
				<a title="{key('i18n','link.recipe.edit.title')}">
					<xsl:attribute name="href">
						<xsl:value-of select="$web-context"/>
						<xsl:text>/editRecipe.do?recipeId=</xsl:text>
						<xsl:value-of select="$recipe/@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','link.recipe.edit')"/>
				</a>
				<xsl:text> - </xsl:text>
				<a title="{key('i18n','link.recipe.delete.title')}">
					<xsl:attribute name="href">
						<xsl:value-of select="$web-context"/>
						<xsl:text>/deleteRecipe.do?recipeId=</xsl:text>
						<xsl:value-of select="$recipe/@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','link.recipe.delete')"/>
				</a>
			</xsl:if>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="xweb:x-data" mode="action-links-2">
		<xsl:text> </xsl:text><!-- required so show up in output template -->

		<!-- Recipe comments link -->
		<xsl:if test="key('appenv','feature.comments') = 'true'">
			<a title="{key('i18n','link.recipe.comments.title')}">
				<xsl:attribute name="href">
					<xsl:value-of select="$web-context"/>
					<xsl:text>/recipeComments.do?recipeId=</xsl:text>
					<xsl:value-of select="$recipe/@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','link.recipe.comments')"/>
			</a>
		</xsl:if>
			
		<xsl:if test="$acting-user">
			<xsl:if test="key('appenv','feature.comments') = 'true'">
				<xsl:text> - </xsl:text>
			</xsl:if>

			<xsl:if test="key('appenv','feature.recipeimages') = 'true' and (
					$acting-user/@userId = $recipe/eat:owner/@userId or 
					$acting-user/../@admin = 'true' )">
				<a title="{key('i18n','link.recipe.image.title')}">
					<xsl:attribute name="href">
						<xsl:value-of select="$web-context"/>
						<xsl:text>/editRecipeImage.do?recipeId=</xsl:text>
						<xsl:value-of select="$recipe/@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','link.recipe.image')"/>
				</a>
				<xsl:text> - </xsl:text>
			</xsl:if>

			<!-- Recipe rating widget -->
			<xsl:if test="count($recipe/eat:rating) &gt; 0">
				<span class="rating-overall">
					<!-- Display user average -->
					<xsl:variable name="tally" select="sum($recipe/eat:rating/@rating)"/>
					<xsl:variable name="cnt" select="count($recipe/eat:rating/@rating)"/>
					<xsl:text> (</xsl:text>
					<xsl:value-of select="format-number($tally div $cnt, '0.#')"/>
					<xsl:text> &#x2022; </xsl:text>
					<xsl:value-of select="$cnt"/>
					<xsl:text>)</xsl:text>
				</span>
			</xsl:if>
			<span class="rating-stars">
			    <img class="star-rating" id="star.0.1" src="{$web-context}/img/star-off.png" alt="1" width="13" height="13" />
			    <img class="star-rating" id="star.0.2" src="{$web-context}/img/star-off.png" alt="2" width="13" height="13" />
			    <img class="star-rating" id="star.0.3" src="{$web-context}/img/star-off.png" alt="3" width="13" height="13" />
			    <img class="star-rating" id="star.0.4" src="{$web-context}/img/star-off.png" alt="4" width="13" height="13" />
			    <img class="star-rating" id="star.0.5" src="{$web-context}/img/star-off.png" alt="5" width="13" height="13" />
			</span>
		</xsl:if>	
	</xsl:template>
	
	<xsl:template match="xweb:x-data" mode="page-body">
		<xsl:if test="$index-section">
			<div class="return-nav">
				<a class="back">
					<xsl:attribute name="href">
						<xsl:value-of select="$web-context"/>
						<xsl:text>/index.do?</xsl:text>
						<xsl:apply-templates select="$index-section" mode="href-direct"/>
					</xsl:attribute>
					<xsl:value-of select="key('i18n','back.to')"/>
					<xsl:text> </xsl:text>
					<span class="index-link">
						<xsl:value-of select="$index-section/@index-key"/>
					</span>
				</a>
			</div>
		</xsl:if>
		
		<xsl:if test="$recipe-criteria">
			<div class="return-nav">
				<a class="back">
					<xsl:attribute name="href">
						<xsl:value-of select="$web-context"/>
						<xsl:text>/search.do?</xsl:text>
						<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="key('i18n','search.results')"/>
				</a>
			</div>
		</xsl:if>
		
		<div class="recipe-view">
			<xsl:apply-templates select="$recipe" mode="display-internal"/>
		</div>
	</xsl:template>
    
</xsl:stylesheet>
