<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:include href="variables.xsl" />
  <xsl:include href="global.xsl" />
  <xsl:include href="navigation.xsl" />

  <xsl:template name="salesLayout">
    <xsl:choose>
      <xsl:when test="Params/cmd = 'revenue'">
        <xsl:call-template name="revenueLayout" />
      </xsl:when>
      <xsl:when test="Params/cmd = 'finish'">
        <xsl:call-template name="finishLayout" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="mainLayout" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="greeting">
    <div id="greeting">
      <img alt="Epitaph" src="/test/images/logo_epitaph.gif" />
      <h2>Welcome back, <xsl:value-of select="first_name" /></h2>
      <xsl:call-template name="messages" />
      <ul>
        <li><a href="/app/tracker">Check Royalty Status</a></li>
        <li><a href="#">Administrate User Accounts</a></li>
        <li><a href="#">Contact Your Account Manager</a></li>
      </ul>
    </div>
  </xsl:template>

  <xsl:template name="mainLayout">
    <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: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:call-template name="salesChart">
      <xsl:with-param name="chartTitle">Top Tracks</xsl:with-param>
      <xsl:with-param name="chartPosition">Right</xsl:with-param>
      <xsl:with-param name="chartAction">as</xsl:with-param>
      <xsl:with-param name="chartType">track_name</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="salesChart">
      <xsl:with-param name="chartTitle">Top Albums</xsl:with-param>
      <xsl:with-param name="chartPosition">Left</xsl:with-param>
      <xsl:with-param name="chartAction">ks</xsl:with-param>
      <xsl:with-param name="chartType">album_name</xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="byLayout">
    <xsl:for-each select="charts/chart">
      <xsl:if test="chart_name = 'revenue_by_service'">
        <xsl:call-template name="pieChart">
        </xsl:call-template>
      </xsl:if>
    </xsl:for-each>
    <xsl:for-each select="lists/list">
      <xsl:if test="name = 'top_tracks_by_services' or name = 'top_tracks_by_formats'">
        <xsl:call-template name="salesChart">
          <xsl:with-param name="chartTitle">Top Tracks</xsl:with-param>
          <xsl:with-param name="chartAction">as</xsl:with-param>
          <xsl:with-param name="chartType">track_name</xsl:with-param>
        </xsl:call-template>
      </xsl:if>
    </xsl:for-each>
    <xsl:for-each select="lists/list">
      <xsl:if test="name = 'top_albums_by_services' or name = 'top_albums_by_formats'">
        <xsl:call-template name="salesChart">
          <xsl:with-param name="chartTitle">Top Albums</xsl:with-param>
          <xsl:with-param name="chartAction">ks</xsl:with-param>
          <xsl:with-param name="chartType">album_name</xsl:with-param>
        </xsl:call-template>
      </xsl:if>
    </xsl:for-each>
 </xsl:template>

  <xsl:template name="topSummaryLayout">
    <xsl:for-each select="charts/chart">
      <xsl:if test="chart_name = 'revenue_by_format'">
        <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:if>
    </xsl:for-each>
    <xsl:for-each select="charts/chart">
      <xsl:if test="chart_name = 'revenue_by_service'">
        <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:if>
    </xsl:for-each>
    <xsl:for-each select="lists/list">
        <xsl:call-template name="salesChart">
          <xsl:with-param name="chartType">
            <xsl:if test="name = 'top_albums_by_services'">album_name</xsl:if>
            <xsl:if test="name = 'top_tracks_by_services'">track_name</xsl:if>
          </xsl:with-param>
        </xsl:call-template>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="topDetailLayout">
    <xsl:for-each select="charts/chart">
      <xsl:if test="chart_name = 'revenue_by_format'">
        <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:if>
    </xsl:for-each>
    <xsl:for-each select="charts/chart">
      <xsl:if test="chart_name = 'revenue_by_service'">
        <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:if>
    </xsl:for-each>
    <xsl:for-each select="lists/list">
      <xsl:if test="name = 'product_detail_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:with-param name="chartType">format</xsl:with-param>
        </xsl:call-template>
      </xsl:if>
    </xsl:for-each>
    <xsl:for-each select="lists/list">
      <xsl:if test="name = 'product_detail_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:with-param name="chartType">service</xsl:with-param>
        </xsl:call-template>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="pieChart">
    <div class="pieChart{$chartPosition}">
      <xsl:if test="normalize-space($chartTitle)">
        <h2><a href="?c={$chartAction}&amp;period={$period}&amp;label_id={$labelID}"><xsl:value-of select="$chartTitle" /></a></h2>
      </xsl:if>
      <map name="{chart_name}" id="{chart_name}">
        <xsl:value-of select="image_map" disable-output-escaping="yes" />
      </map>
      <table class="pieChartTable">
        <tr>
          <td class="pieChartTD"><img src="/test/images/temp/pieOnly2.gif" alt="Chart of {$chartTitle}" usemap="#{chart_name}" class="pieChartImage" /></td>
          <td class="pieLegendTD">
            <div class="pieLegend">
              <div><a href="#"><img src="/test/images/legendColor.gif" class="legend1" /> 58% - iTunes</a></div>
              <div><a href="#"><img src="/test/images/legendColor.gif" class="legend2" /> 11% - Napster</a></div>
              <div><a href="#"><img src="/test/images/legendColor.gif" class="legend3" /> 9% - iTunes Europe</a></div>
            </div>
          </td>
        </tr>
      </table>
    </div>
  </xsl:template>

  <xsl:template name="salesChart">
    <div class="textChart{$chartPosition}">
      <xsl:if test="normalize-space($chartTitle)">
        <h2><a href="?c={$chartAction}&amp;period={$period}&amp;label_id={$labelID}"><xsl:value-of select="$chartTitle" /></a></h2>
      </xsl:if>
      <table class="textChartHi">
        <tr>
          <th class="rowLeft headerRowRank">Rank</th>
          <th class="headerRowRank">Last Rank</th>
          <th class="headerRowTitle">Title</th>
          <th>Units</th>
          <th>Revenue</th>
        </tr>
        <xsl:for-each select="data_row">
          <xsl:call-template name="salesRow">
            <xsl:with-param name="chartAction"><xsl:value-of select="substring($chartAction,1,1)"/></xsl:with-param>
            <xsl:with-param name="chartType"><xsl:value-of select="$chartType" /></xsl:with-param>
          </xsl:call-template>
        </xsl:for-each>
      </table>
    </div>
  </xsl:template>

  <xsl:template name="statusChart">
    <div class="sectionHeader"><h1>Sales Data Status</h1></div>
    <div class="sectionWrap">
      <div class="sectionBody">
        <table id="statusChart">
          <tr>
            <th class="rowLeft">Service</th>
            <th>MONTH</th>
            <th>MONTH</th>
            <th>MONTH</th>
          </tr>
          <xsl:for-each select="Services/Service">
            <xsl:sort select="translate(cell[1]/value,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
            <tr class="row{position()  mod 2}">
              <td><xsl:value-of select="Name" /></td>
              <xsl:if test="Name != 'eMusic'">
                <xsl:for-each select="Month">
                  <xsl:if test="value = 0"><td>&#160;</td></xsl:if>
                  <xsl:if test="value = 1"><td class="statusMonth">&#160;</td></xsl:if>
                </xsl:for-each>
              </xsl:if>
              <xsl:if test="Name = 'eMusic'">
                <xsl:if test="cell[2]/value = 0"><td colspan="3">&#160;</td></xsl:if>
                <xsl:if test="cell[2]/value = 1"><td colspan="3" class="statusQuarter">&#160;</td></xsl:if>
              </xsl:if>
            </tr>
          </xsl:for-each>
        </table>
      </div>
    </div>
  </xsl:template>

  <xsl:template match="salesRow">
    <tr class="row{position() mod 2}">
      <xsl:for-each select="cell">
        <td class="dataRow{key}">
          <xsl:if test="key = 'Rank'">
            <xsl:choose>
              <xsl:when test="value &lt; ../cell[2]/value">
                <xsl:attribute name="class">dataRowRankUp</xsl:attribute>
              </xsl:when>
              <xsl:when test="../cell[2]/value = 'NA'">
                <xsl:attribute name="class">dataRowRankUp</xsl:attribute>
              </xsl:when>
              <xsl:when test="value &gt; ../cell[2]/value">
                <xsl:attribute name="class">dataRowRankDown</xsl:attribute>
              </xsl:when>
              <xsl:otherwise>
                <xsl:attribute name="class">dataRowRankSame</xsl:attribute>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:if>
          <xsl:choose>
            <xsl:when test="key = 'Title'">
              <a href="?c={$chartAction}&amp;period={$period}&amp;label_id={$labelID}&amp;{$chartType}={encoded_value}"><xsl:value-of select="value" /></a>
            </xsl:when>
            <xsl:when test="key = 'Service' or key = 'Format'">
              <a href="?c={$chartAction}&amp;period={$period}&amp;label_id={$labelID}&amp;{$chartType}={value}"><xsl:value-of select="value" /></a>
            </xsl:when>
            <xsl:otherwise>
              <xsl:if test="key = 'Revenue'">$</xsl:if>
              <xsl:value-of select="value" />
            </xsl:otherwise>
          </xsl:choose>
        </td>
      </xsl:for-each>
    </tr>
  </xsl:template>

</xsl:stylesheet>

