<!--                                                                  -->
<!--  Copyright (C) 2010 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/rps/templates/ajax_request.xsl" />
    <xsl:include href="/app/tools/common/production/templates/components/index.xsl" />

    <xsl:variable name="eBookTest">
        <xsl:choose>
            <xsl:when test="/Root/Product/ProductFormat/BookFormatType/Code = 'DG'">1</xsl:when>
            <xsl:otherwise>0</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:template name="pickLayout">
        <form name="chartForm" onSubmit="updateCharts(); return false;">
            <input type="hidden" name="ProductID" id="ProductID" value="{Params/ProductID}" />
            <input type="hidden" name="BookID" id="BookID" value="{Params/BookID}" />
            <input type="hidden" name="DaysParam" id="DaysParam" value="{Params/Days}" />
            <input type="hidden" name="OffsetParam" id="OffsetParam" value="{Params/Offset}" />
            <section class="Bookchart">
                <xsl:call-template name="dateControls" />
                <xsl:choose>
                    <xsl:when test="Params/BookID">
                        <xsl:call-template name="bookPriceChart" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:call-template name="productPriceChart" />
                        <xsl:call-template name="salesChart" />
                    </xsl:otherwise>
                </xsl:choose>
            </section>
        </form>
    </xsl:template>

    <xsl:template name="productPriceChart">
        <div class="Bookchart-container">
            <div class="Bookchart-chart">
                <map name="price_chart" id="price_chart">
                    <xsl:value-of select="PriceChart/ImageMap" disable-output-escaping="yes" />
                </map>
                <img src="{PriceChart/ChartSource}" width="640" height="250" id="" usemap="#price_chart" class="Bookchart-chartimage" />
                <div class="Bookchart-prevnext">
                    <xsl:choose>
                        <xsl:when test="Params/Days = 'ytd'"></xsl:when>
                        <xsl:when test="Params/DateStart and Params/DateEnd"></xsl:when>
                        <xsl:otherwise>
                            <div class="Pagination-previous-next">
                                <div class="Pagination-item">
                                    <xsl:call-template name="uiButton">
                                        <xsl:with-param name="onClick">ajaxReplaceContent('/bookpub/catalog?c=show_chart&amp;ProductID=<xsl:value-of select="Params/ProductID" />&amp;ServiceID=<xsl:value-of select="ServiceID" />&amp;Days=<xsl:value-of select="Days" />&amp;Offset=<xsl:value-of select="Offset + Days" />', 'chartWrapper' )</xsl:with-param>
                                        <xsl:with-param name="content" select="'Previous'"/>
                                        <xsl:with-param name="type" select="'default'"/>
                                        <xsl:with-param name="iconLeftType" select="'arrow-left'"/>
                                    </xsl:call-template>
                                </div>
                                <div class="Pagination-item">
                                    <xsl:if test="Offset &gt; 0">
                                        <xsl:call-template name="uiButton">
                                            <xsl:with-param name="onClick">ajaxReplaceContent('/bookpub/catalog?c=show_chart&amp;ProductID=<xsl:value-of select="Params/ProductID" />&amp;ServiceID=<xsl:value-of select="ServiceID" />&amp;Days=<xsl:value-of select="Days" />&amp;Offset=<xsl:value-of select="Offset - Days" />', 'chartWrapper' )</xsl:with-param>
                                            <xsl:with-param name="content" select="'Next'"/>
                                            <xsl:with-param name="type" select="'default'"/>
                                            <xsl:with-param name="iconRightType" select="'arrow-right'"/>
                                            <xsl:with-param name="disabled">
                                                <xsl:if test="not(Offset &gt; 0)">
                                                    <xsl:text>true</xsl:text>
                                                </xsl:if>
                                            </xsl:with-param>
                                        </xsl:call-template>
                                    </xsl:if>
                                </div>
                            </div>
                        </xsl:otherwise>
                    </xsl:choose>
                </div>
            </div>
            <div class="Bookchart-legend">
                <div class="Bookchart-legenditem">
                    <div class="Bookchart-legend-setprice" />
                    Current Price
                </div>
                <xsl:for-each select="ServiceList/Service">
                    <xsl:sort select="ServiceName" />
                    <xsl:variable name="serviceID" select="ServiceID" />
                    <div class="Bookchart-legenditem">
                        <div class="Bookchart-legend-marker" style="background-color: {/Root/PriceChart/Legend/Item[ServiceID = $serviceID]/Color}" />
                        <xsl:call-template name="uiCheckbox">
                            <xsl:with-param name="caption"><xsl:value-of select="ServiceName" /></xsl:with-param>
                            <xsl:with-param name="checked"><xsl:if test="/Root/SelectedServiceList/SelectedService/ServiceID = $serviceID">true</xsl:if></xsl:with-param>
                            <xsl:with-param name="name" select="'ServiceID'" />
                            <xsl:with-param name="onClick" select="'updateCharts();'" />
                            <xsl:with-param name="value"><xsl:value-of select="ServiceID" /></xsl:with-param>
                        </xsl:call-template>
                    </div>
                </xsl:for-each>
                <div class="Bookchart-legenditem">
                    <div class="Bookchart-legend-nodata" />
                    No Data Available
                </div>
            </div>
        </div>
    </xsl:template>

    <xsl:template name="bookPriceChart">
        <div class="Bookchart-container">
            <div class="Bookchart-chart">
                <map name="price_chart" id="price_chart">
                    <xsl:value-of select="PriceChart/ImageMap" disable-output-escaping="yes" />
                </map>
                <img src="{PriceChart/ChartSource}" width="640" height="250" id="" usemap="#price_chart" class="Bookchart-chartimage" />
                <div class="Bookchart-prevnext">
                    <xsl:choose>
                        <xsl:when test="Params/Days = 'ytd'"></xsl:when>
                        <xsl:when test="Params/DateStart and Params/DateEnd"></xsl:when>
                        <xsl:otherwise>
                            <div class="Pagination-previous-next">
                                <div class="Pagination-item">
                                    <xsl:call-template name="uiButton">
                                        <xsl:with-param name="onClick">ajaxReplaceContent('/bookpub/catalog?c=show_chart&amp;BookID=<xsl:value-of select="Params/BookID" />&amp;ServiceID=<xsl:value-of select="ServiceID" />&amp;Days=<xsl:value-of select="Days" />&amp;Offset=<xsl:value-of select="Offset + Days" />', 'chartWrapper' )</xsl:with-param>
                                        <xsl:with-param name="content" select="'Previous'"/>
                                        <xsl:with-param name="type" select="'default'"/>
                                        <xsl:with-param name="iconLeftType" select="'arrow-left'"/>
                                    </xsl:call-template>
                                </div>
                                <div class="Pagination-item">
                                    <xsl:if test="Offset &gt; 0">
                                        <xsl:call-template name="uiButton">
                                            <xsl:with-param name="onClick">ajaxReplaceContent('/bookpub/catalog?c=show_chart&amp;BookID=<xsl:value-of select="Params/BookID" />&amp;ServiceID=<xsl:value-of select="ServiceID" />&amp;Days=<xsl:value-of select="Days" />&amp;Offset=<xsl:value-of select="Offset - Days" />', 'chartWrapper' )</xsl:with-param>
                                            <xsl:with-param name="content" select="'Next'"/>
                                            <xsl:with-param name="type" select="'default'"/>
                                            <xsl:with-param name="iconRightType" select="'arrow-right'"/>
                                            <xsl:with-param name="disabled">
                                                <xsl:if test="not(Offset &gt; 0)">
                                                    <xsl:text>true</xsl:text>
                                                </xsl:if>
                                            </xsl:with-param>
                                        </xsl:call-template>
                                    </xsl:if>
                                </div>
                            </div>
                        </xsl:otherwise>
                    </xsl:choose>
                </div>
            </div>
            <div class="Bookchart-legend">
                <h3>Monitored Prices</h3>
                <xsl:choose>
                    <xsl:when test="PriceChart/Legend/Item">
                        <xsl:for-each select="PriceChart/Legend/Item">
                            <div class="Bookchart-legenditem">
                                <div class="Bookchart-legend-marker" style="background-color: {Color}" />
                                <xsl:variable name="productID" select="ProductID" />
                                <xsl:variable name="bookProduct" select="/Root/BookProductList/BookProduct[ProductID = $productID]" />
                                <xsl:value-of select="$bookProduct/ProductFormat/BookFormatType/Description" />
                                <xsl:if test="$bookProduct/ProductFormat/BookFormatType/Description = 'eBook'">
                                    <xsl:variable name="priceTypeID" select="$bookProduct/Pricing/Market/Currency[CurrencyCode = 'USD']/Prices/ProductMarketPrice[Status = 'current']/PriceTypeID" />
                                    <xsl:text> </xsl:text>(<xsl:value-of select="/Root/PriceTypes/PriceType[PriceTypeID = $priceTypeID]/Display" />)
                                </xsl:if>
                            </div>
                        </xsl:for-each>
                    </xsl:when>
                    <xsl:otherwise>
                        <div class="Bookchart-legendinfo">
                            No products were monitored in this timeframe.
                        </div>
                    </xsl:otherwise>
                </xsl:choose>
                <div class="Bookchart-legenditem">
                    <div class="Bookchart-legend-nodata" />
                    No Data Available
                </div>
            </div>
        </div>
    </xsl:template>

    <xsl:template name="salesChart">
        <div class="Bookchart-container" id="salesChart">
            <div class="Bookchart-chart">
                <map name="sales_chart" id="sales_chart">
                    <xsl:value-of select="SalesChart/ImageMap" disable-output-escaping="yes" />
                </map>
                <xsl:variable name="chartSource">
                    <xsl:choose>
                        <xsl:when test="SalesChart/ChartSource">
                            <xsl:value-of select="SalesChart/ChartSource" />
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:text>/production/images/bookpub/blankChart.gif</xsl:text>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <img src="{$chartSource}" id="" width="640" height="250" usemap="#sales_chart" class="Bookchart-chartimage" />
            </div>
            <div class="Bookchart-legend">
                <xsl:choose>
                    <xsl:when test="$eBookTest != 1">
                        <h3>Ranking</h3>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:variable name="disabledDropdown">
                            <xsl:choose>
                                <xsl:when test ="/Root/ShowRank = '' and /Root/ShowPOS = ''">1</xsl:when>
                                <xsl:otherwise>0</xsl:otherwise>
                            </xsl:choose>
                        </xsl:variable>
                        <xsl:variable name="selectChartType">
                            <map>
                                <xsl:if test="/Root/ShowRank = 1 or $disabledDropdown = 1">
                                    <mapEntry><name>Ranking</name><value>Rank</value></mapEntry>
                                </xsl:if>
                                <xsl:if test="/Root/ShowPOS = 1">
                                    <mapEntry><name>Units</name><value>UnitPOS</value></mapEntry>
                                    <mapEntry><name>Revenue</name><value>RevenuePOS</value></mapEntry>
                                </xsl:if>
                            </map>
                        </xsl:variable>
                        <xsl:call-template name="uiDropdown">
                            <xsl:with-param name="items" select="$selectChartType"/>
                            <xsl:with-param name="name" select="'ChartType'" />
                            <xsl:with-param name="active" select="ChartType" />
                            <xsl:with-param name="id" select="'ChartType'" />
                            <xsl:with-param name="className" select="'Bookchart-legendselect'" />
                            <xsl:with-param name="onChange" select="'updateCharts();'"/>
                            <xsl:param name="disabled">
                                <xsl:if test="$disabledDropdown = 1">true</xsl:if>
                            </xsl:param>
                        </xsl:call-template>
                    </xsl:otherwise>
                </xsl:choose>
            </div>
        </div>
    </xsl:template>

    <xsl:template name="dateControls">
        <div class="Form-group Form-group-horizontal">
            <xsl:if test="Params/BookID">
                <div class="Form-item">
                    <label class="Form-label">
                        <span class="Form-label-caption">Store</span>
                    </label>
                    <div class="Form-input-wrapper Bookchart-legendselect">
                        <xsl:variable name="selectService">
                            <map>
                                <xsl:for-each select="ServiceList/Service">
                                    <xsl:sort select="ServiceName" />
                                    <mapEntry>
                                        <name><xsl:value-of select="ServiceName" /></name>
                                        <value><xsl:value-of select="ServiceID" /></value>
                                    </mapEntry>
                                </xsl:for-each>
                            </map>
                        </xsl:variable>
                        <xsl:call-template name="uiDropdown">
                            <xsl:with-param name="items" select="$selectService"/>
                            <xsl:with-param name="name" select="'ServiceID'" />
                            <xsl:with-param name="active" select="/Root/ServiceID" />
                            <xsl:with-param name="id" select="'ServiceID'" />
                            <xsl:with-param name="onChange" select="'updateCharts();'"/>
                        </xsl:call-template>
                    </div>
                </div>
            </xsl:if>
            <div class="Form-item">
                <label class="Form-label">
                    <span class="Form-label-caption">Period</span>
                    <div class="Form-input-wrapper">
                        <ul class="Bookchart-period">
                            <xsl:call-template name="dateNav">
                                <xsl:with-param name="days">7</xsl:with-param>
                                <xsl:with-param name="label">7 d</xsl:with-param>
                            </xsl:call-template>
                            <xsl:call-template name="dateNav">
                                <xsl:with-param name="days">14</xsl:with-param>
                                <xsl:with-param name="label">14 d</xsl:with-param>
                            </xsl:call-template>
                            <xsl:call-template name="dateNav">
                                <xsl:with-param name="days">30</xsl:with-param>
                                <xsl:with-param name="label">1 m</xsl:with-param>
                            </xsl:call-template>
                            <xsl:call-template name="dateNav">
                                <xsl:with-param name="days">90</xsl:with-param>
                                <xsl:with-param name="label">3 m</xsl:with-param>
                            </xsl:call-template>
                            <xsl:call-template name="dateNav">
                                <xsl:with-param name="days">180</xsl:with-param>
                                <xsl:with-param name="label">6 m</xsl:with-param>
                            </xsl:call-template>
                            <xsl:call-template name="dateNav">
                                <xsl:with-param name="days">270</xsl:with-param>
                                <xsl:with-param name="label">9 m</xsl:with-param>
                            </xsl:call-template>
                            <xsl:call-template name="dateNav">
                                <xsl:with-param name="days">ytd</xsl:with-param>
                                <xsl:with-param name="label">YTD</xsl:with-param>
                            </xsl:call-template>
                            <xsl:call-template name="dateNav">
                                <xsl:with-param name="days">365</xsl:with-param>
                                <xsl:with-param name="label">1 y</xsl:with-param>
                            </xsl:call-template>
                            <xsl:call-template name="dateNav">
                                <xsl:with-param name="days">730</xsl:with-param>
                                <xsl:with-param name="label">2 y</xsl:with-param>
                            </xsl:call-template>
                            <xsl:call-template name="dateNav">
                                <xsl:with-param name="days">1095</xsl:with-param>
                                <xsl:with-param name="label">3 y</xsl:with-param>
                            </xsl:call-template>
                            <xsl:call-template name="dateNav">
                                <xsl:with-param name="days">1825</xsl:with-param>
                                <xsl:with-param name="label">5 y</xsl:with-param>
                            </xsl:call-template>
                        </ul>
                    </div>
                </label>
            </div>
            <div class="Form-item">
                <label class="Form-label">
                    <span class="Form-label-caption">Custom Period</span>
                    <div class="Form-input-wrapper">
                        <xsl:call-template name="uiDatepicker">
                            <xsl:with-param name="id" select="'DateStart'" />
                            <xsl:with-param name="name" select="'DateStart'" />
                            <xsl:with-param name="value">
                                <xsl:value-of select="Params/DateStart" />
                            </xsl:with-param>
                        </xsl:call-template>
                    </div>
                </label>
            </div>
            <div class="Form-item Bookchart-period">
                to
            </div>
            <div class="Form-item">
                <div class="Form-input-wrapper">
                    <xsl:call-template name="uiDatepicker">
                        <xsl:with-param name="id" select="'DateEnd'" />
                        <xsl:with-param name="name" select="'DateEnd'" />
                        <xsl:with-param name="value">
                            <xsl:value-of select="Params/DateEnd" />
                        </xsl:with-param>
                    </xsl:call-template>
                </div>
            </div>
            <div class="Form-item">
                <div class="Form-input-wrapper">
                    <xsl:call-template name="uiButton">
                        <xsl:with-param name="elemType" select="'submit'" />
                        <xsl:with-param name="content" select="'Update'" />
                    </xsl:call-template>
                </div>
            </div>
            <div class="Form-error Form-item Bookchart-period" id="dateError" />
        </div>
    </xsl:template>

    <xsl:template name="dateNav">
        <xsl:param name="days" />
        <xsl:param name="label" />
        <li class="Bookchart-period-item">
            <a href="javascript:void(0)" onClick="updateCharts('{$days}')">
                <xsl:if test="not(Params/DateStart and Params/DateEnd) and ($days = Params/Days or ($days = 30 and not(normalize-space(Params/Days))))">
                    <xsl:attribute name="class">Bookchart-currentperiod</xsl:attribute>
                </xsl:if>
                <xsl:value-of select="$label" />
            </a>
        </li>
    </xsl:template>

</xsl:stylesheet>
