<%@ include file="/portal/include/include_java.jsp" %>
<fmt:setBundle basename="text.jsp.portlet.dt.messagesToolbar"/>

<sp:portalState var="ps" />
<c:set var="chid" value="${ps.currentPortletId}" />

<c:set var="threadName"><s:replace replace="&#039;" with="\&#039;"><c:out value="${forumGridResultsForm.threadSummary.rootSubject}" /></s:replace></c:set>

<c:set var="threadSubscribeAction"><%=com.appiancorp.suiteapi.forums.DiscussionMetadataCoreService.ACTION_THREAD_SUBSCRIBE%></c:set>
<c:set var="threadUnsubscribeAction"><%=com.appiancorp.suiteapi.forums.DiscussionMetadataCoreService.ACTION_THREAD_UNSUBSCRIBE%></c:set>
<c:set var="threadAttachAction"><%=com.appiancorp.suiteapi.forums.DiscussionMetadataCoreService.ACTION_THREAD_ADD_RESOURCE%></c:set>


<a href="" alt="<fmt:message key='CloseWindow'/>" onclick="window.close();"><p:img page="/components/toolbar/img/delete_toolbar.gif" altKey="CloseWindow"/><fmt:message key="CloseWindow"/></a>

<span id="subscribeLink"><a href="#" alt="<fmt:message key='ReceiveNotifications'/>" onclick="threadSubscriptionManager.handleSubscription();return false;"><p:img page="/components/toolbar/img/search.gif" altKey="Subscribe" /><fmt:message key="Subscribe"/></a></span>
<span id="unsubscribeLink"><a href="#" alt="<fmt:message key='StopReceivingNotific'/>" onclick="threadSubscriptionManager.handleSubscription();return false;"><p:img page="/components/toolbar/img/cancel.gif" altKey="Unsubscribe" /><fmt:message key="Unsubscribe"/></a></span>

<c:choose>
  <c:when test="${forumGridResultsForm.threadSummary.allowedActions[threadAttachAction] == 1}">
    <bel:size id="numResources" collection="${forumGridResultsForm.threadSummary.resources}"/>
    <asi:link href="#" onclick="popup('/forums/resources.do?threadId=${forumGridResultsForm.threadSummary.threadId.id}&origin=forumsPortlet','attachment', 'name=save,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=750,height=500,screenX=50,screenY=50');" title="DownloadOrUploadDocu"><p:img page="/components/toolbar/img/upload.gif" altKey="Attachments"/><fmt:message key="Attachments_2"/> (<c:out value="${numResources}" />)</asi:link>
  </c:when>
</c:choose>

<script type="text/javascript">
  window['threadSubscriptionManager'] = {
    threadId : '<asi:out encoding="js" value="${forumGridResultsForm.threadSummary.threadId.id}"/>',
    isUserSubscribed : eval('<asi:out encoding="js" value="${forumGridResultsForm.threadSummary.userSubscribed}"/>'),
    canSubscribe : '<asi:out encoding="js" value="${forumGridResultsForm.threadSummary.allowedActions[threadSubscribeAction] == 1}"/>',
    canUnsubscribe : '<asi:out encoding="js" value="${forumGridResultsForm.threadSummary.allowedActions[threadUnsubscribeAction] == 1}"/>',
    handleSubscription : function(){
      ensureJSON(function(){
        var isUserSubscribed = threadSubscriptionManager.isUserSubscribed;
        server.clientComponent.handleDiscussionForumsSubscription(isUserSubscribed, threadSubscriptionManager.threadId, 1);
        threadSubscriptionManager.isUserSubscribed = !isUserSubscribed;
        if(threadSubscriptionManager.isUserSubscribed==true){
          STATUS.message('<fmt:message key="YouHaveSubscribed"/>');
        } else {
          STATUS.message('<fmt:message key="YouHaveUnsubscribed"/>');
        }
        threadSubscriptionManager.updateSubscriptionToolbar();
      });
    },
    updateSubscriptionToolbar : function(){
      var isUserSubscribed = threadSubscriptionManager.isUserSubscribed;
      $('#subscribeLink')[isUserSubscribed ? 'hide' : 'show']();
      $('#unsubscribeLink')[isUserSubscribed ? 'show' : 'hide']();
    }
  };
  $(document).ready(function(){
    threadSubscriptionManager.updateSubscriptionToolbar();
  });
</script>