﻿<?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="CustomXML" select="AjaxRequest/Settings/CustomXML"></xsl:variable>
        <xsl:variable name="Origin" select="normalize-space(AjaxRequest/Params/Origin)"></xsl:variable>
        <xsl:variable name="MBrandItemID" select="AjaxRequest/Params/MBrandItemID"></xsl:variable>
        <xsl:variable name="MBrandID" select="AjaxRequest/Params/MBrandID"></xsl:variable>
        <xsl:variable name="IsAuthenticated" select="normalize-space(AjaxRequest/Authenticated)"></xsl:variable>
        <xsl:variable name="LoggedInUserID" select="normalize-space(AjaxRequest/LoggedInUserID)"></xsl:variable>
        <xsl:variable name="HostName" select="normalize-space(AjaxRequest/HostName)"></xsl:variable>
        <xsl:variable name="TemplateURL" select="normalize-space(AjaxRequest/TemplateURL)"></xsl:variable>
        <xsl:variable name="BrandXml" select="AjaxRequest/UserBrands/."></xsl:variable>
        <xsl:variable name="ItemID" select="AjaxRequest/Params/ItemID"></xsl:variable>
        <xsl:variable name="AdditionalData" select="AjaxRequest/Params/AdditionalData/."></xsl:variable>
        <xsl:variable name="CommunityAdmin" select="AjaxRequest/UserRoles/Role[RoleName = 'Community Admin']"></xsl:variable>
        <xsl:variable name="DomainName" select="AjaxRequest/Settings/DomainName"></xsl:variable>
        <xsl:variable name="IsSecureConnection" select="AjaxRequest/IsSecureConnection"></xsl:variable>

        <xsl:variable name="Protocol">
            <xsl:choose>
                <xsl:when test="$IsSecureConnection = 1">
                    <xsl:text>https://</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>http://</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="ClientManagerItemID">
            <xsl:choose>
                <xsl:when test="AjaxRequest/UserBrands/Brand[(PermissionID >= -2) 
                          and (ItemID = $MBrandItemID or ParentPortalItemID = $MBrandItemID)]/ItemID[text()]">
                    <xsl:value-of select="AjaxRequest/UserBrands/Brand[(PermissionID >= -2) 
                                  and (ItemID = $MBrandItemID or ParentPortalItemID = $MBrandItemID)]/ItemID"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:number value="0"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
        <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />


        <xsl:variable name="workingHeaderDivClass">
            <xsl:choose>
                <xsl:when test="$Origin = 'happiness'">
                    <xsl:text>workingAreaHeaderHappiness</xsl:text>
                </xsl:when>
                <xsl:when test="$Origin = 'truestar'">
                    <xsl:text>workingAreaHeaderTrueStar</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>workingAreaHeader</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="selectedLinkClass">
            <xsl:choose>
                <xsl:when test="$Origin = 'truestar'">
                    <xsl:text>selectedBlueTopMenuLink</xsl:text>
                </xsl:when>
                <xsl:when test="$Origin = 'massimo' or $Origin = 'massimobile'">
                    <xsl:text>selectedTopMenuLinkMassimo</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>selectedTopMenuLink</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="defaultLinkClass">
            <xsl:choose>
                <xsl:when test="$Origin = 'truestar'">
                    <xsl:text>defaultBlueTopMenuLink</xsl:text>
                </xsl:when>
                <xsl:when test="$Origin = 'massimo' or $Origin = 'massimobile'">
                    <xsl:text>defaultTopMenuLinkMassimo</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>defaultTopMenuLink</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="AlignTopMenu">
            <xsl:choose>
                <xsl:when test="AjaxRequest/Settings/AlignTopHeaderMenu/Align">
                    <xsl:value-of select="AjaxRequest/Settings/AlignTopHeaderMenu/Align"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>left</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="DisplayBlankTD">
            <xsl:choose>
                <xsl:when test="$AlignTopMenu = 'center'">
                    <xsl:text>none</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>table-cell</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="cellSpacing">
            <xsl:choose>
                <xsl:when test="$AlignTopMenu = 'center'">
                    <xsl:text>2</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>0</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="CommunityTabFontColor">
            <xsl:choose>
                <xsl:when test="$DomainName = 'Massimo' or $Origin = 'massimobile'">
                    <xsl:text>#6D6E71</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>#A7A9AC</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <div id="divWorkingAreaHeader" class="{$workingHeaderDivClass}" align="left">
            <xsl:if test="$IsAuthenticated = 'True'">
                <table id="divWorkingAreaHeader_ActionLink" cellspacing="{$cellSpacing}" height="100%" class="capitalFont" cellpadding="0" align="{$AlignTopMenu}">
                    <tr id="tr_TopMenu">
                        <td id="cellWorkingAreaHeaderLeft" class="leftBar verticallyMiddle" style="display:{$DisplayBlankTD}"></td>
                        <xsl:for-each select="$CustomXML/Tab[(count($BrandXml/Brand[PermissionID > 1]) &gt; 0) 
                                      or (TabName != 'CompanyAdmin' and count($BrandXml/Brand[PermissionID > 1]) = 0)]">

                            <xsl:variable name="CurrentTabName">
                                <xsl:value-of select="TabName"/>
                            </xsl:variable>

                            <xsl:variable name="CurrentTabStrippedDisplayName">
                                <xsl:value-of select="translate(normalize-space(DisplayName),' ','')"/>
                            </xsl:variable>

                            <xsl:variable name="tdClass">
                                <xsl:choose>
                                    <xsl:when test="$AdditionalData/Panel[text()] 
                                                      and $AdditionalData/Panel[
                                                                                    translate(normalize-space(text()), $smallcase, $uppercase) = translate($CurrentTabName, $smallcase, $uppercase)
                                                                                    or 
                                                                                    translate(normalize-space(text()), $smallcase, $uppercase) = translate($CurrentTabStrippedDisplayName, $smallcase, $uppercase)
                                                                                ]">
                                        <xsl:value-of select="$selectedLinkClass"/>
                                    </xsl:when>
                                    <xsl:when test="position() = 1 and not($AdditionalData/Panel)">
                                        <xsl:value-of select="$selectedLinkClass"/>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:value-of select="$defaultLinkClass"/>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:variable>

                            <xsl:variable name="onClickFunction">
                                <xsl:choose>
                                    <xsl:when test="TabName = 'CompanyAdmin'">
                                        <xsl:text>window.location.href = '</xsl:text>
                                        <xsl:value-of select="$Protocol"/>
                                        <xsl:value-of select="$HostName"/>
                                        <xsl:text>/';</xsl:text>
                                    </xsl:when>
                                    <xsl:when test="TabName = 'HomeWorkouts'">
                                        <xsl:text>loadLifespacesDashboardContent('</xsl:text>
                                        <xsl:number value="0"/>
                                        <xsl:text>', 'Limit', '</xsl:text>
                                        <xsl:value-of select="$MBrandItemID"/>
                                        <xsl:text>', 'divDashboardLayout', 'MyWorkouts');</xsl:text>
                                    </xsl:when>
                                    <xsl:when test="TabName = 'Journey' or TabName = 'WorkGoals'">
                                        <xsl:text>loadLifespacesDashboardContent('</xsl:text>
                                        <xsl:number value="0"/>
                                        <xsl:text>', 'Limit', '</xsl:text>
                                        <xsl:value-of select="$MBrandItemID"/>
                                        <xsl:text>', 'divDashboardLayout', 'default');</xsl:text>
                                    </xsl:when>
                                    <xsl:when test="TabName = 'PersonalGoals'">
                                        <xsl:text>loadLifespacesDashboardContent('</xsl:text>
                                        <xsl:number value ="0"/>
                                        <xsl:text>', 'Exclude', '</xsl:text>
                                        <xsl:value-of select="$MBrandItemID"/>
                                        <xsl:text>', 'divDashboardLayout', 'default');</xsl:text>
                                    </xsl:when>
                                    <xsl:when test="TabName = 'Opportunities'">
                                        <xsl:text>loadLifespacesDashboardContent('</xsl:text>
                                        <xsl:number value="0"/>
                                        <xsl:text>', 'Limit', '</xsl:text>
                                        <xsl:value-of select="$MBrandItemID"/>
                                        <xsl:text>', 'divDashboardLayout', 'MyPipeLine');</xsl:text>
                                    </xsl:when>
                                    <xsl:when test="TabName = 'ClientManager'">
                                        <xsl:text>loadLifespacesDashboardContent('</xsl:text>
                                        <xsl:value-of select="$ClientManagerItemID"/>
                                        <xsl:text>', 'All', '</xsl:text>
                                        <xsl:value-of select="$MBrandItemID"/>
                                        <xsl:text>', 'divDashboardLayout', 'ClientManager');</xsl:text>
                                    </xsl:when>
                                    <xsl:when test="TabName = 'Scorecard'">
                                        <xsl:text>loadLifespacesDashboardContent('</xsl:text>
                                        <xsl:number value="0"/>
                                        <xsl:text>', 'Limit', '</xsl:text>
                                        <xsl:value-of select="$MBrandItemID"/>
                                        <xsl:text>', 'divDashboardLayout', 'Scorecard');</xsl:text>
                                    </xsl:when>
                                    <xsl:when test="TabName = 'JoinChallenge' or TabName = 'Challenges'">
                                        <xsl:text>getChallengesList('', 0, '-99','divDashboardLayout');</xsl:text>
                                    </xsl:when>
                                    <xsl:when test="TabName = 'Communities' or TabName = 'Community'">
                                        <!--<xsl:text>OpenSpecifiedURL('http://</xsl:text>
                                        <xsl:value-of select="$HostName"/>
                                        <xsl:text>/Community');</xsl:text>-->
                                    </xsl:when>
                                </xsl:choose>
                            </xsl:variable>


                            <xsl:choose>
                                <xsl:when test="TabName = 'ClientManager'">
                                    <xsl:choose>
                                        <xsl:when test="count($BrandXml/Brand) &gt; 1">                                            
                                            <td name="brandTopMenu" id="td_CustomMenu_{TabName}_{position()}" width="140px" nowrap="nowrap"
                                                 class="ExpandedElement labelTopHeaderNavigation verticallyMiddle onMouseOver paddingLeft10 paddingRight10 {$tdClass}"
                                                 onclick="document.getElementById('divBrandNavigation_CommunityAdmin_ClientDashboard').style.display='block';
                                                            this.setAttribute('class', (this.getAttribute('class') + ' ExpandedElement'))"
                                                 onmouseover="document.getElementById('divBrandNavigation_CommunityAdmin_ClientDashboard').style.display='block';
                                                            this.setAttribute('class', (this.getAttribute('class') + ' ExpandedElement'))"
                                                 onmouseout="setDisplayOnMouseOut(this, event, 'divBrandNavigation_CommunityAdmin_ClientDashboard', 'none');
                                                            replaceCSSClass(this, 'ExpandedElement', '');"
                                                 workingElements="&lt;WorkingElements&gt;&lt;WorkingElement&gt;td_CustomMenu_{TabName}_{position()}&lt;/WorkingElement&gt;&lt;WorkingElement&gt;divBrandNavigation_CommunityAdmin_ClientDashboard&lt;/WorkingElement&gt;&lt;/WorkingElements&gt;"
                                                 activeElement="td_CustomMenu_{TabName}_{position()}"
                                                 onTouchStart="document.getElementById('divBrandNavigation_CommunityAdmin_ClientDashboard').style.display='block';
                                                                this.setAttribute('class', (this.getAttribute('class') + ' ExpandedElement'))">
                                                <div class="textCenter">
                                                    <xsl:value-of select="DisplayName"/>
                                                </div>

                                                <div id="divbrandNavigation">
                                                    <xsl:call-template name="BrandMenuChildNavigation_ClientDashboard">
                                                        <xsl:with-param name="BrandXml" select="$BrandXml"></xsl:with-param>
                                                        <xsl:with-param name="MBrandItemID" select="$MBrandItemID"></xsl:with-param>
                                                        <xsl:with-param name="selectedLinkClass" select="$selectedLinkClass"></xsl:with-param>
                                                        <xsl:with-param name="defaultLinkClass" select="$defaultLinkClass"></xsl:with-param>
                                                    </xsl:call-template>
                                                </div>
                                            </td>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <td name="brandTopMenu" id="td_CustomMenu_{TabName}_{position()}" width="140px" nowrap="nowrap"
                                               class="labelTopHeaderNavigation verticallyMiddle onMouseOver paddingLeft10 paddingRight10 {$tdClass}"
                                               onclick="{$onClickFunction}topMenu_HightlightSelectedLink('divWorkingAreaHeader_ActionLink','td_CustomMenu_{TabName}_{position()}', 
																	'{$defaultLinkClass}', '{$selectedLinkClass}', 'brandTopMenu');">
                                                <div class="textCenter">
                                                    <xsl:value-of select="DisplayName"/>
                                                </div>
                                            </td>
                                        </xsl:otherwise>
                                    </xsl:choose>


                                </xsl:when>
                                <xsl:when test="TabName = 'CompanyAdmin' and count($BrandXml/Brand[PermissionID &gt; 1]) = 1">
                                    <td name="brandTopMenu" id="td_CustomMenu_{TabName}_{position()}" width="140px" nowrap="nowrap" onclick="{$onClickFunction}"
                                        class="labelTopHeaderNavigation verticallyMiddle onMouseOver paddingLeft10 paddingRight10 {$tdClass}">
                                        <div class="textCenter">
                                            <xsl:value-of select="DisplayName"/>
                                        </div>
                                    </td>
                                </xsl:when>
                                <xsl:when test="TabName = 'CompanyAdmin' and count($BrandXml/Brand[PermissionID &gt; 1]) &gt; 1">
                                    <td name="brandTopMenu" id="td_CustomMenu_{TabName}_{position()}" width="140px" nowrap="nowrap"
                                      class="ExpandedElement labelTopHeaderNavigation verticallyMiddle onMouseOver paddingLeft10 paddingRight10 {$tdClass}"
                                        onclick="document.getElementById('divBrandNavigation_CommunityAdmin').style.display='block';
                                        this.setAttribute('class', (this.getAttribute('class') + ' ExpandedElement'))"
                                        onmouseover="document.getElementById('divBrandNavigation_CommunityAdmin').style.display='block';
                                        this.setAttribute('class', (this.getAttribute('class') + ' ExpandedElement'))"
                                        onmouseout="setDisplayOnMouseOut(this, event, 'divBrandNavigation_CommunityAdmin', 'none');
                                        replaceCSSClass(this, 'ExpandedElement', '');"
                                        workingElements="&lt;WorkingElements&gt;&lt;WorkingElement&gt;td_CustomMenu_{TabName}_{position()}&lt;/WorkingElement&gt;&lt;WorkingElement&gt;divBrandNavigation_CommunityAdmin&lt;/WorkingElement&gt;&lt;/WorkingElements&gt;"
                                        activeElement="td_CustomMenu_{TabName}_{position()}"
                                        onTouchStart="document.getElementById('divBrandNavigation_CommunityAdmin').style.display='block';
                                        this.setAttribute('class', (this.getAttribute('class') + ' ExpandedElement'))">

                                        <xsl:choose>
                                            <xsl:when test="count($BrandXml/Brand) = 1">
                                                <div class="textCenter">
                                                    <xsl:value-of select="DisplayName"/>
                                                </div>

                                            </xsl:when>
                                            <xsl:otherwise>
                                                <div class="textCenter">
                                                    <xsl:value-of select="DisplayName"/>
                                                </div>

                                                <div id="divNavigation">
                                                    <xsl:call-template name="BrandMenuChildNavigation">
                                                        <xsl:with-param name="BrandXml" select="$BrandXml"></xsl:with-param>
                                                        <xsl:with-param name="MBrandItemID" select="$MBrandItemID"></xsl:with-param>
                                                        <xsl:with-param name="selectedLinkClass" select="$selectedLinkClass"></xsl:with-param>
                                                        <xsl:with-param name="defaultLinkClass" select="$defaultLinkClass"></xsl:with-param>

                                                    </xsl:call-template>
                                                </div>
                                            </xsl:otherwise>
                                        </xsl:choose>

                                    </td>
                                </xsl:when>
                                <xsl:when test="TabName = 'Community'">
                                    <td name="brandTopMenu" id="td_CustomMenu_{TabName}_{position()}" width="140px" nowrap="nowrap"
                                        class="labelTopHeaderNavigation verticallyMiddle onMouseOver {$defaultLinkClass}"
                                        onclick="topMenu_HightlightSelectedLink('divWorkingAreaHeader_ActionLink','td_CustomMenu_{TabName}_{position()}', 
																	'{$defaultLinkClass}', '{$selectedLinkClass}', 'brandTopMenu');">
                                        <a href="http://{$HostName}/Community"  class="decorationNone  {$defaultLinkClass}">
                                            <div class="textCenter {$defaultLinkClass} onMouseOver paddingLeft10 paddingRight10 ">
                                                <xsl:value-of select="DisplayName"/>
                                            </div>
                                        </a>
                                    </td>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:if test="TabName != 'CompanyAdmin' and TabName != 'ClientManager'">
                                        <td name="brandTopMenu" id="td_CustomMenu_{TabName}_{position()}" width="140px" nowrap="nowrap"
                                          class="labelTopHeaderNavigation verticallyMiddle onMouseOver paddingLeft10 paddingRight10 {$tdClass}"
                                          onclick="{$onClickFunction}topMenu_HightlightSelectedLink('divWorkingAreaHeader_ActionLink','td_CustomMenu_{TabName}_{position()}', 
																	'{$defaultLinkClass}', '{$selectedLinkClass}', 'brandTopMenu');">
                                            <div class="textCenter">
                                                <xsl:value-of select="DisplayName"/>
                                            </div>
                                        </td>
                                    </xsl:if>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:for-each>
                        <td id="td_right">&#xa0;</td>
                    </tr>
                </table>
            </xsl:if>
        </div>
    </xsl:template>

    <xsl:template name="BrandMenuChildNavigation">
        <xsl:param name="BrandXml" select="'N/A'"></xsl:param>
        <xsl:param name="MBrandItemID" select="'N/A'"></xsl:param>
        <xsl:param name="defaultLinkClass" select="'N/A'"></xsl:param>
        <xsl:param name="selectedLinkClass" select="'N/A'"></xsl:param>

        <div id="divBrandNavigation_CommunityAdmin" class="brandNavigationBakground textLeft padding5" style="margin-top:2px;">

            <xsl:for-each select="$BrandXml/Brand[PermissionID &gt; 1]">
                <!--<xsl:sort data-type="text" order="ascending" select="Item"/>-->

                <!--ItemID is the PortalItemID of child portal-->
                <div id="divBrandNavigationChild_{position()}" class="padding3 portalTextClass"
                     onclick="loadLifespacesDashboardContent('{ItemID}', 'All', '{$MBrandItemID}', 'divDashboardLayout', 'default');
                              displayUserPopup('{ItemID}', '{$MBrandItemID}', 'Portal')
							  topMenu_HightlightSelectedLink('divWorkingAreaHeader_ActionLink','divBrandNavigationChild_{position()}', '{$defaultLinkClass}', '{$selectedLinkClass}', 'brandTopMenu');">
                    <span>
                        <xsl:value-of select="Item"/>
                    </span>
                </div>
            </xsl:for-each>
        </div>
    </xsl:template>

    <xsl:template name="BrandMenuChildNavigation_ClientDashboard">
        <xsl:param name="BrandXml" select="'N/A'"></xsl:param>
        <xsl:param name="MBrandItemID" select="'N/A'"></xsl:param>
        <xsl:param name="defaultLinkClass" select="'N/A'"></xsl:param>
        <xsl:param name="selectedLinkClass" select="'N/A'"></xsl:param>

        <div id="divBrandNavigation_CommunityAdmin_ClientDashboard" class="brandNavigationBakground textLeft padding5" style="margin-top:2px;">

            <xsl:for-each select="$BrandXml/Brand">
                <!--<xsl:sort data-type="text" order="ascending" select="Item"/>-->

                <!--ItemID is the PortalItemID of child portal-->
                <div id="divBrandNavigationChild_{position()}" class="padding3 portalTextClass"
                     onclick="loadLifespacesDashboardContent('{ItemID}', 'All', '{$MBrandItemID}', 'divDashboardLayout', 'ClientManager');
                              displayUserPopup('{ItemID}', '{$MBrandItemID}', 'Portal')
							  topMenu_HightlightSelectedLink('divWorkingAreaHeader_ActionLink','divBrandNavigationChild_{position()}', '{$defaultLinkClass}', '{$selectedLinkClass}', 'brandTopMenu');">
                    <span>
                        <xsl:value-of select="Item"/>
                    </span>
                </div>
            </xsl:for-each>
        </div>
    </xsl:template>
</xsl:stylesheet>
