<%
/**
 * 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.
 */
%>

<%
LinkedHashMap<Long, long[]> scopes = AnnouncementsUtil.getAnnouncementScopes(user.getUserId());

SearchContainer searchContainer = new SearchContainer(renderRequest, null, null, "cur1", delta, portletURL, null, "no-entries-were-found");

List<AnnouncementsEntry> results = null;
int total = 0;

int flagValue = AnnouncementsFlagConstants.NOT_HIDDEN;
%>

<div class="unread-entries">
	<%@ include file="/html/portlet/announcements/entry_iterator.jspf" %>
</div>

<%
boolean hasResults = total > 0;
%>

<c:if test="<%= hasResults %>">
	<liferay-ui:search-paginator searchContainer="<%= searchContainer %>" type="article" />
</c:if>

<%
flagValue = AnnouncementsFlagConstants.HIDDEN;

searchContainer = new SearchContainer(renderRequest, null, null, "cur2", delta, portletURL, null, "no-entries-were-found");
%>

<div class="read-entries">
	<%@ include file="/html/portlet/announcements/entry_iterator.jspf" %>
</div>

<%
hasResults = hasResults || (total > 0);
%>

<c:if test="<%= total > 0 %>">
	<liferay-ui:search-paginator searchContainer="<%= searchContainer %>" type="article" />
</c:if>

<aui:script>
	Liferay.provide(
		window,
		'<portlet:namespace />handleEntry',
		function(entryId) {
			var A = AUI();

			var entry = A.one('#<portlet:namespace />' + entryId);

			if (entry) {
				var container = entry.get('parentNode');

				if (container) {
					if (container.hasClass('unread-entries')) {
						<portlet:namespace />markEntry(entry, entryId);
					}
					else {
						<portlet:namespace />toggleContent(entry);
					}
				}
			}
		},
		['aui-base']
	);

	Liferay.provide(
		window,
		'<portlet:namespace />markEntry',
		function(entry, entryId) {
			var A = AUI();

			Liferay.Service.Announcements.AnnouncementsFlag.addFlag({entryId : entryId, flag: <%= AnnouncementsFlagConstants.HIDDEN %>});

			Liferay.Portlet.refresh('#p_p_id<portlet:namespace />');
		},
		['aui-base']
	);

	Liferay.provide(
		window,
		'<portlet:namespace />toggleContent',
		function(entry) {
			var A = AUI();

			var content = entry.all('.entry-content');
			var control = entry.all('.control-entry a');

			if (entry.hasClass('visible')) {
				entry.removeClass('visible');

				if (content) {
					content.hide();
				}

				if (control) {
					control.html('<liferay-ui:message key="show" />');
				}
			}
			else {
				entry.addClass('visible');

				if (content) {
					content.show();
				}

				if (control) {
					control.html('<liferay-ui:message key="hide" />');
				}
			}
		},
		['aui-base']
	);
</aui:script>

<c:if test="<%= !hasResults && portletName.equals(PortletKeys.ALERTS) && !PortletPermissionUtil.contains(permissionChecker, plid, PortletKeys.ANNOUNCEMENTS, ActionKeys.ADD_ENTRY) %>">
	<aui:script use="aui-base">
		var portlet = A.one('#p_p_id<portlet:namespace />');

		if (portlet) {
			portlet.hide();
		}
	</aui:script>
</c:if>