<!--                                                                  -->
<!--  Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved     -->
<!--                                                                  -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" indent="no" doctype-system="http://www.w3.org/TR/html4/strict.dtd" doctype-public="-//W3C//DTD HTML 4.01//EN" encoding="utf-8" />
    <xsl:key name="imprintById" match="/Root/ImprintList/Imprint" use="ImprintID" />

    <xsl:template name="bookDetailView">
        <xsl:param name="path" />
        <xsl:param name="product" />

        <xsl:variable name="coverArtSource">
            <xsl:choose>
                <xsl:when test="$product">
                    <xsl:value-of select="Product/CoverArtWeb" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="Book/CoverArtWeb" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <div class="Album-header-container">
            <h2 class="Album-header-text">Book</h2>
            <xsl:if test="$cmd = 'show_book'">
                <div class="Album-details-column Album-details-edit-button-wrapper">
                    <xsl:if test="Access/bookpub/catalog/edit_book">
                        <xsl:call-template name="uiButton">
                            <xsl:with-param name="content" select="'Edit Book'" />
                            <xsl:with-param name="url">
                                <xsl:text>catalog?BookID=</xsl:text>
                                <xsl:value-of select="$path/BookID" />
                                <xsl:text>&amp;c=edit_book</xsl:text>
                            </xsl:with-param>
                        </xsl:call-template>
                    </xsl:if>
                </div>
            </xsl:if>
        </div>
        <div class="Book-details-wrapper">
            <div class="Album-details-column Album-cover-wrapper">
                <img class="BookCover" src="{$coverArtSource}" />
            </div>
            <div class="Album-details-column">
                <xsl:call-template name="bookView">
                    <xsl:with-param name="path" select="$path" />
                </xsl:call-template>
                <xsl:if test="$product">
                    <xsl:call-template name="productView" />
                </xsl:if>
            </div>
        </div>
    </xsl:template>

    <xsl:template name="bookView">
        <xsl:param name="path" />
        <xsl:variable name="showAddToList">
            <xsl:choose>
                <xsl:when test="ClientTypes/ClientType[ClientTypeID = 10]/IsClientTypeFlag = 1">
                    <xsl:choose>
                        <xsl:when test="$cmd = 'show_book'">1</xsl:when>
                        <xsl:when test="Product/ProductFormat/BookFormatType/Code = 'DG'">1</xsl:when>
                        <xsl:when test="Product/ProductFormat/BookFormatType/Code = 'BB'">1</xsl:when>
                        <xsl:when test="Product/ProductFormat/BookFormatType/Code = 'BC'">1</xsl:when>
                        <xsl:otherwise>0</xsl:otherwise>
                    </xsl:choose>
                </xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <div class="ValuePair">
            <div class="ValuePair-item">
                <div class="ValuePair-label">Title</div>
                <div class="ValuePair-value">
                    <xsl:value-of select="$path/Title" />
                </div>
            </div>
            <div class="ValuePair-item">
                <div class="ValuePair-label">Subtitle</div>
                <div class="ValuePair-value"><xsl:value-of select="$path/Subtitle" /></div>
            </div>
            <xsl:if test="normalize-space($path/ContributorHash/Contributor/Name)">
                <div class="ValuePair-item">
                    <div class="ValuePair-label">Contributor</div>
                    <xsl:for-each select="$path/ContributorHash/Contributor">
                        <div class="ValuePair-value">
                            <xsl:call-template name="contributorCell">
                                <xsl:with-param name="contributor" select="." />
                            </xsl:call-template>
                        </div>
                    </xsl:for-each>
                </div>
            </xsl:if>
            <xsl:if test="normalize-space($path/AudienceCode/Name)">
                <div class="ValuePair-item">
                    <div class="ValuePair-label">Audience Code</div>
                    <div class="ValuePair-value"><xsl:value-of select="$path/AudienceCode/Name" /></div>
                </div>
            </xsl:if>
            <xsl:if test="normalize-space($path/SubjectHash/Subject)">
                <div class="ValuePair-item">
                    <div class="ValuePair-label">Subject Code</div>
                    <xsl:for-each select="$path/SubjectHash/Subject">
                        <div class="ValuePair-value">
                            <xsl:call-template name="subjectCell">
                                <xsl:with-param name="subject" select="." />
                            </xsl:call-template>
                        </div>
                    </xsl:for-each>
                </div>
            </xsl:if>
        </div>
    </xsl:template>

    <xsl:template name="contributorCell">
        <xsl:param name="contributor" />
        <xsl:value-of select="$contributor/Name" />
        &#160;(<xsl:value-of select="$contributor/ContributorRole/Description" />)&#160;
        <a href="/bookpub/analytics?c=a&amp;author={$contributor/ContributorID}" title="Sales Analytics for &quot;{$contributor/Name}&quot;" class="js-tooltip">
            <xsl:call-template name="icon">
                <xsl:with-param name="type" select="'sales-analytics'" />
            </xsl:call-template>
        </a>
    </xsl:template>

    <xsl:template name="subjectCell">
        <xsl:param name="subject" />
        <xsl:value-of select="$subject/SubjectCode" />&#160;:&#160;<xsl:value-of select="$subject/Literal" />
    </xsl:template>

    <xsl:template name="lastModifiedCell">
        <xsl:param name="path" />
        <p class="HelpText">
            Last Modified:&#160;<xsl:value-of select="$path/ModifiedDate" /><xsl:if test="normalize-space($path/ModifiedBy)"> by <xsl:value-of select="$path/ModifiedBy" /></xsl:if>
        </p>
    </xsl:template>

    <xsl:template name="productListView">
        <xsl:if test="BookProductGroups/BookProductList/BookProduct">
            <xsl:variable name="hasImprints" select="boolean(/Root/ImprintList/Imprint)" />
            <table class="Table">
                <thead>
                    <tr class="Table-row Table-row-header">
                        <th class="Table-col" id="bookFormat">Product</th>
                        <th class="Table-col" id="productDate">Release Date</th>
                        <th class="Table-col" id="edition">Edition</th>
                        <th class="Table-col" id="productISBN13">ISBN</th>
                        <xsl:if test="$hasImprints">
                            <th class="Table-col" id="imprint">Imprint</th>
                        </xsl:if>
                        <th class="Table-col">Price Type</th>
                        <th class="Table-col Table-col-amount">Current Price</th>
                        <xsl:if test="Access/bookpub/catalog/edit_book_product">
                            <th class="Table-col Table-col-icon">Edit</th>
                        </xsl:if>
                    </tr>
                </thead>
                <tbody>
                    <xsl:variable name="hardbackCount" select="count(BookProductGroups/BookProductList[Description = 'Hardback']/BookProduct)" />
                    <xsl:variable name="paperbackCount" select="count(BookProductGroups/BookProductList[Description = 'Paperback / softback']/BookProduct)" />
                    <xsl:variable name="ebookAgencyCount">
                        <xsl:choose>
                            <xsl:when test="BookProductGroups/BookProductList[Description = 'eBook (Agency)']/BookProduct">1</xsl:when>
                            <xsl:otherwise>0</xsl:otherwise>
                        </xsl:choose>
                    </xsl:variable>
                    <xsl:variable name="ebookRRPCount" select="count(BookProductGroups/BookProductList[Description = 'eBook']/BookProduct)" />
                    <xsl:variable name="audioCount" select="count(BookProductGroups/BookProductList[Description = 'Audio']/BookProduct)" />
                    <xsl:for-each select="BookProductGroups/BookProductList[Description = 'Hardback']/BookProduct">
                        <xsl:call-template name="productRow" />
                    </xsl:for-each>
                    <xsl:for-each select="BookProductGroups/BookProductList[Description = 'Paperback / softback']/BookProduct">
                        <xsl:call-template name="productRow">
                            <xsl:with-param name="addToPosition" select="$hardbackCount" />
                        </xsl:call-template>
                    </xsl:for-each>
                    <xsl:for-each select="BookProductGroups/BookProductList[Description = 'eBook (Agency)']">
                        <xsl:choose>
                            <xsl:when test="BookProduct/@size &gt; 1">
                                <tr class="Table-row Table-row-data Table-row-heading">
                                    <td class="Table-col" colspan="7">
                                        <xsl:if test="$hasImprints">
                                            <xsl:attribute name="colspan">8</xsl:attribute>
                                        </xsl:if>
                                        <h3 class="Book-details-headerintable">eBook (Agency)</h3>
                                    </td>
                                </tr>
                                <xsl:for-each select="BookProduct">
                                    <xsl:call-template name="productRow">
                                        <xsl:with-param name="position" select="($hardbackCount + $paperbackCount + 1)" />
                                    </xsl:call-template>
                                </xsl:for-each>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:for-each select="BookProduct">
                                    <xsl:call-template name="productRow">
                                        <xsl:with-param name="addToPosition" select="$hardbackCount + $paperbackCount" />
                                    </xsl:call-template>
                                </xsl:for-each>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:for-each>
                    <xsl:for-each select="BookProductGroups/BookProductList[Description = 'eBook']/BookProduct">
                        <xsl:call-template name="productRow">
                            <xsl:with-param name="addToPosition" select="$hardbackCount + $paperbackCount + $ebookAgencyCount" />
                        </xsl:call-template>
                    </xsl:for-each>
                    <xsl:for-each select="BookProductGroups/BookProductList[Description = 'Audio']/BookProduct">
                        <xsl:call-template name="productRow">
                            <xsl:with-param name="addToPosition" select="$hardbackCount + $paperbackCount + $ebookAgencyCount + $ebookRRPCount" />
                        </xsl:call-template>
                    </xsl:for-each>
                    <xsl:for-each select="BookProductGroups/BookProductList[Description = 'Other']/BookProduct">
                        <xsl:call-template name="productRow">
                            <xsl:with-param name="addToPosition" select="$hardbackCount + $paperbackCount + $ebookAgencyCount + $ebookRRPCount + $audioCount" />
                        </xsl:call-template>
                    </xsl:for-each>
                </tbody>
            </table>
        </xsl:if>
        <xsl:if test="Access/bookpub/catalog/edit_book_product">
            <div class="Book-details-headeraftertable">
                <xsl:call-template name="uiButton">
                    <xsl:with-param name="content" select="'Add Product'" />
                    <xsl:with-param name="id" select="'addProductButton'" />
                    <xsl:with-param name="url">
                        <xsl:text>catalog?c=edit_book_product&amp;BookID=</xsl:text>
                        <xsl:value-of select="Book/BookID" />
                    </xsl:with-param>
                </xsl:call-template>
            </div>
        </xsl:if>
    </xsl:template>

    <xsl:template name="productRow">
        <xsl:param name="addToPosition" select="'0'" />
        <xsl:param name="position" />
        <xsl:variable name="product" select="." />
        <tr class="Table-row Table-row-data">
            <xsl:if test="$position">
                <xsl:call-template name="uiEvenOddTrClass">
                    <xsl:with-param name="position" select="$position" />
                </xsl:call-template>
            </xsl:if>
            <td class="Table-col">
                <a href="/bookpub/analytics?c=b&amp;product={ProductID}" title="Sales Analytics" class="js-tooltip">
                    <xsl:call-template name="icon">
                        <xsl:with-param name="type" select="'sales-analytics'" />
                        <xsl:with-param name="role" select="'action'" />
                        <xsl:with-param name="className" select="'Icon-sales-analytics-left'" />
                    </xsl:call-template>
                </a>
                &#160;
                <a href="/bookpub/catalog?c=show_book_product&amp;ProductID={ProductID}">
                    <xsl:value-of select="ProductFormat/BookFormatType/Description" />
                    <xsl:if test="normalize-space(ProductFormat/BookFormatSubtype/Description)">
                        <xsl:text> </xsl:text>
                        <xsl:value-of select="ProductFormat/BookFormatSubtype/Description" />
                    </xsl:if>
                </a>
                <xsl:call-template name="publishingStatusDisplay">
                    <xsl:with-param name="statusID" select="OnixCodePublishingStatus" />
                </xsl:call-template>
            </td>
            <td class="Table-col"><xsl:value-of select="ReleaseDate" /></td>
            <xsl:variable name="edition" select="Edition" />
            <xsl:variable name="editionLength" select="string-length(Edition)" />
            <xsl:variable name="lastDigit" select="substring(Edition, string-length(Edition))" />
            <td  class="Table-col">
                <xsl:value-of select="$edition" />
                <xsl:choose>
                    <xsl:when test="not($lastDigit) and $editionLength = 0"></xsl:when>
                    <xsl:when test="$lastDigit = 0 and $editionLength = 1"></xsl:when>
                    <xsl:when test="$lastDigit = 0 and $editionLength &gt; 1">th</xsl:when>
                    <xsl:when test="$lastDigit = 1 and $edition != 11">st</xsl:when>
                    <xsl:when test="$lastDigit = 2 and $edition != 12">nd</xsl:when>
                    <xsl:when test="$lastDigit = 3 and $edition != 13">rd</xsl:when>
                    <xsl:when test="$lastDigit &gt; 3 and $editionLength = 1">th</xsl:when>
                    <xsl:otherwise>th</xsl:otherwise>
                </xsl:choose>
            </td>
            <td class="Table-col">
                <xsl:value-of select="ISBN13" />
                <xsl:if test="not(normalize-space(ISBN13))"><xsl:value-of select="ISBN10" /></xsl:if>
            </td>
            <xsl:if test="/Root/ImprintList/Imprint">
                <xsl:variable name="productImprintID"><xsl:value-of select="ImprintID" /></xsl:variable>
                <td class="Table-col"><xsl:value-of select="key('imprintById', $productImprintID)/Name" /></td>
            </xsl:if>
            <xsl:variable name="primaryCurrency" select="DefaultPrimary/Currency/CurrencyCode" />
            <xsl:variable name="secondaryCurrency" select="DefaultSecondary/Currency/CurrencyCode" />
            <xsl:variable name="visibleCurrencies" select="$product/Pricing/Market/Currency[CurrencyCode = $primaryCurrency or CurrencyCode = $secondaryCurrency]" />
            <xsl:variable name="currentPrices" select="$visibleCurrencies/Prices/ProductMarketPrice[Status = 'current' and normalize-space(Price) != '' and normalize-space(Price) != '-' and not(number(normalize-space(Price)) = 0)]" />
            <xsl:variable name="currentPrimaryPrices" select="$visibleCurrencies[CurrencyCode = $primaryCurrency]/Prices/ProductMarketPrice[Status = 'current' and normalize-space(Price) != '' and normalize-space(Price) != '-' and not(number(normalize-space(Price)) = 0)]" />
            <xsl:variable name="currentSecondaryPrices" select="$visibleCurrencies[CurrencyCode = $secondaryCurrency]/Prices/ProductMarketPrice[Status = 'current' and normalize-space(Price) != '' and normalize-space(Price) != '-' and not(number(normalize-space(Price)) = 0)]" />
            <td class="Table-col">
                <xsl:for-each select="/Root/PriceTypes/PriceType">
                    <xsl:sort select="Display" />
                    <xsl:variable name="priceTypeID" select="PriceTypeID" />
                    <xsl:if test="(not(normalize-space(/Root/Client/Locale/SecondaryMarketCountryCode)) and $currentPrimaryPrices[PriceTypeID = $priceTypeID]) or (normalize-space(/Root/Client/Locale/SecondaryMarketCountryCode) and $currentPrimaryPrices[PriceTypeID = $priceTypeID] and $currentSecondaryPrices[PriceTypeID = $priceTypeID])">
                        <xsl:value-of select="Display" /><br />
                    </xsl:if>
                </xsl:for-each>
            </td>
            <td class="Table-col Table-col-amount">
                <xsl:for-each select="/Root/PriceTypes/PriceType">
                    <xsl:sort select="Display" />
                    <xsl:variable name="priceTypeID" select="PriceTypeID" />
                    <xsl:if test="(not(normalize-space(/Root/Client/Locale/SecondaryMarketCountryCode)) and $currentPrimaryPrices[PriceTypeID = $priceTypeID]) or (normalize-space(/Root/Client/Locale/SecondaryMarketCountryCode) and $currentPrimaryPrices[PriceTypeID = $priceTypeID] and $currentSecondaryPrices[PriceTypeID = $priceTypeID])">
                        <xsl:call-template name="priceDisplay">
                            <xsl:with-param name="prices" select="$product/Pricing/Market/Currency[CurrencyCode = $primaryCurrency]/Prices" />
                            <xsl:with-param name="priceTypeID" select="$priceTypeID" />
                        </xsl:call-template>
                        <xsl:if test="normalize-space(/Root/Client/Locale/SecondaryMarketCountryCode)">
                            <xsl:text> / </xsl:text>
                            <xsl:call-template name="priceDisplay">
                                <xsl:with-param name="prices" select="$product/Pricing/Market/Currency[CurrencyCode = $secondaryCurrency]/Prices" />
                                <xsl:with-param name="priceTypeID" select="$priceTypeID" />
                            </xsl:call-template>
                        </xsl:if>
                        <br />
                    </xsl:if>
                </xsl:for-each>
            </td>
            <xsl:if test="/Root/Access/bookpub/catalog/edit_book_product">
                <td class="Table-col Table-col-icon">
                    <a href="/bookpub/catalog?c=edit_book_product&amp;ProductID={ProductID}" title="Edit" class="js-tooltip">
                        <xsl:call-template name="icon">
                            <xsl:with-param name="type" select="'edit'" />
                            <xsl:with-param name="role" select="'action'" />
                        </xsl:call-template>
                    </a>
                </td>
            </xsl:if>
        </tr>
    </xsl:template>

    <xsl:template name="priceDisplay">
        <xsl:param name="prices" />
        <xsl:param name="priceTypeID" />
        <xsl:choose>
            <xsl:when test="$prices/ProductMarketPrice[PriceTypeID = $priceTypeID and Status = 'current']">
                <xsl:value-of select="$prices/ProductMarketPrice[PriceTypeID = $priceTypeID]/CurrencyCode" />
                <xsl:text> </xsl:text>
                <xsl:for-each select="$prices/ProductMarketPrice[PriceTypeID = $priceTypeID and Status = 'current']">
                    <xsl:value-of select="Price" /><xsl:if test="position() != last()">, </xsl:if>
                </xsl:for-each>
            </xsl:when>
            <xsl:otherwise>-</xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
