<!-- Written by J. David Eisenberg 23 Mar 2005 -->                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
<xsl:stylesheet
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
	xmlns:redirect="org.apache.xalan.xslt.extensions.Redirect"
	xmlns:xalan="http://xml.apache.org/xalan"
	xmlns:img="info.evccit.utils"
    extension-element-prefixes="xalan redirect img">

<!-- note: added the xmlns:xalan namespace -->

<xsl:output
	method="xml"
	indent="yes"
	omit-xml-declaration="yes"
	doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
	doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
	/>


<xsl:param name="perPage" select="'4'"/>

<!--
	Create a variable that is a copy of all the <animal>
	elements in sorted order.  This is a "result tree fragment":
	the word "result" means it is ready to go into the OUTPUT document.
-->
<xsl:variable name="sorted">
	<xsl:for-each select="/pig-rescue/animal">
		<xsl:sort select="name"/>
		<xsl:copy-of select="."/>
	</xsl:for-each>
</xsl:variable>

<!--
	Using the xalan:nodeset() function, we convert the result
	tree fragment into a node set, which makes it look as if it
	came from the input document
-->
<xsl:variable name="sortedNodes" select="xalan:nodeset($sorted)"/>

<xsl:template match="pig-rescue">
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<link rel="stylesheet" type="text/css" href="bdr.css" />
    <title>The Pigs</title>
</head>
<body>
<div align="left">
<a href="index.html"><img src="images/bellydraggersranch_th.gif"
	alt="Belly Draggers Ranch Banner" title="Belly Draggers Ranch Logo"
	width="225" height="48" border="0"/></a>
</div>
<div align="center">
<h1>The Pigs At Belly Draggers Ranch</h1>
</div>
<div id="navig2">
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about_us.html">About Us</a></li>
<li><a href="history.html">History</a></li>
<li class="thislink">The Pigs</li>
<li><a href="adoption.html">Adoption</a></li>
<li><a href="sponsors.html">Sponsors</a></li>
<li><a href="how_you_can_help.html">How You Can Help</a></li>
<li><a href="whats_new.html">What’s New?</a></li>
</ul>
</div>

<ul>
	<xsl:apply-templates select="$sortedNodes/animal[position() mod
		$perPage = 1]" mode="collect"/>
</ul>


<div align="center">
<img src="images/bluebar1.gif" alt="blue bar horizontal rule" />
</div>

<p>
These pages were generated using an XML file and an XSLT stylesheet created  
by <a href="http://www.catcode.com">David Eisenberg</a>, <a href="http://www.evc-cit.info/">Computer 
and Information Technology</a> Instructor at<a href="http://www.evc.edu/"> Evergreen 
Valley College</a>. <small>If you are interested, the XML file can be viewed <a href="xmlfile.html">here</a> and the XSLT stylesheet used to transform the XML file can be viewed <a href="xslfile.html">here</a>.
This is really technical stuff and not for the faint of heart.</small>
</p>
  

</body>
</html>
</xsl:template>

<!-- this template is in "collect" mode; it collects every Nth
	animal for the index page -->
<xsl:template match="animal" mode="collect">
	<xsl:variable name="start" select="(position()-1)*$perPage + 1"/>
	
	<!-- The choose figures out the right ending number.
		If the start number plus per page is greater than the total
		number of animals, then the $end variable is set to the total
		number of animals. Otherwise, $end is set to the $start plus
		$perPage -1 -->
	<xsl:variable name="end">
		<xsl:choose>
			<xsl:when test="$start + $perPage > count(../animal)">
				<xsl:value-of select="count(../animal)"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$start + $perPage - 1"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	
	<!-- we have to construct the filename in a variable so that
		redirect works properly; it doesn't like curly brace
		notation -->
	<xsl:variable name="filename">
		<xsl:text>animals</xsl:text>
		<xsl:value-of select="position()"/>
		<xsl:text>.html</xsl:text>
	</xsl:variable>

	<li><xsl:call-template name="listNames">
		<xsl:with-param name="start" select="$start"/>
		<xsl:with-param name="end" select="$end"/>
		<xsl:with-param name="filename" select="$filename"/>
	</xsl:call-template></li>
	
	<xsl:call-template name="makeSubfile">
		<xsl:with-param name="start" select="$start"/>
		<xsl:with-param name="end" select="$end"/>
		<xsl:with-param name="filename" select="$filename"/>
	</xsl:call-template>
