<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:saxon="http://icl.com/saxon"
                xmlns:lxslt="http://xml.apache.org/xslt"
                xmlns:xalanredirect="org.apache.xalan.xslt.extensions.Redirect"
                xmlns:exsl="http://exslt.org/common"
                xmlns:html="http://www.w3.org/1999/xhtml"
                exclude-result-prefixes="html exsl"
                extension-element-prefixes="saxon xalanredirect lxslt exsl"
                version="1.0">

<!--
# htmltree - produce a set of static HTML pages from Gallery XML files
#
# Version 1.2
#
# $Id: htmltree.xsl,v 1.8 2002/09/18 11:53:05 ndw Exp $
#
# Copyright (C) 2002 Norman Walsh, All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 dated June, 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
-->

<xsl:param name="css" select="'../album.css'"/>
<xsl:param name="base" select="'/online/www/albums/'"/>
<xsl:param name="indexname" select="'index'"/>
<xsl:param name="html.ext" select="'.html'"/>

<xsl:param name="image.nav.first" select="'../images/nav_first.gif'"/>
<xsl:param name="image.nav.last" select="'../images/nav_last.gif'"/>
<xsl:param name="image.nav.next" select="'../images/nav_next.gif'"/>
<xsl:param name="image.nav.prev" select="'../images/nav_prev.gif'"/>
<xsl:param name="image.nav.home" select="'../images/nav_home.gif'"/>

<xsl:param name="image.nav.adjacent.width" select="72"/>
<xsl:param name="image.nav.end.width" select="27"/>

<xsl:param name="image.nav.dot" select="'../images/nav_dot.gif'"/>
<xsl:param name="image.nav.dot.left" select="'../images/nav_dot_left.gif'"/>
<xsl:param name="image.nav.dot.right" select="'../images/nav_dot_right.gif'"/>
<xsl:param name="image.nav.dot.width" select="8"/>

<!-- 20020905 CAL: added border parameter and a few instances of it
in the output; also made all header tables have cellpadding=0 -->
<xsl:param name="border" select="'1'"/>

<xsl:output method="html"/>

<xsl:strip-space elements="*"/>

<xsl:key name="albums" match="/albumTree/album" use="name"/>

<xsl:template match="albumTree">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="album">
  <xsl:variable name="numItems" select="count(.//nestedAlbum|.//photo)"/>
  <xsl:variable name="itemsPerPage" select="rows * cols"/>
  <xsl:variable name="numPages"
                select="floor(($numItems + $itemsPerPage - 1) div $itemsPerPage)"/>

  <xsl:call-template name="write.album.pages">
    <xsl:with-param name="itemsPerPage" select="$itemsPerPage"/>
    <xsl:with-param name="numPages" select="$numPages"/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="write.album.pages">
  <xsl:param name="itemsPerPage" select="9"/>
  <xsl:param name="numPages" select="1"/>
  <xsl:param name="curPage" select="1"/>

  <xsl:apply-templates select="." mode="pages">
    <xsl:with-param name="curPage" select="$curPage"/>
    <xsl:with-param name="numPages" select="$numPages"/>
    <xsl:with-param name="itemsPerPage" select="$itemsPerPage"/>
  </xsl:apply-templates>

  <xsl:if test="$curPage &lt; $numPages">
    <xsl:call-template name="write.album.pages">
      <xsl:with-param name="itemsPerPage" select="$itemsPerPage"/>
      <xsl:with-param name="numPages" select="$numPages"/>
      <xsl:with-param name="curPage" select="$curPage + 1"/>
    </xsl:call-template>
  </xsl:if>

<!--
  <xsl:message>
    <xsl:value-of select="title"/>
    <xsl:text>: items </xsl:text>
    <xsl:value-of select="$numItems"/>
    <xsl:text>, per page </xsl:text>
    <xsl:value-of select="$itemsPerPage"/>
    <xsl:text>, num pages </xsl:text>
    <xsl:value-of select="$numPages"/>
  </xsl:message>
-->
</xsl:template>

