<xsl:stylesheet version="1.0"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:dbagg3="http://decafbad.com/2004/07/dbagg3/"
                xmlns:atom="http://purl.org/atom/ns#" 
                xmlns:exsl="http://exslt.org/common"
                xmlns:date="http://exslt.org/dates-and-times"
                extension-element-prefixes="date exsl">

  <xsl:output indent="yes" 
	      method="xml" 
	      omit-xml-declaration="yes" />

  <xsl:template name="format-date">
    <xsl:param name="date" />
    <xsl:value-of select="concat(' ',
                          substring(date:month-name($date),1,3), ' ',
                          format-number(date:day-in-month($date),'00'), ' ',
                          format-number(date:hour-in-day($date),'00'), ':',
                          format-number(date:minute-in-hour($date),'00'))" />
  </xsl:template>
  
  <xsl:template match="/">
    <html>
      <head>
        <title>dbagg3 aggregated feeds</title>
        <link rel="stylesheet" href="css/main.css" media="screen" />
        <script language="javascript" src="js/jsrsClient.js"><xsl:text> </xsl:text></script>
        <script language="javascript" src="js/utils.js"><xsl:text> </xsl:text></script>
        <script language="javascript" src="js/agg.js"><xsl:text> </xsl:text></script>
      </head>
      <body>

        <div id="mainContainer">
          <div class="category">
            
            <xsl:for-each select="//atom:feed">
              <xsl:sort select="dbagg3:lastupdate | atom:modified" order="descending" />
              <xsl:variable name="feed_id" select="dbagg3:dbid | atom:id" />
              <xsl:variable name="feed_date" select="dbagg3:lastupdate | atom:modified" />
              <div class="feed" id="f_{$feed_id}">
                
                <div class="feedHeader">
                  <div class="feedTitle">
                    <a href="javascript:feedToggle('{$feed_id}')">[&#160;<span class="expandedHandle" id="fc_{$feed_id}_handle"><img src="images/spacer.gif" width="10" height="10" border="0"/></span>&#160;]</a>
                    <a href="{atom:link[@rel='alternate' and @type='text/html']/@href}"><xsl:value-of select="./atom:title" /></a>
                  </div>
                  <div class="feedMeta">
                    last update: 
                    <xsl:call-template name="format-date">
                      <xsl:with-param name="date" select="$feed_date" />
                    </xsl:call-template>
                    &#8212;
                    [seen]
                    <a href="{./atom:link[@rel='alternate' and @type='text/html']/@href}">[link]</a>
                    [queue]
                    <a href="javascript:entriesForFeedToggle('{$feed_id}')">[<span class="expandedHandleAlt"><img src="images/spacer.gif" width="10" height="10" border="0"/></span><span class="expandedHandleAlt"><img src="images/spacer.gif" width="10" height="10" border="0"/></span>]</a>
                    <a href="javascript:feedToggle('{$feed_id}')">[&#160;<span class="expandedHandleAlt" id="fc_{$feed_id}_handle_alt"><img src="images/spacer.gif" width="10" height="10" border="0"/></span>&#160;]</a>
                  </div>
                  <br style="clear:both" />
                </div>

                <div class="expanded" id="fc_{$feed_id}">
                  <div class="entries" id="fc_{$feed_id}_entries">
                    <xsl:for-each select="atom:entry">
                      <xsl:sort select="dbagg3:created | atom:modified" order="descending" />

                      <xsl:variable name="entry_full_title" select="( atom:title | atom:summary )[1]" />

                      <xsl:variable name="entry_title">
                        <xsl:choose>
                          <xsl:when test="string-length($entry_full_title) > 95">
                            <xsl:value-of select="concat(substring($entry_full_title, 1, 95), '...')" />
                          </xsl:when>
                          <xsl:otherwise>
                            <xsl:value-of select="$entry_full_title" />
                          </xsl:otherwise>
                        </xsl:choose>
                      </xsl:variable>
                      
                      <xsl:variable name="entry_id"    
                                    select="( atom:id | dbagg3:dbid )[last()]" />

                      <xsl:variable name="entry_date"  
                                    select="( atom:modified | dbagg3:created )[last()]" />

                      <xsl:variable name="entry_link"
                                    select="atom:link[@rel='alternate' and @type='text/html']/@href" />

                      <xsl:variable name="entry_content"
                                    select="atom:content" />

                      <xsl:variable name="entry_summary">
                        <xsl:choose>
                          <xsl:when test="string-length(atom:summary) > 0"><xsl:value-of select="atom:summary" /></xsl:when>
                          <xsl:otherwise><xsl:value-of select="atom:content" /></xsl:otherwise>
                        </xsl:choose>
                      </xsl:variable>
                      
                      <xsl:variable name="rowclass">
                        <xsl:choose>
                          <xsl:when test="position() mod 2 = 0">entryEven</xsl:when>
                          <xsl:otherwise>entryOdd</xsl:otherwise>
                        </xsl:choose>
                      </xsl:variable>
                      
                      <div class="entry" id="e_{$entry_id}">

                        <div class="entryHeader {$rowclass}">
                          
                          <span class="entryHandle">
                            <xsl:choose>
                              <xsl:when test="string-length($entry_summary)>0">
                                <a href="javascript:entryToggle('{$entry_id}')">[&#160;<span class="collapsedHandle" id="ec_{$entry_id}_handle"><img src="images/spacer.gif" width="10" height="10" border="0"/></span>&#160;]</a>
                              </xsl:when>
                              <xsl:otherwise>
                                <span class="missingHandle">[&#160;<img src="images/spacer.gif" width="10" height="10" border="0"/>&#160;]</span>
                              </xsl:otherwise>
                            </xsl:choose>
                          </span>

                          <span class="entryDate">
                            <xsl:call-template name="format-date">
                              <xsl:with-param name="date" select="$entry_date" />
                            </xsl:call-template>
                            &#8212;
                          </span>

                          <span class="entryTitle">
                            <xsl:choose>
                              <xsl:when test="$entry_link = ''">
                                <xsl:value-of select="$entry_title" />
                              </xsl:when>
                              <xsl:otherwise>
                                <a href="{$entry_link}" title="{$entry_full_title}"><xsl:value-of select="$entry_title" /></a>
                              </xsl:otherwise>
                            </xsl:choose>
                          </span>
                          
                          <span class="entryControls">
                            [seen]
                            <xsl:choose>
                              <xsl:when test="$entry_link=''">[link]</xsl:when>
                              <xsl:otherwise>[<a href="{$entry_link}">link</a>]</xsl:otherwise>
                            </xsl:choose>
                            [queue]
                            <xsl:choose>
                              <xsl:when test="string-length($entry_summary)>0">
                                <a href="javascript:entryToggle('{$entry_id}')">[&#160;<span class="collapsedHandleAlt" id="ec_{$entry_id}_handle_alt"><img src="images/spacer.gif" width="10" height="10" border="0"/></span>&#160;]</a>
                              </xsl:when>
                              <xsl:otherwise>
                                <span class="missingHandle">[&#160;<img src="images/spacer.gif" width="10" height="10" border="0"/>&#160;]</span>
                              </xsl:otherwise>
                            </xsl:choose>
                          </span>
                          <br style="clear: both" />

                        </div>

                        <div class="collapsed" id="ec_{$entry_id}">
                          <div class="entryBody">
                            <p class="entrySummary" id="es_{$entry_id}">
                              <xsl:value-of select="$entry_summary" disable-output-escaping="yes" />
                            </p>

                            <xsl:if test="$entry_content">
                              <a href="javascript:toggle('efc_{$entry_id}')">[<span class="collapsedHandle" id="efc_{$entry_id}_handle"><img src="images/spacer.gif" width="10" height="10" border="0"/></span>] Read more...</a>
                              <div class="collapsed" id="efc_{$entry_id}">
                                <div class="entryContent">
                                  <xsl:value-of select="$entry_content" disable-output-escaping="yes" />
                                </div>
                              </div>
                            </xsl:if>
                          </div>
                        </div>
                        
                      </div>

                    </xsl:for-each>
                  </div>
                </div>

              </div>
            </xsl:for-each>

          </div>
        </div>

      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>
