#requireResource("confluence.web.resources:ajs")
#requireResource("confluence.web.resources:animation")
#requireResource("confluence.web.resources:comments")

#set ($commentTextareaTabIndex = 200)

<fieldset class="parameters hidden">
    <input type="hidden" id="deleteCommentConfirmMessage" value="$action.getText('remove.comment.confirmation.message')">
    <input type="hidden" id="collapseTooltip" value="$action.getText('comment.collapse.toggle.tooltip')">
</fieldset>

#macro (displayCommentActions $commentId $commentActionSectionId $webSectionKey $webInterfaceContext)
    <ul id="$commentActionSectionId">
        #foreach ($item in $webInterfaceManager.getDisplayableItems($webSectionKey, $webInterfaceContext))
            ## provide first class name for IE6 cause first-child selector doesn't work
            #if ($velocityCount==1) #set ($className="first") #else #set ($className="") #end
            #if ($item.styleClass) #set($className = "$className $item.styleClass") #end
            <li class="$className">
            <a #if ($item.tooltip)title="$item.tooltip.getDisplayableLabel($req, $webInterfaceContext)"#end href="$item.link.getDisplayableUrl($req, $webInterfaceContext)"
               #if ($item.link.id) id="${item.link.id}-$commentId" #end
               ><span>$item.label.getDisplayableLabel($req, $webInterfaceContext)</span></a></li>
        #end
    </ul>
#end

#macro (commentThread $comment)
    #set ($page = $comment.page)
    <li id="comment-thread-$comment.id" class="comment-thread">
        #if ($action.editComment && $action.comment && $action.comment.id == $comment.id && $permissionHelper.canEdit($remoteUser, $comment ))
            <div class="comment edit" id="comment-$comment.id">
                <p class="comment-user-logo">
                    #logoBlock("~$comment.creatorName")
                </p>
                <p class="date" title="$dateFormatter.formatDateTime($comment.lastModificationDate)">
                    $action.formatFriendlyDate($comment.lastModificationDate)
                </p>
                <div class="comment-header">
                    <h4 class="author">$action.getText("comment.author.byline", ["#usernameLink($comment.creatorName)"])</h4>
                </div>
                <div class="comment-body">
                    <form name="editcommentform" method="post" action="$req.contextPath/pages/doeditcomment.action?pageId=$page.id&amp;commentId=$comment.id">
                        #bodytag ("Component" "name='content'" "theme='notable'" "template='wiki-textarea.vm'")
                            #param ("formname" "editcommentform")
                            #param ("spaceKey" "$generalUtil.urlEncode($spaceKey)")
                            #param ("rows" 15)
                            #param ("cols" 70)
                            #param ("width" "100%")
                            #param ("tabindex" $commentTextareaTabIndex)
                            #param ("tdcolor" "f0f0f0")
                            #param ("toolbarExpanded" "false")
                            #param ("initialFocus" "false")
                            #param ("edit" "true")
                            #param ("heartbeat" "false")
                            #param ("wikiContent" $comment.content)
                            #param ("wysiwygContent" "$action.getCommentAsXHtmlForWysiwyg()")
                            #param ("contentId" "$comment.id")
                            #param ("contentType" "comment")
                        #end
                        #commentSubmission()
                    </form>
                </div>
            </div>

        #else
            <div class="comment #if ($comment.id == $focusedCommentId) focused #end " id="comment-$comment.id">
                <p class="comment-user-logo">
                    #logoBlock("~$comment.creatorName")
                </p>
                <p class="date" title="$dateFormatter.formatDateTime($comment.lastModificationDate)">
                    $action.formatFriendlyDate($comment.lastModificationDate)
                </p>
                <div class="comment-header comment-toggle">
                    <h4 class="author">$action.getText("comment.author.byline", ["#usernameLink ($comment.creatorName)"])</h4>
                    #set($excerptHtml = $generalUtil.shortenString($action.getXHtmlComments().get($comment).replaceAll("<[^>]*>",""), 80))
                    <span class="excerpt">$excerptHtml</span>
                </div>
                <div class="comment-body">
                    <div class="comment-content wiki-content">
                        $action.getXHtmlComments().get($comment)
                    </div>
                    <div class="comment-actions">
                        #set ($webInterfaceContext = $action.getWebInterfaceContext($comment))
                        #displayCommentActions($comment.id "comment-actions-secondary" "system.comment.action/secondary" $webInterfaceContext)
                        #displayCommentActions($comment.id "comment-actions-primary" "system.comment.action/primary" $webInterfaceContext)
                    </div>
                </div>
            </div>

            #trackbackRdf ($trackbackUtils.getContentIdentifier($comment) $page.title $trackbackUtils.getPingUrl($comment))

            #if ( $threadComments && $replyToComment == $comment.id && $permissionCheckDispatcher.isPermitted("/pages/doaddcomment.action?pageId=$page.id") && $action.isPrintableVersion()==false )
                <ol class="comment-threads">
                    <li class="comment-thread">
                        <div class="comment reply">
                            <p class="comment-user-logo">
                                #logoBlock("~$remoteUser.name")

                            </p>

                            <div class="comment-header">
                                <h4 class="author">$action.getText("comment.reply.byline", ["#usernameLink($remoteUser.name)"])</h4>
                            </div>

                            <div class="comment-body">
                                #parse ("/includes/alert-anonymous.vm")

                                <form name="threadedcommentform" method="post" action="$req.contextPath/pages/doaddcomment.action?pageId=${page.id}&amp;parentId=${comment.id}">
                                    #bodytag ("Component" "name='content'" "theme='notable'" "template='wiki-textarea.vm'")
                                        #param ("formname" "threadedcommentform")
                                        #param ("spaceKey" "$generalUtil.urlEncode($spaceKey)")
                                        #param ("rows" 15)
                                        #param ("cols" 70)
                                        #param ("width" "100%")
                                        #param ("tabindex" $commentTextareaTabIndex)
                                        #param ("tdcolor" "f0f0f0")
                                        #param ("toolbarExpanded" "false")
                                        #param ("initialFocus" "false")
                                        #param ("edit" "true")
                                        #param ("heartbeat" "false")
                                        #param ("contentId" "0")
                                        #param ("contentType" "comment")
                                    #end
                                    #commentSubmission()
                                </form>
                            </div>
                        </div>
                    </li>
                </ol>
            #end
        #end

        #if ($threadComments)
            #if ($comment.children.size() > 0)
                <ol class="comment-threads">
                #foreach ($childComment in $comment.children)
                    #commentThread($childComment)
                #end
                </ol>
            #end
        #end
    </li>
