<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:date="http://exslt.org/dates-and-times"
  exclude-result-prefixes="xsl date">

<xsl:template match="/page/calendar-summary">
  <table border="0" class="calendar">
    <tr>
      <th>Montag</th>
      <th>Dienstag</th>
      <th>Mittwoch</th>
      <th>Donnerstag</th>
      <th>Freitag</th>
      <th>Samstag</th>
      <th>Sonntag</th>
    </tr>

    <xsl:call-template name="output-week">
      <xsl:with-param name="date">
        <xsl:call-template name="get-past-monday">
          <xsl:with-param name="date" select="concat(date:year(date:date()), '-', date:month-in-year(date:date()), '-01')"/>
        </xsl:call-template>
      </xsl:with-param>
      <xsl:with-param name="stopmonth" select="date:month-in-year(date:add(date:date(), 'P2M'))"/>
    </xsl:call-template>

  </table>
</xsl:template>

<xsl:template name="output-week">
  <xsl:param name="date"/>
  <xsl:param name="stopmonth"/>
  <tr>
    <xsl:call-template name="output-day">
      <xsl:with-param name="date" select="$date"/>
    </xsl:call-template>
  </tr>

  <xsl:variable name="nextweek" select="date:add($date, 'P7D')"/>
  <xsl:message>nextweek: <xsl:value-of select="$nextweek"/></xsl:message>
  <xsl:if test="date:month-in-year($nextweek) != $stopmonth">
    <xsl:call-template name="output-week">
      <xsl:with-param name="date" select="$nextweek"/>
      <xsl:with-param name="stopmonth" select="$stopmonth"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<!--
  td/@class ist wie folgend aufgebaut:
  * Beginnt immer mit 'cal'
  * Dann 'week' für Wochentage, 'sun' für Sonntage
  * '1' für aktuellen Monat, '2' für folgenden Monat, '3' für sonstige Monate
-->
<xsl:template name="output-day">
  <xsl:param name="date"/>
  <td>
    <xsl:attribute name="class">
      <xsl:text>cal</xsl:text>
      <xsl:choose>
        <xsl:when test="date:day-in-week($date) = 1">sun</xsl:when>
        <xsl:otherwise>week</xsl:otherwise>
      </xsl:choose>
      <xsl:choose>
        <xsl:when test="date:month-in-year($date) = date:month-in-year(date:date())">1</xsl:when>
        <xsl:when test="date:month-in-year($date) = date:month-in-year(date:add(date:date(), 'P1M'))">2</xsl:when>
        <xsl:otherwise>3</xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
    <xsl:value-of select="date:day-in-month($date)"/>
    <ul>
      <xsl:for-each select="event">
        <xsl:sort select="date"/>
        <xsl:if test="concat(date:year($date),'-',date:month-in-year($date),'-',date:day-in-month($date)) = concat(date:year(date),'-',date:month-in-year(date),'-',date:day-in-month(date))">
          <li><xsl:value-of select="title"/></li>
        </xsl:if>
      </xsl:for-each>
    </ul>
  </td>
  <xsl:if test="date:day-in-week($date) != 1">  <!-- Sunday? Break and return to output-week... -->
    <xsl:call-template name="output-day">
      <xsl:with-param name="date" select="date:add($date, 'P1D')"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<xsl:template name="calendar-title">
  <xsl:variable name="d1" select="date:date()"/>
  <xsl:variable name="d2" select="date:add(date:date(), 'P1M')"/>

  <xsl:text>Kalender: </xsl:text>

  <xsl:call-template name="get-monthstring">
    <xsl:with-param name="date" select="$d1"/>
  </xsl:call-template>

  <xsl:if test="date:year($d1) != date:year($d2)">
    <xsl:text> </xsl:text>
    <xsl:value-of select="date:year($d1)"/>
  </xsl:if>

  <xsl:text> &amp; </xsl:text>

  <xsl:call-template name="get-monthstring">
    <xsl:with-param name="date" select="$d2"/>
  </xsl:call-template>

  <xsl:text> </xsl:text>

  <xsl:value-of select="date:year($d2)"/>
</xsl:template>

<xsl:template name="get-past-monday">
  <xsl:param name="date"/>
  <xsl:choose>
    <xsl:when test="date:day-in-week($date) = 2">
      <xsl:value-of select="$date"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="get-past-monday">
        <xsl:with-param name="date" select="date:add($date, '-P1D')"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>
