<?xml version="1.0" encoding="UTF-8"?>

<!-- 画像アルバムページ用 -->

<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://www.w3.org/1999/xhtml"
	xmlns:s="http://www.remus.dti.ne.jp/~a-satomi/"
	xmlns:js="http://www.remus.dti.ne.jp/~a-satomi/jsfunc"
	xmlns:ia="http://www.remus.dti.ne.jp/~a-satomi/imagealbum"
	xmlns:xalan="http://xml.apache.org/xalan"
	xmlns:redirect="http://xml.apache.org/xalan/redirect"
	extension-element-prefixes="redirect"
	exclude-result-prefixes="h s js ia xalan">

<xsl:import href="common.xsl"/>

<!-- ================ 出力設定 ================  -->

<xsl:output method="xml"
	encoding="Shift_JIS"
	omit-xml-declaration="no"
	doctype-public="-//W3C//DTD XHTML 1.1//EN"
	doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
	indent="yes"
	xalan:indent-amount="2"
	media-type="application/xhtml+xml"/>

<!-- ================ 各種設定 ================  -->

<!-- CSS ファイル名 -->
<xsl:param name="cssFile">imagealbum.css</xsl:param>

<!-- サブディレクトリ (仮想フルパス) -->
<xsl:param name="subDirPath" select="js:r2a($curFilePath, string(/h:html/ia:imageAlbum/ia:subdir/@path))"/>
<xsl:param name="imgDirPath" select="concat($subDirPath, 'images/')"/>
<xsl:param name="thumbDirPath" select="concat($subDirPath, 'thumbnails/')"/>

<!-- サブディレクトリ深度 (仮想ルート基準、相対パス記法) -->
<xsl:param name="subDirDepth" select="js:pathToGoUp(js:a2r($subDirPath, '/'))"/>

<!-- ページタイトル -->
<xsl:param name="pageTitle" select="string(/h:html/h:h1[1])"/>

<!-- ================ メインテンプレ ================  -->

<xsl:template match="/h:html">
	<html xml:lang="ja">
		<xsl:apply-templates select="h:head"/>
	<body class="indexPage">
		<xsl:apply-templates select="h:h1/@*"/>
		<xsl:call-template name="navi"/>
		<xsl:apply-templates select="h:h1"/>
		<xsl:call-template name="navi"/>
		<xsl:call-template name="footer"/>
	</body>
	</html>
</xsl:template>

<!-- ================ 各画像ページ生成 ================  -->

<xsl:template match="ia:imageAlbum">
	<ol>
		<xsl:apply-templates select="ia:images"/>
	</ol>
</xsl:template>

<xsl:template match="ia:image">
	<xsl:param name="imgNum" select="substring-before(@file, '.')"/>

	<li class="{@align}">
		<a title="Page {$imgNum}" href="{js:a2r($curFilePath, $subDirPath)}{$imgNum}.html">
			<img alt="Page {$imgNum}"
				height="{ancestor::ia:imageAlbum/ia:dim[@align = current()/@align]/ia:thumbs/@height}"
				width="{ancestor::ia:imageAlbum/ia:dim[@align = current()/@align]/ia:thumbs/@width}"
				src="{concat(js:a2r($curFilePath, $thumbDirPath), @file)}"/>
		</a>
	</li>

	<redirect:write select="concat($baseDir, $subDirPath, $imgNum, '.html')">
		<html xml:lang="ja">
			<xsl:call-template name="head_">
				<xsl:with-param name="imgNum" select="$imgNum"/>
			</xsl:call-template>
			<body>

				<xsl:call-template name="navi_">
					<xsl:with-param name="imgNum" select="$imgNum"/>
				</xsl:call-template>

				<div class="section">
					<h1><xsl:value-of select="concat($pageTitle, ' / Page ', $imgNum)"/></h1>
					<div class="picture">
						<img alt="{@file}"
							height="{ancestor::ia:imageAlbum/ia:dim[@align = current()/@align]/ia:images/@height}"
							width="{ancestor::ia:imageAlbum/ia:dim[@align = current()/@align]/ia:images/@width}"
							src="{concat(js:a2r($subDirPath, $imgDirPath), @file)}"/>
					</div>
					<div class="section">
						<h2>こめんと</h2>
						<xsl:choose>
							<xsl:when test="child::node()">
								<xsl:apply-templates select="child::node()"/>
							</xsl:when>
							<xsl:otherwise>
								<p>とくになし</p>
							</xsl:otherwise>
						</xsl:choose>
					</div>
				</div>

				<xsl:call-template name="navi_">
					<xsl:with-param name="imgNum" select="$imgNum"/>
				</xsl:call-template>

				<xsl:call-template name="footer_"/>

			</body>
		</html>
	</redirect:write>
</xsl:template>

<!-- ================ 各連番ページ / head 要素 ================  -->

