<?xml version="1.0"?>

<!--

  Copyright © 2002–2011 Simon Pepping
  License: GNU General Public License Version 2, June 1991

  This software is free software; you may redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation Version 2, June 1991, aka "copyleft" or
  the GPL.

  This software 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.

  Along with this software you should have received a
  copy of the GNU General Public License. If not, write to the Free
  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA, or see http://www.fsf.org/copyleft/gpl.html.

-->

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:date="http://exslt.org/dates-and-times"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
  xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:sp="http://spepping.leverkruid.eu/site/"
  xmlns:dic="http://xslt.spepping.salix/dictionary"
  exclude-result-prefixes = "atom dc date sp dic xhtml"
>

  <xsl:output method="html" doctype-system="" encoding="UTF-8" indent="no" />

  <xsl:param name="dictionary"/>
  <xsl:param name="model" select="'model2'"/>
  <xsl:param name="separator" select="'look-rule.gif'"/>
  <xsl:param name="atom-file" select="'atom.xml'"/>
  <xsl:variable name="atom-feed" select="document($atom-file)"/>
  <xsl:variable name="link-base" select="'http://www.leverkruid.eu/'"/>

  <xsl:template match="sp:link">
	<link>
	  <xsl:choose>
		<xsl:when test="@type='application/atom+xml'">
		  <xsl:attribute name="href">
			<xsl:value-of select="$atom-file"/>
		  </xsl:attribute>
		</xsl:when>
		<xsl:when test="@type='text/css'">
		  <xsl:attribute name="href">
			<xsl:value-of select="concat($model,'.css')"/>
		  </xsl:attribute>
		</xsl:when>
	  </xsl:choose>
	  <xsl:apply-templates select="@*"/>
	</link>
  </xsl:template>

  <xsl:template match="sp:now">
	  <xsl:choose>
		<xsl:when test="@type='full'">
		  <xsl:variable name="picture" select="'[D01] [MNn] [Y0001] [H01]:[m]:[s] ([z])'"/>
		  <xsl:value-of select="format-dateTime(current-dateTime(), $picture, 'en', (), ())"/>
		</xsl:when>
		<xsl:when test="@type='year'">
		  <xsl:value-of select="year-from-date(current-date())"/>
		</xsl:when>
	  </xsl:choose>
  </xsl:template>

  <xsl:template match="xhtml:td[@class='channeldata']/dc:date">
    <xsl:for-each select=". | $atom-feed//atom:updated">
	  <xsl:sort order="descending"/>
<!--
	  <xsl:message>
		<xsl:value-of select="concat(namespace-uri(..),':',name(..))"/>
	  </xsl:message>
      <xsl:message>
		<xsl:value-of select="concat('Position: ', string(position()))"/>
	  </xsl:message>
      <xsl:message>
		<xsl:value-of select="concat('Date: ', .)"/>
	  </xsl:message>