</xsl:template>

<!-- This template is in "display" mode; it's used to
	display all the siblings of the guys we collected -->

<xsl:template match="animal" mode="display">
	<xsl:param name="start"/>
	<xsl:variable name="bigFile"
		select="concat(substring-before(picture/file,'_th.jpg'), '.jpg')"/>
	<xsl:variable name="basePath" select="/pig-rescue/image-base"/>
	<xsl:variable name="dimensions"
		select="img:ImageSize.getDimensions($basePath, picture/file)"/>
	<div id="{concat('a',$start+position()-1)}">
       <table border="0" width="100%">
        <tr valign="top">
        <td style="border-right: 1px solid gray;" width="45%">
        <div align="center">
		<a href="{$basePath}/{$bigFile}"><img
			src="{$basePath}/{picture/file}" 
			alt="{picture/description}"
			width="{$dimensions/@width}"
			height="{$dimensions/@height}"
			title="{picture/caption}"
			hspace="4" /></a><br />

        <h3 align="center"><xsl:value-of select="name"/></h3>
		</div>
               
        <div><span class="subhead">Status: </span>
			<xsl:apply-templates select="gender"/></div>
        <div><span class="subhead">Born: </span>
			<xsl:value-of select="birth"/></div>
		<xsl:if test="death">
			<div><span class="subhead">Died: </span>
				<xsl:value-of select="death"/></div>
		</xsl:if>
        <div><span class="subhead">In: </span>
			<xsl:value-of select="in-date"/></div>
        <div><span class="subhead">From: </span>
			<xsl:value-of select="from"/></div>
		<xsl:if test="sponsor">
        	<div><span class="subhead">Sponsored By: </span>
				<xsl:value-of select="sponsor"/></div>
		</xsl:if>
		<xsl:if test="adopted">
        	<div><span class="subhead">Adopted By: </span>
				<xsl:value-of select="adopted"/></div>
		</xsl:if>
        </td>
        
        <td width="55%">
        <p class="info">
			<xsl:apply-templates select="info"/>
       </p>
	   		<xsl:if test="update">
			<div><span class="subhead">Update <xsl:apply-templates select="update_date"/>:</span></div>
			<p class="update">
				<xsl:value-of select="update"/>
			</p>
			</xsl:if>
	   	
        <xsl:apply-templates select="medical"/>
        <xsl:if test="medical">
        <a>
           <xsl:attribute name="href">javascript:popup('<xsl:value-of select="@id"/>');</xsl:attribute>
        <xsl:value-of select="name"/><xsl:text>’</xsl:text>
        <xsl:if test="substring(name,string-length(name))!='s'">
                <xsl:text>s</xsl:text>
        </xsl:if>
        Medications</a><br />
        </xsl:if>
       </td>
       </tr>
       </table>
	</div>
<hr />
</xsl:template>

<xsl:template match="medical">
    <xsl:variable name="filename">page_<xsl:value-of select="../@id"/>.html</xsl:variable>    
    <redirect:write select="$filename">
    <html>
    <head>
    <title>Medical file for <xsl:value-of select="../name"/></title>
    </head>
    <body>
    <h3>Medical File for <xsl:value-of select="../name"/></h3>
    <table border="1">
        <tr>
            <th>Condition</th>
            <th>Medication</th>
            <th>Dosage</th>
        </tr>
        <xsl:apply-templates select="condition"/>
    </table>    
    </body>
    </html>
    </redirect:write>
</xsl:template>

<xsl:template match="condition">
        <tr>    
            <td><xsl:value-of select="."/></td>
            <td><xsl:value-of select="following-sibling::med[1]"/></td>
            <td><xsl:value-of select="following-sibling::dosage[1]"/></td>
        </tr>    
