<?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:rss="http://purl.org/rss/1.0/"
	xmlns:i='http://www.w3.org/2002/12/cal/ical#'
	xmlns:x='http://www.w3.org/2002/12/cal/prod/Apple_Comp_273def595c8b3485#'
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
	xmlns:contact="http://www.w3.org/2000/10/swap/pim/contact#"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:xhtml="http://www.w3.org/1999/xhtml"
	xmlns:foaf="http://xmlns.com/foaf/0.1/"
	xmlns:dc ="http://purl.org/dc/elements/1.1/"
	xmlns:dcterms="http://purl.org/dc/terms/"
	xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"
	xmlns:date="http://exslt.org/dates-and-times"
	extension-element-prefixes="date rss i x rdf rdfs contact content xhtml foaf dc dcterms trackback "
>

<xsl:template match="/">
	<xsl:apply-templates select="rdf:RDF"/>
</xsl:template>


<xsl:template match="rdf:RDF">
<html xml:lang="ja">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title><xsl:value-of select="date:year($day)"/> 年 <xsl:value-of select="$mm"/> 月 イベントカレンダー / おのひろきおんらいん</title>
	<link rel="stylesheet" href="../onohiroki.css" type="text/css" />
	<link rel="stylesheet" href="calendar.css" type="text/css" />
	<link rel="stylesheet" type="text/css" ><xsl:attribute name="href">holidays<xsl:value-of select="date:year($day)"/>.css</xsl:attribute></link>
	<link rel="alternate" type="application/rss+xml" title="RSS" href="http://onohiroki.cycling.jp/calendar/current-cycling-event-rss.rdf" />
	<link rel="meta" type="application/rdf+xml" title="FOAF"  href="http://onohiroki.cycling.jp/info/foaf.rdf" />
	<link rel="meta" type="application/rdf+xml" title="RDF Calender"  href="cycling-event-cal.rdf" />
	<link rel="meta" type="text/calendar" title="iCalender"  href="cycling-event.ics" />
	<meta name="author" content="Hiroki ONO" />
</head>
<body>
	<div class="header"><p class="navi"><span class="logo"><a href="../">おのひろきおんらいん</a></span></p></div>
	<h1><xsl:value-of select="date:year($day)"/> 年 <xsl:value-of select="$mm"/> 月のイベントカレンダー</h1>
	<xsl:comment>イベント情報「 <xsl:value-of select="i:Vcalendar/x:wrCalname|i:vcalendar/x:wrCalname"/>」をカレンダーで提供します．</xsl:comment>
	<ul class="navi" id="calnavi">
		<li><a><xsl:attribute name="href">cal<xsl:value-of select="substring(date:add($day,'-P1M'),1,7)"/>.html#calnavi</xsl:attribute>
			<xsl:value-of select="date:month-in-year(date:add($day,'-P1M'))"/>
			<xsl:text> </xsl:text>月</a>
		</li>
		<li><a href="calendar.html">イベントカレンダー TOP</a></li>
		<xsl:if test="not(date:month-in-year($day) = date:month-in-year(date:add($now,'P2M')))">
		<li><a><xsl:attribute name="href">cal<xsl:value-of select="substring(date:add($day,'P1M'),1,7)"/>.html#calnavi</xsl:attribute>
			<xsl:value-of select="date:month-in-year(date:add($day,'P1M'))"/>
			<xsl:text> </xsl:text>月</a>
		</li>
		</xsl:if>
	</ul>
	<table class="ical" title="イベントカレンダー" summary ="イベントカレンダー．1列目が月曜日，2列目が火曜日，3列目が水曜日，4列目が木曜日，5列目が金曜日，6列目が土曜日，7列目が日曜日">
	<caption><xsl:value-of select="date:year($day)"/> 年 <xsl:value-of select="$mm"/> 月</caption> 
	<thead>
	<tr><th class="mon">月</th><th class="tue">火</th><th class="wed">水</th><th class="thu">木</th><th class="fri">金</th><th class="sat">土</th><th class="sun">日</th></tr>
	</thead>
	<tbody>
	<xsl:call-template name="caltable"/>
	</tbody>
	</table>
	<xsl:call-template name="eventlist"/>
	<xsl:call-template name="metadatalist"/>
	<hr />
	<address>おのひろき <a  href="mailto:onohiroki@cup.com?subject=onohiroki_online">onohiroki@cup.com</a></address>