<xsl:template name="head_">
	<xsl:param name="imgNum"/>
	<xsl:param name="head_">
		<head>
			<xsl:call-template name="head-meta"/>
			<title><xsl:value-of select="concat($pageTitle, ' / Page ', $imgNum)"/></title>
			<link href="{$rootURI}" title="{$rootTitle}" rel="home" />
			<link href="{js:omitDirIndex(js:chsuffix(js:a2r($subDirPath, $curFilePath), 'html'))}" title="{$pageTitle}" rel="contents" />
			<xsl:if test="preceding-sibling::ia:image[1]">
				<link href="{format-number(number($imgNum) - 1, '00')}.html"
					title="{$pageTitle} / Page {format-number(number($imgNum) - 1, '00')}" rel="prev" />
			</xsl:if>
			<xsl:if test="following-sibling::ia:image[1]">
				<link href="{format-number(number($imgNum) + 1, '00')}.html"
					title="{$pageTitle} / Page {format-number(number($imgNum) + 1, '00')}" rel="next" />
			</xsl:if>
			<xsl:call-template name="head-metaLink"/>
			<xsl:call-template name="head-miscLink"/>
			<xsl:call-template name="head-css"/>
			<xsl:call-template name="head-script"/>
		</head>
	</xsl:param>
	<xsl:apply-templates select="xalan:nodeset($head_)/node()" mode="imagealbum-each-page"/>
</xsl:template>

<!-- ================ 各連番ページ / ナビリンク ================  -->

<xsl:template name="navi_">
	<xsl:param name="imgNum"/>
	<xsl:param name="navi_">
		<ul class="navigation">
			<li class="home">
				<a rel="home" accesskey="T" title="{$rootTitle} [T]" href="{$rootURI}">
					<img alt="←" height="15" width="15" src="{$commonDir}/img/icon/LeftArrow.png" />
					<xsl:value-of select="$rootLabel"/>
				</a>
			</li>
			<li class="contents">
				<a rel="contents" accesskey="I" title="{$pageTitle} [I]" href="{js:omitDirIndex(js:chsuffix(js:a2r($subDirPath, $curFilePath), 'html'))}">
					<img alt="←" height="15" width="15" src="{$commonDir}/img/icon/LeftArrow.png" />
					<xsl:value-of select="js:ct($pageTitle, 12)"/>
				</a>
			</li>
			<xsl:if test="preceding-sibling::ia:image[1]">
				<li class="prev">
					<a rel="prev" accesskey="P" title="{$pageTitle} / Page {format-number(number($imgNum) - 1, '00')} [P]"
						href="{format-number(number($imgNum) - 1, '00')}.html">
						<img alt="←" height="15" width="15" src="{$commonDir}/img/icon/LeftArrow.png" />
						<xsl:value-of select="concat('Page ', format-number(number($imgNum) - 1, '00'))"/>
					</a>
				</li>
			</xsl:if>
			<xsl:if test="following-sibling::ia:image[1]">
				<li class="next">
					<a rel="next" accesskey="N" title="{$pageTitle} / Page {format-number(number($imgNum) + 1, '00')} [P]"
						href="{format-number(number($imgNum) + 1, '00')}.html">
						<img alt="→" height="15" width="15" src="{$commonDir}/img/icon/RightArrow.png" />
						<xsl:value-of select="concat('Page ', format-number(number($imgNum) + 1, '00'))"/>
					</a>
				</li>
			</xsl:if>
		</ul>
	</xsl:param>
	<xsl:apply-templates select="xalan:nodeset($navi_)/node()" mode="imagealbum-each-page"/>
</xsl:template>

<!-- ================ 各連番ページ / フッタ ================  -->

<xsl:template name="footer_">
	<xsl:param name="footer_">
		<xsl:call-template name="footer">
			<xsl:with-param name="useAccessLogger" select="false()"/>
		</xsl:call-template>
	</xsl:param>
	<xsl:apply-templates select="xalan:nodeset($footer_)/node()" mode="imagealbum-each-page"/>
</xsl:template>

<!-- ================ 各連番ページ / xalan:nodeset() 内ノード処理 ================  -->

<xsl:template match="h:*" mode="imagealbum-each-page" priority="-5.0">
	<xsl:element name="{local-name()}">
		<xsl:apply-templates select="node() | @*" mode="imagealbum-each-page"/>
	</xsl:element>
</xsl:template>

<!-- href, src 属性値の相対パス深度を調整 -->
<xsl:template match="@*" mode="imagealbum-each-page" priority="-5.0">
	<xsl:choose>
		<xsl:when test="js:match(string(name()), '/^(href|src)$/') and not(js:match(string(../@rel), '/^(contents|prev|next)$/'))">
			<xsl:attribute name="{name()}">
				<xsl:value-of select="js:replace(string(self::node()), '/^[\.\/]+/', $subDirDepth)"/>
			</xsl:attribute>
		</xsl:when>
		<xsl:otherwise>
			<xsl:copy/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="text()" mode="imagealbum-each-page" priority="-5.0">
	<xsl:value-of select="self::node()" disable-output-escaping="yes"/>
</xsl:template>

<!-- テキストノードとして存在している script 要素の src 属性値の相対パス深度を調整 -->
<xsl:template match="text()[js:match(string(self::node()), '/(&lt;\/?script|^&quot;.+&quot;$)/')]" mode="imagealbum-each-page">
	<xsl:value-of select="js:replace(string(self::node()), '/(\.\.\/)+/', $subDirDepth)" disable-output-escaping="yes"/>
</xsl:template>

</xsl:stylesheet>