<xsl:template match="album" mode="pages">
  <xsl:param name="curPage" select="1"/>
  <xsl:param name="numPages" select="1"/>
  <xsl:param name="itemsPerPage" select="9"/>

  <xsl:variable name="filename">
    <xsl:value-of select="$base"/>
    <xsl:value-of select="name"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="$indexname"/>
    <xsl:if test="$curPage &gt; 1">
      <xsl:text>-</xsl:text>
      <xsl:value-of select="$curPage"/>
    </xsl:if>
    <xsl:value-of select="$html.ext"/>
  </xsl:variable>

  <xsl:variable name="parentName" select="parentAlbumName"/>

  <xsl:message>Writing <xsl:value-of select="$filename"/></xsl:message>

  <xsl:call-template name="write.chunk">
    <xsl:with-param name="filename" select="$filename"/>
    <xsl:with-param name="content">
      <html>
        <head>
          <title>
            <xsl:value-of select="title"/>
          </title>
          <link rel="stylesheet" type="text/css" href="{$css}"/>
        </head>
        <body>
          <h1><xsl:value-of select="title"/></h1>
          <table class="header" summary="Header" width="100%"
                 border="{$border}" cellspacing="0">
            <tr>
              <td colspan="5">
                <xsl:value-of select="count(.//nestedAlbum|.//photo)"/>
                <xsl:text> items in this album</xsl:text>
                <xsl:if test="$numPages &gt; 1">
                  <xsl:text> on </xsl:text>
                  <xsl:value-of select="$numPages"/>
                  <xsl:text> pages</xsl:text>
                </xsl:if>
              </td>
            </tr>

            <xsl:if test="parentAlbumName and /albumTree/album[@name=$parentName]">
              <tr>
                <td align="right" colspan="5">
                  <xsl:apply-templates select="." mode="showParents"/>
                </td>
              </tr>
            </xsl:if>

            <xsl:apply-templates select="." mode="navigation">
              <xsl:with-param name="curPage" select="$curPage"/>
              <xsl:with-param name="numPages" select="$numPages"/>
            </xsl:apply-templates>
          </table>

          <xsl:variable name="first" select="($curPage - 1) * $itemsPerPage"/>
          <xsl:variable name="last" select="$curPage * $itemsPerPage"/>
          <xsl:variable name="cols" select="number(cols)"/>

          <xsl:variable name="row-start-images"
                        select=".//nestedAlbum[position() &gt; $first
                                               and position() &lt;= $last
                                               and position() mod $cols = 1]
                                |.//photo[position() &gt; $first
                                          and position() &lt;= $last
                                          and position() mod $cols = 1]"/>

          <table width="100%" border="0" cellpadding="5" cellspacing="5"
                 summary="Album items">
            <xsl:for-each select="$row-start-images">
              <tr>
                <xsl:choose>
                  <xsl:when test="local-name(.) = 'photo'">
                    <xsl:call-template name="photo-row">
                      <xsl:with-param name="photo" select="."/>
                      <xsl:with-param name="cols" select="$cols"/>
                    </xsl:call-template>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:call-template name="album-row">
                      <xsl:with-param name="album" select="."/>
                      <xsl:with-param name="cols" select="$cols"/>
                    </xsl:call-template>
                  </xsl:otherwise>
                </xsl:choose>
              </tr>
            </xsl:for-each>
          </table>

          <table class="header" summary="Header" width="100%"
                 border="{$border}" cellspacing="0">
            <xsl:apply-templates select="." mode="navigation">
              <xsl:with-param name="curPage" select="$curPage"/>
              <xsl:with-param name="numPages" select="$numPages"/>
            </xsl:apply-templates>
            <xsl:if test="parentAlbumName and /albumTree/album[@name=$parentName]">
              <tr>
                <td align="right" colspan="5">
                  <xsl:apply-templates select="." mode="showParents"/>
                </td>
              </tr>
            </xsl:if>
          </table>
        </body>
      </html>
    </xsl:with-param>
  </xsl:call-template>

  <xsl:apply-templates select=".//photo" mode="pages">
    <xsl:with-param name="itemsPerPage" select="$itemsPerPage"/>
  </xsl:apply-templates>

  <xsl:apply-templates select=".//photo" mode="pages">
    <xsl:with-param name="full" select="1"/>
    <xsl:with-param name="itemsPerPage" select="$itemsPerPage"/>
  </xsl:apply-templates>

  <xsl:apply-templates select=".//photo[exifData]" mode="properties"/>
</xsl:template>

