<?xml version="1.0" encoding="iso-8859-1"?>

<!--
 (C) Mise 2004
 Non-commercial usage is granted without explicit permission
-->

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes" encoding="ISO-8859-1"
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>

  <!-- The _now parameter is a UNIX timestamp for current time (GMT+1) -->
  <xsl:param name="_now"/>

  <xsl:template match="page">

    <xsl:variable name="row">
      <xsl:value-of select="(position() mod 2) + 1"/>
      <xsl:if test="$_now - timestamp &lt;= (60 * 60 * 24)">
        <xsl:text>bold</xsl:text>
      </xsl:if>
    </xsl:variable>
    <tr>

      <td class="row{$row}" style="text-align:right">
        <xsl:text>&#160;&#160;</xsl:text>
        <xsl:if test="count(in/diff) &gt; 0">
          <a class="nl" href="javascript:diff('in',{position()})">
            <xsl:text>+</xsl:text>
            <xsl:value-of select="count(in/diff)"/>
          </a>
        </xsl:if>
      </td>
        
      <td class="row{$row}" style="text-align:right">
        <xsl:if test="count(out/diff) &gt; 0">
          <a class="nl" href="javascript:diff('out',{position()})">
            <xsl:text>-</xsl:text>
            <xsl:value-of select="count(out/diff)"/>
          </a>
        </xsl:if>
        <xsl:text>&#160;&#160;</xsl:text>
      </td>

      <td class="row{$row}" style="text-align:center;">
        <xsl:choose>
          <xsl:when test="error = 99">
            <span class="gone">not monitored</span>
          </xsl:when>
          <xsl:when test="error = 1">
            <span class="offline">-- offline --</span>
            </xsl:when>
          <xsl:when test="error = 2">
            <span class="broken">-- broken --</span>
            </xsl:when>
          <xsl:otherwise>
            <span class="online">-- online --</span>
          </xsl:otherwise>
        </xsl:choose>
      </td>
      <td class="row{$row}">
        <a href="go.ico?{timestamp}&amp;q={homepage}" title="{homepage}">
          <xsl:value-of select="name"/>
        </a>
        <xsl:if test="homepage != checkurl">
          <xsl:text> (</xsl:text>
          <a href="go.ico?{timestamp}&amp;q={checkurl}" title="{checkurl}">
            <xsl:text>updates</xsl:text>
          </a>
          <xsl:text>)</xsl:text>
        </xsl:if>
      </td>
      <td class="row{$row}">
        <xsl:value-of select="timetext"/>
      </td>
    </tr>
  </xsl:template>


  <xsl:template match="in|out" mode="diff">

    <xsl:param name="array"/>
    <xsl:param name="difftype"/>
      
    <xsl:copy-of select="$array"/>
    <xsl:choose>
      <xsl:when test="$difftype = 'in'">
        <xsl:text>[2] = </xsl:text>
      </xsl:when>
      <xsl:when test="$difftype = 'out'">
        <xsl:text>[3] = </xsl:text>
      </xsl:when>      
    </xsl:choose>

    <xsl:if test="count(diff) = 0">
      <xsl:text>'';
      </xsl:text>
    </xsl:if>

    <xsl:for-each select="diff">
      <xsl:text>'</xsl:text>
      <tr>
        <xsl:text>'+'</xsl:text>

        <xsl:choose>
          <xsl:when test="@line != ''">

            <xsl:variable name="break">
              <xsl:if test="not(((number(@line) + 1) = following-sibling::diff/@line) or (position() = last())) and (following-sibling::diff/@line != '')">
                <xsl:text>break</xsl:text>
              </xsl:if>
            </xsl:variable>

            <td class="diffn{$break}">
              <xsl:text>'+"</xsl:text>
              <xsl:value-of select="@line"/>
              <xsl:text>"+'</xsl:text>
            </td>

            <xsl:text>'+'</xsl:text>
              
            <td class="diffl{$break}">
              <xsl:text>'+'</xsl:text>
              <span class="{$difftype}">
                <xsl:choose>
                  <xsl:when test="$difftype = 'in'">
                    <xsl:text>+ '+"</xsl:text>
                  </xsl:when>
                  <xsl:when test="$difftype = 'out'">
                    <xsl:text>- '+"</xsl:text>
                  </xsl:when>
                </xsl:choose>
              </span>
              <xsl:value-of select="."/>
              <xsl:text> "+'</xsl:text>
            </td>

          </xsl:when>
          <xsl:otherwise>

            <td class="diffnMore">
              <xsl:text>'+'&#160;'+'</xsl:text>
            </td>
            <xsl:text>'+'</xsl:text>
            <td class="difflMore">
              <xsl:text>'+"... and more ..."+'</xsl:text>
            </td>
            
          </xsl:otherwise>
        </xsl:choose>

        <xsl:text>'+"</xsl:text>
      </tr>

      <xsl:choose>
        <xsl:when test="position()=last()">
          <xsl:text>";
          </xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>" +
          </xsl:text>
        </xsl:otherwise>
      </xsl:choose>

    </xsl:for-each>

  </xsl:template>

    
  <xsl:template match="page" mode="diff">

    <xsl:variable name="array">
      <xsl:text>difflist[</xsl:text>
      <xsl:value-of select="position()"/>
      <xsl:text>]</xsl:text>
    </xsl:variable>

    <xsl:if test="boolean(in/diff) or boolean(out/diff)">
      <xsl:value-of select="$array"/>
      <xsl:text>[1] = "</xsl:text>
      <xsl:value-of select="name"/>
      <xsl:text>";
      </xsl:text>

      <xsl:apply-templates select="in" mode="diff">
        <xsl:with-param name="difftype">
          <xsl:text>in</xsl:text>
        </xsl:with-param>
        <xsl:with-param name="array">
          <xsl:value-of select="$array"/>
        </xsl:with-param>
      </xsl:apply-templates>

      <xsl:apply-templates select="out" mode="diff">
        <xsl:with-param name="difftype">
          <xsl:text>out</xsl:text>
        </xsl:with-param>
        <xsl:with-param name="array">
          <xsl:value-of select="$array"/>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:if>
      
  </xsl:template>


  <xsl:template match="/root/page" mode="jarray">
    <xsl:if test="boolean(out/diff) or boolean(in/diff)">
      <xsl:text>difflist[</xsl:text>
      <xsl:value-of select="position()"/>
      <xsl:text>] = new Array(3);
      </xsl:text>
    </xsl:if>
  </xsl:template>


  <xsl:template match="/">

    <html>
      <head>
        <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type"/>
        <title>Alternative keeptrack</title>
        <link href="style.css" rel="stylesheet" type="text/css"/>
        <link href="rubberslug.css" rel="alternate stylesheet" type="text/css" title="Rubberslug ;-)"/>
     </head>

     <body>
     <script type="text/javascript">


      <xsl:text>
        var difflist = new Array(</xsl:text>
        <xsl:value-of select="count(/root/page)"/>
        <xsl:text>);
      </xsl:text>

       <xsl:apply-templates select="/root/page" mode="jarray">
         <xsl:sort data-type="number" order="descending" select="timestamp"/>
         <xsl:sort data-type="text" order="ascending" select="name"/>
       </xsl:apply-templates> 

       <xsl:apply-templates select="/root/page" mode="diff">
         <xsl:sort data-type="number" order="descending" select="timestamp"/>
         <xsl:sort data-type="text" order="ascending" select="name"/>
       </xsl:apply-templates> 



      function diff(what, which) {

        document.getElementById('diffheader').innerHTML = 
        ((what=='in') ? "New text" : "Removed text") + " :: " + difflist[which][1];
   
      <!-- this is for IE being stupid (yes, IE6 too) and not understanding CSS white-space:pre -->

        var blob = difflist[which][(what=='in')?2:3];
        blob = blob.replace(/\ \ /g, ' &amp;#160;');
        blob = blob.replace(/\&lt;td\ /g, '&lt;td nowrap="nowrap" ');

        document.getElementById('diff').innerHTML = blob;

      }

