﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:fo="http://www.w3.org/1999/XSL/Format"
	xmlns:am="http://www.ani-mayhem.com/"
	xmlns:db="http://www.ani-mayhem.com/database"
	exclude-result-prefixes="am db">
	<xsl:variable name="cgi">http://www.ani-mayhem.com/cardcatalog.aspx?xmlid=</xsl:variable>
	<xsl:template match="/">
		<fo:root>
			<fo:layout-master-set>
				<fo:simple-page-master
					master-name="page"
					page-width="210mm"
					page-height="297mm"
					margin-top="12.7mm"
					margin-bottom="6.35mm"
					margin-left="12.7mm"
					margin-right="12.7mm">
					<fo:region-body
						margin-top="0mm"
						margin-bottom="25.4mm"
						margin-left="0mm"
						margin-right="0mm"/>
					<fo:region-after
						extent="25.4mm"/>
				</fo:simple-page-master>
			</fo:layout-master-set>
			<fo:bookmark-tree>
				<fo:bookmark internal-destination="{//am:card[db:Type='Character'][1]/@xml:id}">
					<fo:bookmark-title>Character</fo:bookmark-title>
				</fo:bookmark>
				<fo:bookmark internal-destination="{//am:card[db:Type='Enhancement'][1]/@xml:id}">
					<fo:bookmark-title>Enhancement</fo:bookmark-title>
				</fo:bookmark>
				<fo:bookmark internal-destination="{//am:card[db:Type='Equipment'][1]/@xml:id}">
					<fo:bookmark-title>Equipment</fo:bookmark-title>
				</fo:bookmark>
				<fo:bookmark internal-destination="{//am:card[db:Type='Flash Effect'][1]/@xml:id}">
					<fo:bookmark-title>Flash Effect</fo:bookmark-title>
				</fo:bookmark>
				<fo:bookmark internal-destination="{//am:card[db:Type='Global Effect'][1]/@xml:id}">
					<fo:bookmark-title>Global Effect</fo:bookmark-title>
				</fo:bookmark>
				<fo:bookmark internal-destination="{//am:card[db:Type='Haven'][1]/@xml:id}">
					<fo:bookmark-title>Haven</fo:bookmark-title>
				</fo:bookmark>
				<fo:bookmark internal-destination="{//am:card[db:Type='Item'][1]/@xml:id}">
					<fo:bookmark-title>Item</fo:bookmark-title>
				</fo:bookmark>
				<fo:bookmark internal-destination="{//am:card[db:Type='Location'][1]/@xml:id}">
					<fo:bookmark-title>Location</fo:bookmark-title>
				</fo:bookmark>
				<fo:bookmark internal-destination="{//am:card[db:Type='Major Disaster'][1]/@xml:id}">
					<fo:bookmark-title>Major Disaster</fo:bookmark-title>
				</fo:bookmark>
				<fo:bookmark internal-destination="{//am:card[db:Type='Minor Disaster'][1]/@xml:id}">
					<fo:bookmark-title>Minor Disaster</fo:bookmark-title>
				</fo:bookmark>
				<fo:bookmark internal-destination="{//am:card[db:Type='Combat'][1]/@xml:id}">
					<fo:bookmark-title>Combat</fo:bookmark-title>
				</fo:bookmark>
				<fo:bookmark internal-destination="{//am:card[db:Type='Charm Combat'][1]/@xml:id}">
					<fo:bookmark-title>Charm Combat</fo:bookmark-title>
				</fo:bookmark>
			</fo:bookmark-tree>
			<fo:page-sequence master-reference="page">
				<fo:static-content flow-name="xsl-region-after">
					<fo:block background-color="transparent">
						<fo:block>
							<fo:leader leader-length="184.6mm" leader-pattern="rule"/>
						</fo:block>
						<fo:block text-align="start">
							Ani-Mayhem created by AnimeCafe, Inc.
						</fo:block>
						<fo:block text-align="center">
							Ani-Mayhem Database <xsl:text disable-output-escaping="yes">&amp;#0169;</xsl:text> Ani-Mayhem Online
						</fo:block>
						<fo:block text-align="end">
							Page <fo:page-number/>
						</fo:block>
					</fo:block>
				</fo:static-content>
				<fo:flow flow-name="xsl-region-body">
					<fo:block font-size="20pt">
						Ani-Mayhem Online
					</fo:block>
					<fo:block font-size="15pt">
						Database
					</fo:block>
					<fo:block font-size="15pt" padding-bottom="4mm">
						December 17, 2011
					</fo:block>
					<xsl:apply-templates/>
				</fo:flow>
			</fo:page-sequence>
		</fo:root>
	</xsl:template>
	<xsl:template match="am:ani-mayhem">
		<fo:table font-size="10pt" table-layout="fixed" width="184.6mm">
			<fo:table-column column-number="1" column-width="61.6mm"/>
			<fo:table-column column-number="2" column-width="30mm"/>
			<fo:table-column column-number="3" column-width="30mm"/>
			<fo:table-column column-number="4" column-width="43mm"/>
			<fo:table-column column-number="5" column-width="20mm"/>
			<fo:table-header>
				<fo:table-row>
					<fo:table-cell>
						<fo:block>Name</fo:block>
					</fo:table-cell>
					<fo:table-cell>
						<fo:block>Type</fo:block>
					</fo:table-cell>
					<fo:table-cell>
						<fo:block>Rarity</fo:block>
					</fo:table-cell>
					<fo:table-cell>
						<fo:block>Series</fo:block>
					</fo:table-cell>
					<fo:table-cell>
						<fo:block>Set</fo:block>
					</fo:table-cell>
				</fo:table-row>
			</fo:table-header>
			<fo:table-body>
				<xsl:apply-templates select="am:card"/>
			</fo:table-body>
		</fo:table>
	</xsl:template>
	<xsl:template match="am:card">
		<fo:table-row>
			<xsl:if test="position() mod 2 != 0">
				<xsl:attribute name="background-color">#E0E0E0</xsl:attribute>
			</xsl:if>
			<xsl:apply-templates/>
		</fo:table-row>
	</xsl:template>
	<xsl:template match="db:Name">
		<fo:table-cell id="{../@xml:id}">
			<fo:block>
				<fo:basic-link external-destination="{$cgi}{../@xml:id}">
					<xsl:value-of select="substring(., 1, string-length() - string-length(substring-after(string(current()[contains(text(),' / ')]), substring-before(current()[contains(text(),' / ')], ' / '))))"/>
				</fo:basic-link>
				<xsl:if test="contains(., ' / ')">
					<xsl:text> / </xsl:text>
					<fo:basic-link external-destination="{$cgi}{//am:card[substring-before(db:Name, ' / ') = substring-after(current(), ' / ')]/@xml:id}">
						<xsl:value-of select="substring-after(., '/ ')"/>
					</fo:basic-link>
				</xsl:if>
			</fo:block>
		</fo:table-cell>
	</xsl:template>
	<xsl:template match="db:Type | db:Rarity | db:Series | db:Set">
		<fo:table-cell>
			<fo:block>
				<xsl:apply-templates/>
			</fo:block>
		</fo:table-cell>
	</xsl:template>
</xsl:stylesheet>