</body>
</html>
</xsl:template>

<xsl:template name="metadatalist">
<xsl:param name="h2" select="'yes'" />
<xsl:param name="description" select="'no'" />
<xsl:param name="ical" select="'yes'" />
<xsl:if test="$h2 = 'yes'">
<h2>メタデータリスト</h2>
</xsl:if>
<xsl:if test="$description = 'yes'">
<p>iCalendar ファイルや RDF/XML ファイルとしてイベントカレンダーのメタデータを配布します．iCalendar ファイルは，<a href="http://apple.co.jp/ical/index.html" title="アップル - iCal">iCal</a> や <a href="http://jt.mozilla.gr.jp/projects/calendar/" title="Calendar - 標準に準拠したカレンダー クライアント プロジェクト">Mozilla Calendar</a>，Outlook などで取り込む事ができます．</p>
<ul>
<li>詳細:<a href="../comp-ical-xslt.html">イベントカレンダーについて公開している情報</a></li>
</ul>
</xsl:if>
<dl>
<dt class="icalendar"><a href="cycling-event.ics" title="iCal で管理している公開できる自転車関連のイベント情報の iCalendar ファイル">iCalendar</a></dt>
<dd>iCal で管理している公開できる自転車関連のイベント情報
<xsl:if test="$ical = 'yes'">
<ul>
<li><a href="webcal://onohiroki.cycling.jp/calendar/cycling-event.ics">Apple iCal へ取込み</a></li>
<li><a href="http://ical.mac.com/onohiroki/cycling-event">.Mac マイカレンダー cycling-event</a></li>
</ul>
</xsl:if>
</dd>
<dt class="rdfical"><a href="cycling-event-cal.rdf" title="iCal で管理している公開できる自転車関連のイベント情報の RDFical の RDF/XML ファイル"><img src="../image/png/rdf.png" alt="" />RDFical</a></dt>
<dd>iCal で管理している公開できる自転車関連のイベント情報の RDF/XML 版</dd>
<dt class="rss"><a href="current-cycling-event-rss.rdf" title="今月のイベント情報の RSS 1.0"><img src="../image/png/rdf.png" alt="" />RSS</a></dt>
<dd>今月からのイベント情報を RSS 1.0 形式にしたもの．</dd>
</dl>

</xsl:template>

<xsl:template match="i:Vcalendar|i:vcalendar">
<dl>
	<xsl:for-each select="i:component/i:Vevent|i:component/i:vevent">
	<xsl:sort select="i:dtstart/i:date|i:dtstart/i:dateTime"/>
		<dt><xsl:value-of select="i:dtstart/i:date|i:dtstart/i:dateTime"/></dt><dd><xsl:value-of select="i:summary"/></dd>
	</xsl:for-each>
</dl>
</xsl:template>