//     </script>

       <h1>Alternative keeptrack</h1>
          <p>

<!--
text here
<br/><br/>
-->
          <xsl:text>Currently monitoring </xsl:text>
          <xsl:value-of select="count(/root/page[error!='99'])"/>
          <xsl:text> pages. </xsl:text>
         
          <xsl:choose>
            <xsl:when test="count(/root/page[error='99']) = 1">
              <xsl:text> 1 page is not monitored for some reason.</xsl:text>
            </xsl:when>
            <xsl:when test="count(/root/page[error='99']) &gt; 1">
              <xsl:value-of select="count(/root/page[error='99'])"/>
              <xsl:text> pages are not monitored for some reason.</xsl:text>
            </xsl:when>
          </xsl:choose>
<br/>
Use the <b>+</b>/<b>-</b> links in the <i>Difference</i> column to see what has changed.<br/>
<br/><i>We're back online again if you hadn't noticed~</i>
<br/>

    </p>

    <table cellspacing="0" border="0">
      <tr>
        <td colspan="5" align="right">
<!-- funny graphic here -->
        </td>
     </tr>
      <tr>
        <td class="header" colspan="5" id="diffheader">
          &#160;
        </td>
      </tr>
      <tr>
        <td colspan="5" class="diffmonitor">
          <div>
            <table cellspacing="0" border="0" class="nopadd" style="width:100%" id="diff">
              <tr>
                <td class="diffn">
                  &#160;
                </td>
                <td class="diffl">
                  <xsl:text>&#160;</xsl:text>
                </td>
              </tr>
            </table>
          </div>
        </td>
      </tr>
      <tr>
        <td class="header" colspan="2">Difference</td>
        <td class="header">Status</td>
        <td class="header">Name</td>
        <td class="header">Content changed</td>
      </tr>

      <xsl:apply-templates select="/root/page">
        <xsl:sort data-type="number" order="descending" select="timestamp"/>
        <xsl:sort data-type="text" order="ascending" select="name"/>
      </xsl:apply-templates> 

    </table>


    <h2>Resources</h2>

    <ul>
      <li>
        The <a href="master.xml">XML master list</a>. It is updated every hour, on the hour.
      </li>
      <li>
        Example of a <a href="stylesheet.xsl">XSL stylesheet</a>
        (what you see now).
      </li>
      <li>
        Regarding the complete backend source code: No, it is not free, sorry.
      </li>
      <li>
        If you don't want to be on this list,
        <a href="mailto:henrik(!at)x25.se">tell me about it</a>.
      </li>
      <li>
        If you <i>do</i> want to be on this list or want to suggest a page,
        <a href="mailto:henrik(!at)x25.se">tell me about it</a> too.
      </li>
    </ul>
    <p>
      <!-- because interoperable is a complicated word -->
      <a href="http://validator.w3.org/check?uri=referer"><img
          src="valid-xhtml10" style="border:none"
          alt="Valid XHTML 1.0 Strict" height="31" width="88" /></a>
    </p>
    </body>
    </html>
  </xsl:template>

</xsl:stylesheet>
