<?xml version="1.0" encoding="UTF-8"?>

<!--
/**
 * Rendering stylesheet for Liferea (item view: item rendering)
 *
 * Copyright (C) 2006-2009 Lars Lindner <lars.lindner@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 *
 * This program 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
--> 

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

<xsl:output method="xml" version="1.0" indent="yes"/>

<!-- the local pixmap directory (e.g. /usr/share/liferea/pixmaps/) -->
<xsl:param name="pixmapsDir"/>
<xsl:param name="baseUrl"/>
<xsl:param name="summary"/>	<!-- 1=summary rendering, 0=normal rendering -->
<xsl:param name="single"/>	<!-- 1=single item rendering, 0=2 pane mode -->

<xsl:template match="/itemset">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
</head>
<xsl:element name="body">  
<!-- GeoRSS OpenStreet map rendering needs this -->
<xsl:if test="item/attributes/attribute[ @name = 'point' ]">
   <xsl:attribute name="onload">load()</xsl:attribute>
</xsl:if>

<!-- base URL of the itemset -->
<div href="{$baseUrl}">

<xsl:for-each select="item">
  <xsl:choose>
    <xsl:when test="$summary = '1'">
      <xsl:call-template name="item_summary"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="item"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:for-each>

</div> <!-- end of base URL div -->

</xsl:element>
</html>

</xsl:template>

<!-- ==================================================================== -->

<!---single item detailed mode rendering -->
<xsl:template name="item">

<!-- base URL of parent feed -->
<div href="{/itemset/feed/feedSource}">

<!-- the item -->

<xsl:variable name="onMouseOver">
   <xsl:text disable-output-escaping='yes'>doShow('</xsl:text>
   <xsl:value-of select="sourceId"/>-<xsl:value-of select="nr"/>
   <xsl:text disable-output-escaping='yes'>');</xsl:text>
</xsl:variable>

<!-- entering header starts delayed show of the item menu -->
<!-- leaving header stops show counter -->
<div onmouseover="{$onMouseOver}" onmouseout="stopShow();">

<!-- header table -->
<xsl:variable name="flagclass">
  <xsl:if test="mark = '0'">un</xsl:if>
  <xsl:text>flagged</xsl:text>
</xsl:variable>
<table class="itemhead {$flagclass}" cellspacing="0" cellpadding="0">

<tr>
  <td valign="middle" class="headleft">
    <!-- Feed link as favicon -->
    
    <xsl:variable name="favicon">
      <xsl:choose>
        <xsl:when test="not(sourceFavicon)">
          <xsl:value-of select="/itemset/feed/favicon"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="sourceFavicon"/>
        </xsl:otherwise>
      </xsl:choose>   
    </xsl:variable>
    
    <a class="favicon" href="{/itemset/feed/attributes/attribute[ @name = 'homepage' ]}">
      <img src="{$favicon}"/>
    </a>
  </td>
  <td width="100%" valign="middle" class="headright">
    <!-- Item title as link -->
    <a class="itemhead" href="{source}">
      <xsl:value-of select="title"/>
      <!-- the title added above might be empty, in this 
           case we add the date of the post -->
      <xsl:if test="title = ''">
         <xsl:value-of select="timestr"/> 
      </xsl:if>
    </a>
  </td>
</tr>
</table>

<!-- header metadata (author + categories + date + source feed) -->
<table class='headmeta' cellspacing="0" cellpadding="0">

<xsl:if test="attributes/attribute[ @name = 'slash' ]">
<tr>
 <td valign="top" class='slash'> 
   <xsl:apply-templates select="attributes/attribute[ @name = 'slash']"/>
 </td>
</tr> 
</xsl:if>

<xsl:if test="attributes/attribute[ @name = 'realSourceUrl' ]">
<tr>
 <td valign="top" class='source'> 
     <_span>Source</_span>
     <b><span class='source'>
       <a href="{attributes/attribute[ @name = 'realSourceUrl' ]}">
         <xsl:value-of select="attributes/attribute[ @name = 'realSourceTitle' ]"/>
       </a>
     </span></b>
 </td>
</tr>
</xsl:if>

<!-- If there is no item set source then this is a merged item list
     rendering and we should display the source feed title for the item.
     Only exception: News Bins which do not have an itemset source. -->
<xsl:if test="not(/itemset/source) and /itemset/feed/feedTitle">
<tr>
 <td valign="top" class='source'> 
     <_span>Feed</_span>
     <b><span class='source'>
       <a href="{/itemset/feed/attributes/attribute[ @name = 'homepage' ]}">
         <xsl:value-of select="/itemset/feed/feedTitle"/>
       </a>
     </span></b>
 </td>
</tr>
</xsl:if>

<xsl:if test="attributes/attribute[ @name = 'category' ]">
<tr>
 <td valign="top" class='categories'>
     <_span>Filed under</_span>
     <b><span class='categories'>
       <xsl:for-each select="attributes/attribute[ @name = 'category' ]">
         <xsl:if test="position() != 1">, </xsl:if>
         <xsl:value-of select="." disable-output-escaping='yes'/>
       </xsl:for-each>
     </span></b>
 </td>
</tr>
</xsl:if>

<xsl:if test="attributes/attribute[ @name = 'author' ]">
 <tr>
   <td valign="top" class='author'>
     <_span>Author</_span>
     <b><span class='author'>
       <xsl:value-of select="attributes/attribute[ @name = 'author' ]" disable-output-escaping='yes'/>
     </span></b>
   </td>
 </tr>
</xsl:if>  

<xsl:if test="attributes/attribute[ @name = 'sharedby' ]">
 <tr>
   <td valign="top" class='sharedby'>
     <_span>Shared by</_span>
     <b><span class='sharedby'>
       <xsl:value-of select="attributes/attribute[ @name = 'sharedby' ]" disable-output-escaping='yes'/>
     </span></b>
   </td>
 </tr>
</xsl:if>  

<!-- Indicate Atom "via" and "related" links -->
<xsl:for-each select="attributes/attribute[ @name = 'via' ]">
<tr>
 <td valign="top" class='source'> 
     <_span>Via</_span>
     <b><span class='source'>
       <a href="{.}">
         <xsl:value-of select="."/>
       </a>
     </span></b>
 </td>
</tr>
</xsl:for-each>
<xsl:for-each select="attributes/attribute[ @name = 'related' ]">
<tr>
 <td valign="top" class='source'> 
     <_span>Related</_span>
     <b><span class='source'>
       <a href="{.}">
         <xsl:value-of select="."/>
       </a>
     </span></b>
 </td>
</tr>
</xsl:for-each>

<!-- Indicate all duplicates -->
<xsl:for-each select="duplicates/duplicateNode">
<tr>
 <td valign="top" class='source'> 
     <_span>Also posted in</_span>
     <b><span class='source'>
         <xsl:value-of select="."/>
     </span></b>
 </td>
</tr>
</xsl:for-each>

<xsl:if test="attributes/attribute[ @name = 'creator' ]">
 <tr>
   <td valign="top" class='creator'>
     <_span>Creator</_span>
     <b><span class='creator'>
       <xsl:value-of select="attributes/attribute[ @name = 'creator' ]" disable-output-escaping='yes'/>
     </span></b>
   </td>
 </tr>
</xsl:if>  

</table> <!-- end of header metadata -->

<!-- construct unique id to allow Javascript showing/hiding of itemmenu -->
<xsl:variable name="hiddenMenu">
   <xsl:if test="$single = 0">hidden</xsl:if>
</xsl:variable>

<table id="{sourceId}-{nr}" class="headmeta {$hiddenMenu}"
       cellspacing="0" cellpadding="0">
<tr>
 <td class='itemmenu'> 

  <!-- for comment link and search engine link -->

  <!-- Flagging link -->
  <a class="flag" href="liferea-flag://{sourceId}-{sourceNr}">
    <xsl:variable name="png">
      <xsl:if test="mark = '0'">grayflag.png</xsl:if>
      <xsl:if test="mark = '1'">flag.png</xsl:if>
    </xsl:variable>
    <img border="0" class="flagbtn" src="{$pixmapsDir}{$png}"/>
    <_span>flag</_span>
  </a>
   
  <!-- Tagging link -->
  <!--<a class="bookmark" href="liferea-tag://{sourceId}-{nr}">
    <img border="0" class="tagbtn" src="{$pixmapsDir}tag.png"/>
    <_span>add tag</_span>
  </a>-->

  <!-- Bookmarking link -->
  <a class="bookmark" href="liferea-bookmark://{sourceId}-{nr}">
    <img border="0" class="bookmarkbtn" src="{$pixmapsDir}bookmark.png"/>
    <_span>bookmark</_span>
  </a>

  <!-- Comments link -->
  <xsl:if test="attributes/attribute[ @name = 'commentsUri' ]">
    <a class="comments" href="{attributes/attribute[ @name = 'commentsUri' ]}">
      <img border="0" class="commentsbtn" src="{$pixmapsDir}comments.png"/>
      <_span>comments</_span>
    </a>
  </xsl:if>

  <!-- link cosmos search -->
  <a class="linksearch"
     href="liferea-link-search://{sourceId}-{nr}">
    <img border="0" class="linksearchbtn" src="{$pixmapsDir}technorati.png"/>
    <_span>link cosmos</_span>
  </a>

 </td><!-- end of comment and search engine link -->
</tr>
</table> <!-- itemmenutable -->

<xsl:variable name="shading">
  <xsl:if test="readStatus = '0'">itemshaded</xsl:if>
  <xsl:if test="readStatus != '0'">itemunshaded</xsl:if>
</xsl:variable>

<div class="{$shading}">
<div class='content'>
  <!-- insertion div for GeoRSS coordinates, adding an OpenStreet map -->
  <xsl:if test="attributes/attribute[ @name = 'point' ]">
  	<p id="map" style="width:400px;height:200px;float:right; border: 1px solid #000;"></p>
  </xsl:if>
  
  <!-- extra item content handling for GeoRSS coordinates, adding an OpenStreet map -->
  <xsl:if test="attributes/attribute[ @name = 'point' ]">  
	  <script src="http://www.openlayers.org/api/OpenLayers.js"></script>
	  <script src="http://www.openstreetmap.org/openlayers/OpenStreetMap.js"></script>
	  <script type="text/javascript">
		  <![CDATA[var lat=]]><xsl:value-of select="substring-before(attributes/attribute[ @name = 'point' ],' ')"/><![CDATA[
		  var lon=]]><xsl:value-of select="substring-after(attributes/attribute[ @name = 'point' ],' ')"/>
		  var zoom=9


		  var map;
        	  function load() {

		  map = new OpenLayers.Map ("map", {
                  controls:[
			  new OpenLayers.Control.Navigation(),
			  new OpenLayers.Control.PanZoom(),
		          new OpenLayers.Control.Attribution()],
        	  maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
		  maxResolution: 156543.0399,
		  numZoomLevels: 19,
		  units: 'm',
		  projection: new OpenLayers.Projection("EPSG:900913"),
		  displayProjection: new OpenLayers.Projection("EPSG:4326")
		  } );

		  layerTilesAtHome = new OpenLayers.Layer.OSM.Osmarender("Osmarender");
		  map.addLayer(layerTilesAtHome);

		  var lonLat = new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
		  layerMarkers = new OpenLayers.Layer.Markers("Markers");
		  map.addLayer(layerMarkers);

		  map.setCenter (lonLat, zoom);
		  var size = new OpenLayers.Size(21,25);
		  var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
		  var icon = new OpenLayers.Icon('http://www.openstreetmap.org/openlayers/img/marker.png',size,offset);
		  layerMarkers.addMarker(new OpenLayers.Marker(lonLat,icon));
        	  }

	  </script>
  </xsl:if>

  <!-- the item's content -->
  <p>
    <!-- optional gravatar -->
    <xsl:if test="attributes/attribute[ @name = 'gravatar' ]">
       <img align='left' class='gravatar' src="{attributes/attribute[ @name = 'gravatar' ]}"/>
    </xsl:if>
    
    <!-- the real text content -->
    <xsl:value-of select="description" disable-output-escaping='yes'/>
  </p>
  
  <!-- photo namespace handling -->
  <xsl:if test="attributes/attribute[ @name = 'photo' ]">
     <xsl:element name="img">
        <xsl:attribute name="src">
	   <xsl:call-template name="substring-after-last">
	     <xsl:with-param name="input"  select="attributes/attribute[ @name = 'photo' ]" />
	     <xsl:with-param name="substr" select="','" />
	   </xsl:call-template>
        </xsl:attribute>
     </xsl:element>
  </xsl:if>
  
  <!-- comment handling -->
  <xsl:if test="attributes/attribute[ @name = 'commentFeedUri' ] and not(./commentsSuppressed) and $single = 1"> 
     <p>     
     <xsl:choose>
        <xsl:when test="count(comments/item) = 0">
           <b><_span>No comments yet.</_span></b>
        </xsl:when>
        <xsl:otherwise>
	   <b><_span>Comments</_span></b>
	</xsl:otherwise>
     </xsl:choose>
  
     <!-- Display "comment feed updating" hint -->
     <xsl:choose>
        <xsl:when test="comments/updateState = 'updating'">
           (<span>
	      <_span>Updating...</_span>
	    </span>)
	</xsl:when>
        <xsl:otherwise>
           (<a href="liferea-refresh-comments://{sourceId}-{sourceNr}">
              <_span>Refresh</_span>
           </a>)
        </xsl:otherwise>
     </xsl:choose>
     
     </p>
     
     <!-- Display error box if necessary -->
     <xsl:if test="comments/updateError">
        <p>
	   <div id="errors">
	      <div id="updateError">
	         <xsl:value-of select="comments/updateError"/>
	      </div>
	   </div>
	</p>
     </xsl:if>
     
     <p>     
        <xsl:apply-templates select="comments/item">
           <xsl:sort select="time"/>
        </xsl:apply-templates>
     </p>    
  </xsl:if>
</div>
</div> <!-- end of shading div -->
</div> <!-- end of base URL div -->

</div> <!-- item -->

<!-- extra item content handling for GeoRSS coordinates, adding an OpenStreet map -->
<xsl:if test="attributes/attribute[ @name = 'point' ]">  
	<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
	<script src="http://www.openstreetmap.org/openlayers/OpenStreetMap.js"></script>
	<script type="text/javascript">
		  <![CDATA[var lat=]]><xsl:value-of select="substring-before(attributes/attribute[ @name = 'point' ],' ')"/><![CDATA[
		  var lon=]]><xsl:value-of select="substring-after(attributes/attribute[ @name = 'point' ],' ')"/>
		  var zoom=9


		  var map;
        	  function load() {

		  map = new OpenLayers.Map ("map", {
                  controls:[
			  new OpenLayers.Control.Navigation(),
			  new OpenLayers.Control.PanZoom(),
		          new OpenLayers.Control.Attribution()],
        	  maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
		  maxResolution: 156543.0399,
		  numZoomLevels: 19,
		  units: 'm',
		  projection: new OpenLayers.Projection("EPSG:900913"),
		  displayProjection: new OpenLayers.Projection("EPSG:4326")
		  } );

		  layerTilesAtHome = new OpenLayers.Layer.OSM.Osmarender("Osmarender");
		  map.addLayer(layerTilesAtHome);

		  var lonLat = new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
		  layerMarkers = new OpenLayers.Layer.Markers("Markers");
		  map.addLayer(layerMarkers);

		  map.setCenter (lonLat, zoom);
		  var size = new OpenLayers.Size(21,25);
		  var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
		  var icon = new OpenLayers.Icon('http://www.openstreetmap.org/openlayers/img/marker.png',size,offset);
		  layerMarkers.addMarker(new OpenLayers.Marker(lonLat,icon));
        	  }

	</script>
</xsl:if>

</xsl:template>

<!-- ==================================================================== -->

<!-- slashdot namespace -->
<xsl:template match="attributes/attribute[ @name = 'slash']">
  <span class="slashSection"><_span>Section</_span></span>
  <span class="slashValue"><xsl:value-of select="substring-before(., ',')"/></span>
  <span class="slashDepartment"><_span>Department</_span></span>
  <span class="slashValue"><xsl:value-of select="substring-after(., ',')"/></span>
</xsl:template>

<!-- ==================================================================== -->

<!-- comment rendering -->
<xsl:template match="comments/item">
   <div class="comment">
      <div class="comment_title"><xsl:value-of select="title"/></div>
      <div class="comment_body"><xsl:value-of select="description" disable-output-escaping='yes'/></div>
   </div>
</xsl:template>

<!-- ==================================================================== -->

<!--- single item summary mode rendering -->
<xsl:template name="item_summary">

<xsl:variable name="summaryShading">
   <xsl:choose>
      <xsl:when test="readStatus = '0'">summaryshaded</xsl:when>
      <xsl:otherwise>summaryunshaded</xsl:otherwise>
   </xsl:choose> summary<xsl:if test="mark = '0'">un</xsl:if>flagged
</xsl:variable>
    
<div class="{$summaryShading}">
  <table cellspacing="0" cellpadding="0" width="100%">
  <tr>
    <td class="summarytime" valign="top">
      <xsl:value-of select="timestr"/>
    </td>
    <td width="100%">
      <!-- Item title as link -->
      <a href="{source}">
        <xsl:value-of select="title"/>
      </a>

      <!-- summary mode means most of the items have no description,
           but lets output them for those who have one... -->
      <xsl:if test="description">
        <br/><br/>
        <xsl:value-of select="description" disable-output-escaping='yes'/>
      </xsl:if>

    </td>
  </tr>
  </table>
    
  <hr class='summary'/>

</div>
</xsl:template>

<!-- ==================================================================== -->

<!--- substring after last occurance also from "XSLT Cookbook"... -->
<xsl:template name="substring-after-last">
    <xsl:param name="input" />
    <xsl:param name="substr" />
    
    <!-- Extract the string which comes after the first occurence -->
    <xsl:variable name="temp" select="substring-after($input,$substr)"/>
    
    <xsl:choose>
      <!-- If it still contains the search string then recursively process -->
      <xsl:when test="$substr and contains($temp,$substr)">
        <xsl:call-template name="substring-after-last">
          <xsl:with-param name="input"  select="$temp" />
          <xsl:with-param name="substr" select="$substr" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$temp" />
      </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>
