<?xml version="1.0" encoding="UTF-8"?>
<!--
 - Desc:    This file is part of the eCromedos document preparation system
 - Date:    2006/03/09
 - Author:  Tobias Koch (tkoch@ecromedos.net)
 - License: GNU General Public License, version 2
 - URL:     http://www.ecromedos.net
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!--
  - Catch node 'figure' and determine context
-->
<xsl:template match="figure">
	<xsl:choose>
		<xsl:when test="ancestor::p">
			<xsl:call-template name="figure.inline"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:call-template name="figure.block"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - Set an inline figure
-->
<xsl:template name="figure.inline">

	<xsl:variable name="style">
		<xsl:choose>
			<xsl:when test="@align='right'">
				<xsl:text>float: right; padding: 2ex 0 2ex 2ex;</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>float: left; padding: 2ex 2ex 2ex 0;</xsl:text>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:variable name="width">
		<xsl:choose>
			<xsl:when test="img/@screen-width">
				<xsl:text>width:</xsl:text>
				<xsl:value-of select="normalize-space(img/@screen-width)"/>
				<xsl:text>;</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text></xsl:text>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:variable name="prefix">
		<xsl:call-template name="figure.prefix"/>
	</xsl:variable>

	<span style="{$width} {$style}">
		<xsl:apply-templates select="label"/>
		<img style="display: block" alt="">
			<xsl:attribute name="src">
				<xsl:value-of select="normalize-space(img/@src)"/>
			</xsl:attribute>
		</img>
		<xsl:if test="caption">
		<span style="display: block; font-size: xx-small;">
			<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
		</span>
		<span style="display: block; text-align: center;">
			<xsl:if test="$prefix != ''">
				<b>
				<xsl:call-template name="i18n.print">
					<xsl:with-param name="key" select="'figure'"/>
					<xsl:with-param name="number" select="$prefix"/>
				</xsl:call-template>
				<xsl:text>: </xsl:text>
				</b>
			</xsl:if>
			<xsl:apply-templates select="caption"/>
		</span>
		</xsl:if>
	</span>
</xsl:template>

<!--
  - Set a block figure
-->
<xsl:template name="figure.block">

	<xsl:variable name="align">
		<xsl:choose>
			<xsl:when test="@align='center'">
				<xsl:text>center</xsl:text>
			</xsl:when>
			<xsl:when test="@align='right'">
				<xsl:text>right</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>left</xsl:text>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:variable name="prefix">
		<xsl:call-template name="figure.prefix"/>
	</xsl:variable>
	
	<xsl:apply-templates select="label"/>
	<table border="0" cellspacing="0" cellpadding="0" style="width: 100%"><tr><td align="{$align}">
		<table border="0" cellspacing="0" cellpadding="0" width="1px">
			<tr>
				<td>
					<img style="vertical-align: middle;" alt="">
						<xsl:attribute name="src">
							<xsl:value-of select="normalize-space(img/@src)"/>
						</xsl:attribute>
					</img>
				</td>
			</tr>
			<!-- caption -->
			<xsl:if test="caption">
			<tr>
				<td style="font-size: xx-small;">
					<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
				</td>
			</tr>
			<tr align="center">
				<td>
					<xsl:if test="$prefix != ''">
						<b>
						<xsl:call-template name="i18n.print">
							<xsl:with-param name="key" select="'figure'"/>
							<xsl:with-param name="number" select="$prefix"/>
						</xsl:call-template>
						<xsl:text>: </xsl:text>
						</b>
					</xsl:if>
					<xsl:apply-templates select="caption"/>
				</td>
			</tr>
			</xsl:if>
		</table>
	</td></tr></table>
</xsl:template>

<!--
  - Get combined counter for figure
-->
<xsl:template name="figure.prefix">
	<xsl:choose>
		<xsl:when test="ancestor::chapter">
			<xsl:number count="chapter"/>
			<xsl:text>.</xsl:text>
			<xsl:variable name="id" select="generate-id(ancestor::chapter)"/>
			<xsl:number value="count(preceding::figure[
				generate-id(ancestor::chapter) = $id
				and descendant::caption]) + 1"/>
		</xsl:when>
		<xsl:when test="ancestor::appendix">
			<xsl:number count="appendix" format="A"/>
			<xsl:text>.</xsl:text>
			<xsl:variable name="id" select="generate-id(ancestor::appendix)"/>
			<xsl:number value="count(preceding::figure[
				generate-id(ancestor::appendix) = $id
				and descendant::caption]) + 1"/>
		</xsl:when>
		<xsl:when test="ancestor::section">
			<xsl:number count="section"/>
			<xsl:text>.</xsl:text>
			<xsl:variable name="id" select="generate-id(ancestor::section)"/>
			<xsl:number value="count(preceding::figure[
				generate-id(ancestor::section) = $id
				and descendant::caption]) + 1"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:text>0.</xsl:text>
			<xsl:number value="count(
				preceding::figure/ancestor::*[name() = 'preface']) + 1"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>
