<?xml-stylesheet href="/parts/xsltdoc.xsl" type="text/xsl" media="screen"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:i="http://www.w3.org/2002/12/cal/ical#"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:rss="http://purl.org/rss/1.0/"
  xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
  xmlns:rs="http://purl.org/net/rdf/papers/sitemap#"
  xmlns:doas="http://purl.org/net/ns/doas#"
  xmlns:foaf="http://xmlns.com/foaf/0.1/"
  exclude-result-prefixes="rdf i dc geo rss rs doas"
>

<!-- avoid Opera error
 <xsl:variable name="_doas" select="document('')//rdf:Description[1]"/>
-->
 <xsl:template name="_doas_description">
  <rdf:RDF xmlns="http://purl.org/net/ns/doas#">
   <rdf:Description rdf:about="">
    <title>RDFical calendar stylesheet</title>
    <description>This stylesheet is designed to convert RDFical file to visible XHTML with manthly calendar. This also handles RSS file with RDFical vocabulary.</description>
    <author rdf:parseType="Resource">
     <name>Masahide Kanzaki</name>
     <homepage rdf:resource="http://www.kanzaki.com/"/>
    </author>
    <created>2003-09-22</created>
    <release rdf:parseType="Resource">
     <revision>1.5</revision>
     <created>2004-02-20</created>
    </release>
    <rights>(c) 2003-2004 by the author, copyleft under LGPL</rights>
    <license rdf:resource="http://creativecommons.org/licenses/LGPL/2.1/"/>
   </rdf:Description>
  </rdf:RDF>
 </xsl:template>


 <xsl:output method="html"/>
 <xsl:variable name="mkcateg"><xsl:value-of select="/rdf:RDF/i:Vcalendar/*[local-name()='mkCateg']"/></xsl:variable>
 <xsl:variable name="calname"><xsl:value-of select="/rdf:RDF/i:Vcalendar/*[local-name()='wrCalname']|/rdf:RDF/rss:channel/rss:title"/></xsl:variable>

 <!-- title, h1 and footer comes here to be displayed if this XSLT directly called from UA -->
 <xsl:template match="/">
 <!--** Starting template: pepares XHTML frame work and calls template for rdf:RDF. Note the resulting <body> element has onload="newcal()" attribute to automatically generate a monthly calendar with javascript -->
  <html>
   <head>
    <title>RDFical view: <xsl:value-of select="$calname"/></title>
    <xsl:call-template name="htmlhead"/>
   </head>
   <body onload="newcal('{rdf:RDF/@xml:lang}','allevents');">
    <xsl:call-template name="banner"/>
    <h1><xsl:value-of select="$calname"/><xsl:if test="not($calname)">RDFical XSLT rendering</xsl:if></h1>
    <xsl:apply-templates select="rdf:RDF"/>
    <xsl:call-template name="footer">
     <xsl:with-param name="cy">2003-2004</xsl:with-param>
     <xsl:with-param name="status">Status: XSLT <!--created <xsl:value-of select="$_doas/doas:created"/>; modified <xsl:value-of select="$_doas/doas:release/doas:created"/>-->2003-2004. Stylesheet copyleft under GPL.</xsl:with-param>
    </xsl:call-template>
   </body>
  </html>
 </xsl:template>

 <!-- main data handling -->
 <xsl:template match="rdf:RDF">
  <xsl:call-template name="events"/>
  <div id="components"><img src="/parts/icals.gif" alt="components listed below..."/> <xsl:call-template name="prevNext"/></div>
  <xsl:choose>
   <!-- standard RDFical -->
   <xsl:when test="i:Vcalendar">
    <xsl:apply-templates select="i:Vcalendar"/>
   </xsl:when>
   <!-- RSS with RDFical vocab -->
   <xsl:otherwise>
    <p><xsl:value-of select="rss:channel/rss:description"/></p>
    <h2 id="components">Event 'Components'</h2>
    <p>（Those events not belong to Vcalendar, but use its vocabulary）</p>
    <xsl:apply-templates select=".//i:Vevent|.//i:Vtimezone|*[rdf:type/@rdf:resource='i:Vevent']"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template match="i:Vcalendar">
 <!-- if the file has Vcalendar element -->
  <xsl:variable name="vpos" select="position()"/>
  <xsl:if test="i:component">
   <h2 id="components{$vpos}">Components<xsl:if test="*[local-name()='wrCalname']"> of &quot;<xsl:value-of select="*[local-name()='wrCalname']"/>&quot;</xsl:if></h2>
   <xsl:apply-templates select="i:component/*"/>
  </xsl:if>
  <h2 id="cal-prop{$vpos}">Calendar Properties</h2>
  <table class="cp" cellpadding="5">
   <xsl:apply-templates select="*[local-name() != 'component']"/>
  </table>
 </xsl:template>

 <!-- Nested Property values -->
 <xsl:template match="*">
  <dfn><xsl:value-of select="local-name()"/></dfn>: 
  <xsl:call-template name="propval"/>
 </xsl:template>

 <!-- Properties from ical will have hyperlink to definition HTML -->
 <xsl:template match="i:*">
  <dfn><a href="http://www.kanzaki.com/docs/ical/{local-name()}.html"><xsl:value-of select="local-name()"/></a></dfn>: 
  <xsl:call-template name="propval"/>
 </xsl:template>

 <!-- Calender properties -->
 <xsl:template match="i:Vcalendar/*">
  <tr>
   <th><xsl:value-of select="name()"/></th>
   <td><xsl:call-template name="valOrRsrource"/></td>
  </tr>
 </xsl:template>
 <!-- Calender properties from ical samespace-->
 <xsl:template match="i:Vcalendar/i:*">
  <tr>
   <th><a href="http://www.kanzaki.com/docs/ical/{local-name()}.html"><xsl:value-of select="local-name()"/></a></th>
   <td><xsl:call-template name="valOrRsrource"/></td>
  </tr>
 </xsl:template>

 <!-- Calendar Component Elements -->
 <!-- original:i:component/*, but changed to cover RSS with RDFical -->
 <xsl:template match="i:Vevent|i:Vtimezone|i:Valarm|i:Vtodo|i:Vjournal|i:Vfreebusy|*[rdf:type/@rdf:resource='i:Vevent']">
  <table border="1" cellspacing="0" cellpadding="5" class="component">
   <xsl:attribute name="id"><xsl:value-of select="generate-id(.)"/></xsl:attribute>
   <caption><xsl:value-of select="local-name()"/> (<xsl:value-of select="position()"/>)</caption>
   <thead><tr><th class="property">Property</th><th class="value">Value</th></tr></thead>
   <tbody>
    <xsl:apply-templates select="i:dtstart"/>
    <xsl:apply-templates select="i:dtend"/>
    <xsl:apply-templates select="i:summary"/>
    <xsl:apply-templates select="i:description"/>
    <xsl:apply-templates select="*[local-name()!='dtstart' and local-name()!='dtend' and local-name()!='summary' and local-name()!='description']"/>
    <xsl:apply-templates select="../../rss:*"/>
   </tbody>
  </table>
 </xsl:template>


 <xsl:template match="i:Vcalendar/i:prodid">
  <tr>
   <th><a href="http://www.kanzaki.com/docs/ical/{local-name()}.html"><xsl:value-of select="local-name()"/></a></th>
   <td id="icalprodid"><xsl:value-of select="."/></td>
  </tr>
 </xsl:template>


 <!-- apply from imgdesc 2004-01-27 -->
 <xsl:template name="propval">
  <xsl:choose>
   <xsl:when test="@rdf:nodeID[ancestor::i:Vevent]">
    <span class="f">(<xsl:call-template name="collectnodes">
     <xsl:with-param name="nid"><xsl:value-of select="@rdf:nodeID"/></xsl:with-param>
    </xsl:call-template>)</span>
   </xsl:when>
   <xsl:when test="@rdf:about[ancestor::i:Vevent]">
    <span class="f">(<xsl:call-template name="collectresourse">
     <xsl:with-param name="ab"><xsl:value-of select="@rdf:about"/></xsl:with-param>
    </xsl:call-template>)</span>
   </xsl:when>
   <xsl:when test="@*|*">
    <span class="f">(<xsl:apply-templates select="@*"/>
    <xsl:apply-templates select="*"/>); </span>
   </xsl:when>
   <xsl:otherwise>
    <xsl:call-template name="valOrRsrource"/>; 
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="collectnodes">
  <xsl:param name="nid"/>
  <!--<dfn>nodeID</dfn>:<xsl:value-of select="$nid"/>; -->
  <xsl:for-each select="//*[@rdf:nodeID=$nid]">
   <xsl:call-template name="valOrRsrource"/>; 
  </xsl:for-each>
 </xsl:template>

 <xsl:template name="collectnodeelts">
  <xsl:param name="nid"/>
  <!--<dfn>nodeID</dfn>:<xsl:value-of select="$nid"/>; -->
  <xsl:for-each select="//*[@rdf:nodeID=$nid]">
   <xsl:if test="*">
    <xsl:call-template name="propval"/>; 
   </xsl:if>
  </xsl:for-each>
 </xsl:template>

 <xsl:template name="collectresourse">
  <xsl:param name="ab"/>
  <!--<dfn>about</dfn>:<xsl:value-of select="$ab"/>; -->
  <xsl:variable name="anode" select="//*[@rdf:about=$ab]"/>
  <xsl:choose>
   <xsl:when test="count($anode) &gt; 1">
    <xsl:for-each select="$anode">
     <xsl:call-template name="valOrRsrource"/>; 
    </xsl:for-each>
   </xsl:when>
   <xsl:when test="starts-with($ab,'http:')">
    about: <a href="{$ab}"><xsl:value-of select="$ab"/></a>; 
   </xsl:when>
   <xsl:otherwise>
    <dfn>about</dfn>: <xsl:value-of select="$ab"/>; 
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="concatelt">
  <xsl:param name="d"/>
  <xsl:param name="d2"/>
  <xsl:for-each select="$d">
   <xsl:value-of select="."/><xsl:text> </xsl:text>
  </xsl:for-each>
  <xsl:for-each select="$d2">
   (<!--<xsl:if test="not(../@rdf:parseType)"><xsl:value-of select="local-name(..)"/>: </xsl:if>--><xsl:value-of select="."/>)<xsl:text> </xsl:text>
  </xsl:for-each>
 </xsl:template>

 <xsl:template match="foaf:mbox_sha1sum">
  <dfn><xsl:value-of select="local-name()"/></dfn>: 
  <acronym title="{.}"><xsl:value-of select="substring(.,1,6)"/>...</acronym> ; 
 </xsl:template>

 
 <!-- Attribute value -->
 <xsl:template match="@*">
  <dfn><xsl:value-of select="local-name()"/></dfn>: 
  <xsl:value-of select="."/>; 
 </xsl:template>

 <xsl:template match="@rdf:nodeID|@rdf:parseType">
 </xsl:template>

 <xsl:template match="i:rrule/i:*">
  <dfn><xsl:value-of select="local-name()"/></dfn>: 
  <xsl:value-of select="."/>; 
 </xsl:template>

 <xsl:template match="@i:*">
  <dfn><a href="http://www.kanzaki.com/docs/ical/{local-name()}.html"><xsl:value-of select="local-name()"/></a></dfn>: 
  <xsl:value-of select="."/>; 
 </xsl:template>

 <!-- Component properties from others -->
 <xsl:template match="i:component/*/*|i:Vevent/*|rss:item/rss:*">
  <tr>
   <th class="xterms"><xsl:value-of select="name()"/></th>
   <td>
    <xsl:call-template name="valOrRsrource"/>
   </td>
  </tr>
 </xsl:template>

 <xsl:template name="valOrRsrource">
  <xsl:choose>
   <xsl:when test="*">
    <xsl:apply-templates select="*"/>
   </xsl:when>
   <xsl:when test="@rdf:resource">
    <xsl:apply-templates select="@rdf:resource"/>
   </xsl:when>
   <!--test 2004-01-27 ancestor with this XSL target element-->
   <xsl:when test="@rdf:nodeID[ancestor::i:Vevent]">
    <xsl:call-template name="collectnodeelts">
     <xsl:with-param name="nid"><xsl:value-of select="@rdf:nodeID"/></xsl:with-param>
    </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="."/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <!-- Component properties from ical namespace -->
 <xsl:template match="i:Vevent/i:*|i:Vtimezone/i:*|i:component/*/i:*">
  <tr>
   <th>
    <a href="http://www.kanzaki.com/docs/ical/{local-name()}.html"><xsl:value-of select="local-name()"/></a>
   </th>
   <td>
    <xsl:call-template name="valOrRsrource"/>
    <xsl:call-template name="geomap"/>
   </td>
  </tr>
 </xsl:template>

 <!-- add a map button if contains geo: properties -->
 <xsl:template name="geomap">
  <xsl:if test=".//geo:lat">
   <button><xsl:attribute name="onclick"><xsl:value-of select="concat('geo.showmap(',.//geo:lat,',',.//geo:long)"/>,'<xsl:value-of select="..//i:tzid"/>');</xsl:attribute>Map</button>
   <xsl:if test="contains($mkcateg,'Rehearsal')">
    <button><xsl:attribute name="onclick"><xsl:value-of select="concat('geo.showmap(',.//geo:lat,',',.//geo:long)"/>,'<xsl:value-of select="..//i:tzid"/>','park');</xsl:attribute>P-Map</button>
   </xsl:if>
  </xsl:if>
 </xsl:template>

 <!-- Component properties: esp. dtstart and dtend -->
 <xsl:template match="i:component/*/i:dtstart|i:component/*/i:dtend|i:Vevent/i:dtstart|i:Vevent/i:dtend">
  <tr>
   <th class="dtse"><a href="http://www.kanzaki.com/docs/ical/{local-name()}.html"><xsl:value-of select="local-name()"/></a></th>
   <td><xsl:apply-templates select="*"/></td>
  </tr>
 </xsl:template>


 <!-- Component property values: esp. date and dateTime -->
 <xsl:template match="i:date|i:dateTime">
  <xsl:value-of select="."/>
 </xsl:template>

 <!-- Component property value: esp. tzid -->
 <xsl:template match="i:tzid">
  (<dfn><a href="http://www.kanzaki.com/docs/ical/tzid.html"><xsl:value-of select="local-name()"/></a></dfn>: <xsl:value-of select="."/>)
 </xsl:template>

 <!-- Show URI refrence with readable format -->
 <xsl:template match="@rdf:resource">
  <xsl:choose>
   <xsl:when test="contains(.,'http://www.w3.org/2002/12/cal/ical#')">
    <em>#<xsl:value-of select="substring-after(.,'#')"/></em>
   </xsl:when>
   <xsl:when test="starts-with(.,'#')">
    <xsl:variable name="ab" select="substring(.,2)"/>
    <xsl:for-each select="//*[@rdf:ID=$ab]">
     <xsl:call-template name="valOrRsrource"/>; 
    </xsl:for-each>
   </xsl:when>
   <!--
   <xsl:when test="local-name(../.)='depiction'">
    <a href="{.}">image</a>
   </xsl:when>
   -->
   <xsl:otherwise>
    <xsl:call-template name="respoint"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="respoint">
  <xsl:variable name="ab" select="."/>
  <xsl:variable name="elt" select="name(../.)"/>
  <xsl:variable name="anode" select="//*[@rdf:about=$ab]"/>
  <xsl:choose>
   <xsl:when test="$elt='an:annotates'">
    <a href="{.}"><xsl:value-of select="."/></a>
   </xsl:when>
   <xsl:when test="$anode">
    <xsl:for-each select="$anode">
     <xsl:choose>
     <!-- test reffering to the same image -->
      <xsl:when test="local-name() = 'Image'"><a href="{@rdf:about}">image</a></xsl:when>
      <xsl:otherwise><xsl:call-template name="valOrRsrource"/>; </xsl:otherwise>
     </xsl:choose>
    </xsl:for-each>
   </xsl:when>
   <xsl:otherwise>
    <a href="{.}">
     <xsl:choose>
      <xsl:when test="string-length(.) &gt; 50"><xsl:value-of select="substring(.,1,48)"/>...</xsl:when>
      <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
     </xsl:choose>
    </a>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <!-- Previous/Next calendar -->
 <xsl:template name="prevNext">
  <xsl:if test="i:Vcalendar/rs:prev">
   | <a id="refprev" href="{i:Vcalendar/rs:prev/@rdf:resource}">&lt;&lt; Previous calendar data</a> 
  </xsl:if>
  <xsl:if test="i:Vcalendar/rs:next">
   | <a id="refnext" href="{i:Vcalendar/rs:next/@rdf:resource}">Next calendar data &gt;&gt;</a> | 
  </xsl:if>
 </xsl:template>

 <!-- XHTML head elements -->
 <xsl:template name="htmlhead">
  <link rel="stylesheet" href="//www.kanzaki.com/parts/kan01.css" type="text/css" />
  <link rel="stylesheet" href="//www.kanzaki.com/parts/ical.css" type="text/css" />
  <style type="text/css">.f .f .f{font-size:small}</style>
  <script type="text/javascript" src="//www.kanzaki.com/parts/cal.js">//</script>
  <xsl:if test="//i:Vevent/i:geo">
   <script type="text/javascript" src="/parts/geo.js">//</script>
   <!--script type="text/javascript" src="http://www.kanzaki.com/parts/geo.js">//</script-->
  </xsl:if>
 </xsl:template>

 <!-- Event Summary -->
 <xsl:template name="events">
  <xsl:variable name="ev" select=".//i:Vevent"/>
  <xsl:variable name="evc" select="count($ev)"/>
  <xsl:if test="$evc">
   <h2 id="esum">Event Summary</h2>
   <p><xsl:value-of select="$evc"/> Event<xsl:if test="$evc &gt; 1">s</xsl:if> found in this <a href="http://www.kanzaki.com/docs/sw/rdf-calendar.html">RDFical</a> file. See <a href="#components">Components section</a> for detail.</p>
   <div id='mcal'>(If everything goes well, you'll find a monthly calendar here...)</div>
   <table id="allevents" border="1" cellspacing="0" cellpadding="3">
    <thead><tr><th>Starts</th><th>Ends</th><th>Summary</th><th>Location</th><th>Rules</th></tr></thead>
    <tbody id="slist">
    <xsl:for-each select="$ev">
     <xsl:sort select="i:dtstart/i:dateTime|i:dtstart/i:date"/>
     <!--tr title="{i:dtstart/i:dateTime|i:dtstart/i:date}"-->
     <tr>
      <td><xsl:value-of select="i:dtstart/i:dateTime|i:dtstart/i:date"/></td>
      <td><xsl:value-of select="i:dtend/i:dateTime|i:dtend/i:date"/></td>
      <td><a href="#{generate-id(.)}"><xsl:value-of select="i:summary|../../rss:title"/></a></td>
      <td><xsl:value-of select="i:location"/></td>
      <td>
       <xsl:if test="i:rrule">
        <xsl:call-template name="evt-reccur">
         <xsl:with-param name="r" select="i:rrule"/>
        </xsl:call-template>
       </xsl:if>
      </td>
     </tr>
    </xsl:for-each>
    </tbody>
   </table>
  </xsl:if>
 </xsl:template>

 <xsl:template name="evt-reccur">
  <xsl:param name="r"/>
  <small>
  <xsl:value-of select="$r/i:freq"/> (
  <xsl:choose>
   <xsl:when test="$r/i:byday|$r/i:bymonth">
    <xsl:value-of select="$r/i:byday"/>
    <xsl:if test="$r/i:bymonth"> in month <xsl:value-of select="$r/i:bymonth"/></xsl:if>
    <xsl:if test="$r/i:count">: <xsl:value-of select="$r/i:count"/> time<xsl:if test="number($r/i:count)&gt;1">(s)</xsl:if></xsl:if>
    <xsl:if test="$r/i:until"> until <xsl:value-of select="$r/i:until"/></xsl:if>
   </xsl:when>
   <xsl:otherwise>*see reccurence rule</xsl:otherwise>
  </xsl:choose>
  )</small>
 </xsl:template>
 
 <!--<xsl:include href="./calscript.xsl"/>-->
 <xsl:include href="./banner-footer.xsl"/>

</xsl:stylesheet>