<xsl:template name="caltable">
<xsl:param name="dd" select="$beginningmonth" />
<xsl:choose>
	<xsl:when test="date:day-in-week($dd) = 1 and $dd = $beginningmonth">
		<xsl:call-template name="caltable">
			<xsl:with-param name="dd"><xsl:value-of select="date:add($dd,'-P6D')"/></xsl:with-param>
		</xsl:call-template>
	</xsl:when>

	<xsl:when test="$dd = $beginningmonth and not(date:day-in-week($dd) = 2)">
		<xsl:call-template name="caltable">
			<xsl:with-param name="dd"><xsl:value-of select="date:add($dd,concat(concat('-P',string(date:day-in-week($dd)-2)),'D'))"/></xsl:with-param>
		</xsl:call-template>
	</xsl:when>
	<xsl:when test="not(date:month-in-year($dd) = date:month-in-year(date:add($beginningmonth,'P1M')))">
		<tr>
			<td class="mon"><xsl:attribute name="id">b<xsl:value-of select="translate($dd,'-','')"/></xsl:attribute>
				<xsl:if test="not(date:month-in-year($dd) = date:month-in-year($beginningmonth))">
					<xsl:attribute name="class">bit</xsl:attribute>
				</xsl:if>
				<p class="date"><xsl:attribute name="id">d<xsl:value-of select="translate($dd,'-','')"/></xsl:attribute>
				<xsl:value-of select="date:day-in-month($dd)"/></p>
				<xsl:for-each select="/rdf:RDF/i:Vcalendar/i:component/i:Vevent|/rdf:RDF/i:vcalendar/i:component/i:vevent">
					<xsl:if test="substring(i:dtstart/i:date|i:dtstart/i:dateTime,1,10) = substring($dd,1,10)">
						<p class="event">
						<a><xsl:attribute name="href">cal<xsl:value-of select="substring(i:dtstart/i:date|i:dtstart/i:dateTime,1,7)"/>.html#uid<xsl:value-of select="i:uid"/></xsl:attribute>
						<xsl:value-of select="i:summary"/>
						</a>
						</p>
						
					</xsl:if>
				</xsl:for-each>
			</td>
			<td class="tue"><xsl:attribute name="id">b<xsl:value-of select="translate(date:add($dd,'P1D'),'-','')"/></xsl:attribute>
				<xsl:if test="not(date:month-in-year(date:add($dd,'P1D')) = date:month-in-year($beginningmonth))">
					<xsl:attribute name="class">bit</xsl:attribute>
				</xsl:if>
				<p class="date"><xsl:attribute name="id">d<xsl:value-of select="translate(date:add($dd,'P1D'),'-','')"/></xsl:attribute>
				<xsl:value-of select="date:day-in-month(date:add($dd,'P1D'))"/></p>
				<xsl:for-each select="/rdf:RDF/i:Vcalendar/i:component/i:Vevent|/rdf:RDF/i:vcalendar/i:component/i:vevent">
					<xsl:if test="substring(i:dtstart/i:date|i:dtstart/i:dateTime,1,10) = substring(date:add($dd,'P1D'),1,10)">
						<p class="event">
						<a><xsl:attribute name="href">cal<xsl:value-of select="substring(i:dtstart/i:date|i:dtstart/i:dateTime,1,7)"/>.html#uid<xsl:value-of select="i:uid"/></xsl:attribute>
						<xsl:value-of select="i:summary"/>
						</a>
						</p>
					</xsl:if>
				</xsl:for-each>
			</td>
			<td class="wed"><xsl:attribute name="id">b<xsl:value-of select="translate(date:add($dd,'P2D'),'-','')"/></xsl:attribute>

				<xsl:if test="not(date:month-in-year(date:add($dd,'P2D')) = date:month-in-year($beginningmonth))">
					<xsl:attribute name="class">bit</xsl:attribute>
				</xsl:if>
				<p class="date"><xsl:attribute name="id">d<xsl:value-of select="translate(date:add($dd,'P2D'),'-','')"/></xsl:attribute>
				<xsl:value-of select="date:day-in-month(date:add($dd,'P2D'))"/></p>
				<xsl:for-each select="/rdf:RDF/i:Vcalendar/i:component/i:Vevent|/rdf:RDF/i:vcalendar/i:component/i:vevent">
					<xsl:if test="substring(i:dtstart/i:date|i:dtstart/i:dateTime,1,10) = substring(date:add($dd,'P2D'),1,10)">
						<p class="event">
						<a><xsl:attribute name="href">cal<xsl:value-of select="substring(i:dtstart/i:date|i:dtstart/i:dateTime,1,7)"/>.html#uid<xsl:value-of select="i:uid"/></xsl:attribute>
						<xsl:value-of select="i:summary"/>
						</a>
						</p>
					</xsl:if>
				</xsl:for-each>
			</td>
			<td class="thu"><xsl:attribute name="id">b<xsl:value-of select="translate(date:add($dd,'P3D'),'-','')"/></xsl:attribute>
				<xsl:if test="not(date:month-in-year(date:add($dd,'P3D')) = date:month-in-year($beginningmonth))">
					<xsl:attribute name="class">bit</xsl:attribute>
				</xsl:if>
				<p class="date"><xsl:attribute name="id">d<xsl:value-of select="translate(date:add($dd,'P3D'),'-','')"/></xsl:attribute>
				<xsl:value-of select="date:day-in-month(date:add($dd,'P3D'))"/></p>
				<xsl:for-each select="/rdf:RDF/i:Vcalendar/i:component/i:Vevent|/rdf:RDF/i:vcalendar/i:component/i:vevent">
					<xsl:if test="substring(i:dtstart/i:date|i:dtstart/i:dateTime,1,10) = substring(date:add($dd,'P3D'),1,10)">
						<p class="event">
						<a><xsl:attribute name="href">cal<xsl:value-of select="substring(i:dtstart/i:date|i:dtstart/i:dateTime,1,7)"/>.html#uid<xsl:value-of select="i:uid"/></xsl:attribute>
						<xsl:value-of select="i:summary"/>
						</a>
						</p>
					</xsl:if>
				</xsl:for-each>
			</td>
			<td class="fri"><xsl:attribute name="id">b<xsl:value-of select="translate(date:add($dd,'P4D'),'-','')"/></xsl:attribute>
				<xsl:if test="not(date:month-in-year(date:add($dd,'P4D')) = date:month-in-year($beginningmonth))">
					<xsl:attribute name="class">bit</xsl:attribute>
				</xsl:if>
				<p class="date"><xsl:attribute name="id">d<xsl:value-of select="translate(date:add($dd,'P4D'),'-','')"/></xsl:attribute>
				<xsl:value-of select="date:day-in-month(date:add($dd,'P4D'))"/></p>
				<xsl:for-each select="/rdf:RDF/i:Vcalendar/i:component/i:Vevent|/rdf:RDF/i:vcalendar/i:component/i:vevent">
					<xsl:if test="substring(i:dtstart/i:date|i:dtstart/i:dateTime,1,10) = substring(date:add($dd,'P4D'),1,10)">
						<p class="event">
						<a><xsl:attribute name="href">cal<xsl:value-of select="substring(i:dtstart/i:date|i:dtstart/i:dateTime,1,7)"/>.html#uid<xsl:value-of select="i:uid"/></xsl:attribute>
						<xsl:value-of select="i:summary"/>
						</a>
						</p>
					</xsl:if>
				</xsl:for-each>
			</td>
			<td class="sat"><xsl:attribute name="id">b<xsl:value-of select="translate(date:add($dd,'P5D'),'-','')"/></xsl:attribute>
				<xsl:if test="not(date:month-in-year(date:add($dd,'P5D')) = date:month-in-year($beginningmonth))">
					<xsl:attribute name="class">bit</xsl:attribute>
				</xsl:if>
				<p class="date"><xsl:attribute name="id">d<xsl:value-of select="translate(date:add($dd,'P5D'),'-','')"/></xsl:attribute>
				<xsl:value-of select="date:day-in-month(date:add($dd,'P5D'))"/></p>
				<xsl:for-each select="/rdf:RDF/i:Vcalendar/i:component/i:Vevent|/rdf:RDF/i:vcalendar/i:component/i:vevent">
					<xsl:if test="substring(i:dtstart/i:date|i:dtstart/i:dateTime,1,10) = substring(date:add($dd,'P5D'),1,10)">
						<p class="event">
						<a><xsl:attribute name="href">cal<xsl:value-of select="substring(i:dtstart/i:date|i:dtstart/i:dateTime,1,7)"/>.html#uid<xsl:value-of select="i:uid"/></xsl:attribute>
						<xsl:value-of select="i:summary"/>
						</a>
						</p>
					</xsl:if>
				</xsl:for-each>
			</td>
			<td  class="sun"><xsl:attribute name="id">b<xsl:value-of select="translate(date:add($dd,'P6D'),'-','')"/></xsl:attribute>
				<xsl:if test="not(date:month-in-year(date:add($dd,'P6D')) = date:month-in-year($beginningmonth))">
					<xsl:attribute name="class">bit</xsl:attribute>
				</xsl:if>
				<p class="date"><xsl:attribute name="id">d<xsl:value-of select="translate(date:add($dd,'P6D'),'-','')"/></xsl:attribute>
				<xsl:value-of select="date:day-in-month(date:add($dd,'P6D'))"/></p>
				<xsl:for-each select="/rdf:RDF/i:Vcalendar/i:component/i:Vevent|/rdf:RDF/i:vcalendar/i:component/i:vevent">
					<xsl:if test="substring(i:dtstart/i:date|i:dtstart/i:dateTime,1,10) = substring(date:add($dd,'P6D'),1,10)">
						<p class="event">
						<a><xsl:attribute name="href">cal<xsl:value-of select="substring(i:dtstart/i:date|i:dtstart/i:dateTime,1,7)"/>.html#uid<xsl:value-of select="i:uid"/></xsl:attribute>
						<xsl:value-of select="i:summary"/>
						</a>
						</p>
					</xsl:if>
				</xsl:for-each>
			</td>

		</tr>
		<xsl:call-template name="caltable">
			<xsl:with-param name="dd"><xsl:value-of select="date:add($dd,'P7D')"/></xsl:with-param>
		</xsl:call-template>
	</xsl:when>
