﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <xsl:variable name="RDUserDataXML" select="AjaxRequest/RDUserDetailXML/."></xsl:variable>
        <xsl:variable name="CurrentPageNumber" select="AjaxRequest/Params/PageNumber"></xsl:variable>
        <xsl:variable name="UserID" select="AjaxRequest/Params/UserID"></xsl:variable>
        <xsl:variable name="PortalItemID" select="AjaxRequest/Params/PortalItemID"></xsl:variable>
        <xsl:variable name="RecentGoalItemID" select="AjaxRequest/Params/RecentGoalItemID"></xsl:variable>
        <xsl:variable name="TotalPages" select="AjaxRequest/RDUserDetailXML/TotalPages"></xsl:variable>
        <xsl:variable name="TotalRecords" select="AjaxRequest/RDUserDetailXML/TotalRecords"></xsl:variable>
        <xsl:variable name="Action" select="AjaxRequest/Action"></xsl:variable>

        <xsl:call-template name="RDUserData">
            <xsl:with-param name="RDUserDetailXML" select="$RDUserDataXML"></xsl:with-param>
            <xsl:with-param name="CurrentPageNumber" select="$CurrentPageNumber"></xsl:with-param>
            <xsl:with-param name="UserID" select="$UserID"></xsl:with-param>
            <xsl:with-param name="PortalItemID" select="$PortalItemID"></xsl:with-param>
            <xsl:with-param name="RecentGoalItemID" select="$RecentGoalItemID"></xsl:with-param>
            <xsl:with-param name="TotalPages" select="$TotalPages"></xsl:with-param>
            <xsl:with-param name="TotalRecords" select="$TotalRecords"></xsl:with-param>
            <xsl:with-param name="Action" select="$Action"></xsl:with-param>
        </xsl:call-template>

    </xsl:template>

    <xsl:template name="RDUserData">
        <xsl:param name="RDUserDetailXML" select="'N/A'"></xsl:param>
        <xsl:param name="CurrentPageNumber" select="'N/A'"></xsl:param>
        <xsl:param name="UserID" select="'N/A'"></xsl:param>
        <xsl:param name="PortalItemID" select="'N/A'"></xsl:param>
        <xsl:param name="RecentGoalItemID" select="'N/A'"></xsl:param>
        <xsl:param name="TotalPages" select="'N/A'"></xsl:param>
        <xsl:param name="TotalRecords" select="'N/A'"></xsl:param>
        <xsl:param name="Action" select="'N/A'"></xsl:param>
        <xsl:param name="Source" select="'N/A'"></xsl:param>

        <xsl:variable name="UserMetricCount"
                      select="count($RDUserDetailXML/TopMetricKeysXML/TopMetricKeys) + 1"></xsl:variable>
        <xsl:variable name="UserMetricTDWidth" select="(90)div($UserMetricCount)"/>

        <xsl:variable name="NextPageNumber">
            <xsl:number value="$CurrentPageNumber + 1"/>
        </xsl:variable>

        <xsl:variable name="PrevPageNumber">
            <xsl:number value="$CurrentPageNumber - 1"/>
        </xsl:variable>
        
        <xsl:variable name="DivWidth">
            <xsl:choose>
                <xsl:when test="$Source = 'clientactivity'">
                    <xsl:text>600px</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>900px</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <div id="div_DataIntervalBody" class="marginTop10">
            <div id="div_DataInterval" class="marginRight10" style="display:inline-block;">
                <span class="packageTitle marginLeft10">
                    <xsl:if test="$TotalPages &gt; 1 and $NextPageNumber &lt;= $TotalPages and $TotalRecords &gt; 0">
                        <span style="font-size:22px;width:20px;" class="marginRight5 onMouseOver" title="Prev"
                              onclick="LoadUserDetailInterface_Paging('{$UserID}','{$PortalItemID}','{$RecentGoalItemID}','{$NextPageNumber}', 'div_UserData_Leaderboard', 'optGoals');">&lt;</span>
                    </xsl:if>
                    <span>
                        <xsl:value-of select="$RDUserDetailXML/IntervalNameXML/IntervalName/PeriodIntervalName"/>
                    </span>

                    <xsl:if test="$TotalPages &gt; 1 and $PrevPageNumber &gt; 0">
                        <span style="font-size:22px;width:20px;" class="marginLeft5 onMouseOver" title="Next"
                              onclick="LoadUserDetailInterface_Paging('{$UserID}','{$PortalItemID}','{$RecentGoalItemID}','{$PrevPageNumber}', 'div_UserData_Leaderboard', 'optGoals');">&gt;</span>
                    </xsl:if>
                </span>
            </div>
            <div id="div_DateRange" style="display:inline-block;" class="marginLeft50">
                <span class="packageTitle">
                    Date Range:
                </span>
                <span class="publicationSummary">
                    <xsl:value-of select="$RDUserDetailXML/GoalStartDate"/> - <xsl:value-of select="$RDUserDetailXML/GoalEndDate"/>
                </span>
            </div>
        </div>

        <div id="div_UserDataBody" style="clear:both;" class="marginLeft10 paddingTop10">
            <table class="tableMaster" cellpadding="0" cellspacing="0" >
                <tr>
                    <td width="25px" class="textCenter">
                        <table height="100%" cellpadding="0" cellspacing="0">
                            <tr>
                                <td width="100px" height="25px" nowrap="nowrap" class="textCenter verticallyMiddle"
                                    style="border-bottom:1px solid #D2D3D4;border-right:1px solid #D2D3D4;border-left:1px solid #D2D3D4;border-top:1px solid #D2D3D4;">
                                    <span class="packageTitle" style="white-space:nowrap;display:inline-block;">
                                        <xsl:value-of select="$RDUserDetailXML/SourcePeriodType"/>
                                    </span>
                                </td>
                            </tr>
                            <xsl:for-each select="$RDUserDetailXML/PeriodXML/Period">
                                <tr>
                                    <td width="100px" height="25px" class="textCenter verticallyMiddle" nowrap="nowrap"
                                        style="border-bottom:1px solid #D2D3D4;border-right:1px solid #D2D3D4;border-left:1px solid #D2D3D4;">
                                        <span class="publicationSummary"
                                              style="padding-left: 2px;margin-left: 5px;margin-right: 10px;white-space:nowrap;display:inline-block;">
                                            <xsl:value-of select="Period"/>
                                        </span>
                                    </td>
                                </tr>
                            </xsl:for-each>
                        </table>
                    </td>
                    <td>
                        <xsl:variable name="MaxWidth">
                            <xsl:value-of select="count($RDUserDetailXML/TopMetricKeysXML/TopMetricKeys)*110"/>
                        </xsl:variable>

                        <div style="max-width:{$DivWidth};overflow-x: scroll; display:inline-block;">
                            <table height="100%" width="{$MaxWidth}px" cellpadding="0" cellspacing="0">
                                <tr>
                                    <xsl:for-each select="$RDUserDetailXML/TopMetricKeysXML/TopMetricKeys">
                                        <td width="110px" height="25px" nowrap="nowrap" class="textCenter verticallyMiddle"
                                            style="border-bottom:1px solid #D2D3D4;border-right:1px solid #D2D3D4;border-top:1px solid #D2D3D4;">
                                            <span class="packageTitle" style="white-space:nowrap;display:inline-block;">
                                                <xsl:value-of select="Metric"/>
                                            </span>

                                        </td>
                                    </xsl:for-each>
                                </tr>
                                <xsl:for-each select="$RDUserDetailXML/PeriodXML/Period">
                                    <xsl:variable name="CurrentPeriodKey" select="PeriodKey"></xsl:variable>
                                    <tr>
                                        <xsl:for-each select="$RDUserDetailXML/TopMetricKeysXML/TopMetricKeys">
                                            <xsl:variable name="CurrentMetricKey" select="Metric"></xsl:variable>
                                            <td class="textRight verticallyMiddle" width="110px" height="25px"
                                                style="border-bottom:1px solid #D2D3D4;border-right:1px solid #D2D3D4;">
                                                <xsl:choose>
                                                    <xsl:when test="string(number($RDUserDetailXML/UserVaultDataXML/UserDetail[Metric = $CurrentMetricKey and PeriodKey = $CurrentPeriodKey]/Amount)) != 'NaN'">
                                                        <div class="publicationSummary" style="height:15px;padding-left: 2px;margin-left: 5px;margin-right: 10px;">
                                                            <xsl:value-of select="format-number($RDUserDetailXML/UserVaultDataXML/UserDetail[Metric = $CurrentMetricKey and PeriodKey = $CurrentPeriodKey]/Amount, '###,###')"/>
                                                        </div>
                                                    </xsl:when>
                                                    <xsl:otherwise>
                                                        <div style="height:15px;padding-left: 2px;margin-left: 5px;margin-right: 10px;"></div>
                                                    </xsl:otherwise>
                                                </xsl:choose>
                                            </td>
                                        </xsl:for-each>
                                    </tr>
                                </xsl:for-each>
                            </table>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </xsl:template>
</xsl:stylesheet>
