<?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"
	xmlns:date="http://exslt.org/dates-and-times" exclude-result-prefixes="eat xweb date"
	extension-element-prefixes="date">

	<xsl:import href="global-variables.xsl"/>

	<xsl:template match="eat:recipe" mode="view">
		<h1>
			<xsl:value-of select="eat:name"/>
		</h1>
		<div class="recipe-view">
			<xsl:apply-templates select="." mode="display-internal"/>
		</div>
	</xsl:template>

	<xsl:template match="eat:recipe" mode="display">
		<div class="recipe-view">
			<h2>
				<xsl:value-of select="eat:name"/>
			</h2>
			<xsl:apply-templates select="." mode="display-internal"/>
		</div>
	</xsl:template>

	<xsl:template match="eat:recipe" mode="display-internal">
		<xsl:if test="eat:image">
			<div>
				<img alt="{eat:name}" class="recipe">
					<xsl:attribute name="src">
						<xsl:value-of select="$web-context"/>
						<xsl:value-of select="key('appenv','recipe.image.base.url')"/>
						<xsl:value-of select="eat:image"/>
					</xsl:attribute>
				</img>
			</div>
		</xsl:if>
		<p>
			<xsl:value-of select="eat:excerpt"/>
		</p>
		<xsl:if test="string(eat:directions)">
			<p>
				<xsl:value-of select="eat:directions"/>
			</p>
		</xsl:if>
		<xsl:if test="string(eat:source)">
			<p>
				<b><xsl:value-of select="key('i18n','recipe.source.displayName')"/>:</b>
				<xsl:text> </xsl:text>
				<xsl:apply-templates select="eat:source"/>
			</p>
		</xsl:if>
		<table class="view">
			<tr>
				<th><xsl:value-of select="key('i18n','recipe.base.displayName')"/>:</th>
				<td>
					<xsl:apply-templates select="eat:base"/>
				</td>
				<th><xsl:value-of select="key('i18n','recipe.prepTime.displayName')"/>:</th>
				<td>
					<xsl:apply-templates select="eat:prep-time"/>
				</td>
			</tr>
			<tr>
				<th><xsl:value-of select="key('i18n','recipe.ethnicity.displayName')"/>:</th>
				<td>
					<xsl:apply-templates select="eat:ethnicity"/>
				</td>
				<th><xsl:value-of select="key('i18n','recipe.difficulty.displayName')"/>:</th>
				<td>
					<xsl:apply-templates select="eat:difficulty"/>
				</td>
			</tr>
			<tr>
				<th><xsl:value-of select="key('i18n','recipe.course.displayName')"/>:</th>
				<td>
					<xsl:apply-templates select="eat:course"/>
				</td>
				<th><xsl:value-of select="key('i18n','recipe.servings.displayName')"/>:</th>
				<td>
					<xsl:value-of select="@servingSize"/>
				</td>
			</tr>
		</table>

		<div class="viewLabel">
			<xsl:value-of select="key('i18n','recipe.ingredients.displayName')"/>: </div>

		<table class="view">
			<xsl:choose>
				<xsl:when test="../@mode = 'converted'">
					<xsl:apply-templates select="eat:ingredient" mode="converted"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="eat:ingredient" mode="display"/>
				</xsl:otherwise>
			</xsl:choose>
		</table>

		<xsl:if test="eat:step">

			<div class="viewLabel">
				<xsl:value-of select="key('i18n','recipe.steps.displayName')"/>: </div>

			<div class="viewBox">
				<ol>
					<xsl:apply-templates select="eat:step" mode="display"/>
				</ol>
			</div>

		</xsl:if>

		<br style="clear: both;"/>

		<xsl:if test="@createdDate">
			<div class="recipe-footer">
				<xsl:value-of select="key('i18n','created.on')"/>
				<xsl:text> </xsl:text>
				<xsl:value-of select="date:format-date(string(@createdDate),'d MMM yyyy')"/>
				<xsl:if test="eat:owner/@login">
					<xsl:text> </xsl:text>
					<xsl:value-of select="key('i18n','by')"/>
					<xsl:text> </xsl:text>
					<xsl:choose>
						<xsl:when test="$acting-user/../@admin = 'true'">
							<a href="{$web-context}/user.do?userId={eat:owner/@userId}"
								rel="popup|600|400|0|1">
								<xsl:value-of select="eat:owner/@login"/>
							</a>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="eat:owner/@login"/>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:if test="$acting-user">
						<!-- handy link to search for all recipes creatd by a user -->
						<a title="{key('i18n','link.quick.searchowner.title')}"
							href="{$web-context}/search.do?mode=advanced&amp;criteria.advanced[0].type=3&amp;criteria.advanced[0].op=1&amp;criteria.advanced[0].query={eat:owner/@login}">
							<img class="quicklink" width="8" height="9"
								src="{$web-context}/img/link-r.png"
								alt="{key('i18n','link.quick.searchowner')}"/>
						</a>
					</xsl:if>
				</xsl:if>
			</div>
		</xsl:if>
	</xsl:template>

	<xsl:template match="eat:recipe/eat:step" mode="display">
		<li>
			<xsl:value-of select="."/>
		</li>
	</xsl:template>

	<xsl:template match="eat:recipe/eat:ingredient" mode="converted">
		<xsl:variable name="system" select="../eat:system/@systemId"/>
		<xsl:variable name="mass" select="eat:unit/@mass"/>
		<tr>
			<xsl:choose>
				<xsl:when
					test="not(eat:unit) or ($system = 1 and eat:unit/@toUS = 0) or 
					($system = 2 and eat:unit/@toMetric = 0)">
					<!-- no conversion necessary -->
					<td>
						<xsl:call-template name="ingredient-quantity">
							<xsl:with-param name="quantity" select="@quantity"/>
						</xsl:call-template>
					</td>
					<td>
						<xsl:apply-templates select="eat:unit"/>
					</td>
				</xsl:when>
				<xsl:when test="$mass = 'true' and $system = 1">
					<!-- ok, dealing with US ounces: if over 16 oz convert to pounds -->
					<xsl:choose>
						<xsl:when test="@quantity &lt; 16">
							<td>
								<xsl:call-template name="ingredient-quantity">
									<xsl:with-param name="quantity" select="@quantity"/>
								</xsl:call-template>
							</td>
							<td>
								<xsl:call-template name="ingredient-unit">
									<xsl:with-param name="quantity" select="@quantity"/>
									<xsl:with-param name="abbreviation" select="'oz'"/>
								</xsl:call-template>
							</td>
						</xsl:when>
						<xsl:otherwise>
							<td>
								<xsl:call-template name="ingredient-quantity">
									<xsl:with-param name="quantity" select="@quantity div 16.0"/>
								</xsl:call-template>
							</td>
							<td>
								<xsl:call-template name="ingredient-unit">
									<xsl:with-param name="quantity" select="@quantity div 16.0"/>
									<xsl:with-param name="abbreviation" select="'lb'"/>
								</xsl:call-template>
							</td>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:when test="$mass = 'true' and $system = 2">
					<!-- ok, dealing with metric grams -->
					<xsl:choose>
						<xsl:when test="@quantity &lt; 1">
							<!-- convert to milligrams -->
							<td>
								<xsl:call-template name="ingredient-quantity">
									<xsl:with-param name="quantity" select="@quantity * 1000"/>
								</xsl:call-template>
							</td>
							<td>
								<xsl:call-template name="ingredient-unit">
									<xsl:with-param name="quantity" select="@quantity * 1000"/>
									<xsl:with-param name="abbreviation" select="'mg'"/>
								</xsl:call-template>
							</td>
						</xsl:when>
						<xsl:when test="@quantity &lt; 1000000">
							<!-- keep as grams  -->
							<td>
								<xsl:call-template name="ingredient-quantity">
									<xsl:with-param name="quantity" select="@quantity"/>
								</xsl:call-template>
							</td>
							<td>
								<xsl:call-template name="ingredient-unit">
									<xsl:with-param name="quantity" select="@quantity"/>
									<xsl:with-param name="abbreviation" select="'g'"/>
								</xsl:call-template>
							</td>
						</xsl:when>
						<xsl:otherwise>
							<!-- convert to kilograms  -->
							<td>
								<xsl:call-template name="ingredient-quantity">
									<xsl:with-param name="quantity" select="@quantity div 1000000.0"
									/>
								</xsl:call-template>
							</td>
							<td>
								<xsl:call-template name="ingredient-unit">
									<xsl:with-param name="quantity" select="@quantity div 1000000.0"/>
									<xsl:with-param name="abbreviation" select="'kg'"/>
								</xsl:call-template>
							</td>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:when test="$mass = 'false' and $system = 1">
					<!-- ok, dealing with US teaspoons -->
					<xsl:choose>
						<xsl:when test="@quantity &lt; 3">
							<!-- keep as teaspoons -->
							<td>
								<xsl:call-template name="ingredient-quantity">
									<xsl:with-param name="quantity" select="@quantity"/>
								</xsl:call-template>
							</td>
							<td>
								<xsl:call-template name="ingredient-unit">
									<xsl:with-param name="quantity" select="@quantity"/>
									<xsl:with-param name="abbreviation" select="'t'"/>
								</xsl:call-template>
							</td>
						</xsl:when>
						<xsl:when test="@quantity &lt; 12">
							<!-- convert to tablespoons -->
							<td>
								<xsl:call-template name="ingredient-quantity">
									<xsl:with-param name="quantity" select="@quantity div 3.0"/>
								</xsl:call-template>
							</td>
							<td>
								<xsl:call-template name="ingredient-unit">
									<xsl:with-param name="quantity" select="@quantity div 3.0"/>
									<xsl:with-param name="abbreviation" select="'T'"/>
								</xsl:call-template>
							</td>
						</xsl:when>
						<xsl:when test="@quantity &lt; 768">
							<!-- convert to cups -->
							<td>
								<xsl:call-template name="ingredient-quantity">
									<xsl:with-param name="quantity" select="@quantity div 48.0"/>
								</xsl:call-template>
							</td>
							<td>
								<xsl:call-template name="ingredient-unit">
									<xsl:with-param name="quantity" select="@quantity div 48.0"/>
									<xsl:with-param name="abbreviation" select="'c'"/>
								</xsl:call-template>
							</td>
						</xsl:when>
						<xsl:otherwise>
							<!-- convert to gallons + fl oz  -->
							<xsl:variable name="gallons" select="floor(@quantity div 768.0)"/>
							<xsl:variable name="ounces" select="@quantity mod 768"/>
							<xsl:comment> gals = <xsl:value-of select="$gallons"/> tsp =
									<xsl:value-of select="$ounces"/>
							</xsl:comment>
							<td colspan="2">
								<xsl:call-template name="ingredient-quantity">
									<xsl:with-param name="quantity" select="$gallons"/>
								</xsl:call-template>
								<xsl:text> </xsl:text>
								<xsl:call-template name="ingredient-unit">
									<xsl:with-param name="quantity" select="$gallons"/>
									<xsl:with-param name="abbreviation" select="'gal'"/>
								</xsl:call-template>
								<xsl:if test="$ounces &gt; 0">
									<xsl:text>, </xsl:text>
									<xsl:call-template name="ingredient-quantity">
										<xsl:with-param name="quantity" select="$ounces div 16.0"/>
									</xsl:call-template>
									<xsl:text> </xsl:text>
									<xsl:call-template name="ingredient-unit">
										<xsl:with-param name="quantity" select="$ounces div 16.0"/>
										<xsl:with-param name="abbreviation" select="'fl.oz'"/>
									</xsl:call-template>
								</xsl:if>
							</td>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:when test="$mass = 'false' and $system = 2">
					<!-- ok, dealing with metric milliliters -->
					<xsl:choose>
						<xsl:when test="@quantity &lt; 1000">
							<!-- keep as milliliters -->
							<td>
								<xsl:call-template name="ingredient-quantity">
									<xsl:with-param name="quantity" select="@quantity"/>
								</xsl:call-template>
							</td>
							<td>
								<xsl:call-template name="ingredient-unit">
									<xsl:with-param name="quantity" select="@quantity"/>
									<xsl:with-param name="abbreviation" select="'ml'"/>
								</xsl:call-template>
							</td>
						</xsl:when>
						<xsl:otherwise>
							<!-- convert to liters  -->
							<td>
								<xsl:call-template name="ingredient-quantity">
									<xsl:with-param name="quantity" select="@quantity div 1000.0"/>
								</xsl:call-template>
							</td>
							<td>
								<xsl:call-template name="ingredient-unit">
									<xsl:with-param name="quantity" select="@quantity div 1000.0"/>
									<xsl:with-param name="abbreviation" select="'L'"/>
								</xsl:call-template>
							</td>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:otherwise>
					<xsl:comment> ack! </xsl:comment>
					<!-- ack! -->
				</xsl:otherwise>
			</xsl:choose>
			<td>
				<xsl:text> </xsl:text>
				<xsl:apply-templates select="eat:ingredient"/>
				<xsl:apply-templates select="eat:recipe"/>
				<xsl:if test="string-length(@qualifier) &gt; 0">
					<xsl:text> - </xsl:text>
					<i>
						<xsl:value-of select="@qualifier"/>
					</i>
				</xsl:if>
			</td>
		</tr>
	</xsl:template>

	<xsl:template match="eat:recipe/eat:ingredient" mode="display">
		<tr>
			<td>
				<xsl:call-template name="ingredient-quantity">
					<xsl:with-param name="quantity" select="@quantity"/>
				</xsl:call-template>
			</td>
			<td>
				<xsl:text> </xsl:text>
				<xsl:apply-templates select="eat:unit"/>
				<xsl:text> </xsl:text>
				<xsl:apply-templates select="eat:ingredient"/>
				<xsl:apply-templates select="eat:recipe"/>
				<xsl:if test="string-length(@qualifier) &gt; 0">
					<xsl:text> - </xsl:text>
					<i>
						<xsl:value-of select="@qualifier"/>
					</i>
				</xsl:if>
			</td>
		</tr>
	</xsl:template>

	<xsl:template match="eat:base">
		<xsl:choose>
			<xsl:when test="$aux/eat:ui/eat:base">
				<xsl:variable name="base-id" select="@baseId"/>
				<xsl:value-of select="$aux/eat:ui/eat:base[@baseId = $base-id]"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="eat:course">
		<xsl:variable name="display-key">
			<xsl:text>course.</xsl:text>
			<xsl:choose>
				<xsl:when test="$aux/eat:ui/eat:course">
					<xsl:variable name="course-id" select="@courseId"/>
					<xsl:value-of
						select="translate($aux/eat:ui/eat:course[@courseId = $course-id],' ','')"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="translate(string(.),' ','')"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:choose>
			<xsl:when test="key('i18n',$display-key)">
				<xsl:value-of select="key('i18n',$display-key)"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/>
			</xsl:otherwise>
		</xsl:choose>

	</xsl:template>

	<xsl:template match="eat:difficulty">
		<xsl:choose>
			<xsl:when test="$aux/eat:ui/eat:difficulty">
				<xsl:variable name="difficulty-id" select="@difficultyId"/>
				<xsl:value-of select="$aux/eat:ui/eat:difficulty[@difficultyId = $difficulty-id]"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="eat:ethnicity">
		<xsl:choose>
			<xsl:when test="$aux/eat:ui/eat:ethnicity">
				<xsl:variable name="ethnicity-id" select="@ethnicityId"/>
				<xsl:value-of select="$aux/eat:ui/eat:ethnicity[@ethnicityId = $ethnicity-id]"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="eat:prep-time">
		<xsl:choose>
			<xsl:when test="$aux/eat:ui/eat:prep-time">
				<xsl:variable name="preptime-id" select="@prepTimeId"/>
				<xsl:value-of select="$aux/eat:ui/eat:prep-time[@prepTimeId = $preptime-id]"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="eat:unit">
		<xsl:variable name="unit-key">
			<xsl:text>unit.</xsl:text>
			<xsl:choose>
				<xsl:when test="$aux/eat:ui/eat:unit">
					<xsl:variable name="unit-id" select="@unitId"/>
					<xsl:value-of
						select="translate($aux/eat:ui/eat:unit[@unitId = $unit-id]/@abbreviation,' ','')"
					/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="translate(@abbreviation,' ','')"/>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:if test="../@quantity &gt; 1">
				<xsl:text>.plural</xsl:text>
			</xsl:if>
		</xsl:variable>
		<xsl:choose>
			<xsl:when test="key('i18n',$unit-key)">
				<xsl:value-of select="key('i18n',$unit-key)"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
		
	</xsl:template>

	<xsl:template match="eat:ingredient">
		<xsl:choose>
			<xsl:when test="$aux/eat:ui/eat:ingredient">
				<xsl:variable name="ingredient-id" select="@ingredientId"/>
				<xsl:value-of
					select="$aux/eat:ui/eat:ingredient[@ingredientId = $ingredient-id]/eat:name"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="eat:name"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="eat:ingredient/eat:recipe">
		<a href="{$web-context}/recipe.do?recipeId={@recipeId}">
			<xsl:choose>
				<xsl:when test="string-length(eat:name) = 0">
					<xsl:variable name="recipe-id" select="@recipeId"/>
					<xsl:value-of select="$aux/eat:ui/eat:recipe[@recipeId = $recipe-id]/eat:name"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="eat:name"/>
				</xsl:otherwise>
			</xsl:choose>
		</a>
	</xsl:template>

	<xsl:template name="ingredient-quantity">
		<xsl:param name="quantity"/>
		<xsl:variable name="whole">
			<xsl:choose>
				<xsl:when test="contains($quantity,'.')">
					<xsl:value-of select="substring-before($quantity,'.')"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$quantity"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="frac" select="$quantity - $whole"/>
		<!--
		<xsl:comment>
			whole = <xsl:value-of select="$whole"/>
			frac = <xsl:value-of select="$frac"/>
		</xsl:comment>
		-->
		<xsl:choose>
			<xsl:when test="$frac = 0">
				<xsl:value-of select="$whole"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:if test="not($whole = 0)">
					<xsl:value-of select="$whole"/>
					<xsl:text> </xsl:text>
				</xsl:if>
				<xsl:choose>
					<xsl:when test="$frac = .75">
						<xsl:text>&#x00BE;</xsl:text>
					</xsl:when>
					<xsl:when
						test="(format-number($frac,'.00') = '.67') or (format-number($frac,'.00') = '.66')">
						<xsl:text>&#x2154;</xsl:text>
					</xsl:when>
					<xsl:when test="$frac = .5">
						<xsl:text>&#x00BD;</xsl:text>
					</xsl:when>
					<xsl:when test="format-number($frac,'.00') = '.33'">
						<xsl:text>&#x2153;</xsl:text>
					</xsl:when>
					<xsl:when test="$frac = .25">
						<xsl:text>&#x00BC;</xsl:text>
					</xsl:when>
					<xsl:when test="floor($frac * 8) =  $frac * 8">
						<xsl:value-of select="$frac * 8"/>
						<xsl:text>/8</xsl:text>
					</xsl:when>
					<xsl:when test="floor($frac * 16) =  $frac * 16">
						<xsl:value-of select="$frac * 16"/>
						<xsl:text>/16</xsl:text>
					</xsl:when>
					<xsl:when test="floor($frac * 32) =  $frac * 32">
						<xsl:value-of select="$frac * 32"/>
						<xsl:text>/32</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="format-number($frac,'.00#')"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="ingredient-unit">
		<xsl:param name="quantity"/>
		<xsl:param name="abbreviation"/>
		<xsl:variable name="unit-key">
			<xsl:text>unit.</xsl:text>
			<xsl:choose>
				<xsl:when test="$abbreviation">
					<xsl:value-of select="translate($abbreviation,' ','')"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:variable name="unit-id" select="@unitId"/>
					<xsl:value-of
						select="translate($aux/eat:ui/eat:unit[@unitId = $unit-id]/@abbreviation,' ','')"
					/>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:if test="$quantity &gt; 1">
				<xsl:text>.plural</xsl:text>
			</xsl:if>
		</xsl:variable>
		<xsl:value-of select="key('i18n',$unit-key)"/>
	</xsl:template>

</xsl:stylesheet>
