<?xml version="1.0"?> 
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  exclude-result-prefixes="java" xmlns:java="http://xml.apache.org/xslt/java"
  version="1.0"
>
	<xsl:template name="tv.showFilters">
		<xsl:param name="width">95%</xsl:param>
		<xsl:param name="params"/>
		
		<table border="0" align="center" width="{$width}" cellpadding="0" cellspacing="0">
			<tr>
				<td style="font-size: 12px">
<!--
														<input type="text" size="25" style="height: 20px" name="terms" value="{@terms}" onFocus="this.select()"/>
-->
					<script language="javascript">
						var filterShown=false;

						function hideFilter() {
							filterShown=false;
							filter.style.visibility='hidden';
						}
												
						function toggleFilter() {
							filter.style.visibility=filterShown?'hidden':'visible';
							filterShown=!filterShown;
						}
					</script>

					<xsl:if test="params/prefix or params/terms or params/category or params/rating>-1"><a href="favourite?op=create&amp;prefix={params/prefix}&amp;terms={params/terms}&amp;category={params/category}&amp;ratingKey={params/rating}"><img border="0" src="/images/favourite.png"></img></a></xsl:if>

					<span id="filterButton" onClick="toggleFilter()" align="center">
						<xsl:choose>
							<xsl:when test="params/prefix or params/terms or params/category or params/rating>-1">Filter</xsl:when>
							<xsl:otherwise>Filter Programmes</xsl:otherwise>
						</xsl:choose>
					</span> 

					<!-- this was a tough little bugger -->										
					<xsl:if test="boolean(currentSet)">
						<xsl:variable name="url"><xsl:value-of select="$page"/>?prefix=<xsl:if test="params/terms">&amp;terms=<xsl:value-of select="terms"/></xsl:if><xsl:if test="params/category">&amp;category=<xsl:value-of select="params/category"/></xsl:if><xsl:if test="params/rating&gt;-1">&amp;rating=<xsl:value-of select="params/rating"/></xsl:if><xsl:if test="$params">&amp;<xsl:value-of select="$params"/></xsl:if></xsl:variable>
						channel set: <a href="channel?setKey={currentSet/params/key}"><b><xsl:value-of select="currentSet/name"/></b></a>&#160;<a href="{$url}"><img border="0" src="/images/remove.png"/></a>&#160;&#160;
					</xsl:if>
					
					<xsl:if test="params/prefix and params/prefix!=''">
						<xsl:variable name="url"><xsl:value-of select="$page"/>?prefix=<xsl:if test="params/terms">&amp;terms=<xsl:value-of select="terms"/></xsl:if><xsl:if test="params/category">&amp;category=<xsl:value-of select="params/category"/></xsl:if><xsl:if test="params/rating&gt;-1">&amp;rating=<xsl:value-of select="params/rating"/></xsl:if><xsl:if test="params/channelSet&gt;-1">&amp;channelSet=<xsl:value-of select="params/channelSet"/></xsl:if><xsl:if test="$params">&amp;<xsl:value-of select="$params"/></xsl:if></xsl:variable>
						starting with: <b><xsl:value-of select="params/prefix"/></b>&#160;<a href="{$url}"><img border="0" src="/images/remove.png"/></a>&#160;&#160;
					</xsl:if>
					<xsl:if test="params/terms and params/terms!=''">
						<xsl:variable name="url"><xsl:value-of select="$page"/>?prefix=<xsl:if test="params/prefix!=''"><xsl:value-of select="params/prefix"/></xsl:if><xsl:if test="params/category">&amp;category=<xsl:value-of select="params/category"/></xsl:if><xsl:if test="params/rating&gt;-1">&amp;rating=<xsl:value-of select="params/rating"/></xsl:if><xsl:if test="params/channelSet&gt;-1">&amp;channelSet=<xsl:value-of select="params/channelSet"/></xsl:if><xsl:if test="$params">&amp;<xsl:value-of select="$params"/></xsl:if></xsl:variable>
					  terms: <b><xsl:value-of select="params/terms"/></b>&#160;<a href="{$url}"><img border="0" src="/images/remove.png"/></a>&#160;&#160;
					</xsl:if>
					<xsl:if test="params/category and params/category!=''">
						<xsl:variable name="url"><xsl:value-of select="$page"/>?prefix=<xsl:if test="params/prefix!=''"><xsl:value-of select="params/prefix"/></xsl:if><xsl:if test="params/terms">&amp;terms=<xsl:value-of select="params/terms"/></xsl:if><xsl:if test="params/rating&gt;-1">&amp;rating=<xsl:value-of select="params/rating"/></xsl:if><xsl:if test="params/channelSet&gt;-1">&amp;channelSet=<xsl:value-of select="params/channelSet"/></xsl:if><xsl:if test="$params">&amp;<xsl:value-of select="$params"/></xsl:if></xsl:variable>
						category: <b><xsl:value-of select="params/category"/></b>&#160;<a href="{$url}"><img border="0" src="/images/remove.png"/></a>&#160;&#160;
					</xsl:if>
					<xsl:if test="params/rating&gt;-1">
						<xsl:variable name="url"><xsl:value-of select="$page"/>?prefix=<xsl:if test="params/prefix!=''"><xsl:value-of select="params/prefix"/></xsl:if><xsl:if test="params/terms">&amp;terms=<xsl:value-of select="params/terms"/></xsl:if><xsl:if test="params/category">&amp;category=<xsl:value-of select="params/category"/></xsl:if><xsl:if test="params/channelSet&gt;-1">&amp;channelSet=<xsl:value-of select="params/channelSet"/></xsl:if><xsl:if test="$params">&amp;<xsl:value-of select="$params"/></xsl:if></xsl:variable>
						<xsl:variable name="ratingName"><xsl:for-each select="rating">
							<xsl:if test="params/key=../params/rating"><xsl:value-of select="params/name"/></xsl:if>
						</xsl:for-each></xsl:variable>
					  rating: <b><xsl:value-of select="$ratingName"/></b>&#160;<a href="{$url}"><img border="0" src="/images/remove.png"/></a>&#160;&#160;
					</xsl:if>
					
					<br/>
					<form name="filterForm">
            <!--
						<input type="hidden" name="page" value="{$page}"/>
						<xsl:call-template name="dumpParams">
							<xsl:with-param name="params"><xsl:value-of select="$params"/></xsl:with-param>
						</xsl:call-template>
						-->
            
						<table border="0" style="font-size: 12px" id="filter">
              <!-- pointless
							<tr>
								<td>starting with:</td>
								<td>
									<select name="prefix" onChange="document.filterForm.submit()">
										<option value="">any</option>
										<xsl:call-template name="tv.getPrefixOptions"/>
									</select>
								</td>
							</tr>
               -->
							<tr>
								<td>terms:</td>
								<td><input type="text" height="20" name="terms" value="{params/terms}"/></td>
							</tr>
              <tr>
                <td>filter duplicates:</td>
                <td><input type="checkbox" name="filterDups">
                <xsl:choose>
                <xsl:when test="params/filterDups='false'"></xsl:when>
                <xsl:otherwise><xsl:attribute name="checked">true</xsl:attribute></xsl:otherwise>
                </xsl:choose>
                </input></td>
              </tr>
              <svc op="onGetChannelSets" sig="ca.reachable.tv.listings.service.ChannelService.getChannelSets()"/>
              <svc op="onGetCategories" sig="ca.reachable.tv.listings.service.CategoryService.getCategories()"/>
              <!-- rating currently disabled
							<tr>
								<td>rating:</td>
								<td>
									<select name="rating" style="height: 20px" onChange="document.filterForm.submit()">
										<option value="">any</option>
										<xsl:for-each select="rating">
											<xsl:sort select="@name"/>
											<option value="{@key}">
												<xsl:if test="@key=../@rating"><xsl:attribute name="selected"/></xsl:if>
												<xsl:value-of select="@name"/>
											</option>
										</xsl:for-each>
									</select>									
								</td>
							</tr>
            <script language="javascript">
              var i;
              var sel;
              <xsl:if test="../@rating!=''">
              for (i=0;i&lt;document.filterForm.rating.options.length;i++) {
                if (document.filterForm.rating.options[i].value=<xsl:value-of select="../@rating"/>) {
                  sel=i;
                  break;
                }
              }
              </xsl:if>
              if (sel&gt;0)
                document.filterForm.rating.selectedIndex=sel;
            </script>
               -->
						</table>
					</form>
				</td>
			</tr>
		</table>
	</xsl:template>

  <xsl:template match="onGetChannelSets">
    <tr>
      <td>channel set:</td>
      <td>
        <select name="setKey" onChange="document.filterForm.submit()">
          <option value="">all</option>
          <option value="auto:HDTV">HDTV</option>
          <!-- not yet implemented
          <option value="auto:favourites">favourite shows</option>
           -->
          <xsl:for-each select="result/item">
            <option value="{params/key}">
              <xsl:if test="params/currentSet=name">
                <xsl:attribute name="selected">selected</xsl:attribute>
              </xsl:if>
              <xsl:value-of select="name"/>
            </option>
          </xsl:for-each>
        </select>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="onGetCategories">
    <xsl:if test="result/item">
      <xsl:variable name="cat"><xsl:value-of select="params/category"/></xsl:variable>
      <tr>
        <td>category:</td>
        <td>
        <!-- 
      <textarea rows="40" cols="80"><xsl:copy-of select="."/></textarea>
       -->
          <select name="category" style="height: 20px" onChange="document.filterForm.submit()">
            <option value="">any</option>
            <xsl:for-each select="result/item">
              <option value="{@name}">
                <xsl:if test="@name=$cat"><xsl:attribute name="selected"/></xsl:if>
                <xsl:value-of select="@name"/>
              </option>
            </xsl:for-each>
          </select>
        </td>
      </tr>
    </xsl:if>
  </xsl:template>

	<xsl:template name="tv.getPrefixOptions">
		<xsl:param name="curr">abcdefghijklmnopqrstuvwxyz0</xsl:param>
		<xsl:variable name="start"><xsl:value-of select="substring($curr,1,1)"/></xsl:variable>
		<option value="{$start}">
			<xsl:if test="@prefix=$start"><xsl:attribute name="selected"/></xsl:if>
			<xsl:value-of select="$start"/>
		</option>
		<xsl:variable name="next"><xsl:value-of select="substring($curr,2,string-length($curr))"/></xsl:variable>
		<xsl:if test="$next!=''">
			<xsl:call-template name="tv.getPrefixOptions">
				<xsl:with-param name="curr"><xsl:value-of select="$next"/></xsl:with-param>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>		

	<xsl:template name="dumpParams">
		<xsl:param name="params"/>
		<xsl:choose>
				<xsl:when test="contains($params,',')">
					<xsl:variable name="param"><xsl:value-of select="substring-before($params,',')"/></xsl:variable>
					<input type="hidden" name="{substring-before($param,'=')}" value="{substring-after($param,'=')}"/>
					<xsl:call-template name="dumpParams">
						<xsl:with-param name="params"><xsl:value-of select="substring-after($params,',')"/></xsl:with-param>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
					<input type="hidden" name="{substring-before($params,'=')}" value="{substring-after($params,'=')}"/>
				</xsl:otherwise>
		</xsl:choose>
		
	</xsl:template>
	
    <xsl:template name="tv.dumpIMDBInfo">
        <xsl:for-each select="augments/item/movie">
        <p/>
    <xsl:if test="rating/num">
        Rating: <xsl:value-of select="rating/num"/>/<xsl:value-of select="rating/den"/> (<xsl:value-of select="rating/votes"/> votes)<br/>
      </xsl:if>
        <xsl:if test="directors">
        Director<xsl:if test="count(directors/item)&gt;1">s</xsl:if>:
        <xsl:for-each select="directors/item">
          <a href="{@url}"><xsl:value-of select="."/></a><xsl:if test="position()&lt;count(../item)">, </xsl:if>
        </xsl:for-each>
        <br/>
        </xsl:if>
        <xsl:if test="writers">
        Writer<xsl:if test="count(writers/item)&gt;1">s</xsl:if>:
        <xsl:for-each select="writers/item">
          <a href="{person/@url}"><xsl:value-of select="person"/></a> (<xsl:value-of select="contribution"/>)<xsl:if test="position()&lt;count(../item)">, </xsl:if>
        </xsl:for-each>
        <br/>
        </xsl:if>
        <xsl:if test="cast/item">
        Cast:<br/>
        <div style="position: relative; margin-left: 10px; top: -10px">
        <xsl:for-each select="cast/item">
          <xsl:choose>
            <xsl:when test="person/@url"><a href="{person/@url}"><xsl:value-of select="person"/></a></xsl:when>
            <xsl:otherwise><xsl:value-of select="person"/></xsl:otherwise>
          </xsl:choose>
          as
          <xsl:choose>
            <xsl:when test="character/@url"><a href="{character/@url}"><xsl:value-of select="character"/></a></xsl:when>
            <xsl:otherwise><xsl:value-of select="character"/></xsl:otherwise>
          </xsl:choose>
          <br/>
        </xsl:for-each>
        </div>
        </xsl:if>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>
