<!--                                                                  -->
<!--  Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved     -->
<!--  $Id$  -->
<!--                                                                  -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:dyn="http://exslt.org/dynamic" extension-element-prefixes="dyn">
    <xsl:output method="html" indent="no" doctype-system="" encoding="utf-8" />

    <xsl:template match="Root">
        <div>
            <table class="Table">
                <thead>
                    <tr class="Table-row Table-row-header">
                        <th class="Table-col">Catalog</th>
                        <th class="Table-col">Title</th>
                        <th class="Table-col">Artist</th>
                        <th class="Table-col">Tracks</th>
                        <th class="Table-col">Album ID</th>
                    </tr>
                </thead>
                <tbody>
                    <xsl:for-each select="Distribution/Albums/Album">
                        <xsl:variable name="row">
                            <xsl:choose>
                                <xsl:when test="position() mod 2 = 1">row1</xsl:when>
                                <xsl:otherwise>row0</xsl:otherwise>
                            </xsl:choose>
                        </xsl:variable>

                        <tr class="Table-row Table-row-data">
                            <xsl:call-template name="displayAlbum" />
                        </tr>

                        <xsl:if test="count(Tracks/Track)">
                            <tr class="Table-row Table-row-data">
                                <td class="Table-col" colspan="5">
                                    <table class="Table">
                                        <thead>
                                            <tr class="Table-row Table-row-header">
                                                <th class="Table-col"><xsl:text> </xsl:text></th>
                                                <th class="Table-col">Disc</th>
                                                <th class="Table-col">Track</th>
                                                <th class="Table-col">Title</th>
                                                <th class="Table-col">Artist</th>
                                                <th class="Table-col">ISRC</th>
                                                <th class="Table-col">ID</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <xsl:for-each select="Tracks/Track">
                                                <tr class="Table-row Table-row-data">
                                                    <xsl:call-template name="displayTrack" />
                                                </tr>
                                            </xsl:for-each>
                                        </tbody>
                                    </table>
                                </td>
                            </tr>
                        </xsl:if>

                    </xsl:for-each>
                </tbody>
            </table>
        </div>
    </xsl:template>

    <xsl:template name="displayAlbum">
        <xsl:call-template name="tableDataItem">
            <xsl:with-param name="value" select="catalog_no" />
        </xsl:call-template>

        <xsl:call-template name="tableDataItem">
            <xsl:with-param name="value" select="album_title" />
            <xsl:with-param name="width" select="'45'" />
        </xsl:call-template>

        <xsl:call-template name="tableDataItem">
            <xsl:with-param name="value" select="album_artist" />
            <xsl:with-param name="width" select="'20'" />
        </xsl:call-template>

        <xsl:call-template name="tableDataItem">
            <xsl:with-param name="value" select="track_count" />
            <xsl:with-param name="class" select="'alignCenter'" />
        </xsl:call-template>

        <xsl:call-template name="tableDataItem">
            <xsl:with-param name="value" select="client_album_id" />
            <xsl:with-param name="width" select="'15'" />
        </xsl:call-template>
    </xsl:template>


    <xsl:template name="displayTrack">
        <td></td>
        <xsl:call-template name="tableDataItem">
            <xsl:with-param name="value" select="disc_no" />
        </xsl:call-template>

        <xsl:call-template name="tableDataItem">
            <xsl:with-param name="value" select="track_number" />
        </xsl:call-template>

        <xsl:call-template name="tableDataItem">
            <xsl:with-param name="value" select="track_title" />
            <xsl:with-param name="width" select="'35'" />
        </xsl:call-template>

        <xsl:call-template name="tableDataItem">
            <xsl:with-param name="value" select="track_artist" />
            <xsl:with-param name="width" select="'20'" />
        </xsl:call-template>

        <xsl:call-template name="tableDataItem">
            <xsl:with-param name="value" select="isrc" />
        </xsl:call-template>

        <xsl:call-template name="tableDataItem">
            <xsl:with-param name="value" select="client_track_id" />
            <xsl:with-param name="width" select="'10'" />
        </xsl:call-template>
    </xsl:template>


    <xsl:template name="tableDataItem">
        <xsl:param name="message" />
        <xsl:param name="status" />
        <xsl:param name="value" />
        <xsl:param name="width" />

        <xsl:variable name="errorClass">
            <xsl:if test="$status"> Table-col-error</xsl:if>
        </xsl:variable>

        <xsl:variable name="cellData">
            <xsl:choose>
                <xsl:when test="$width">
                    <xsl:call-template name="truncateString">
                        <xsl:with-param name="string" select="$value" />
                        <xsl:with-param name="width"  select="$width" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$value" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="title">
            <xsl:choose>
                <xsl:when test="$status and not ($status = 7 or $status = 8)">
                    <xsl:value-of select="$message" />
                </xsl:when>
                <xsl:when test="string-length($cellData) != string-length($value)">
                    <xsl:value-of select="$value" />
                </xsl:when>
            </xsl:choose>
        </xsl:variable>

        <td title="{$title}" class="Table-col {$errorClass}">
            <xsl:value-of select="$cellData" />
        </td>
    </xsl:template>

    <xsl:template name="truncateString">
        <xsl:param name="string" />
        <xsl:param name="width" />

        <xsl:choose>
            <xsl:when test="string-length($string) &gt; $width - 3">
                <xsl:value-of select="substring( $string, 1, $width - 3 )" />...
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$string" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