-->
	  <xsl:if test="position()=1">
		<xsl:call-template name="pubdate">
		  <xsl:with-param name="dateTimeZone" select="."/>
		</xsl:call-template>
	  </xsl:if>
	</xsl:for-each>
  </xsl:template>

  <xsl:template match="sp:projects">
	<xsl:choose>
	  <xsl:when test="@type='toc'">
		<xsl:apply-templates select="$atom-feed/atom:feed/atom:entry" mode="toc"/>
	  </xsl:when>
	  <xsl:when test="@type='full'">
        <xsl:call-template name="doitems">
          <xsl:with-param name="items" select="$atom-feed/atom:feed/atom:entry"/>
          <xsl:with-param name="odd" select="true()"/>
        </xsl:call-template>
	  </xsl:when>
	</xsl:choose>
  </xsl:template>

  <xsl:template match="atom:entry" mode="toc">
    <xsl:param name="link" select="substring-after(atom:link[@type='text/html']/@href, $link-base)"/>
    <tr>
      <td>
        <a href="{$link}">
          <xsl:apply-templates select="atom:title"/>
        </a>
      </td>
    </tr>
  </xsl:template>

  <xsl:template name="doitems">
    <xsl:param name="items"/>
    <xsl:param name="odd"/>
    <xsl:apply-templates select="$items[1]">
      <xsl:with-param name="odd" select="$odd"/>
    </xsl:apply-templates>
    <xsl:if test="count($items)!=1">
      <xsl:call-template name="doitems">
        <xsl:with-param name="items" select="$items[position()!=1]"/>
        <xsl:with-param name="odd" select="not($odd)"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template match="atom:entry">
    <xsl:param name="link" select="substring-after(atom:link[@type='text/html']/@href, $link-base)"/>
    <xsl:param name="odd"/>
    <tr itemscope="" itemtype="http://schema.org/Product" itemid="{atom:id}">
      <td>
        <xsl:attribute name="class">
          <xsl:choose>
            <xsl:when test="$odd">
              <xsl:text>itemcellodd</xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text>itemcelleven</xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:attribute>
        <table class="itemtable">
          <tbody>
            <tr>
              <td class="itemtitle" itemprop="name">
                <a href="{$link}" itemprop="url">
                  <xsl:apply-templates select="atom:title"/>
                </a>
              </td>
            </tr>
            <tr>
              <td class="itemdesc">
                <xsl:apply-templates select="atom:content"/>
              </td>
            </tr>
            <tr>
              <td class="itemdata">
                  <xsl:text>[</xsl:text>
                  <xsl:apply-templates
                    select="atom:category|atom:link/@hreflang|atom:rights|atom:updated"
                    mode="metadata"/>
                  <xsl:text>]</xsl:text>
              </td>
            </tr>
          </tbody>
        </table>
      </td>
    </tr>
    <tr><td><img src="{$separator}"
	alt="-------------------------------------------------------------------"
	/></td></tr>
  </xsl:template>

  <xsl:template match="atom:content">
    <xsl:choose>
      <xsl:when test="@type='text' or not(@type)">
        <p itemprop="description"><xsl:apply-templates/></p>
      </xsl:when>
      <xsl:when test="@type='xhtml'">
        <xsl:apply-templates/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template
    match="atom:category|atom:link/@hreflang|atom:rights|atom:updated"
    mode="metadata">
    <xsl:if test="position()!=1">
      <xsl:text> | </xsl:text>
    </xsl:if>
    <xsl:apply-templates select="." mode="prefixed"/>
  </xsl:template>

  <xsl:template match="atom:category" mode="prefixed">
    <span class="keyword">Subject</span>
    <xsl:text> </xsl:text>
	<xsl:value-of select="@label"/>
  </xsl:template>

  <xsl:template match="@hreflang" mode="prefixed">
    <span class="keyword">Language</span>
    <xsl:text> </xsl:text>
    <xsl:call-template name="languageName"/>
  </xsl:template>

  <xsl:template match="atom:rights" mode="prefixed">
    <span class="keyword">Rights</span>
    <xsl:text> </xsl:text>
    <xsl:apply-templates/>    
  </xsl:template>

  <xsl:template match="atom:updated" mode="prefixed">
    <span class="keyword">Last updated</span>
    <xsl:text> </xsl:text>
    <xsl:call-template name="pubdate"/>
  </xsl:template>

  <xsl:template match="atom:updated" name="pubdate">
    <xsl:param name="dateTimeZone" select="."/>

    <time id="last-updated" datetime="{$dateTimeZone}" pubdate="pubdate">
      <xsl:call-template name="dateTimeZone">
        <xsl:with-param name="dateTimeZone" select="$dateTimeZone"/>
      </xsl:call-template>
    </time>
  </xsl:template>
  
  <xsl:template name="dateTimeZone">
    <xsl:param name="dateTimeZone" select="."/>

    <xsl:variable name="zone">
      <xsl:choose>
        <xsl:when test="not(empty(timezone-from-dateTime($dateTimeZone)))">
          <xsl:value-of select="' ([z])'"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="''"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="picture" select="concat('[D01] [MNn] [Y0001] [H01]:[m]:[s]',$zone)"/>
    
    <xsl:value-of select="format-dateTime($dateTimeZone, $picture, 'en', (), () )"/>
  </xsl:template>

  <xsl:template match="dc:language" name="languageName">
    <xsl:param name="isolang" select="."/>
    <xsl:choose>
      <xsl:when test="$dictionary">
        <xsl:value-of select="document($dictionary)/dic:dictionary/dic:languages/dic:language[@iso=$isolang]"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="defaultLanguageName">
          <xsl:with-param name="isolang" select="$isolang"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="defaultLanguageName">
    <xsl:param name="isolang"/>
    <xsl:choose>
      <xsl:when test="$isolang='en'">
        <xsl:text>English</xsl:text>
      </xsl:when>
      <xsl:when test="$isolang='nl'">
        <xsl:text>Dutch</xsl:text>
      </xsl:when>
      <xsl:when test="$isolang='de'">
        <xsl:text>German</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$isolang"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="atom:*">
	<xsl:apply-templates select="@*|node()"/>
  </xsl:template>

  <xsl:template match="xhtml:*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>
  
  <xsl:template match="@*">
    <xsl:copy/>
  </xsl:template>
  
  <xsl:template match="atom:content/xhtml:div">
    <div itemprop="description">
      <xsl:apply-templates select="@*|node()"/>
    </div>
  </xsl:template>

</xsl:stylesheet>