#end

#* make sure you pass in an AbstractPage as a decorator parameter when you call this inline decorator *#
#set ($page = $params.get("page"))

<div id="comments-section" class="pageSection">
    #* If we are showing the "add comment" area, or there are existing comments, show the "Comments" and hide link. *#
    #if (($action.showCommentArea || $page.comments.size() > 0) && !$action.isPrintableVersion())
        <div class="section-header #if (!$action.commentsShowing) collapsed #end">
            <h2 id="comments-section-title" class="section-title">
                <a name="comments">$action.getText("comments.count", [$page.comments.size()])</a>
            </h2>
            &nbsp;
            <ul id="comment-top-links" class="global-comment-actions">
                <li id="comment-show-hide">
                    <a href="$req.contextPath$generalUtil.customGetPageUrl($page)showComments=false#comments" id="comments-hide" #if (!$action.commentsShowing) class="hidden" #end>$action.getText('comment.hide')</a>
                    <a href="$req.contextPath$generalUtil.customGetPageUrl($page)showComments=true#comments" id="comments-show" #if ($action.commentsShowing) class="hidden" #end>$action.getText('comment.show')</a>
                </li>
                <li id="comments-expand-collapse" #if (!$action.commentsShowing) class="hidden" #end>
                    <a href="#" id="comments-collapse">$action.getText("comment.collapse.all")</a>
                    <span id="collapse-wait" class="hidden">$action.getText("comment.collapse.progress")</span>
                    <span id="expand-wait" class="hidden">$action.getText("comment.expand.progress")</span>
                    <a href="#" class="hidden" id="comments-expand">$action.getText("comment.expand.all")</a>
                </li>
                #if ($permissionHelper.canComment($remoteUser, $page))
                <li>
                    <a href="$req.contextPath$generalUtil.customGetPageUrl($page)showComments=true&showCommentArea=true#addcomment" id="add-comment-top">$action.getText('action.add.comment')</a>
                </li>
                #end
            </ul>
        </div>
    #end

    #* Show expanded comments. If there are none, then show nothing at all (no heading, zilch!) *#
    #if ($page.comments.size() > 0)
        #if ($threadComments)
            #set ($topLevelComments = $page.topLevelComments)
        #else
            #set ($topLevelComments = $page.comments)
        #end
        <ol class="comment-threads top-level #if (!$action.commentsShowing) hidden #end" id="page-comments">
        #foreach ($comment in $topLevelComments)
            #commentThread($comment)
        #end
        </ol>
    #end

    #if (!$action.isPrintableVersion())
        #if ($action.showCommentArea)
            #if ($permissionHelper.canComment($remoteUser, $page))
            <div class="comment add" id="addcomment">
                <p class="comment-user-logo">
                    #logoBlock("~$remoteUser.name")
                </p>

                <div class="comment-header">
                    <h4 class="author">$action.getText("comment.author.byline", ["#usernameLink($remoteUser.name)"])</h4>
                </div>

                <div class="comment-body">
                    #parse ("/includes/alert-anonymous.vm")

                    <form name="inlinecommentform" method="post" action="$req.contextPath/pages/doaddcomment.action?pageId=${page.id}">
                        #bodytag ("Component" "name='content'" "theme='notable'" "template='wiki-textarea.vm'")
                            #param ("formname" "inlinecommentform")
                            #param ("spaceKey" "$generalUtil.urlEncode($spaceKey)")
                            #param ("rows" 15)
                            #param ("cols" 70)
                            #param ("width" "100%")
                            #param ("tabindex" $commentTextareaTabIndex)
                            #param ("tdcolor" "f0f0f0")
                            #param ("edit" "true")
                            #param ("toolbarExpanded" "false")
                            #param ("initialFocus" "false")
                            #param ("contentId" "0")
                            #param ("contentType" "comment")
                        #end
                        #commentSubmission()
                    </form>
                </div>
            </div>
            #end
        #else
            #if ($permissionHelper.canComment($remoteUser, $page))
            <p class="noprint">
                <a href="$req.contextPath$generalUtil.customGetPageUrl($page)showComments=true&amp;showCommentArea=true#addcomment" id="add-comment-bottom" accesskey="$action.getTextStrict("add.comment.accesskey")">$action.getText('action.add.comment')</a>
            </p>
            #end
        #end
    #end
</div>
