<!--                                                                  -->
<!--  Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved     -->
<!--                                                                  -->
<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:include href="/app/tools/bookpub/templates/global.xsl" />
    <xsl:include href="book_detail.xsl" />

    <xsl:variable name="productMonitoring">
        <xsl:choose>
            <xsl:when test="/Root/ClientTypes/ClientType[ClientTypeID = 10]/IsClientTypeFlag = 1">1</xsl:when>
            <xsl:otherwise>0</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="productType" select="/Root/Form/TypeList/Type[ProductTypeID = /Root/Form/Product/ProductTypeID]/Type" />
    <xsl:variable name="eBookTest">
        <xsl:choose>
            <xsl:when test="/Root/Product/ProductFormat/BookFormatType/Description = 'eBook'">1</xsl:when>
            <xsl:otherwise>0</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:template name="pickLayout">
        <div class="Album-information" id="bookProductView">
            <xsl:call-template name="helpLink" />
            <xsl:call-template name="successMessageGeneral">
                <xsl:with-param name="successMessage">Product successfully updated!</xsl:with-param>
            </xsl:call-template>
            <xsl:call-template name="bookDetailView">
                <xsl:with-param name="path" select="/Root/Book"></xsl:with-param>
                <xsl:with-param name="product" select="/Root/Product"></xsl:with-param>
            </xsl:call-template>
            <xsl:choose>
                <xsl:when test="Product/Pricing/Market">
                    <xsl:call-template name="pricingMarkets" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:call-template name="emptyPricingMarket" />
                </xsl:otherwise>
            </xsl:choose>
            <xsl:call-template name="lastModifiedCell">
                <xsl:with-param name="path" select="/Root/Product" />
            </xsl:call-template>
            <xsl:call-template name="buttons" />
        </div>
    </xsl:template>

    <xsl:template name="submitButton">
        <xsl:if test="Access/bookpub/catalog/edit_book_product">
            <xsl:call-template name="uiButton">
                <xsl:with-param name="type" select="'primary'" />
                <xsl:with-param name="name" select="'editButton'" />
                <xsl:with-param name="id" select="'editButton'" />
                <xsl:with-param name="content" select="'Edit'" />
                <xsl:with-param name="className" select="'Form-button'" />
                <xsl:with-param name="url">catalog?ProductID=<xsl:value-of select="Product/ProductID" />&amp;c=edit_book_product</xsl:with-param>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <xsl:template name="cancelButton">
        <xsl:call-template name="uiButton">
            <xsl:with-param name="type" select="'default'" />
            <xsl:with-param name="name" select="'Cancel'" />
            <xsl:with-param name="elemType" select="'button'" />
            <xsl:with-param name="content" select="'Return to Book'" />
            <xsl:with-param name="className" select="'Form-button'" />
            <xsl:with-param name="url">catalog?BookID=<xsl:value-of select="Book/BookID" />&amp;c=show_book</xsl:with-param>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="productView">
        <div class="ValuePair">
            <xsl:if test="/Root/Product/ContributorHash/Contributor">
                <div class="ValuePair-item">
                    <div class="ValuePair-label">Additional Contributors</div>
                    <xsl:for-each select="/Root/Product/ContributorHash/Contributor">
                        <div class="ValuePair-value">
                            <xsl:call-template name="productContributorCell">
                                <xsl:with-param name="contributor" select="." />
                            </xsl:call-template>
                        </div>
                    </xsl:for-each>
                </div>
            </xsl:if>
            <xsl:if test="normalize-space(Product/ImprintID)">
                <div class="ValuePair-item">
                    <div class="ValuePair-label">Imprint</div>
                    <xsl:variable name="productImprintID"><xsl:value-of select="Product/ImprintID" /></xsl:variable>
                    <div class="ValuePair-value"><xsl:value-of select="/Root/ImprintList/Imprint[ImprintID = $productImprintID]/Name" /></div>
                </div>
            </xsl:if>
            <xsl:if test="normalize-space(Product/PublisherID)">
                <div class="ValuePair-item">
                    <div class="ValuePair-label">Publisher</div>
                    <xsl:variable name="productPublisherID"><xsl:value-of select="Product/PublisherID" /></xsl:variable>
                    <div class="ValuePair-value"><xsl:value-of select="/Root/PublisherList/Publisher[PublisherID = $productPublisherID]/Name" /></div>
                </div>
            </xsl:if>
            <xsl:if test="normalize-space(Product/ProductFormat/BookFormatType/Description)">
                <div class="ValuePair-item">
                    <div class="ValuePair-label">Format</div>
                    <div class="ValuePair-value">
                        <xsl:value-of select="Product/ProductFormat/BookFormatType/Description" />
                        <xsl:if test="normalize-space(Product/ProductFormat/BookFormatSubtype/Description)">
                            <xsl:text> </xsl:text>
                            <xsl:value-of select="Product/ProductFormat/BookFormatSubtype/Description" />
                        </xsl:if>
                        <xsl:call-template name="publishingStatusDisplay">
                            <xsl:with-param name="statusID" select="Product/OnixCodePublishingStatus" />
                        </xsl:call-template>
                    </div>
                </div>
            </xsl:if>
            <xsl:if test="normalize-space(Product/Edition)">
                <div class="ValuePair-item">
                    <div class="ValuePair-label">Edition</div>
                    <xsl:variable name="edition" select="Product/Edition" />
                    <xsl:variable name="editionLength" select="string-length(Product/Edition)" />
                    <xsl:variable name="lastDigit" select="substring(Product/Edition, string-length(Product/Edition))" />
                    <div class="ValuePair-value">
                        <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>
                    </div>
                </div>
            </xsl:if>
            <xsl:if test="normalize-space(Product/ReleaseDate)">
                <div class="ValuePair-item">
                    <div class="ValuePair-label">Release Date</div>
                    <div class="ValuePair-value"><xsl:value-of select="Product/ReleaseDate" /></div>
                </div>
            </xsl:if>
            <xsl:if test="normalize-space(Product/Language)">
                <div class="ValuePair-item">
                    <div class="ValuePair-label">Language</div>
                    <div class="ValuePair-value"><xsl:value-of select="Product/Language" /></div>
                </div>
            </xsl:if>
            <xsl:if test="normalize-space(Product/NumPages)">
                <div class="ValuePair-item">
                    <div class="ValuePair-label">Page Count</div>
                    <div class="ValuePair-value"><xsl:value-of select="Product/NumPages" /></div>
                </div>
            </xsl:if>
            <xsl:if test="normalize-space(Product/ISBN13) or normalize-space(Product/ISBN10) or normalize-space(Product/WorkID)">
                <div class="ValuePair-item">
                    <div class="ValuePair-label">Identifiers</div>
                    <xsl:if test="normalize-space(Product/ISBN13)">
                        <div class="ValuePair-value">
                            ISBN 13: <xsl:value-of select="Product/ISBN13" />
                        </div>
                    </xsl:if>
                    <xsl:if test="normalize-space(Product/ISBN10)">
                        <div class="ValuePair-value">
                            ISBN 10: <xsl:value-of select="Product/ISBN10" />
                        </div>
                    </xsl:if>
                    <xsl:if test="normalize-space(Product/WorkID)">
                        <div class="ValuePair-value">
                            Work ID: <xsl:value-of select="Product/WorkID" />
                        </div>
                    </xsl:if>
                </div>
            </xsl:if>
        </div>
    </xsl:template>

    <xsl:template name="pricingMarkets">
        <xsl:variable name="regionID" select="RegionID" />
        <xsl:if test="/Root/Regions/Region[RegionID = $regionID]/Display != 'All'" >
            <h2>Prices for Supply Territory: <xsl:value-of select="/Root/Regions/Region[RegionID = $regionID]/Display" /> </h2>
        </xsl:if>
        <xsl:variable name="primaryMarket" select="Product/Pricing/Market/Currency[CurrencyCode = /Root/Client/Locale/CurrencyFormat/CurrencyCode]" />
        <xsl:choose>
            <xsl:when test="$primaryMarket">
                <xsl:for-each select="$primaryMarket">
                    <xsl:call-template name="priceList">
                        <xsl:with-param name="currencyCodeName" select="$primaryMarket/CurrencyCodeName" />
                        <xsl:with-param name="currencyCode" select="/Root/Client/Locale/CurrencyFormat/CurrencyCode" />
                    </xsl:call-template>
                </xsl:for-each>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="priceList">
                    <xsl:with-param name="currencyCodeName" select="/Root/Client/Locale/CurrencyFormat/Description" />
                    <xsl:with-param name="currencyCode" select="/Root/Client/Locale/CurrencyFormat/CurrencyCode" />
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:variable name="secondaryMarket" select="Product/Pricing/Market/Currency[CurrencyCode = /Root/Client/Locale/SecondaryMarketCurrencyFormat/CurrencyCode]" />
        <xsl:choose>
            <xsl:when test="$secondaryMarket">
                <xsl:for-each select="$secondaryMarket">
                    <xsl:call-template name="priceList">
                        <xsl:with-param name="currencyCodeName" select="$secondaryMarket/CurrencyCodeName" />
                        <xsl:with-param name="currencyCode" select="/Root/Client/Locale/SecondaryMarketCurrencyFormat/CurrencyCode" />
                    </xsl:call-template>
                </xsl:for-each>
            </xsl:when>
            <xsl:when test="/Root/Client/Locale/SecondaryMarketCurrencyFormat/CurrencyCode">
                <xsl:call-template name="priceList">
                    <xsl:with-param name="currencyCodeName" select="/Root/Client/Locale/SecondaryMarketCurrencyFormat/Description" />
                    <xsl:with-param name="currencyCode" select="/Root/Client/Locale/SecondaryMarketCurrencyFormat/CurrencyCode" />
                </xsl:call-template>
            </xsl:when>
        </xsl:choose>
        <xsl:for-each select="Product/Pricing/Market/Currency">
            <xsl:sort select="CurrencyCode" />
            <xsl:choose>
                <xsl:when test="CurrencyCode = /Root/Client/Locale/CurrencyFormat/CurrencyCode"></xsl:when>
                <xsl:when test="CurrencyCode = /Root/Client/Locale/SecondaryMarketCurrencyFormat/CurrencyCode"></xsl:when>
                <xsl:otherwise>
                    <xsl:call-template name="priceList">
                        <xsl:with-param name="currencyCodeName" select="CurrencyCodeName" />
                        <xsl:with-param name="currencyCode" select="CurrencyCode" />
                    </xsl:call-template>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="emptyPricingMarket">
        <xsl:call-template name="priceList">
            <xsl:with-param name="currencyCodeName" select="/Root/Client/Locale/CurrencyFormat/Description" />
            <xsl:with-param name="currencyCode" select="/Root/Client/Locale/CurrencyFormat/CurrencyCode" />
        </xsl:call-template>
        <xsl:if test="/Root/Client/Locale/SecondaryMarketCurrencyFormat/CurrencyCode">
            <xsl:call-template name="priceList">
                <xsl:with-param name="currencyCodeName" select="/Root/Client/Locale/SecondaryMarketCurrencyFormat/Description" />
                <xsl:with-param name="currencyCode" select="/Root/Client/Locale/SecondaryMarketCurrencyFormat/CurrencyCode" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <xsl:template name="priceList">
        <xsl:param name="currencyCodeName" />
        <xsl:param name="currencyCode" />
        <div class="Catalog-pricelist">
            <h3><xsl:value-of select="$currencyCodeName" />s (<xsl:value-of select="$currencyCode" />)</h3>
            <table class="Table">
                <thead>
                    <xsl:call-template name="priceListHeaderRow" />
                </thead>
                <tbody>
                    <xsl:choose>
                        <xsl:when test="Prices/ProductMarketPrice">
                            <xsl:for-each select="Prices/ProductMarketPrice">
                                <xsl:sort select="PriceTypeID" />                              
                                <xsl:sort select="PriceTypeQualifierID" />
                                <xsl:sort select="RegionID" />
                                <xsl:sort select="DiscountID" />
                                <xsl:sort select="Status" data-type="text" />
                                <xsl:call-template name="priceListRow" />
                            </xsl:for-each>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template name="emptyPriceListRow" />
                        </xsl:otherwise>
                    </xsl:choose>
                </tbody>
            </table>
        </div>
    </xsl:template>

    <xsl:template name="priceListHeaderRow">
        <tr class="Table-row Table-row-header">
            <th class="Table-col Catalog-col-status">Effective</th>
            <th class="Table-col Table-col-amount Catalog-col-price">Price</th>
            <th class="Table-col Catalog-col-type">Type</th>
            <th class="Table-col Catalog-col-qualifier">Qualifier</th>
            <th class="Table-col Catalog-col-discount">Discount Code</th>
            <th class="Table-col Catalog-col-country">Country</th>
        </tr>
    </xsl:template>

    <xsl:template name="priceListRow">
        <tr class="Table-row Table-row-data">
            <td class="Table-col">
                <xsl:choose>
                    <xsl:when test="Status = 'current' and not(normalize-space(StartDate))">
                        Current
                    </xsl:when>
                    <xsl:when test="Status = 'current'">
                        <xsl:value-of select="StartDate" /> - Current
                    </xsl:when>
                    <xsl:when test="not(normalize-space(StartDate)) and normalize-space(EndDate)">
                        Initial Delivery - <xsl:value-of select="EndDate" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="StartDate" /> - <xsl:value-of select="EndDate" />
                    </xsl:otherwise>
                </xsl:choose>
            </td>
            <td class="Table-col Table-col-amount Catalog-col-price"><xsl:value-of select="Price" /></td>
            <td class="Table-col">
                <xsl:variable name="priceTypeID" select="PriceTypeID" />
                <xsl:value-of select="/Root/PriceTypes/PriceType[PriceTypeID = $priceTypeID]/Display" />
            </td>
            <td class="Table-col">
                <xsl:call-template name="priceTypeQualifierName">
                    <xsl:with-param name="id" select="PriceTypeQualifierID" />
                </xsl:call-template>
            </td>
            <td class="Table-col"><xsl:value-of select="DiscountCode" /></td>
            <td class="Table-col">
                <xsl:variable name="regionID" select="RegionID" />
                <xsl:value-of select="/Root/Regions/Region[RegionID = $regionID]/Display" />
                
                <xsl:if test="/Root/Regions/Region[RegionID = $regionID]/Included/Country/@size > 12">
                    
                    <xsl:variable name="fullRegionList">
                        <xsl:for-each select="/Root/Regions/Region[RegionID = $regionID]/Included/Country">
                            <xsl:if test="position() != 1">, </xsl:if><xsl:value-of select="." />
                        </xsl:for-each>
                    </xsl:variable> 
                    
                    <a title="Full Country List" class="HelpIcon js-tooltip" onClick="$('#regionList{ProductMarketPriceID}').toggle()">
                        <xsl:call-template name="icon">
                            <xsl:with-param name="type" select="'info'" />
                            <xsl:with-param name="className" select="'HelpIcon-icon'" />
                        </xsl:call-template>                    
                    </a>
                    
                    <div class="hidden" id="regionList{ProductMarketPriceID}">
                        <xsl:value-of select="$fullRegionList" />                        
                    </div>
                    
                </xsl:if>
            </td>
        </tr>
    </xsl:template>

    <xsl:template name="emptyPriceListRow">
        <tr class="Table-row Table-row-data">
            <td class="Table-col" colspan="6">
                No price information has been provided for this product.
            </td>
        </tr>
    </xsl:template>

    <xsl:template name="productContributorCell">
        <xsl:param name="contributor" />
        <xsl:variable name="contributorRoleID"><xsl:value-of select="$contributor/ContributorRoleID" /></xsl:variable>
        <xsl:value-of select="$contributor/Name" />&#160;(<xsl:value-of select="/Root/ContributorRoleList/ContributorRole[ContributorRoleID = $contributorRoleID]/Description" />)
    </xsl:template>

</xsl:stylesheet>