<xsl:template match="photo" mode="pages">
  <xsl:param name="full" select="0"/>
  <xsl:param name="itemsPerPage" select="9"/>

  <xsl:variable name="filename">
    <xsl:value-of select="$base"/>
    <xsl:value-of select="../name"/>
    <xsl:text>/</xsl:text>
    <xsl:choose>
      <xsl:when test="$full != 0">
        <xsl:value-of select="image/name"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="image/resizedName"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:value-of select="$html.ext"/>
  </xsl:variable>

  <xsl:variable name="parentPage" select="floor((count(preceding-sibling::photo)
                                                +$itemsPerPage)
                                          div $itemsPerPage)"/>

  <xsl:variable name="parentPageName">
    <xsl:value-of select="$indexname"/>
    <xsl:if test="$parentPage &gt; 1">
      <xsl:text>-</xsl:text>
      <xsl:value-of select="$parentPage"/>
    </xsl:if>
    <xsl:value-of select="$html.ext"/>
  </xsl:variable>

  <!--
       <xsl:message>Writing <xsl:value-of select="$filename"/></xsl:message>
  -->

  <xsl:call-template name="write.chunk">
    <xsl:with-param name="filename" select="$filename"/>
    <xsl:with-param name="content">
      <html>
        <head>
          <title>
            <xsl:value-of select="../title"/>
            <xsl:text> :: </xsl:text>
            <xsl:value-of select="caption"/>
          </title>
          <link rel="stylesheet" type="text/css" href="{$css}"/>
        </head>
        <body>
          <h1><xsl:value-of select="../title"/></h1>
          <table class="header" summary="Header" width="100%"
                 border="{$border}" cellspacing="0">

            <xsl:if test="exifData">
              <tr>
                <td colspan="5" align="right">
                  <a href="#" onClick="javascript:nw=window.open('{image/name}.exif.html','Properties','height=500,width=500,location=no,scrollbars=yes,menubars=no,toolbars=no,resizable=yes');return false;">
                    <span>[Photo Properties]</span>
                  </a>
                </td>
              </tr>
            </xsl:if>

            <tr>
              <td align="right" colspan="5">
                <xsl:apply-templates select=".." mode="showParents"/>
                <a href="{$parentPageName}">
                  <xsl:value-of select="../title"/>
                </a>
                <xsl:text>&#160;</xsl:text>
                <a href="{$parentPageName}">
                  <img src="{$image.nav.home}" alt="[Up]" border="0"/>
                </a>
              </td>
            </tr>

            <xsl:apply-templates select="." mode="navigation">
              <xsl:with-param name="full" select="$full"/>
            </xsl:apply-templates>
          </table>

          <table width="100%" border="0" cellpadding="5" cellspacing="5"
                 summary="Item">
            <tr>
              <td align="center" valign="middle">
                <xsl:choose>
                  <xsl:when test="$full != 0">
                    <img src="{image/name}.{image/type}" alt="[Image]"/>
                  </xsl:when>
                  <xsl:otherwise>
                    <a href="{image/name}{$html.ext}">
                      <img src="{image/resizedName}.{image/type}"
                           alt="[Image]" border="0"/>
                    </a>
                  </xsl:otherwise>
                </xsl:choose>
              </td>
            </tr>
            <tr>
              <td align="center">
                <xsl:value-of select="caption"/>
              </td>
            </tr>
          </table>

          <table class="header" summary="Header" width="100%"
                 border="${border}" cellspacing="0">
            <xsl:apply-templates select="." mode="navigation">
              <xsl:with-param name="full" select="$full"/>
            </xsl:apply-templates>
            <tr>
              <td align="right" colspan="5">
                <xsl:apply-templates select=".." mode="showParents"/>
                <a href="{$parentPageName}">
                  <xsl:value-of select="../title"/>
                </a>
                <xsl:text>&#160;</xsl:text>
                <a href="{$parentPageName}">
                  <img src="{$image.nav.home}" alt="[Up]" border="0"/>
                </a>
              </td>
            </tr>
          </table>
        </body>
      </html>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template match="photo" mode="properties">
  <xsl:variable name="filename">
    <xsl:value-of select="$base"/>
    <xsl:value-of select="../name"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="image/name"/>
    <xsl:text>.exif</xsl:text>
    <xsl:value-of select="$html.ext"/>
  </xsl:variable>

  <!--
       <xsl:message>Writing <xsl:value-of select="$filename"/></xsl:message>
  -->

  <xsl:call-template name="write.chunk">
    <xsl:with-param name="filename" select="$filename"/>
    <xsl:with-param name="content">
      <html>
        <head>
          <title>
            <xsl:value-of select="caption"/>
            <xsl:text> :: Photo Properties</xsl:text>
          </title>
          <link rel="stylesheet" type="text/css" href="{$css}"/>
        </head>
        <body>
          <table summary="Properties table" width="100%" border="0">
            <tr>
              <td colspan="2" align="center">Photo Properties</td>
            </tr>
            <tr>
              <td colspan="2" align="center">
                <img src="{thumbnail/name}.{thumbnail/type}" alt="[Image]"/>
              </td>
            </tr>
            <tr>
              <td colspan="2" align="center">
                <xsl:value-of select="caption"/>
              </td>
            </tr>

            <tr><td colspan="2">&#160;</td></tr>

            <xsl:variable name="exifHalf" select="floor(count(exifData/exif) div 2)"/>

            <xsl:for-each select="exifData/exif[position() &lt;= $exifHalf]">
              <tr>
                <td>
                  <span class="exif-label">
                    <xsl:value-of select="@field"/>
                  </span>
                  <span class="exif-sep">
                    <xsl:text>: </xsl:text>
                  </span>
                  <span class="exif-value">
                    <xsl:value-of select="."/>
                  </span>
                </td>
                <td>
                  <span class="exif-label">
                    <xsl:value-of select="following-sibling::exif[$exifHalf]/@field"/>
                  </span>
                  <span class="exif-sep">
                    <xsl:text>: </xsl:text>
                  </span>
                  <span class="exif-value">
                    <xsl:value-of select="following-sibling::exif[$exifHalf]"/>
                  </span>
                </td>
              </tr>
            </xsl:for-each>

            <tr><td colspan="2">&#160;</td></tr>

            <tr>
              <td colspan="2" align="center">
                <span class="upload-label">File upload date</span>
                <span class="upload-sep">: </span>
                <span class="upload-value">
                  <xsl:value-of select="uploadDate/day"/>
                  <xsl:text> </xsl:text>
                  <xsl:call-template name="month.name">
                    <xsl:with-param name="month" select="uploadDate/month"/>
                  </xsl:call-template>
                  <xsl:text> </xsl:text>
                  <xsl:value-of select="uploadDate/year"/>
                </span>
              </td>
            </tr>

            <tr>
              <td colspan="2" align="center">
                <span class="capture-label">Item capture date</span>
                <span class="capture-sep">: </span>
                <span class="capture-value">
                  <xsl:value-of select="itemCaptureDate/day"/>
                  <xsl:text> </xsl:text>
                  <xsl:call-template name="month.name">
                    <xsl:with-param name="month" select="itemCaptureDate/month"/>
                  </xsl:call-template>
                  <xsl:text> </xsl:text>
                  <xsl:value-of select="itemCaptureDate/year"/>
                </span>
              </td>
            </tr>

            <xsl:if test="keywords">
              <tr>
                <td colspan="2" align="center">
                  <span class="keyword-label">Keywords</span>
                  <span class="keyword-sep">: </span>
                  <span class="keyword-value">
                    <xsl:value-of select="keywords"/>
                  </span>
                </td>
              </tr>
            </xsl:if>

            <tr><td colspan="2">&#160;</td></tr>

            <tr>
              <td colspan="2" align="center">
                <form action="#">
                  <input type="submit" value="Done" onclick='parent.close()'/>
                </form>
              </td>
            </tr>
          </table>
        </body>
      </html>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template match="photo" mode="navigation">
  <xsl:param name="full" select="0"/>

  <xsl:variable name="prev.filename">
    <xsl:choose>
      <xsl:when test="$full != 0">
        <xsl:value-of select="preceding-sibling::photo[1]/image/name"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="preceding-sibling::photo[1]/image/resizedName"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:value-of select="$html.ext"/>
  </xsl:variable>

  <xsl:variable name="next.filename">
    <xsl:choose>
      <xsl:when test="$full != 0">
        <xsl:value-of select="following-sibling::photo[1]/image/name"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="following-sibling::photo[1]/image/resizedName"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:value-of select="$html.ext"/>
  </xsl:variable>

  <xsl:variable name="first.filename">
    <xsl:choose>
      <xsl:when test="$full != 0">
        <xsl:value-of select="preceding-sibling::photo[last()]/image/name"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="preceding-sibling::photo[last()]/image/resizedName"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:value-of select="$html.ext"/>
  </xsl:variable>

  <xsl:variable name="last.filename">
    <xsl:choose>
      <xsl:when test="$full != 0">
        <xsl:value-of select="following-sibling::photo[last()]/image/name"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="following-sibling::photo[last()]/image/resizedName"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:value-of select="$html.ext"/>
  </xsl:variable>

  <xsl:if test="preceding-sibling::photo or following-sibling::photo">
    <tr>
      <xsl:choose>
        <xsl:when test="not(preceding-sibling::photo)">
          <td colspan="3" align="center">
            <xsl:value-of select="count(preceding-sibling::photo)+1"/>
            <xsl:text> (of </xsl:text>
            <xsl:value-of select="count(../photo)"/>
            <xsl:text>)</xsl:text>
          </td>
          <td align="center" width="{$image.nav.adjacent.width}">
            <a href="{$next.filename}">
              <img src="{$image.nav.next}" alt="Next" border="0"/>
            </a>
          </td>
          <td align="center" width="{$image.nav.end.width}">
            <a href="{$last.filename}">
              <img src="{$image.nav.last}" alt="Last" border="0"/>
            </a>
          </td>
        </xsl:when>
        <xsl:when test="not(following-sibling::photo)">
          <td align="center" width="{$image.nav.end.width}">
            <a href="{$first.filename}">
              <img src="{$image.nav.first}" alt="First" border="0"/>
            </a>
          </td>
          <td align="center" width="{$image.nav.adjacent.width}">
            <a href="{$prev.filename}">
              <img src="{$image.nav.prev}" alt="Prev" border="0"/>
            </a>
          </td>
          <td colspan="3" align="center">
            <xsl:value-of select="count(preceding-sibling::photo)+1"/>
            <xsl:text> (of </xsl:text>
            <xsl:value-of select="count(../photo)"/>
            <xsl:text>)</xsl:text>
          </td>
        </xsl:when>
        <xsl:otherwise>
          <td align="center" width="{$image.nav.end.width}">
            <a href="{$first.filename}">
              <img src="{$image.nav.first}" alt="First" border="0"/>
            </a>
          </td>
          <td align="center" width="{$image.nav.adjacent.width}">
            <a href="{$prev.filename}">
              <img src="{$image.nav.prev}" alt="Prev" border="0"/>
            </a>
          </td>
          <td align="center">
            <xsl:value-of select="count(preceding-sibling::photo)+1"/>
            <xsl:text> (of </xsl:text>
            <xsl:value-of select="count(../photo)"/>
            <xsl:text>)</xsl:text>
          </td>
          <td align="center" width="{$image.nav.adjacent.width}">
            <a href="{$next.filename}">
              <img src="{$image.nav.next}" alt="Next" border="0"/>
            </a>
          </td>
          <td align="center" width="{$image.nav.end.width}">
            <a href="{$last.filename}">
              <img src="{$image.nav.last}" alt="Last" border="0"/>
            </a>
          </td>
        </xsl:otherwise>
      </xsl:choose>
    </tr>
  </xsl:if>
