<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet exclude-result-prefixes="java" version="1.0"
    xmlns:java="http://xml.apache.org/xslt/java" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
    <xsl:output method="xml"/>
    <xsl:param name="user"/>
    <xsl:param name="render"/>
    <xsl:param name="PLAY_BRIEF">Play video</xsl:param>  
    <xsl:param name="PLAY_HELP">Play video in a seperate window.  <b>f</b> to toggle fullscreen, space to pause.</xsl:param>  
  <xsl:template name="video.dumpVideo">
  <xsl:variable name="pos"><xsl:value-of select="java:hashCode(path)"/></xsl:variable>
    <table border="0" cellpadding="0" cellspacing="0" class="video" id="{pos}">
    <tr>
      <td align="center"  onclick="play('{path}')" class="video-thumb">
        <xsl:call-template name="video.dumpThumb"/>
      </td>
      <td valign="top"  class="video-info">
        <xsl:call-template name="video.dumpInfo"/>
      </td>
      <td valign="top" align="center" class="video-icons" width="15px">
        <xsl:call-template name="video.dumpIcons"/>
      </td>
    </tr>
    </table>
  </xsl:template>
    
  <xsl:template name="video.dumpThumb">
    <xsl:choose>
      <xsl:when test="shot">
        <a onclick="play('{path}')">
          <img src="/images/video_thumb_overlay_blank.png" class="thumb" border="0"
            id="{modified}"
            style="background: url(/media/video/{shot})"
            onMouseOver="this.src='/images/video_thumb_overlay.png'; showHelp('{$PLAY_BRIEF}','{$PLAY_HELP}');"  
            onMouseOut="this.src='/images/video_thumb_overlay_blank.png'; hideHelp();" 
            />
        </a>
      </xsl:when>
      <xsl:otherwise>
        <a onclick="play('{path}')">
          <img src="/images/video_thumb_blank.png" border="0" class="thumb"/>
        </a>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="video.dumpScript">
    <xsl:call-template name="service.script">
     <xsl:with-param name="service">ca.reachable.video.VideoService</xsl:with-param>
    </xsl:call-template>
    <script> 
    function play(file) { window.open( 'player.page?file='+file+'&amp;type=window&amp;fullscreen=true', 'videowindow',
      'width=640,height=505,top=0,left=0,titlebar=0,toolbar=0,location=0,directories=no,status=0,menubar=0,scrollbars=0,copyhistory=no,resizable=no');
    }
    <xsl:if test="java:hasPermission($user,'ca.reachable.video.VideoService.delete')">
    var deletePosition;
    function confirmDelete(name,path,position) {
      if (confirm('Are you sure you wish to delete\n'+name+'?')) {
        deleteVideo(path);
        deletePosition=position;
      }
    } 
    function onDeleteVideo(url, http_request) {
      document.location.href=document.location.href;
    }
    </xsl:if>
    </script>
  </xsl:template>  

  <xsl:template name="video.dumpInfo">
    <xsl:param name="noDesc"/>
    <xsl:param name="datePattern">MMM dd HH:mm</xsl:param>
    
    <xsl:variable name="hasAiring"><xsl:for-each select="augments/item/airing">1</xsl:for-each></xsl:variable>
    <xsl:choose>
      <xsl:when test="$hasAiring!=''">
      <xsl:for-each select="augments/item/airing">
      <xsl:variable name="prodTime"><xsl:value-of select="programme/production-date/@time"/></xsl:variable>
      <xsl:variable name="year"><xsl:if test="$prodTime!=''"><xsl:value-of select="java:xsl.XSLUtil.format($prodTime,'yyyy')"/></xsl:if></xsl:variable>
      <xsl:variable name="isMovie"><xsl:for-each select="programme/categories/item"><xsl:if test="@name='Movie'">1</xsl:if></xsl:for-each></xsl:variable>
      <xsl:choose>
      <xsl:when test="string-length($isMovie)&gt;0">
        <a onclick="play('{../../../path}')" 
          onMouseOver="var elem=document.getElementById('{../../../modified}'); if (elem) elem.src='/images/video_thumb_overlay.png'; showHelp('{$PLAY_BRIEF}','{$PLAY_HELP}');"
          onMouseOut="var elem=document.getElementById('{../../../modified}'); if (elem) elem.src='/images/video_thumb_overlay_blank.png'; hideHelp();"
        >
          <b>
            <xsl:value-of select="programme/name"/> 
          </b>
        </a> <xsl:if test="$year!=''">(<xsl:value-of select="$year"/>)</xsl:if>  
          <a target="_new" href="http://imdb.com/find?s=tt&amp;q={programme/name} {$year}&amp;x=0&amp;y=0">
          <img class="icon" border="0" src="/images/icons/imdb.png"/>
          </a>
        
      </xsl:when>
      <xsl:otherwise>
        <a href="?style=shows&amp;show={programme/name}" style="word-wrap: break-word; font-weight: bold">
            <xsl:value-of select="programme/name"/>
        </a>
      </xsl:otherwise>
      </xsl:choose>
            

        <xsl:if test="programme/subtitle">
        <br/> 
        <a onclick="play('{../../../path}')" 
          onMouseOver="var elem=document.getElementById('{../../../modified}'); if (elem) elem.src='/images/video_thumb_overlay.png'; showHelp('{$PLAY_BRIEF}','{$PLAY_HELP}');"
          onMouseOut="var elem=document.getElementById('{../../../modified}'); if (elem) elem.src='/images/video_thumb_overlay_blank.png'; hideHelp();"
        >
          <i><xsl:value-of select="programme/subtitle"/></i>
        </a>
        </xsl:if>
        <br/>
        <span class="stats">
          <xsl:value-of select="../../../length-str"/>
          <xsl:if test="programme/categories/item">
            <span style="padding-left: 3px">
            <xsl:for-each select="programme/categories/item">
              <xsl:if test="position()&gt;1">,</xsl:if>
              <a href="?style=categories&amp;category={@name}"><xsl:value-of select="@name"/></a>
            </xsl:for-each>.
             </span><br/>
          </xsl:if>
          
          <xsl:value-of select="java:xsl.XSLUtil.format(start/@time,$datePattern)"/> on <a href="?style=channels&amp;channel={channel/num}"><xsl:value-of select="channel/num"/>&amp;nbsp;<xsl:value-of select="channel/call"/></a>
        </span>
        <xsl:if test="$noDesc='' and programme/desc">
          <span class="desc">
            <p/>
            <xsl:value-of
              select="java:xsl.tv.TVUtil.formatWithIMDBLinks(programme/desc)" />
          </span>
        </xsl:if> 
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <a onclick="play('{path}')" style="font-weight: bold; word-wrap: break-word" 
          onMouseOver="var elem=document.getElementById('{modified}'); if (elem) elem.src='/images/video_thumb_overlay.png'; showHelp('{$PLAY_BRIEF}','{$PLAY_HELP}');"
          onMouseOut="var elem=document.getElementById('{modified}'); if (elem) elem.src='/images/video_thumb_overlay_blank.png'; hideHelp();"
        >
          <xsl:value-of select="name"/>
        </a>
        <br/>
        <span class="stats">
        <xsl:value-of select="info/length-str"/>&amp;nbsp;
        <xsl:value-of select="java:xsl.XSLUtil.format(modified,$datePattern)"/>
        </span>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="video.dumpIcons">
    <xsl:param name="datePattern">MMM dd HH:mm</xsl:param>
    
    <xsl:call-template name="control.showAnIcon">
      <xsl:with-param name="type">play</xsl:with-param>
      <xsl:with-param name="prefix">play<xsl:value-of select="position()"/></xsl:with-param>
      <xsl:with-param name="display"><xsl:value-of select="$PLAY_BRIEF"/></xsl:with-param>
      <xsl:with-param name="help"><xsl:value-of select="$PLAY_HELP"/></xsl:with-param>
      <xsl:with-param name="onClick">play('<xsl:value-of select="path"/>')</xsl:with-param>
    </xsl:call-template>
    
    <xsl:variable name="movieName"><xsl:value-of select="airing/programme/name"/></xsl:variable>
    <xsl:variable name="prodTime"><xsl:value-of select="airing/programme/production-date/@time"/></xsl:variable>
    <xsl:for-each select="airing/programme/categories/item">
      <xsl:if test="@name='Movie'">
        <xsl:variable name="year"><xsl:if test="$prodTime!=''"> (<xsl:value-of select="java:xsl.XSLUtil.format($prodTime,'yyyy')"/>)</xsl:if></xsl:variable>
        <xsl:call-template name="control.showAnIcon">
          <xsl:with-param name="type">imdb</xsl:with-param>
          <xsl:with-param name="prefix">imdb<xsl:value-of select="position()"/></xsl:with-param>
          <xsl:with-param name="display">Visit IMDB</xsl:with-param>
          <xsl:with-param name="help">Find <xsl:value-of select="$movieName"/> <xsl:value-of select="$year"/> on IMDB.</xsl:with-param>
          <xsl:with-param name="url">http://imdb.com/find?s=tt&amp;q=<xsl:value-of select="$movieName"/><xsl:value-of select="$year"/>&amp;x=0&amp;y=0</xsl:with-param>
          <xsl:with-param name="target">_new</xsl:with-param>
        </xsl:call-template>
      </xsl:if>
    </xsl:for-each>
          
    <xsl:call-template name="control.showAnIcon">
      <xsl:with-param name="type">download</xsl:with-param>
      <xsl:with-param name="prefix">play<xsl:value-of select="position()"/></xsl:with-param>
      <xsl:with-param name="display">Download</xsl:with-param>
      <xsl:with-param name="help">Download video file.</xsl:with-param>
      <xsl:with-param name="url">/media/video<xsl:value-of select="path"/></xsl:with-param>
    </xsl:call-template>
    
    <!-- delete -->
    <xsl:if test="java:hasPermission($user,'ca.reachable.video.VideoService.delete')">
      <xsl:variable name="title"><xsl:value-of select="airing/programme/name"/><xsl:if test="airing/programme/subtitle"> - <xsl:value-of select="airing/programme/subtitle"/></xsl:if> recorded on <xsl:value-of select="java:xsl.XSLUtil.format(airing/start/@time,$datePattern)"/></xsl:variable> 
      <xsl:call-template name="control.showAnIcon">
        <xsl:with-param name="type">delete</xsl:with-param>
        <xsl:with-param name="prefix">delete<xsl:value-of select="position()"/></xsl:with-param>
        <xsl:with-param name="display">Delete</xsl:with-param>
        <xsl:with-param name="help">Delete video: <xsl:value-of select="$title"/></xsl:with-param>
        <xsl:with-param name="onClick">confirmDelete('<xsl:value-of select="$title"/>','<xsl:value-of select="path"/>',<xsl:value-of select="position()"/>)</xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
  
  <xsl:template name="video.header">
    <xsl:variable name="others"><xsl:if test="params/category and params/category!='*all*'">&amp;category=<xsl:value-of select="params/category"/></xsl:if><xsl:if test="params/show and params/show!='*all*'">&amp;show=<xsl:value-of select="params/show"/></xsl:if><xsl:if test="params/channel and params/channel!=0">&amp;channel=<xsl:value-of select="params/channel"/></xsl:if></xsl:variable>
    <xsl:variable name="currStyle"><xsl:if test="params/style">&amp;style=<xsl:value-of select="params/style"/></xsl:if></xsl:variable>
    <xsl:variable name="currOrder"><xsl:if test="params/order">&amp;order=<xsl:value-of select="params/order"/></xsl:if><xsl:value-of select="$others"/></xsl:variable>
    <xsl:variable name="currPage"><xsl:if test="params/page">&amp;page=<xsl:value-of select="params/page"/></xsl:if></xsl:variable>
    <xsl:variable name="currSpan"><xsl:if test="params/span">&amp;span=<xsl:value-of select="params/span"/></xsl:if></xsl:variable>
    <xsl:variable name="currPage"><xsl:value-of select="result/@curr-page"/></xsl:variable>
    <xsl:variable name="maxPage"><xsl:value-of select="result/num-pages"/></xsl:variable>
    <xsl:variable name="currSpan"><xsl:value-of select="params/span"/></xsl:variable>