</xsl:choose>

</xsl:template>

<xsl:template name="eventlist">
	<table class="eventlist" title="イベント一覧" summary ="イベント一覧．1列目が日時，2列目がイベント名，3列目がコメント，4 列目がURL">
	<caption>イベント一覧</caption> 
	<thead>
		<tr><th>日時</th><th>イベント名</th><th>コメントと URL</th></tr>
	</thead>
	<tbody>
	<xsl:for-each select="/rdf:RDF/*/i:component">
	<xsl:if test="date:month-in-year(*/i:dtstart/*) = $mm">
	<xsl:variable name="num" select="position()-1"/>
		<xsl:if test="not(string(/rdf:RDF/*/i:component[$num]/*/i:uid) = string(*/i:uid)) or not(date:month-in-year(/rdf:RDF/*/i:component[$num]/*/i:dtstart/*) = date:month-in-year(*/i:dtstart/*))">
			<xsl:variable name="uid" select="*/i:uid"/>
			<tr class="eventlist">
				<td class="date">
					<xsl:attribute name="id">uid<xsl:value-of select="$uid"/></xsl:attribute>
				<xsl:for-each select="/rdf:RDF/*/i:component">
				<xsl:if test="*/i:uid = $uid and date:month-in-year(*/i:dtstart/*) = $mm">
					<a><xsl:attribute name="id">d<xsl:value-of select="substring(*/i:dtstart/i:date|*/i:dtstart/i:dateTime,1,10)"/>-<xsl:value-of select="$uid"/></xsl:attribute>
					<xsl:value-of select="date:day-in-month(*/i:dtstart/*)"/> 日
					<xsl:text> </xsl:text>
					<xsl:value-of select="substring-after(*/i:dtstart/*,'T')"/>
					</a>
					<br />
				</xsl:if>
				</xsl:for-each>
				</td>
				<td class="summary"><xsl:value-of select="*/i:summary"/></td>
				<td class="description">
				<xsl:if test="*/i:description">
					<p>
					<xsl:call-template name="stringreplace">
						<xsl:with-param name="string" select="string(*/i:description)" />
						<xsl:with-param name="search" select="'\n'" />
						<xsl:with-param name="replace" select="' '" />
						<xsl:with-param name="replacetag" select="'br'" />
					</xsl:call-template>
					</p>
				</xsl:if>
				<p class="url"><xsl:if test="*/i:url/@rdf:resource"><a><xsl:attribute name="href"><xsl:value-of select="*/i:url/@rdf:resource"/></xsl:attribute><xsl:value-of select="*/i:url/@rdf:resource"/></a></xsl:if></p>
				</td>
				
			</tr>
		</xsl:if>
	</xsl:if>
	</xsl:for-each>
	</tbody>
	</table>
</xsl:template>

<xsl:template name="stringreplace">
	<xsl:param name="string" select="''" />
	<xsl:param name="search" select="'¥n'" />
	<xsl:param name="replace" select="' '" />
	<xsl:param name="replacetag" select="''" />
	
	<xsl:choose>
	<xsl:when test="contains($string, $search)">
		<xsl:value-of select="substring-before($string,$search)"/>
		<xsl:choose>
			<xsl:when test="$replacetag = 'br'">
				<br />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$replace"/>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:call-template name="stringreplace">
			<xsl:with-param name="string" select="substring-after($string,$search)" />
			<xsl:with-param name="search" select="$search" />
			<xsl:with-param name="replace" select="$replace" />
			<xsl:with-param name="replacetag" select="$replacetag" />
		</xsl:call-template>
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="$string"/>
	</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet> 
