<!--                                                                  -->
<!--  Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved     -->
<!--  $Id$  -->
<!--                                                                  -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


  <xsl:template name="catalogLicenseViewLayout">
    <xsl:call-template name="catalogLicenseView" />
    <xsl:call-template name="buttons" />
  </xsl:template>


  <xsl:template name="catalogLicenseView">
    <div class="section">
      <xsl:call-template name="successMessageGeneral">
        <xsl:with-param name="successMessage">Album successfully updated!</xsl:with-param>
      </xsl:call-template>
      <xsl:call-template name="albumDetailView" /><br /><br />
    </div>  
    
    <xsl:call-template name="catalogViewTabs" />
    
    <div class="section withTabs"> 
      <xsl:call-template name="trackLicenseView" /><br /><br />
    </div>
  </xsl:template>
  

  <xsl:template name="trackLicenseView">
		<xsl:variable name="showCopy">
			<xsl:if test="count(Form/Album/TrackList/Track) &gt; 1">1</xsl:if>
		</xsl:variable>
    <fieldset id="trackLicenseViewTable">
      <legend>Track Licenses</legend>
        <table class="textChart">
          <tbody>
            <xsl:if test="Form/Album/TrackList/MechanicalEligible > 0">
              <xsl:if test="Form/Album/TrackList/Track/TrackLicenseList/TrackLicense">
								<tr>
	    	          <th class="rowLeft trackNumCol">Track</th>
  	    	        <th>Track Name</th>
    	  	        <th>Product Type</th>
    	  	        <th>Region</th>
      	  	      <th>Publisher</th>
		              <th>Admin</th>
    		          <th>Agent</th>
        		      <th class="amount">Share</th>
            		  <th class="status">Status</th>
		              <xsl:if test="$showCopy = 1">
    		            <th class="editCol">Copy</th>
        		      </xsl:if>
            		  <th class="editCol">Edit</th>
		            </tr>
							</xsl:if>
              <xsl:for-each select="Form/Album/TrackList/Track/TrackLicenseList/TrackLicense[../../MechanicalExempt = 0]">
								<xsl:call-template name="trackLicenseViewRow" />
              </xsl:for-each>
              <tr>
                <td colspan="10" class="rowLeft right">
									<xsl:if test="$showCopy = 1">
										<xsl:attribute name="colspan">11</xsl:attribute>
									</xsl:if>
                  <div class="bottomButton">
                    Select Track:
                    <select name="addLicenseList" id="addLicenseList" onChange="activateButton(this.value,'addLicenseButton')">
                      <option value="">Choose...</option>
                      <xsl:for-each select="Form/Album/TrackList/Track">
                        <xsl:if test="MechanicalExempt = 0">
                          <option value="{TrackID}">(<xsl:value-of select="TrackOrder" />) <xsl:value-of select="substring(Title,1,50)" /><xsl:if test="substring(Title,51,1)">...</xsl:if></option>
                        </xsl:if>
                      </xsl:for-each>
                    </select>&#160;
                    <input type="button" value="Add License" id="addLicenseButton" disabled="disabled" onClick="javascript:addTrackLicense('addLicenseList')" />
                  </div>
                  <xsl:call-template name="activeLicenseChecker">
                    <xsl:with-param name="stopPosition" select="count(Form/Album/TrackList/Track)" />
                    <xsl:with-param name="trackPosition">0</xsl:with-param>
                    <xsl:with-param name="activeLicenseCount">0</xsl:with-param>
                  </xsl:call-template>  
                </td>
              </tr>
            </xsl:if>
            <xsl:if test="Form/Album/TrackList/MechanicalEligible = 0">
              <tr>
                <td colspan="10" class="rowLeft emptyMessage">
                  <xsl:if test="$showCopy = 1">
                    <xsl:attribute name="colspan">11</xsl:attribute>
                  </xsl:if>
									<br />This album has no tracks eligible for mechanical royalties.
								</td>
              </tr>
            </xsl:if>
         </tbody>
      </table>
    </fieldset>
  </xsl:template>

  <xsl:template name="activeLicenseChecker">
    <xsl:param name="trackPosition"/>
    <xsl:param name="stopPosition"/>
    <xsl:param name="activeLicenseCount"/>
    <xsl:if test="$trackPosition = $stopPosition">
      <xsl:if test="Form/Album/TrackList/MechanicalEligible &gt; $activeLicenseCount">
        <xsl:variable name="tracksWithoutLicense"><xsl:value-of select="Form/Album/TrackList/MechanicalEligible - $activeLicenseCount" /></xsl:variable>
        <p>
          <xsl:if test="$tracksWithoutLicense = 1">
            <strong>1</strong> track currently has no active license associated with it.
          </xsl:if>
          <xsl:if test="$tracksWithoutLicense != 1">
          	<strong><xsl:value-of select="$tracksWithoutLicense" /></strong> tracks currently have no active licenses associated with them.
          </xsl:if>
        </p>
      </xsl:if>
    </xsl:if>

    <xsl:if test="$trackPosition != $stopPosition">
      <xsl:variable name="activeLicense">
      	<xsl:for-each select="Form/Album/TrackList/Track[MechanicalExempt = 0]">
      		<xsl:if test="@i = $trackPosition">
        		<xsl:for-each select="TrackLicenseList/TrackLicense">
							<xsl:if test="Inactive = 0">1</xsl:if>
        		</xsl:for-each>
        	</xsl:if>
        </xsl:for-each>
      </xsl:variable>

      <xsl:variable name="activeLicenseCountTest">
        <xsl:if test="normalize-space($activeLicense)">
          <xsl:value-of select="$activeLicenseCount + 1" />
        </xsl:if>
         <xsl:if test="not(normalize-space($activeLicense))">
          <xsl:value-of select="$activeLicenseCount" />
        </xsl:if>
      </xsl:variable>

      <xsl:call-template name="activeLicenseChecker">
        <xsl:with-param name="stopPosition" select="$stopPosition" />
        <xsl:with-param name="trackPosition" select="$trackPosition + 1" />
        <xsl:with-param name="activeLicenseCount" select="$activeLicenseCountTest" />
      </xsl:call-template>
    </xsl:if>

  </xsl:template>

  <xsl:template name="trackLicenseViewRow">
    <tr class="row{position() mod 2}">
      <td class="rowLeft">
        <xsl:if test="../../TrackOrder &lt; 10">0</xsl:if><xsl:value-of select="../../TrackOrder" />
      </td>
      <td><a href="license?c=show&amp;TrackLicenseID={TrackLicenseID}"><xsl:value-of select="../../Title" /></a></td>
      <td>
        <xsl:choose>
          <xsl:when test="normalize-space(ProductTypeID)">
            <xsl:variable name="productTypeID" select="ProductTypeID" />
            <xsl:value-of select="/Root/Form/TypeList/Type[ProductTypeID = $productTypeID]/Description" />
          </xsl:when>
          <xsl:otherwise>All Products</xsl:otherwise>
        </xsl:choose>    
      </td>
      <td>
        <xsl:if test="normalize-space(RegionID) and RegionID != 0">
          <xsl:variable name="regionID" select="RegionID" />
          <xsl:value-of select="/Root/Form/RegionList/Region[RegionID = $regionID]/Name" />
        </xsl:if>   
      </td>      
      <td>
        <xsl:choose>
          <xsl:when test="Type = 4">Public Domain</xsl:when>
          <xsl:otherwise><xsl:value-of select="PublisherName" /></xsl:otherwise>
        </xsl:choose>
      </td>
      <td>
        <xsl:if test="Type != 4"><xsl:value-of select="Publisher/AdminName" /></xsl:if>
      </td>
      <td>
        <xsl:if test="Type != 4"><xsl:value-of select="Publisher/AgentName" /></xsl:if>
      </td>
      <td class="amount"><xsl:value-of select="Share" />%</td>
			<td class="status">
				<xsl:if test="Inactive = 1">Inactive</xsl:if>
        <xsl:if test="Inactive = 0">Active</xsl:if>
			</td>
      <xsl:if test="count(/Root/Form/Album/TrackList/Track) &gt; 1">
        <td class="icon">
          <a href="/rps/license?c=copy&amp;TrackLicenseID={TrackLicenseID}" title="Copy">
            <img src="/production/images/icons/office/Copy-(16x16).gif" height="16" width="16" />
          </a>
        </td>
      </xsl:if>
      <td class="icon">
        <a href="/rps/license?c=edit&amp;TrackLicenseID={TrackLicenseID}" title="Edit">
          <img src="/production/images/icons/office/Edit-(16x16).gif" height="16" width="16" />
        </a>
      </td>
    </tr>
  </xsl:template>


</xsl:stylesheet>
