<%
/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
%>

<%
PortletURL portletURL = renderResponse.createRenderURL();

portletURL.setParameter("struts_action", "/message_boards/view_message");
portletURL.setParameter("messageId", String.valueOf(message.getMessageId()));

SearchContainer searchContainer = null;

if (threadView.equals(MBThreadConstants.THREAD_VIEW_FLAT)) {
	int cur = 0;
	int delta = SearchContainer.DEFAULT_DELTA;

	if (request.getParameter(SearchContainer.DEFAULT_CUR_PARAM) == null) {
		int positionInThread = MBMessageLocalServiceUtil.getPositionInThread(message.getMessageId());

		if (positionInThread > delta) {
			cur = (int)Math.ceil(((double)positionInThread) / delta);
		}
		else {
			cur = SearchContainer.DEFAULT_CUR;
		}
	}

	searchContainer = new SearchContainer(renderRequest, null, null, SearchContainer.DEFAULT_CUR_PARAM, cur, delta, portletURL, null, null);

	int total = MBMessageServiceUtil.getThreadMessagesCount(scopeGroupId, category.getCategoryId(), message.getThreadId(), WorkflowConstants.STATUS_ANY);

	searchContainer.setTotal(total);

	messages = MBMessageServiceUtil.getThreadMessages(scopeGroupId, category.getCategoryId(), message.getThreadId(), WorkflowConstants.STATUS_ANY, searchContainer.getStart(), searchContainer.getEnd());

	searchContainer.setResults(messages);
}
%>

<c:if test="<%= threadView.equals(MBThreadConstants.THREAD_VIEW_FLAT) && (searchContainer.getTotal() > searchContainer.getDelta()) %>">
	<div class="taglib-search-iterator-page-iterator-top">
		<liferay-ui:search-paginator searchContainer="<%= searchContainer %>" />
	</div>
</c:if>

<%
boolean editable = true;

int depth = 0;

for (int i = 0; i < messages.size(); i++) {
	message = messages.get(i);

	if ((!message.isApproved() && (message.getUserId() != user.getUserId()) && !permissionChecker.isCommunityAdmin(scopeGroupId)) || !permissionChecker.hasPermission(scopeGroupId, MBMessage.class.getName(), message.getMessageId(), ActionKeys.VIEW)) {
		continue;
	}

	String className = "portlet-section-alternate results-row alt";
	String classHoverName = "portlet-section-alternate-hover results-row alt hover";

	if (MathUtil.isOdd(i)) {
		className = "portlet-section-body results-row";
		classHoverName = "portlet-section-body-hover results-row hover";
	}
%>

	<%@ include file="/html/portlet/message_boards/view_thread_message.jspf" %>

<%
}
%>

<c:if test="<%= threadView.equals(MBThreadConstants.THREAD_VIEW_FLAT) && (searchContainer.getTotal() > searchContainer.getDelta()) %>">
	<div class="taglib-search-iterator-page-iterator-bottom">
		<liferay-ui:search-paginator searchContainer="<%= searchContainer %>" />
	</div>
</c:if>