</xsl:template>

<xsl:template match="album" mode="navigation">
  <xsl:param name="curPage" select="1"/>
  <xsl:param name="numPages" select="1"/>

  <xsl:variable name="prev.filename">
    <xsl:value-of select="$indexname"/>
    <xsl:if test="$curPage &gt; 2">
      <xsl:text>-</xsl:text>
      <xsl:value-of select="$curPage - 1"/>
    </xsl:if>
    <xsl:value-of select="$html.ext"/>
  </xsl:variable>

  <xsl:variable name="next.filename">
    <xsl:value-of select="$indexname"/>
    <xsl:text>-</xsl:text>
    <xsl:value-of select="$curPage + 1"/>
    <xsl:value-of select="$html.ext"/>
  </xsl:variable>

  <xsl:variable name="first.filename">
    <xsl:value-of select="$indexname"/>
    <xsl:value-of select="$html.ext"/>
  </xsl:variable>

  <xsl:variable name="last.filename">
    <xsl:value-of select="$indexname"/>
    <xsl:if test="$numPages &gt; 1">
      <xsl:text>-</xsl:text>
      <xsl:value-of select="$numPages"/>
    </xsl:if>
    <xsl:value-of select="$html.ext"/>
  </xsl:variable>

  <xsl:if test="$numPages &gt; 1">
    <tr>
      <xsl:choose>
        <xsl:when test="$curPage = 1">
          <td colspan="3" align="center">
            <xsl:call-template name="page.map">
              <xsl:with-param name="curPage" select="$curPage"/>
              <xsl:with-param name="numPages" select="$numPages"/>
            </xsl:call-template>
          </td>
          <td align="center" width="{$image.nav.adjacent.width}">
            <a href="{$next.filename}">
              <img src="{$image.nav.next}" alt="Next" border="0"/>
            </a>
          </td>
          <td align="center" width="{$image.nav.end.width}">
            <a href="{$last.filename}">
              <img src="{$image.nav.last}" alt="Last" border="0"/>
            </a>
          </td>
        </xsl:when>
        <xsl:when test="$curPage = $numPages">
          <td align="center" width="{$image.nav.end.width}">
            <a href="{$first.filename}">
              <img src="{$image.nav.first}" alt="First" border="0"/>
            </a>
          </td>
          <td align="center" width="{$image.nav.adjacent.width}">
            <a href="{$prev.filename}">
              <img src="{$image.nav.prev}" alt="Prev" border="0"/>
            </a>
          </td>
          <td colspan="3" align="center">
            <xsl:call-template name="page.map">
              <xsl:with-param name="curPage" select="$curPage"/>
              <xsl:with-param name="numPages" select="$numPages"/>
            </xsl:call-template>
          </td>
        </xsl:when>
        <xsl:otherwise>
          <td align="center" width="{$image.nav.end.width}">
            <a href="{$first.filename}">
              <img src="{$image.nav.first}" alt="First" border="0"/>
            </a>
          </td>
          <td align="center" width="{$image.nav.adjacent.width}">
            <a href="{$prev.filename}">
              <img src="{$image.nav.prev}" alt="Prev" border="0"/>
            </a>
          </td>
          <td align="center">
            <xsl:call-template name="page.map">
              <xsl:with-param name="curPage" select="$curPage"/>
              <xsl:with-param name="numPages" select="$numPages"/>
            </xsl:call-template>
          </td>
          <td align="center" width="{$image.nav.adjacent.width}">
            <a href="{$next.filename}">
              <img src="{$image.nav.next}" alt="Next" border="0"/>
            </a>
          </td>
          <td align="center" width="{$image.nav.end.width}">
            <a href="{$last.filename}">
              <img src="{$image.nav.last}" alt="Last" border="0"/>
            </a>
          </td>
        </xsl:otherwise>
      </xsl:choose>
    </tr>
  </xsl:if>