</xsl:template>

<xsl:template match="gender">
<xsl:choose>
	<xsl:when test=".='M'">
		Male
		<xsl:if test="@spay-neuter='yes'"> (Neutered)</xsl:if>
	</xsl:when>
	<xsl:when test=".='F'">
		Female
		<xsl:if test="@spay-neuter='yes'"> (Spayed)</xsl:if>
	</xsl:when>
</xsl:choose>
</xsl:template>

<!-- This template lists the animal names on a page -->
<xsl:template name="listNames">
	<xsl:param name="start"/>
	<xsl:param name="end"/>
	<xsl:param name="filename"/>
	<xsl:for-each select="$sortedNodes/animal[position() >= $start and position() <= $end]">
		<xsl:variable name="url">
			<xsl:value-of select="$filename"/>
			<xsl:text>#a</xsl:text>
			<xsl:value-of select="$start+position()-1"/>
		</xsl:variable>
		<a href="{$url}"><xsl:value-of select="name"/></a>
		<xsl:choose>
			<xsl:when test="($start+position() < $end)
				and ($end - $start > 1)">
			<xsl:text>, </xsl:text>
			</xsl:when>
			<xsl:when test="($start+position() = $end)
				and ($end - $start >= 2)">
				<xsl:text>, and </xsl:text>
				</xsl:when>
			<xsl:when test="($start+position() = $end)
				and ($end - $start = 1)">
				<xsl:text> and </xsl:text>
			</xsl:when>
			</xsl:choose>
	</xsl:for-each>
</xsl:template>


<!-- This template creates the subfiles.
	It needs the $start and $end parameters so that it can create
	an accurate <title> element, and it needs $filename so
	it can open up the file in the first place. -->
<xsl:template name="makeSubfile">
	<xsl:param name="start"/>
	<xsl:param name="end"/>
	<xsl:param name="filename"/>

	<!-- calculate this once, for use in next/back links -->	
	<xsl:variable name="currentPage"
		select="(($start - 1) div $perPage) + 1"/>

	<!-- use <redirect:write> to open up a file and send output to it -->
	<redirect:write select="$filename">
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<link rel="stylesheet" type="text/css" href="bdr.css" />
    <title>Animals <xsl:value-of select="$start"/> - <xsl:value-of select="$end"/> (Page <xsl:value-of select="$currentPage"/>)</title>
<script type="text/javascript">
var windowPtr;
function popup( whichPage )
{
	windowPtr = window.open(
		"page_" + whichPage + ".html",
		"popupWindow",
		"width=600,height=200,resizable=yes,scrollbars=yes");
	windowPtr.focus();
}
</script>
</head>
<body>
<div align="left">
<a href="index.html"><img src="images/bellydraggersranch_th.gif"
    width="225" height="48" border="0" 
    alt="Belly Draggers Ranch Banner"
    title="Belly Draggers Ranch Logo" /></a>
</div>
<div align="center">
<h1>Animals <xsl:value-of select="$start"/> - <xsl:value-of select="$end"/>
	(Page <xsl:value-of select="$currentPage"/>)</h1>
</div>
		<xsl:apply-templates select="self::animal | 
			following-sibling::animal[position() < $perPage]" mode="display">
			<xsl:with-param name="start" select="$start"/>
		</xsl:apply-templates>

<table border="0" width="100%">
<tr>
	<td width="33%"> <!-- "previous" link -->
		<xsl:if test="$start != 1">
			<a href="animals{$currentPage - 1}.html"><< Page <xsl:value-of select="$currentPage - 1"/></a>
		</xsl:if>
	</td>
	<td width="33%" align="center"><a href="the_pigs.html">Index</a></td>
	<td width="33%" align="right"> <!-- "next" link -->
		<xsl:if test="$end != count(/pig-rescue/animal)">
			<a href="animals{$currentPage + 1}.html">Page <xsl:value-of select="$currentPage + 1"/> >></a>
		</xsl:if>	
	</td>
</tr>
</table>
</body>
</html>
</redirect:write>

</xsl:template>
</xsl:stylesheet>