﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <xsl:variable name ="Origin" select ="normalize-space(LinkedPublications/Origin)"></xsl:variable>
        <xsl:variable name ="Source" select ="normalize-space(LinkedPublications/Source)"></xsl:variable>
        <xsl:variable name ="BrandID" select ="normalize-space(LinkedPublications/BrandID)"></xsl:variable>
        <xsl:variable name ="IsAuthenticated" select ="normalize-space(LinkedPublications/Authenticated)"></xsl:variable>
        <xsl:variable name ="PromoteKey" select ="normalize-space(LinkedPublications/PromoteKey)"></xsl:variable>
        <xsl:variable name ="HostName" select ="normalize-space(LinkedPublications/HostName)"></xsl:variable>
        <xsl:variable name ="TemplateUrl" select ="normalize-space(LinkedPublications/TemplateUrl)"></xsl:variable>
        <xsl:variable name="BrandPermissionID" select="normalize-space(LinkedPublications/BrandPermissionID)"></xsl:variable>
        <xsl:variable name="isCommunityAdmin" select="normalize-space(LinkedPublications/isCommunityAdmin)"></xsl:variable>
        <xsl:variable name="CommunityPermissionID" select="normalize-space(LinkedPublications/CommunityPermissionID)"></xsl:variable>
              
        <xsl:variable name="displayStyle">
            <xsl:choose>
                <xsl:when test="count(LinkedPublications/Publication) &gt; 3">
                    <xsl:text>display:inline</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>display:none</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="IsCommunityAdminUser">
            <xsl:choose>
                <xsl:when test="$isCommunityAdmin">
                    <xsl:number value="1"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:number value="0"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
      
        <div id="divPublications">           
            <xsl:choose>
                <xsl:when test="$Source != 'Default'">
                    <table class="tableMaster">
                        <tr>
                            <td width="15">
                                <a class="link" href="http://{$HostName}/{$PromoteKey}/Feed" title="Subscribe to my feed">
                                    <img src="{$TemplateUrl}\Web\Images\RSSFeed.bmp"
                                         style="border:0; display: none;" onload='setWidthAndHeightImage(this, "20");'/>
                                </a>
                            </td>
                            <td style="padding-bottom: 10px;">
                                <a class="link" href="http://{$HostName}/{$PromoteKey}/Feed" title="Subscribe to my feed">
                                    <span class="label">
                                        Publication Feeds
                                    </span>
                                </a>
                            </td>
                        </tr>
                    </table>
                </xsl:when>
            </xsl:choose>

            <xsl:if test="count(LinkedPublications/Publication) &gt; 0 and $Origin !='Brand'">
                <table class="tableMaster" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td align="center">
                            <xsl:for-each select="LinkedPublications/Publication[position() &lt;= 3]">
                                <xsl:call-template name="DisplayPublicationsLinkedToCommunity">
                                    <xsl:with-param name="ImageURL" select="ImageURL"></xsl:with-param>
                                    <xsl:with-param name="HostName" select="$HostName"></xsl:with-param>
                                    <xsl:with-param name="ItemID" select="normalize-space(ItemID)"></xsl:with-param>
                                    <xsl:with-param name="Title" select="Title"></xsl:with-param>
                                    <xsl:with-param name="Summary" select="Summary"></xsl:with-param>
                                    <xsl:with-param name="BrandID" select="$BrandID"></xsl:with-param>
                                </xsl:call-template>
                            </xsl:for-each>
                        </td>
                    </tr>
                </table>
            </xsl:if>
            <xsl:if test ="$Origin = 'Brand'">
                <table class="tableMaster" border="0" cellspacing="0" cellpadding="0">
                    <tr  id="tr_publicationList">
                        <td align="center">
                            <div id="RelatedPosts_Default">
                                <xsl:for-each select="LinkedPublications/Publication[position() &lt;= 3]">
                                    <xsl:call-template name="DisplayPublicationsLinkedToCommunity">
                                        <xsl:with-param name="ImageURL" select="ImageURL"></xsl:with-param>
                                        <xsl:with-param name="HostName" select="$HostName"></xsl:with-param>
                                        <xsl:with-param name="ItemID" select="normalize-space(ItemID)"></xsl:with-param>
                                        <xsl:with-param name="Title" select="Title"></xsl:with-param>
                                        <xsl:with-param name="Summary" select="Summary"></xsl:with-param>
                                        <xsl:with-param name="BrandID" select="$BrandID"></xsl:with-param>
                                    </xsl:call-template>
                                </xsl:for-each>
                            </div>
                            <div id="RelatedPosts_Expanded" style="display:none;">
                                <xsl:for-each select="LinkedPublications/Publication[position() &gt; 3]">
                                    <xsl:call-template name="DisplayPublicationsLinkedToCommunity">
                                        <xsl:with-param name="ImageURL" select="ImageURL"></xsl:with-param>
                                        <xsl:with-param name="HostName" select="$HostName"></xsl:with-param>
                                        <xsl:with-param name="ItemID" select="normalize-space(ItemID)"></xsl:with-param>
                                        <xsl:with-param name="Title" select="Title"></xsl:with-param>
                                        <xsl:with-param name="Summary" select="Summary"></xsl:with-param>
                                        <xsl:with-param name="BrandID" select="$BrandID"></xsl:with-param>
                                    </xsl:call-template>
                                </xsl:for-each>
                            </div>

                        </td>
                    </tr>
                    <tr>
                        <td style="margin-top: 10px; text-align: center;" align="center">
                            <div id="div_ShowAllRelatedPosts"  style="{$displayStyle};">
                                <a class="link"
                                   onmouseover="this.style.textDecoration='underline';"
                                   onmouseout="this.style.textDecoration='none';"
                                   onClick="return showRelatedPostContent(
                        'RelatedPosts_Default', 
                        'RelatedPosts_Expanded',
                        'div_ShowAllRelatedPosts');">
                                    View All Posts
                                </a>
                            </div>             
                            
                            <xsl:if test="$CommunityPermissionID != -1 and $IsAuthenticated = 'True' and ($BrandPermissionID &gt;= 4 or $isCommunityAdmin = 1 or LinkedPublications/UserRoles/Role[RoleName = 'Brand Admin'])">
                                <div>
                                    <span id="div_AddContent" style="display: inline;margin-right:10px;">
                                        <a class="link"
                                               href="http://{$HostName}/AddPublicationContent.aspx?BrandID={$BrandID}"
                                               onmouseover="this.style.textDecoration='underline';"
                                               onmouseout="this.style.textDecoration='none';">
                                            Add a Blog Post                                            
                                        </a>
                                    </span>
                                    <span id="div_ShowAllDrafts" style="margin-right:10px;">
                                        <a class="link"
                                               onmouseover="this.style.textDecoration='underline';"
                                               onmouseout="this.style.textDecoration='none';"
                                               onClick="return getAllPostsRelatedToBrand('{$BrandID}','Drafts',{$IsCommunityAdminUser},{$BrandPermissionID});">
                                            View All Drafts
                                        </a>
                                    </span>                                       
                                    <span id="div_ShowAllLive" style="display:none;">
                                        <a class="link"
                                               onmouseover="this.style.textDecoration='underline';"
                                               onmouseout="this.style.textDecoration='none';"
                                               onClick="return getAllPostsRelatedToBrand('{$BrandID}','AllPosts',{$isCommunityAdmin},{$BrandPermissionID});">
                                            View All Live
                                        </a>
                                    </span>
                                </div>
                            </xsl:if>
                        </td>
                    </tr>
                </table>
            </xsl:if>
        </div>
    </xsl:template>

    <xsl:template name="DisplayPublicationsLinkedToCommunity">

        <xsl:param name="ImageURL" select="'N/A'"></xsl:param>
        <xsl:param name="HostName" select="'N/A'"></xsl:param>
        <xsl:param name="ItemID" select="'N/A'"></xsl:param>
        <xsl:param name="Title" select="'N/A'"></xsl:param>
        <xsl:param name="Summary" select="'N/A'"></xsl:param>
        <xsl:param name="BrandID" select="'N/A'"></xsl:param>

        <table class="tableMaster" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td align="center" width="100" style="padding-bottom:10px; padding-top:5px;">
                    <xsl:if test="$ImageURL != ''">
                        <div align="center">
                            <xsl:choose>
                                <xsl:when test="$BrandID &gt; 0">
                                    <a href="http://{$HostName}/Publication.aspx?ID={$ItemID}">
                                        <img src="{$ImageURL}"
                                             style="display:none; margin-left: 0px; border: 0px; vertical-align: top;"
                                             onload='setWidthAndHeightImage(this,"80");'></img>
                                    </a>
                                </xsl:when>
                                <xsl:otherwise>
                                    <a href="https://www.coachsimple.net/Publication.aspx?ID={$ItemID}">
                                        <img src="{$ImageURL}"
                                             style="display:none; margin-left: 0px; border: 0px; vertical-align: top;"
                                             onload='setWidthAndHeightImage(this,"80");'></img>
                                    </a>
                                </xsl:otherwise>
                            </xsl:choose>
                        </div>
                    </xsl:if>
                </td>
                <td valign="top" align="left" style="text-align:left;padding-bottom:10px;">
                    <div>
                        <xsl:choose>
                            <xsl:when test="$BrandID &gt; 0">
                                <a class="link" href="http://{$HostName}/Publication.aspx?ID={$ItemID}">
                                    <span id="spanPublicationTitle">
                                        <xsl:value-of select="$Title"/>
                                    </span>
                                </a>
                            </xsl:when>
                            <xsl:otherwise>
                                <a class="link" href="https://www.coachsimple.net/Publication.aspx?ID={$ItemID}">
                                    <span id="spanPublicationTitle">
                                        <xsl:value-of select="$Title"/>
                                    </span>
                                </a>
                            </xsl:otherwise>
                        </xsl:choose>
                    </div>
                    <div>
                        <span id="spanPublicationMemo" class="label">
                            <xsl:value-of select="$Summary"/>
                        </span>
                    </div>
                </td>
            </tr>
        </table>
    </xsl:template>

</xsl:stylesheet>