</xsl:template>

<xsl:template name="page.map">
  <xsl:param name="thisPage" select="1"/>
  <xsl:param name="curPage" select="1"/>
  <xsl:param name="numPages" select="1"/>

  <xsl:variable name="filename">
    <xsl:value-of select="$indexname"/>
    <xsl:if test="$thisPage &gt; 1">
      <xsl:text>-</xsl:text>
      <xsl:value-of select="$thisPage"/>
    </xsl:if>
    <xsl:value-of select="$html.ext"/>
  </xsl:variable>

  <xsl:choose>
    <xsl:when test="$thisPage = $curPage">
      <img src="{$image.nav.dot.left}" alt=" - "/>
      <b>
        <xsl:value-of select="$thisPage"/>
      </b>
    </xsl:when>
    <xsl:when test="$thisPage = $curPage + 1">
      <img src="{$image.nav.dot.right}" alt=" - "/>
      <a href="{$filename}">
        <xsl:value-of select="$thisPage"/>
      </a>
    </xsl:when>
    <xsl:otherwise>
      <img src="{$image.nav.dot}" alt=" . "/>
      <a href="{$filename}">
        <xsl:value-of select="$thisPage"/>
      </a>
    </xsl:otherwise>
  </xsl:choose>

  <xsl:choose>
    <xsl:when test="$thisPage = $numPages">
      <xsl:choose>
        <xsl:when test="$thisPage = $curPage">
          <img src="{$image.nav.dot.right}" alt=" - "/>
        </xsl:when>
        <xsl:otherwise>
          <img src="{$image.nav.dot}" alt=" . "/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="page.map">
        <xsl:with-param name="thisPage" select="$thisPage + 1"/>
        <xsl:with-param name="curPage" select="$curPage"/>
        <xsl:with-param name="numPages" select="$numPages"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="album-row">
  <xsl:param name="album" select="."/>
  <xsl:param name="cols" select="1"/>
  <xsl:param name="curCol" select="1"/>

  <xsl:variable name="albumNode" select="/albumTree/album[@name=$album]"/>

  <xsl:choose>
    <xsl:when test="$album">
      <td align="center" valign="top" width="{floor(100 div $cols)}%">
        <a href="../{$albumNode/@name}/{$indexname}{$html.ext}">
          <xsl:apply-templates select="$albumNode" mode="album-highlight"/>
        </a>
        <span class="album-title">
          <br/>
          <span class="album-title-label">Album</span>
          <span class="album-title-sep">: </span>
          <a href="../{$albumNode/@name}/{$indexname}{$html.ext}">
            <xsl:value-of select="$albumNode/title"/>
          </a>
        </span>

        <xsl:if test="$albumNode/description != 'No description'">
          <span class="album-description">
            <br/>
            <xsl:value-of select="$albumNode/description"/>
          </span>
        </xsl:if>

        <span class="album-changed">
          <br/>
          <br/>
          <span class="album-changed-label">Changed: </span>
          <xsl:value-of select="$albumNode/last_mod_time/day"/>
          <xsl:text> </xsl:text>
          <xsl:call-template name="month.name">
            <xsl:with-param name="month" select="$albumNode/last_mod_time/month"/>
          </xsl:call-template>
          <xsl:text> </xsl:text>
          <xsl:value-of select="$albumNode/last_mod_time/year"/>
        </span>
        <span class="album-contains">
          <br/>
          <span class="album-contains-label">Contains: </span>
          <xsl:value-of select="count($albumNode//photo)
                                + count($albumNode//nestedAlbum)"/>
          <xsl:text> items</xsl:text>
        </span>
      </td>
    </xsl:when>
    <xsl:otherwise>
      <td  width="{floor(100 div $cols)}%">
        <xsl:text>&#160;</xsl:text>
      </td>
    </xsl:otherwise>
  </xsl:choose>

  <xsl:if test="$curCol &lt; $cols">
    <xsl:call-template name="album-row">
      <xsl:with-param name="album" select="$album/following-sibling::nestedAlbum[1]"/>
      <xsl:with-param name="cols" select="$cols"/>
      <xsl:with-param name="curCol" select="$curCol + 1"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<xsl:template match="album" mode="album-highlight">
  <xsl:choose>
    <xsl:when test="photo[@highlight='1']">
      <img alt="[Image]" border="0">
        <xsl:attribute name="src">
          <xsl:text>../</xsl:text>
          <xsl:value-of select="@name"/>
          <xsl:text>/</xsl:text>
          <xsl:value-of select="photo[@highlight='1']/thumbnail/name"/>
          <xsl:text>.</xsl:text>
          <xsl:value-of select="photo[@highlight='1']/thumbnail/type"/>
        </xsl:attribute>
      </img>
    </xsl:when>
    <xsl:when test="nestedAlbum[@highlight='1']">
      <xsl:variable name="hlAlbum" select="nestedAlbum[@highlight='1'][1]"/>
      <xsl:apply-templates select="/albumTree/album[@name=$hlAlbum]"
                           mode="album-highlight"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="fAlbum" select="nestedAlbum[1]"/>
      <xsl:apply-templates select="/albumTree/album[@name=$fAlbum]"
                           mode="album-highlight"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="photo-row">
  <xsl:param name="photo" select="."/>
  <xsl:param name="cols" select="1"/>
  <xsl:param name="curCol" select="1"/>

  <xsl:choose>
    <xsl:when test="$photo">
      <td align="center" valign="middle" width="{floor(100 div $cols)}%">
        <a href="{$photo/image/resizedName}{$html.ext}">
          <img alt="[Image]" border="0">
            <xsl:attribute name="src">
              <xsl:text>../</xsl:text>
              <xsl:value-of select="$photo/ancestor::album/@name"/>
              <xsl:text>/</xsl:text>
              <xsl:value-of select="$photo/thumbnail/name"/>
              <xsl:text>.</xsl:text>
              <xsl:value-of select="$photo/thumbnail/type"/>
            </xsl:attribute>
          </img>
        </a>
        <br/>
        <xsl:value-of select="$photo/caption"/>
      </td>
    </xsl:when>
    <xsl:otherwise>
      <td  width="{floor(100 div $cols)}%">
        <xsl:text>&#160;</xsl:text>
      </td>
    </xsl:otherwise>
  </xsl:choose>

  <xsl:if test="$curCol &lt; $cols">
    <xsl:call-template name="photo-row">
      <xsl:with-param name="photo" select="$photo/following-sibling::photo[1]"/>
      <xsl:with-param name="cols" select="$cols"/>
      <xsl:with-param name="curCol" select="$curCol + 1"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<!-- ====================================================================== -->

<xsl:template match="album" mode="showParents">
  <xsl:if test="parentAlbumName">
    <xsl:variable name="parentName" select="parentAlbumName"/>
    <xsl:variable name="parentAlbum" select="/albumTree/album[@name=$parentName]"/>

    <xsl:if test="$parentAlbum">
      <xsl:apply-templates select="$parentAlbum" mode="showParents"/>
      <a href="../{$parentAlbum/name}/{$indexname}{$html.ext}">
        <xsl:value-of select="$parentAlbum/title"/>
      </a>
      <xsl:text>&#160;</xsl:text>
      <a href="../{$parentAlbum/name}/{$indexname}{$html.ext}">
        <img src="{$image.nav.home}" alt="[Up]" border="0"/>
      </a>
      <xsl:text>&#160;</xsl:text>
    </xsl:if>
  </xsl:if>
</xsl:template>

<!-- ====================================================================== -->

<xsl:template name="month.name">
  <xsl:param name="month" select="1"/>
  <xsl:choose>
    <xsl:when test="number($month) = 1">Jan</xsl:when>
    <xsl:when test="number($month) = 2">Feb</xsl:when>
    <xsl:when test="number($month) = 3">Mar</xsl:when>
    <xsl:when test="number($month) = 4">Apr</xsl:when>
    <xsl:when test="number($month) = 5">May</xsl:when>
    <xsl:when test="number($month) = 6">Jun</xsl:when>
    <xsl:when test="number($month) = 7">Jul</xsl:when>
    <xsl:when test="number($month) = 8">Aug</xsl:when>
    <xsl:when test="number($month) = 9">Sep</xsl:when>
    <xsl:when test="number($month) = 10">Oct</xsl:when>
    <xsl:when test="number($month) = 11">Nov</xsl:when>
    <xsl:when test="number($month) = 12">Dec</xsl:when>
    <xsl:otherwise>[<xsl:value-of select="$month"/>]</xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- ====================================================================== -->

<xsl:template name="write.chunk">
  <xsl:param name="filename"/>
  <xsl:param name="content"/>

  <xsl:choose>
    <xsl:when test="element-available('exsl:document')">
      <exsl:document href="{$filename}"
                     method="html"
                     encoding="ISO-8859-1"
                     indent="yes">
        <xsl:copy-of select="$content"/>
      </exsl:document>
    </xsl:when>
    <xsl:when test="element-available('saxon:output')">
      <saxon:output href="{$filename}"
                    method="html"
                    encoding="ISO-8859-1"
                    indent="yes">
        <xsl:copy-of select="$content"/>
      </saxon:output>
    </xsl:when>

    <xsl:when test="element-available('xalanredirect:write')">
      <!-- Xalan uses xalanredirect -->
      <xalanredirect:write file="{$filename}">
        <xsl:copy-of select="$content"/>
      </xalanredirect:write>
    </xsl:when>

    <xsl:otherwise>
      <!-- it doesn't matter since we won't be making chunks... -->
      <xsl:message terminate="yes">
        <xsl:text>Can't make chunks with </xsl:text>
        <xsl:value-of select="system-property('xsl:vendor')"/>
        <xsl:text>'s processor.</xsl:text>
      </xsl:message>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>
