<!--                                                                  -->
<!--  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:variable name="chartPosition" />
    <xsl:variable name="chartTitle" />
    <xsl:variable name="chartAction" />

    <xsl:variable name="countryCount" select="count(Root/Report/Countries/Country)" />
    <xsl:variable name="imprintCount" select="count(Root/Report/Imprints/Imprint)" />
    <xsl:variable name="publisherCount" select="count(Root/Report/Publishers/Publisher)" />

    <xsl:variable name="showLastRank">
        <xsl:choose>
            <xsl:when test="normalize-space(/Root/Params/end_period)">0</xsl:when>
            <xsl:otherwise>1</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:template name="pickLayout">
        <xsl:if test="$tab = 'analytics' and normalize-space(Report/Period/PeriodID)">
            <div id="subSubNavWrapper">
                <xsl:call-template name="subSubNav" />
            </div>
        </xsl:if>
        <xsl:if test="Report/Period">
            <xsl:call-template name="analyticsPeriodNav" />
        </xsl:if>
        <xsl:call-template name="reportsLayout" />
        <xsl:call-template name="customRangePop" />
    </xsl:template>

    <xsl:template name="customRangePop">
        <div id="customRangePicker" style="display: none">
            <div class="Modal-wrapper Modal-title-wrapper">
                <h3 class="Modal-title-text">Custom Date Range</h3>
                <a href="javascript:hideInlinePop()" title="Close" id="closePop">
                    <div class="Modal-close-trigger Icon-trigger"><svg xmlns="http://www.w3.org/2000/svg" class="Icon Icon-close Icon-action-role Icon-default-size"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/production/redesign/assets/icons/sprite.svg#close"></use></svg></div>
                </a>
            </div>
            <form id="customrange" name="customRange" onsubmit="loadCustomRange('{$view}','{$service}','{$format}','{$author}','{Report/ProductInfo/ProductID}'); return false" action="#">
                <div class="Modal-wrapper Modal-content-wrapper js-rs-html-content">
                    <div class="Modal-content Custom-period-content">
                        <xsl:call-template name="uiWarning">
                            <xsl:with-param name="id" select="'customRangeError'" />
                            <xsl:with-param name="type" select="'error'" />
                            <xsl:with-param name="hidden" select="'true'" />
                            <xsl:with-param name="description" select="'Custom Range Error'" />
                        </xsl:call-template>
                        <input type="hidden" name="custom_period" id="custom_period" value="{/Root/Params/period}" />
                        <input type="hidden" name="custom_period_date" id="custom_period_date" value="{/Root/Report/Period/StartDate}" />
                        <input type="hidden" name="custom_end_period" id="custom_end_period" value="{/Root/Params/end_period}" />
                        <input type="hidden" name="custom_end_period_date" id="custom_end_period_date" value="{/Root/Report/EndPeriod/StartDate}" />
                        <div class="Custom-period-field Form-label-caption">From</div>
                        <div class="Custom-period-field">
                            <label class="Form-label">
                                <span class="Form-label-caption">Month</span>
                                <div class="Form-input-wrapper">
                                    <xsl:variable name="customPeriodMonthFrom">
                                        <xsl:call-template name="monthOptions" />
                                    </xsl:variable>
                                    <xsl:call-template name="uiDropdown">
                                        <xsl:with-param name="items" select="$customPeriodMonthFrom"/>
                                        <xsl:with-param name="id" select="'fromMonth'"/>
                                        <xsl:with-param name="className" select="'Dropdown-dateselect'"/>
                                    </xsl:call-template>
                                </div>
                            </label>
                        </div>
                        <div class="Custom-period-field">
                            <label class="Form-label">
                                <span class="Form-label-caption">Year</span>
                                <div class="Form-input-wrapper">
                                    <xsl:variable name="customPeriodYearFrom">
                                        <map>
                                            <mapEntry><name>Choose&#8230;</name><value></value></mapEntry>
                                            <xsl:for-each select="Report/PeriodNav/Year">
                                                <mapEntry>
                                                    <name><xsl:value-of select="Year" /></name>
                                                    <value><xsl:value-of select="Year" /></value>
                                                </mapEntry>
                                            </xsl:for-each>
                                        </map>
                                    </xsl:variable>
                                    <xsl:call-template name="uiDropdown">
                                        <xsl:with-param name="items" select="$customPeriodYearFrom"/>
                                        <xsl:with-param name="id" select="'fromYear'"/>
                                        <xsl:with-param name="className" select="'Dropdown-dateselect'"/>
                                    </xsl:call-template>
                                </div>
                            </label>
                        </div>
                        <div class="Custom-period-field Form-label-caption">To</div>
                        <div class="Custom-period-field">
                            <label class="Form-label">
                                <span class="Form-label-caption">Month</span>
                                <div class="Form-input-wrapper">
                                    <xsl:variable name="customPeriodMonthTo">
                                        <xsl:call-template name="monthOptions" />
                                    </xsl:variable>
                                    <xsl:call-template name="uiDropdown">
                                        <xsl:with-param name="items" select="$customPeriodMonthTo"/>
                                        <xsl:with-param name="id" select="'toMonth'"/>
                                        <xsl:with-param name="className" select="'Dropdown-dateselect'"/>
                                    </xsl:call-template>
                                </div>
                            </label>
                        </div>
                        <div class="Custom-period-field">
                            <label class="Form-label">
                                <span class="Form-label-caption">Year</span>
                                <div class="Form-input-wrapper">
                                    <xsl:variable name="customPeriodYearTo">
                                        <map>
                                            <mapEntry><name>Choose&#8230;</name><value></value></mapEntry>
                                            <xsl:for-each select="Report/PeriodNav/Year">
                                                <mapEntry>
                                                    <name><xsl:value-of select="Year" /></name>
                                                    <value><xsl:value-of select="Year" /></value>
                                                </mapEntry>
                                            </xsl:for-each>
                                        </map>
                                    </xsl:variable>
                                    <xsl:call-template name="uiDropdown">
                                        <xsl:with-param name="items" select="$customPeriodYearTo"/>
                                        <xsl:with-param name="id" select="'toYear'"/>
                                        <xsl:with-param name="className" select="'Dropdown-dateselect'"/>
                                    </xsl:call-template>
                                </div>
                            </label>
                        </div>
                    </div>
                    <div class="Modal-wrapper Modal-buttons-wrapper">
                        <div class="Modal-buttons">
                            <xsl:call-template name="uiButton">
                                <xsl:with-param name="content" select="'Apply Filter'"/>
                                <xsl:with-param name="elemType" select="'submit'"/>
                                <xsl:with-param name="id" select="'popSubmitButton'"/>
                                <xsl:with-param name="type" select="'primary'" />
                                <xsl:with-param name="className" select="'Modal-button'" />
                            </xsl:call-template>
                            <xsl:call-template name="uiButton">
                                <xsl:with-param name="content" select="'Cancel'"/>
                                <xsl:with-param name="onClick">hideInlinePop()</xsl:with-param>
                                <xsl:with-param name="className" select="'Modal-button'" />
                            </xsl:call-template>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </xsl:template>

    <xsl:template name="monthOptions">
        <map>
            <mapEntry><name>Choose&#8230;</name><value></value></mapEntry>
            <mapEntry><name>Jan</name><value>01</value></mapEntry>
            <mapEntry><name>Feb</name><value>02</value></mapEntry>
            <mapEntry><name>Mar</name><value>03</value></mapEntry>
            <mapEntry><name>Apr</name><value>04</value></mapEntry>
            <mapEntry><name>May</name><value>05</value></mapEntry>
            <mapEntry><name>Jun</name><value>06</value></mapEntry>
            <mapEntry><name>Jul</name><value>07</value></mapEntry>
            <mapEntry><name>Aug</name><value>08</value></mapEntry>
            <mapEntry><name>Sep</name><value>09</value></mapEntry>
            <mapEntry><name>Oct</name><value>10</value></mapEntry>
            <mapEntry><name>Nov</name><value>11</value></mapEntry>
            <mapEntry><name>Dec</name><value>12</value></mapEntry>
        </map>
    </xsl:template>

    <xsl:template name="reportsLayout">
        <script src="/production/javascript/jquery/jquery-3.6.0.min.js"></script>
        <script src="/production/javascript/jquery/jquery.local.js"></script>
        <script type="text/javascript" src="/production/javascript/jquery/jquery.ie-select-style.min.js"></script>
        <script src="/production/javascript/bookpub/analytics.js?v=2"></script>
        <section class="Analytics-wrapper">
            <xsl:choose>
                <xsl:when test="$view = 'sf' and /Root/Totals/Units != '0'">
                    <xsl:call-template name="byLayout" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:choose>
                        <xsl:when test="$view = 'is' or $view = 'gs'">
                            <xsl:call-template name="byLayout" />
                            <xsl:call-template name="topLayout" />
                        </xsl:when>
                        <xsl:when test="$view = 'fs' or $view = 'ss'">
                            <xsl:call-template name="byLayout" />
                        </xsl:when>
                        <xsl:when test="$view = 'f'">
                            <xsl:call-template name="byLayout">
                            <xsl:with-param name="chartTitle">By Service</xsl:with-param>
                            <xsl:with-param name="chartAction">ss</xsl:with-param>
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:when test="$view = 's'">
                            <xsl:call-template name="byLayout">
                            <xsl:with-param name="chartTitle">By Format</xsl:with-param>
                            <xsl:with-param name="chartAction">fs</xsl:with-param>
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:when test="$view = 'bs' or $view = 'cs' or $view = 'as'">
                            <xsl:call-template name="topLayout" />
                        </xsl:when>
                        <xsl:when test="$view = 'b' or $view = 'c' or $view = 'a'">
                            <xsl:call-template name="productDetailLayout" />
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template name="mainLayout" />
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:if test="not(Report/Pies/Pie) and not(Report/Tables/Table) and $view != 'm' and $view != ''">
                <xsl:call-template name="noData" />
            </xsl:if>
        </section>
    </xsl:template>

    <xsl:template name="noData">
        <p>No data meets this criteria.</p>
    </xsl:template>

    <xsl:template name="greeting">
        <div class="Analytics-section Analytics-greeting">
            <div class="Analytics-greeting-inner"><xsl:call-template name="uiClientLogo" /></div>
            <div class="Analytics-greeting-inner"><xsl:call-template name="clientTotals" /></div>
            <div class="Analytics-greeting-inner"><xsl:call-template name="clientLinks" /></div>
        </div>
    </xsl:template>

    <xsl:template name="clientLinks">
        <xsl:call-template name="messages" />
    </xsl:template>

    <xsl:template name="clientTotals">
        <xsl:choose>
            <xsl:when test="Report/Totals and ((translate(Report/Totals/Revenue, translate(Report/Totals/Revenue,'0123456789', ''), '') != 0) or (translate(Report/Totals/Units, translate(Report/Totals/Units,'0123456789', ''), '') != 0))">
                <xsl:call-template name="pageTotals" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="noData" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="mainLayout">
        <xsl:if test="$view = 'm'">
            <xsl:call-template name="greeting" />
        </xsl:if>
        <xsl:if test="Report/Pies/Pie">
            <div class="Analytics-dualsection">
                <xsl:for-each select="Report/Pies/Pie[PieName = 'top_formats']">
                    <xsl:call-template name="pieChart">
                        <xsl:with-param name="chartTitle">Top Formats</xsl:with-param>
                        <xsl:with-param name="chartPosition">Right</xsl:with-param>
                        <xsl:with-param name="chartAction">fs</xsl:with-param>
                    </xsl:call-template>
                </xsl:for-each>
                <xsl:for-each select="Report/Pies/Pie[PieName = 'top_services']">
                    <xsl:call-template name="pieChart">
                        <xsl:with-param name="chartTitle">Top Services</xsl:with-param>
                        <xsl:with-param name="chartPosition">Left</xsl:with-param>
                        <xsl:with-param name="chartAction">ss</xsl:with-param>
                    </xsl:call-template>
                </xsl:for-each>
            </div>
        </xsl:if>
        <xsl:if test="Report/Tables/Table">
            <xsl:for-each select="Report/Tables/Table[TableName = 'top_books']">
                <xsl:call-template name="salesChart">
                    <xsl:with-param name="chartTitle">Top Books</xsl:with-param>
                    <xsl:with-param name="chartAction">bs</xsl:with-param>
                </xsl:call-template>
            </xsl:for-each>
            <xsl:for-each select="Report/Tables/Table[TableName = 'top_chapters']">
                <xsl:call-template name="salesChart">
                    <xsl:with-param name="chartTitle">Top Chapters</xsl:with-param>
                    <xsl:with-param name="chartAction">cs</xsl:with-param>
                </xsl:call-template>
            </xsl:for-each>
            <xsl:for-each select="Report/Tables/Table[TableName = 'top_authors']">
                <xsl:call-template name="salesChart">
                    <xsl:with-param name="chartTitle">Top Authors</xsl:with-param>
                    <xsl:with-param name="chartAction">as</xsl:with-param>
                </xsl:call-template>
            </xsl:for-each>
        </xsl:if>
    </xsl:template>

    <xsl:template name="byLayout">
        <xsl:param name="chartTitle" />
        <xsl:param name="chartAction" />
        <xsl:if test="Report/Pies/Pie or (Report/Totals and (translate(Report/Totals/Revenue, translate(Report/Totals/Revenue,'0123456789', ''), '') != 0) and (translate(Report/Totals/Units, translate(Report/Totals/Units,'0123456789', ''), '') != 0))">
            <div class="Analytics-section Analytics-greeting">
                <div class="Analytics-greeting-inner">
                    <xsl:choose>
                        <xsl:when test="$view = 'gs'">
                            <h2>Summary for
                                <xsl:choose>
                                    <xsl:when test="$country != ''">
                                        <xsl:value-of select="$country" />
                                    </xsl:when>
                                    <xsl:otherwise>All Countries</xsl:otherwise>
                                </xsl:choose>
                            </h2>
                        </xsl:when>
                        <xsl:when test="$view = 'is'">
                            <h2>Summary for
                                <xsl:choose>
                                    <xsl:when test="$imprintID != ''">
                                        <xsl:value-of select="$imprintName" />
                                    </xsl:when>
                                    <xsl:otherwise>All Imprints</xsl:otherwise>
                                </xsl:choose>
                            </h2>
                        </xsl:when>
                    </xsl:choose>
                    <xsl:call-template name="pageTotals" />
                </div>
            </div>
        </xsl:if>
        <div class="Analytics-section">
            <xsl:if test="$view != 'sf' and Report/Pies/Pie">
                <xsl:attribute name="class">Analytics-dualsection</xsl:attribute>
            </xsl:if>
            <xsl:for-each select="Report/Pies/Pie">
                <xsl:call-template name="pieChart">
                    <xsl:with-param name="chartPosition">Left</xsl:with-param>
                    <xsl:with-param name="chartTitle" select="$chartTitle" />
                    <xsl:with-param name="chartAction" select="$chartAction" />
                </xsl:call-template>
            </xsl:for-each>
        </div>
        <xsl:for-each select="Report/Tables/Table[TableName = 'top_books']">
            <xsl:call-template name="salesChart">
                <xsl:with-param name="chartTitle">Top Books</xsl:with-param>
                <xsl:with-param name="chartAction">bs</xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>
        <xsl:for-each select="Report/Tables/Table[TableName = 'top_chapters']">
            <xsl:call-template name="salesChart">
                <xsl:with-param name="chartTitle">Top Chapters</xsl:with-param>
                <xsl:with-param name="chartAction">cs</xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>
        <xsl:for-each select="Report/Tables/Table[TableName = 'top_authors']">
            <xsl:call-template name="salesChart">
                <xsl:with-param name="chartTitle">Top Authors</xsl:with-param>
                <xsl:with-param name="chartAction">as</xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="pageTotals">
        <p class="Analytics-totals">Total Revenue:
            <strong>
                <xsl:call-template name="formatDollars">
                    <xsl:with-param name="dollarAmount" select="Report/Totals/Revenue" />
                </xsl:call-template>
            </strong>
        </p>
        <p class="Analytics-totals">Total Units:
            <strong>
                <xsl:value-of select="Report/Totals/Units" />
            </strong>
        </p>
        <xsl:if test="Report/Totals/Rank >= 0 or Report/Totals/PrevRank >= 0">
            <p class="Analytics-totals">Rank:
                <strong>
                    <xsl:choose>
                        <xsl:when test="Report/Totals/Rank = 0">-</xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="Report/Totals/Rank" />
                        </xsl:otherwise>
                    </xsl:choose>
                </strong>
            </p>
            <xsl:if test="$showLastRank = 1">
                <p class="Analytics-totals">Last Rank:&#160;
                    <strong>
                        <xsl:choose>
                            <xsl:when test="Report/Totals/PrevRank = 0">-</xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="Report/Totals/PrevRank" />
                            </xsl:otherwise>
                        </xsl:choose>
                    </strong>
                </p>
            </xsl:if>
        </xsl:if>
    </xsl:template>

    <xsl:template name="topLayout">
        <xsl:for-each select="Report/Tables/Table[TableName = 'top_books']">
            <xsl:call-template name="salesChart">
                <xsl:with-param name="chartAction">bs</xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>
        <xsl:for-each select="Report/Tables/Table[TableName = 'top_chapters']">
            <xsl:call-template name="salesChart">
                <xsl:with-param name="chartAction">cs</xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>
        <xsl:for-each select="Report/Tables/Table[TableName = 'top_authors']">
            <xsl:call-template name="salesChart">
                <xsl:with-param name="chartAction">as</xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>
        <xsl:for-each select="Report/Tables/Table[TableName = 'top_imprints']">
            <xsl:call-template name="salesChart">
                <xsl:with-param name="chartAction">is</xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>
        <xsl:for-each select="Report/Tables/Table[TableName = 'top_countries']">
            <xsl:call-template name="salesChart">
                <xsl:with-param name="chartAction">ms</xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>
        <xsl:if test="Report/Pies/Pie">
            <xsl:variable name="pieChartTitle">
                <xsl:if test="$view = 'cs'">Chapter Revenue </xsl:if>
                <xsl:if test="$view = 'bs'">Book Revenue </xsl:if>
            </xsl:variable>
            <div class="Analytics-dualsection">
                <xsl:for-each select="Report/Pies/Pie[PieName = 'top_formats']">
                    <xsl:call-template name="pieChart">
                        <xsl:with-param name="chartTitle"><xsl:value-of select="$pieChartTitle" />By Format</xsl:with-param>
                        <xsl:with-param name="chartPosition">Right</xsl:with-param>
                        <xsl:with-param name="chartAction">fs</xsl:with-param>
                    </xsl:call-template>
                </xsl:for-each>
                <xsl:for-each select="Report/Pies/Pie[PieName = 'top_services']">
                    <xsl:call-template name="pieChart">
                        <xsl:with-param name="chartTitle"><xsl:value-of select="$pieChartTitle" />By Service</xsl:with-param>
                        <xsl:with-param name="chartPosition">Left</xsl:with-param>
                        <xsl:with-param name="chartAction">ss</xsl:with-param>
                    </xsl:call-template>
                </xsl:for-each>
            </div>
        </xsl:if>
    </xsl:template>

    <xsl:template name="rankNav">
        <xsl:param name="pageNum" />
        <xsl:if test="/Root/Report/Records/MaxPageNum &gt; 1">
            <div class="Pagination-previous-next">
                <div class="Pagination-item">
                    <xsl:if test="$pageNum != '' and $pageNum != 1">
                        <xsl:call-template name="uiButton">
                            <xsl:with-param name="url">
                                <xsl:if test="$pageNum != '' and $pageNum != 1">
                                    <xsl:call-template name="argsHref">
                                        <xsl:with-param name="baseURL">/bookpub/analytics?p=1</xsl:with-param>
                                    </xsl:call-template>
                                </xsl:if>
                            </xsl:with-param>
                            <xsl:with-param name="content">
                                First <xsl:value-of select="/Root/Report/Records/Limit" />
                            </xsl:with-param>
                            <xsl:with-param name="type" select="'default'"/>
                            <xsl:with-param name="iconLeftType" select="'arrow-left'"/>
                            <xsl:with-param name="disabled">
                                <xsl:if test="not($pageNum != '' and $pageNum != 1)">
                                    <xsl:text>true</xsl:text>
                                </xsl:if>
                            </xsl:with-param>
                        </xsl:call-template>
                        <xsl:call-template name="uiButton">
                            <xsl:with-param name="url">
                                <xsl:if test="$pageNum != '' and $pageNum != 1">
                                    <xsl:call-template name="argsHref">
                                        <xsl:with-param name="baseURL">/bookpub/analytics?p=<xsl:value-of select="$pageNum - 1" /></xsl:with-param>
                                    </xsl:call-template>
                                </xsl:if>
                            </xsl:with-param>
                            <xsl:with-param name="content">
                                Previous <xsl:value-of select="/Root/Report/Records/Limit" />
                            </xsl:with-param>
                            <xsl:with-param name="type" select="'default'"/>
                            <xsl:with-param name="iconLeftType" select="'arrow-left'"/>
                            <xsl:with-param name="disabled">
                                <xsl:if test="not($pageNum != '' and $pageNum != 1)">
                                    <xsl:text>true</xsl:text>
                                </xsl:if>
                            </xsl:with-param>
                        </xsl:call-template>
                    </xsl:if>
                </div>
                <div class="Pagination-item">
                    <xsl:if test="not($pageNum) or $pageNum &lt; /Root/Report/Records/MaxPageNum">
                        <xsl:call-template name="uiButton">
                            <xsl:with-param name="url">
                                <xsl:choose>
                                    <xsl:when test="$pageNum &lt; /Root/Report/Records/MaxPageNum">
                                        <xsl:call-template name="argsHref">
                                            <xsl:with-param name="baseURL">/bookpub/analytics?p=<xsl:value-of select="$pageNum + 1" /></xsl:with-param>
                                        </xsl:call-template>
                                    </xsl:when>
                                    <xsl:when test="not($pageNum)">
                                        <xsl:call-template name="argsHref">
                                            <xsl:with-param name="baseURL">/bookpub/analytics?p=<xsl:value-of select="2" /></xsl:with-param>
                                        </xsl:call-template>
                                    </xsl:when>
                                </xsl:choose>
                            </xsl:with-param>
                            <xsl:with-param name="content">
                                Next <xsl:value-of select="/Root/Report/Records/Limit" />
                            </xsl:with-param>
                            <xsl:with-param name="type" select="'default'"/>
                            <xsl:with-param name="iconRightType" select="'arrow-right'"/>
                            <xsl:with-param name="disabled">
                                <xsl:if test="not(not($pageNum) or $pageNum &lt; /Root/Report/Records/MaxPageNum)">
                                    <xsl:text>true</xsl:text>
                                </xsl:if>
                            </xsl:with-param>
                        </xsl:call-template>
                        <xsl:call-template name="uiButton">
                            <xsl:with-param name="url">
                                <xsl:if test="not($pageNum) or $pageNum &lt; /Root/Report/Records/MaxPageNum">
                                    <xsl:call-template name="argsHref">
                                        <xsl:with-param name="baseURL">/bookpub/analytics?p=<xsl:value-of select="/Root/Report/Records/MaxPageNum" /></xsl:with-param>
                                    </xsl:call-template>
                                </xsl:if>
                            </xsl:with-param>
                            <xsl:with-param name="content">
                                Last <xsl:value-of select="/Root/Report/Records/Limit" />
                            </xsl:with-param>
                            <xsl:with-param name="type" select="'default'"/>
                            <xsl:with-param name="iconRightType" select="'arrow-right'"/>
                            <xsl:with-param name="disabled">
                                <xsl:if test="not(not($pageNum) or $pageNum &lt; /Root/Report/Records/MaxPageNum)">
                                    <xsl:text>true</xsl:text>
                                </xsl:if>
                            </xsl:with-param>
                        </xsl:call-template>
                    </xsl:if>
                </div>
            </div>
        </xsl:if>
    </xsl:template>

    <xsl:template name="productDetailLayout">
        <xsl:if test="$view != 'a' and normalize-space(Report)">
            <xsl:if test="$country != '' or $imprintCount &gt; 1">
                <div class="Analytics-section">
                    <h2>
                        <xsl:if test="$country != ''">
                            <xsl:value-of select="$country" />
                        </xsl:if>
                        <xsl:if test="$country != '' and $imprintCount &gt; 1"> - </xsl:if>
                        <xsl:if test="$imprintCount &gt; 1">
                            <xsl:value-of select="$imprintName" />
                        </xsl:if>
                    </h2>
                </div>
            </xsl:if>
            <div class="Analytics-dualsection Analytics-dualsection-data">
                <div class="Analytics-dualsection-left Analytics-dualsection-inner">
                    <xsl:choose>
                        <xsl:when test="$view = 'b'">
                            <p>Title: <strong><xsl:value-of select="Report/ProductInfo/Title" /></strong></p>
                            <xsl:if test="Report/ProductInfo/SubTitle != ''">
                                <p>Sub Title: <strong><xsl:value-of select="Report/ProductInfo/SubTitle" /></strong></p>
                            </xsl:if>
                            <xsl:if test="Report/ProductInfo/Authors != ''">
                                <p>Author: <strong><xsl:value-of select="Report/ProductInfo/Authors" /></strong>
                                    <xsl:if test="count(Report/ProductInfo/Authors) != 1">
                                        <a class="js-tooltip">
                                            <xsl:attribute name="title">
                                                <xsl:for-each select="Report/ProductInfo/Authors[position() != 1]">
                                                    <xsl:value-of select="." />
                                                    <xsl:if test="position() != last()">
                                                        ,&#160;&#160;
                                                    </xsl:if>
                                                </xsl:for-each>
                                            </xsl:attribute>
                                            <xsl:call-template name="icon">
                                                <xsl:with-param name="className" select="'ViewMore'"/>
                                                <xsl:with-param name="type" select="'more'" />
                                            </xsl:call-template>
                                        </a>
                                    </xsl:if>
                                </p>
                            </xsl:if>
                            <xsl:if test="Report/ProductInfo/Edition != ''">
                                <p>Edition: <strong><xsl:value-of select="Report/ProductInfo/Edition" /></strong></p>
                            </xsl:if>
                            <xsl:if test="Report/ProductInfo/ISBN10 != ''">
                                <p>ISBN10: <strong><xsl:value-of select="Report/ProductInfo/ISBN10" /></strong></p>
                            </xsl:if>
                            <xsl:if test="Report/ProductInfo/ISBN13 != ''">
                                <p>ISBN13: <strong><xsl:value-of select="Report/ProductInfo/ISBN13" /></strong></p>
                            </xsl:if>
                        </xsl:when>
                        <xsl:when test="$view = 'c'">
                            <p>Chapter: <strong><xsl:value-of select="Report/ProductInfo/ChapterTitle" /></strong></p>
                            <xsl:if test="Report/ProductInfo/AuthorName != ''">
                                <p>Author: <strong><xsl:value-of select="Report/ProductInfo/AuthorName" /></strong></p>
                            </xsl:if>
                            <xsl:if test="Report/ProductInfo/Title != ''">
                                <p>Book: <strong><xsl:value-of select="Report/ProductInfo/Title" /></strong></p>
                            </xsl:if>
                            <xsl:if test="Report/ProductInfo/ISBN10 != ''">
                                <p>ISBN10: <strong><xsl:value-of select="Report/ProductInfo/ISBN10" /></strong></p>
                            </xsl:if>
                            <xsl:if test="Report/ProductInfo/ISBN13 != ''">
                                <p>ISBN13: <strong><xsl:value-of select="Report/ProductInfo/ISBN13" /></strong></p>
                            </xsl:if>
                        </xsl:when>
                    </xsl:choose>
                </div>
                <div class="Analytics-dualsection-right Analytics-dualsection-inner">
                    <xsl:call-template name="pageTotals" />
                </div>
            </div>
        </xsl:if>
        <xsl:if test="$view = 'a' and normalize-space(Report)">
            <div class="Analytics-section">
                <xsl:call-template name="pageTotals" />
            </div>
        </xsl:if>
        <xsl:if test="Report/Pies/Pie">
            <div class="Analytics-dualsection">
                <xsl:for-each select="Report/Pies/Pie[PieName = 'top_formats']">
                    <xsl:call-template name="pieChart">
                        <xsl:with-param name="chartTitle">By Format</xsl:with-param>
                        <xsl:with-param name="chartPosition">Right</xsl:with-param>
                        <xsl:with-param name="chartAction">fs</xsl:with-param>
                    </xsl:call-template>
                </xsl:for-each>
                <xsl:for-each select="Report/Pies/Pie[PieName = 'top_services']">
                    <xsl:call-template name="pieChart">
                        <xsl:with-param name="chartTitle">By Service</xsl:with-param>
                        <xsl:with-param name="chartPosition">Left</xsl:with-param>
                        <xsl:with-param name="chartAction">ss</xsl:with-param>
                    </xsl:call-template>
                </xsl:for-each>
            </div>
        </xsl:if>
        <xsl:if test="Report/Tables/Table">
            <div class="Analytics-dualsection">
                <xsl:for-each select="Report/Tables/Table[TableName = 'chapter_revenue_by_format' or TableName = 'book_revenue_by_format' or TableName = 'author_revenue_by_format']">
                    <xsl:call-template name="salesChart">
                        <xsl:with-param name="chartPosition">Right</xsl:with-param>
                        <xsl:with-param name="chartAction">f</xsl:with-param>
                    </xsl:call-template>
                </xsl:for-each>
                <xsl:for-each select="Report/Tables/Table[TableName = 'chapter_revenue_by_service' or TableName = 'book_revenue_by_service' or TableName = 'author_revenue_by_service']">
                    <xsl:call-template name="salesChart">
                        <xsl:with-param name="chartPosition">Left</xsl:with-param>
                        <xsl:with-param name="chartAction">s</xsl:with-param>
                    </xsl:call-template>
                </xsl:for-each>
            </div>

            <xsl:if test="Report/Tables/Table[TableName = 'book_revenue_by_country']">
                <xsl:for-each select="Report/Tables/Table[TableName = 'book_revenue_by_country']">
                    <xsl:call-template name="salesChart">
                        <xsl:with-param name="chartAction">m</xsl:with-param>
                    </xsl:call-template>
                </xsl:for-each>
            </xsl:if>

        </xsl:if>
    </xsl:template>

    <xsl:template name="chartTitle">
        <xsl:param name="chartTitle" />
        <xsl:param name="chartAction" />
        <xsl:if test="normalize-space($chartTitle)">
            <h3>
                <a>
                    <xsl:call-template name="argsLink">
                        <xsl:with-param name="skip">c</xsl:with-param>
                        <xsl:with-param name="skip2">format</xsl:with-param>
                        <xsl:with-param name="baseURL">/bookpub/analytics?c=<xsl:value-of select="$chartAction" />
                        </xsl:with-param>
                    </xsl:call-template>
                    <xsl:value-of select="$chartTitle" />
                </a>
            </h3>
        </xsl:if>
    </xsl:template>

    <xsl:template name="pieChart">
        <xsl:param name="chartPosition" />
        <xsl:param name="chartTitle" />
        <xsl:param name="chartAction" />
        <div class="Analytics-dualsection-inner Analytics-dualsection-{$chartPosition}">
            <xsl:call-template name="chartTitle">
                <xsl:with-param name="chartTitle" select="$chartTitle" />
                <xsl:with-param name="chartAction" select="$chartAction" />
            </xsl:call-template>
            <div class="PieChart">
                <xsl:attribute name="data-items">
                    [
                        <xsl:for-each select="Legend/Item">

                            <xsl:variable name="pieSectionTip">
                                <xsl:call-template name="pieSectionTip">
                                    <xsl:with-param name="value" select="Value" />
                                </xsl:call-template>
                            </xsl:variable>

                            {
                                "value": "<xsl:value-of select="Value" />",
                                "label": "<xsl:value-of select="Label" />",
                                "link": "<xsl:value-of select="Link" />",
                                "color": "<xsl:value-of select="Color" />",
                                "sectionTip": "<xsl:value-of select="$pieSectionTip" />"
                            }
                            <xsl:if test="position() != last()">,</xsl:if>
                        </xsl:for-each>
                    ]
                </xsl:attribute>
            </div>
            <div class="Analytics-pielegend">
                <xsl:for-each select="Legend/Item">
                    <p class="Analytics-pielegend-item">
                        <xsl:variable name="pieSectionTip">
                            <xsl:call-template name="pieSectionTip">
                                <xsl:with-param name="value" select="Value" />
                            </xsl:call-template>
                        </xsl:variable>
                        <xsl:if test="Color !='#0'">
                            <i class="Analytics-legendcolor" style="background-color: {Color}"/>
                        </xsl:if>
                        <xsl:value-of select="Percent" />&#160;&#x2014;&#160;
                        <a class="js-tooltip" title="{$pieSectionTip}">
                            <xsl:if test="Link != 'NONE'">
                                <xsl:attribute name="href">
                                    <xsl:value-of select="Link" />
                                </xsl:attribute>
                            </xsl:if>
                            <xsl:value-of select="Label" />
                        </a>
                    </p>
                </xsl:for-each>
                <xsl:if test="Legend/ExtraItem">
                    <div class="Analytics-pielegend">
                        <xsl:for-each select="Legend/ExtraItem">
                            <p class="Analytics-pielegend-item">
                                <a class="js-tooltip" href="{Link}" title="{Label}">
                                    <xsl:value-of select="Label" />
                                </a>
                            </p>
                        </xsl:for-each>
                    </div>
                </xsl:if>
            </div>
        </div>
    </xsl:template>

    <xsl:template name="salesChart">
        <xsl:param name="chartPosition" />
        <xsl:param name="chartTitle" />
        <xsl:param name="chartAction" />
        <div class="Analytics-section">
            <xsl:if test="$chartPosition">
                <xsl:attribute name="class">Analytics-dualsection-inner Analytics-dualsection-<xsl:value-of select="$chartPosition"/></xsl:attribute>
            </xsl:if>
            <xsl:call-template name="rankNav">
                <xsl:with-param name="pageNum" select="/Root/Params/p" />
            </xsl:call-template>
            <xsl:call-template name="chartTitle">
                <xsl:with-param name="chartTitle" select="$chartTitle" />
                <xsl:with-param name="chartAction" select="$chartAction" />
            </xsl:call-template>
            <table class="Table Table-with-highlight">
                <thead>
                    <xsl:call-template name="headerRow" />
                </thead>
                <tbody>
                    <xsl:for-each select="DataRow">
                        <xsl:call-template name="dataRow">
                            <xsl:with-param name="chartAction">
                                <xsl:choose>
                                    <xsl:when test="$chartAction = 'is'">m</xsl:when>
                                    <xsl:otherwise>
                                        <xsl:value-of select="substring($chartAction,1,1)"/>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:with-param>
                        </xsl:call-template>
                    </xsl:for-each>
                </tbody>
            </table>
            <xsl:call-template name="rankNav">
                <xsl:with-param name="pageNum" select="/Root/Params/p" />
            </xsl:call-template>
        </div>
    </xsl:template>

    <xsl:template name="headerRow">
        <tr class="Table-row Table-row-header">
            <xsl:for-each select="HeaderRow/Cell[Value != 'Data' and ($showLastRank = 1 or ($showLastRank = 0 and Value != 'PrevRank'))]">
                <th class="Table-col">
                    <xsl:attribute name="class">
                        <xsl:choose>
                            <xsl:when test="Value = 'ProductInfo' or Value = 'AuthorInfo'">Table-col Analytics-coltitle</xsl:when>
                            <xsl:when test="Value = 'PrevRank' or Value = 'Rank'">Table-col Analytics-colRank</xsl:when>
                        </xsl:choose>
                        <xsl:if test="Value = 'Units' or Value = 'Revenue' or Key = 'Data'">Analytics-colnumbers Table-col Table-col-amount</xsl:if>
                    </xsl:attribute>
                    <xsl:choose>
                        <xsl:when test="Value = 'ProductInfo'">
                            <xsl:choose>
                                <xsl:when test="../../TableName = 'chapter_revenue_by_service' or ../../TableName = 'book_revenue_by_service' or ../../TableName = 'book_revenue_by_service'">Service</xsl:when>
                                <xsl:when test="../../TableName = 'chapter_revenue_by_format' or ../../TableName = 'book_revenue_by_format' or ../../TableName = 'author_revenue_by_format'">Format</xsl:when>
                                <xsl:when test="../../TableName = 'top_imprints'">Imprint Name</xsl:when>
                                <xsl:when test="../../TableName = 'top_countries' or ../../TableName = 'book_revenue_by_country'">Country</xsl:when>
                                <xsl:otherwise>Title</xsl:otherwise>
                            </xsl:choose>
                        </xsl:when>
                        <xsl:when test="Value = 'AuthorInfo'">Author</xsl:when>
                        <xsl:when test="Value = 'PrevRank'">Last&#160;Rank</xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="Value" />
                        </xsl:otherwise>
                    </xsl:choose>
                </th>
            </xsl:for-each>
        </tr>
    </xsl:template>

    <xsl:template name="dataRow">
        <xsl:param name="chartAction" />
        <tr class="Table-row Table-row-data">
            <xsl:for-each select="Cell[$showLastRank = 1 or ($showLastRank = 0 and Key != 'PrevRank')]">
                <td class="Table-col">
                    <xsl:attribute name="class">Table-col Analytics-col<xsl:value-of select="Key" />
                        <xsl:if test="Key = 'Rank'">
                            <xsl:choose>
                                <xsl:when test="$showLastRank = 0"> Analytics-rank-same</xsl:when>
                                <xsl:when test="Value = '' and ../Cell[Key = 'PrevRank']/Value = ''"> Analytics-rank-same</xsl:when>
                                <xsl:when test="Value &lt; ../Cell[Key = 'PrevRank']/Value or ../Cell[Key = 'PrevRank']/Value = 'NA' or ../Cell[Key = 'PrevRank']/Value = ''"> Analytics-rank-up</xsl:when>
                                <xsl:when test="Value &gt; ../Cell[Key = 'PrevRank']/Value"> Analytics-rank-down</xsl:when>
                                <xsl:otherwise> Analytics-rank-same</xsl:otherwise>
                            </xsl:choose>
                        </xsl:if>
                    </xsl:attribute>
                    <xsl:choose>
                        <xsl:when test="Key = 'ProductInfo' or Key = 'AuthorInfo' or Key = 'Service' or Key = 'Format'">
                            <a>
                                <xsl:if test="ProductInfo/Authors">
                                    <xsl:attribute name="class">Analytics-reportmainlink</xsl:attribute>
                                </xsl:if>
                                <xsl:variable name="baseURL">/bookpub/analytics?c=<xsl:value-of select="$chartAction" /></xsl:variable>
                                <xsl:choose>
                                    <xsl:when test="ProductInfo/ChapterTitle or ProductInfo/Title">
                                        <xsl:call-template name="argsLink">
                                            <xsl:with-param name="skip">c</xsl:with-param>
                                            <xsl:with-param name="baseURL">
                                                <xsl:value-of select="$baseURL" />&amp;product=<xsl:value-of select="ProductInfo/ProductID" />
                                            </xsl:with-param>
                                            <xsl:with-param name="skip2">product</xsl:with-param>
                                            <xsl:with-param name="skip3">format</xsl:with-param>
                                            <xsl:with-param name="skip4">service</xsl:with-param>
                                        </xsl:call-template>
                                    </xsl:when>
                                    <xsl:when test="AuthorInfo/AuthorName">
                                        <xsl:call-template name="argsLink">
                                            <xsl:with-param name="skip">c</xsl:with-param>
                                            <xsl:with-param name="baseURL">
                                                <xsl:value-of select="$baseURL" />&amp;author=<xsl:value-of select="AuthorInfo/AuthorID" />
                                            </xsl:with-param>
                                            <xsl:with-param name="skip2">author</xsl:with-param>
                                            <xsl:with-param name="skip3">format</xsl:with-param>
                                            <xsl:with-param name="skip4">service</xsl:with-param>
                                        </xsl:call-template>
                                    </xsl:when>
                                    <xsl:when test="ProductInfo/ServiceName">
                                        <xsl:call-template name="argsLink">
                                            <xsl:with-param name="skip">c</xsl:with-param>
                                            <xsl:with-param name="baseURL">
                                                <xsl:value-of select="$baseURL" />&amp;service=<xsl:value-of select="ProductInfo/ServiceID" />
                                            </xsl:with-param>
                                            <xsl:with-param name="skip2">service</xsl:with-param>
                                            <xsl:with-param name="skip3">format</xsl:with-param>
                                        </xsl:call-template>
                                    </xsl:when>
                                    <xsl:when test="ProductInfo/ImprintID">
                                        <xsl:call-template name="argsLink">
                                            <xsl:with-param name="skip">c</xsl:with-param>
                                            <xsl:with-param name="baseURL">
                                                <xsl:value-of select="$baseURL" />&amp;imprint=<xsl:value-of select="ProductInfo/ImprintID" />
                                            </xsl:with-param>
                                            <xsl:with-param name="skip2">imprint</xsl:with-param>
                                            <xsl:with-param name="skip3">format</xsl:with-param>
                                            <xsl:with-param name="skip4">service</xsl:with-param>
                                        </xsl:call-template>
                                    </xsl:when>
                                    <xsl:when test="ProductInfo/CountryID">
                                        <xsl:call-template name="argsLink">
                                            <xsl:with-param name="skip">c</xsl:with-param>
                                            <xsl:with-param name="baseURL">
                                                <xsl:value-of select="$baseURL" />&amp;country=<xsl:value-of select="ProductInfo/CountryID" />
                                            </xsl:with-param>
                                            <xsl:with-param name="skip2">country</xsl:with-param>
                                            <xsl:with-param name="skip3">format</xsl:with-param>
                                            <xsl:with-param name="skip4">service</xsl:with-param>
                                        </xsl:call-template>
                                    </xsl:when>
                                    <xsl:when test="ProductInfo/FormatType">
                                        <xsl:call-template name="argsLink">
                                            <xsl:with-param name="skip">c</xsl:with-param>
                                            <xsl:with-param name="baseURL">
                                                <xsl:value-of select="$baseURL" />&amp;format=<xsl:value-of select="ProductInfo/FormatType" />
                                            </xsl:with-param>
                                            <xsl:with-param name="skip2">format</xsl:with-param>
                                            <xsl:with-param name="skip3">service</xsl:with-param>
                                        </xsl:call-template>
                                    </xsl:when>
                                </xsl:choose>
                                <xsl:value-of select="ProductInfo/ChapterTitle" />
                                <xsl:value-of select="ProductInfo/Title" />
                                <xsl:value-of select="AuthorInfo/AuthorName" />
                                <xsl:value-of select="ProductInfo/ServiceName" />
                                <xsl:value-of select="ProductInfo/FormatName" />
                                <xsl:value-of select="ProductInfo/ImprintName" />
                                <xsl:value-of select="ProductInfo/CountryName" />
                            </a>
                            <xsl:if test="ProductInfo/Authors">
                                <br />
                                <xsl:value-of select="ProductInfo/Authors" />
                                <xsl:if test="count(ProductInfo/Authors) != 1"> (and others)</xsl:if>
                            </xsl:if>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:choose>
                                <xsl:when test="Value = ''">-</xsl:when>
                                <xsl:when test="Key = 'Revenue' or Key = 'Data'">
                                    <xsl:call-template name="formatDollars">
                                        <xsl:with-param name="dollarAmount" select="Value" />
                                    </xsl:call-template>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="Value" />
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:otherwise>
                    </xsl:choose>
                </td>
            </xsl:for-each>
        </tr>
    </xsl:template>

    <xsl:template name="statusChart">
        <div class="sectionHeader">
            <h1>Sales Data Status</h1>
        </div>
        <div class="sectionBody">
            <div class="section" id="statusChart">
                <table class="Table Table-with-highlight">
                    <thead>
                        <tr class="Table-row Table-row-header">
                            <xsl:for-each select="HeaderRow/Cell">
                                <th class="Table-col">
                                    <xsl:value-of select="Value" />
                                </th>
                            </xsl:for-each>
                        </tr>
                    </thead>
                    <tbody>
                        <xsl:for-each select="DataRow">
                            <tr class="Table-row Table-row-data">
                                <td class="Table-col">
                                    <xsl:value-of select="Cell[1]/Value" />
                                </td>
                                <xsl:for-each select="Cell[position() &gt; 1]">
                                    <xsl:if test="Value = 0">
                                        <td class="Table-col">&#160;</td>
                                    </xsl:if>
                                    <xsl:if test="Value = 1">
                                        <td class="Table-col statusMonth">&#160;</td>
                                    </xsl:if>
                                </xsl:for-each>
                            </tr>
                        </xsl:for-each>
                    </tbody>
                </table>
            </div>
        </div>
    </xsl:template>

</xsl:stylesheet>