<!--     
    style: <xsl:value-of select="params/style"/>
    order: <xsl:value-of select="params/order"/>
    category: <xsl:value-of select="params/category"/>
    show: <xsl:value-of select="params/show"/>
    channel: <xsl:value-of select="params/channel"/>
    <p/>
    <xsl:for-each select="result">
    <xsl:call-template name="util_XML">
    <xsl:with-param name="rows">80</xsl:with-param>
    </xsl:call-template>
    </xsl:for-each>
    <p/>
 -->    
 
    <script>
    <xsl:variable name="pageSuffix"><xsl:if test="params/span">&amp;span=<xsl:value-of select="params/span"/></xsl:if><xsl:if test="params/from"></xsl:if></xsl:variable>
    function pageChange(page) {
      var sel=document.getElementById('spanSel');
      var span=sel.options[sel.selectedIndex].value;
      var from=0;
      if (span!=-1)
        from=(page-1)*span;
      document.location.href='?from='+from+'<xsl:value-of select="$pageSuffix"/>';
    }
    
    <xsl:variable name="spanSuffix"><xsl:if test="params/page">&amp;page=<xsl:value-of select="params/page"/></xsl:if><xsl:if test="params/from"></xsl:if></xsl:variable>
    function spanChange(span) {
      document.location.href='?span='+span+'<xsl:value-of select="$spanSuffix"/>';
    }
    </script>
 
    
    <table width="100%" border="0" align="center" style="margin: 5px">
    <!-- disabled until ready 
      <tr>
      <td></td>
      <td align="center">
        <xsl:choose>
          <xsl:when test="not(params/style) or params/style='' or params/style='categories'">
            <b>categories</b>
          </xsl:when>
          <xsl:otherwise>
            <a href="index.page?style=categories{$currOrder}">categories</a> 
          </xsl:otherwise>
        </xsl:choose> 
        - 
        <xsl:choose>
          <xsl:when test="params/style='shows'">
            <b>shows</b>
          </xsl:when>
          <xsl:otherwise>
            <a href="index.page?style=shows{$currOrder}">shows</a> 
          </xsl:otherwise>
        </xsl:choose> 
        - 
        <xsl:choose>
          <xsl:when test="params/style='channels'">
            <b>channels</b>
          </xsl:when>
          <xsl:otherwise>
            <a href="index.page?style=channels{$currOrder}">channels</a> 
          </xsl:otherwise>
        </xsl:choose> 
      </td>
      <td align="center">
        <xsl:choose>
          <xsl:when test="not(params/order) or params/order='' or params/order='newest'">
            <b>newest first</b>
          </xsl:when>
          <xsl:otherwise>
            <a href="index.page?{$currStyle}">newest first</a> 
          </xsl:otherwise>
        </xsl:choose> 
        - 
        <xsl:choose>
          <xsl:when test="params/order='oldest'">
            <b>oldest first</b>
          </xsl:when>
          <xsl:otherwise>
            <a href="index.page?order=oldest{$currStyle}">oldest first</a> 
          </xsl:otherwise>
        </xsl:choose> 
        - 
        <xsl:choose>
          <xsl:when test="params/order='alpha'">
            <b>alphabetical</b>
          </xsl:when>
          <xsl:otherwise>
            <a href="index.page?order=alpha{$currStyle}">alphabetical</a> 
          </xsl:otherwise>
        </xsl:choose> 
      </td>
      <td></td>
      </tr>
       -->
      <tr>
      <td align="left" width="80">
        <xsl:if test="number($currPage)&gt;1">
        <xsl:attribute name="onclick">pageChange(<xsl:value-of select="number($currPage)-1"/>)</xsl:attribute>
        &lt;&lt; Prev
        </xsl:if>
      </td>
      <td align="center">
        Page <select style="height: 30px" onChange="pageChange(this.options[this.selectedIndex].value)">
        <xsl:call-template name="dumpPageOptions"/>
        </select>
      </td>  
      <td align="center">
        Showing <select  style="height: 30px" onChange="spanChange(this.options[this.selectedIndex].value)" id="spanSel">
        <option>
        <xsl:if test="not(params/span) or params/span='20'"><xsl:attribute name="selected">true</xsl:attribute></xsl:if>
        20</option>
        <option>
        <xsl:if test="params/span='50'"><xsl:attribute name="selected">true</xsl:attribute></xsl:if>
        50</option>
        <option>
        <xsl:if test="params/span='100'"><xsl:attribute name="selected">true</xsl:attribute></xsl:if>
        100</option>
        <option value="-1">
        <xsl:if test="params/span='-1'"><xsl:attribute name="selected">true</xsl:attribute></xsl:if>
        All</option>
        </select>
      </td>
      <td align="right" width="80">
        <xsl:if test="$currPage&lt;$maxPage">
        <xsl:attribute name="onclick">pageChange(<xsl:value-of select="number($currPage)+1"/>)</xsl:attribute>
        Next &gt;&gt;
        </xsl:if>
      </td>
      </tr>
    </table>
    <p/>
  </xsl:template>
  <xsl:template name="dumpPageOptions">
    <xsl:param name="curr">1</xsl:param>
    <xsl:param name="selected"><xsl:value-of select="result/@curr-page"/></xsl:param>
    <xsl:param name="span"/>
    
    <option><xsl:if test="number($selected)=number($curr)"><xsl:attribute name="selected">true</xsl:attribute></xsl:if>
    <xsl:value-of select="$curr"/></option>
    <xsl:if test="number($curr)&lt;number(result/num-pages)">
      <xsl:call-template name="dumpPageOptions">
        <xsl:with-param name="curr"><xsl:value-of select="number($curr)+1"/></xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

