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

<%
Calendar cal = (Calendar)selCal.clone();

int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

if (cal.getFirstDayOfWeek() == Calendar.MONDAY) {
	dayOfWeek--;
}

cal.add(Calendar.DATE, 1 - dayOfWeek);

Format dateFormatDayOfWeek = FastDateFormatFactoryUtil.getSimpleDateFormat("EEE", locale);
%>

<div>
	<div class="event-selector">

		<%
		String taglibOnChange = "location.href = '" + portletURL + "&" + renderResponse.getNamespace() + "month=" + selMonth + "&" + renderResponse.getNamespace() + "day=" + selDay + "&" + renderResponse.getNamespace() + "year=" + selYear + "&" + renderResponse.getNamespace() + "eventType=' + this.value;";
		%>

		<aui:select changesContext="<%= true %>" label="" name="eventType" onChange="<%= taglibOnChange %>" title="events">
			<aui:option label="all-events" value="" />

			<%
			for (int i = 0; i < CalEventConstants.TYPES.length; i++) {
			%>

				<aui:option label="<%= CalEventConstants.TYPES[i] %>" selected="<%= eventType.equals(CalEventConstants.TYPES[i]) %>" />

			<%
			}
			%>

		</aui:select>
	</div>

	<table class="date-selector">
	<tr>
		<td>

			<%
			cal.add(Calendar.DATE, -7);
			%>

			<a href="<%= portletURL.toString() %>&<portlet:namespace />month=<%= cal.get(Calendar.MONTH) %>&<portlet:namespace />day=<%= cal.get(Calendar.DATE) %>&<portlet:namespace />year=<%= cal.get(Calendar.YEAR) %>&<portlet:namespace />eventType=<%= HtmlUtil.escape(eventType) %>">
			<img border="0" src="<%= themeDisplay.getPathThemeImages() %>/arrows/01_left.png" />
			</a>
		</td>
		<td>

			<%
			cal.add(Calendar.DATE, 7);
			%>

			<liferay-ui:message key="week-of" />

			<%= dateFormatDate.format(Time.getDate(cal)) %>
		</td>
		<td>

			<%
			cal.add(Calendar.DATE, 7);
			%>

			<a href="<%= portletURL.toString() %>&<portlet:namespace />month=<%= cal.get(Calendar.MONTH) %>&<portlet:namespace />day=<%= cal.get(Calendar.DATE) %>&<portlet:namespace />year=<%= cal.get(Calendar.YEAR) %>&<portlet:namespace />eventType=<%= HtmlUtil.escape(eventType) %>">
			<img border="0" src="<%= themeDisplay.getPathThemeImages() %>/arrows/01_right.png" />
			</a>
		</td>
	</tr>
	</table>
</div>

<br />

<div>
	<c:if test="<%= CalendarPermission.contains(permissionChecker, scopeGroupId, ActionKeys.ADD_EVENT) %>">
		<portlet:renderURL var="editEventURL">
			<portlet:param name="struts_action" value="/calendar/edit_event" />
			<portlet:param name="redirect" value="<%= currentURL %>" />
			<portlet:param name="month" value="<%= String.valueOf(selMonth) %>" />
			<portlet:param name="day" value="<%= String.valueOf(selDay) %>" />
			<portlet:param name="year" value="<%= String.valueOf(selYear) %>" />
		</portlet:renderURL>

		<aui:button onClick="<%= editEventURL %>" value="add-event" />
	</c:if>

	<portlet:renderURL var="viewURL">
		<portlet:param name="struts_action" value="/calendar/view" />
		<portlet:param name="tabs1" value="<%= tabs1 %>" />
		<portlet:param name="month" value="<%= String.valueOf(curMonth) %>" />
		<portlet:param name="day" value="<%= String.valueOf(curDay) %>" />
		<portlet:param name="year" value="<%= String.valueOf(curYear) %>" />
	</portlet:renderURL>

	<c:if test="<%= (curMonth != selMonth) || (curDay != selDay) || (curYear != selYear) %>">
		<aui:button onClick="<%= viewURL %>" value="current-week" />
	</c:if>
</div>

<br />

<table class="calendar">
<thead>
	<tr class="portlet-section-header results-header">

		<%
		cal.add(Calendar.DATE, -7);

		for (int i = 0; i < 7; i++) {
			int daysIndex = (selCal.getFirstDayOfWeek() + i - 1) % 7;
		%>

			<td align="center" height="25" width="<%= (i == 0 || i == 6) ? "15" : "14" %>%">
				<a href="<portlet:renderURL><portlet:param name="struts_action" value="/calendar/view" /><portlet:param name="tabs1" value="day" /><portlet:param name="month" value="<%= String.valueOf(cal.get(Calendar.MONTH)) %>" /><portlet:param name="day" value="<%= String.valueOf(cal.get(Calendar.DATE)) %>" /><portlet:param name="year" value="<%= String.valueOf(cal.get(Calendar.YEAR)) %>" /></portlet:renderURL>">
				<%= LanguageUtil.get(pageContext, days[daysIndex]) %>
				</a>
			</td>

		<%
			cal.add(Calendar.DATE, 1);
		}

		cal.add(Calendar.DATE, -7);
		%>

	</tr>
</thead>
<tbody>
	<tr>

		<%
		for (int i = 0; i < 7; i++) {
			String className = StringPool.BLANK;

			if ((cal.get(Calendar.MONTH) == curMonth) &&
				(cal.get(Calendar.DATE) == curDay) &&
				(cal.get(Calendar.YEAR) == curYear)) {

				className = "current-day";
			}
			else if ((cal.get(Calendar.MONTH) == selMonth) &&
					 (cal.get(Calendar.DATE) == selDay) &&
					 (cal.get(Calendar.YEAR) == selYear)) {

				className = "selected-day";
			}
		%>

			<td class="lfr-top <%= className %>" height="325" width="<%= (i == 0 || i == 6) ? "15" : "14" %>%">
				<table border="0" cellpadding="8" cellspacing="0" width="100%">
				<tr>
					<td>
						<a href="<portlet:renderURL><portlet:param name="struts_action" value="/calendar/view" /><portlet:param name="tabs1" value="day" /><portlet:param name="month" value="<%= String.valueOf(cal.get(Calendar.MONTH)) %>" /><portlet:param name="day" value="<%= String.valueOf(cal.get(Calendar.DATE)) %>" /><portlet:param name="year" value="<%= String.valueOf(cal.get(Calendar.YEAR)) %>" /></portlet:renderURL>">
						<%= dateFormatDate.format(Time.getDate(cal)) %>
						</a>
					</td>
					<td align="right">
						<c:if test="<%= CalendarPermission.contains(permissionChecker, scopeGroupId, ActionKeys.ADD_EVENT) %>">
							<a href="<portlet:renderURL><portlet:param name="struts_action" value="/calendar/edit_event" /><portlet:param name="redirect" value="<%= currentURL %>" /><portlet:param name="month" value="<%= String.valueOf(cal.get(Calendar.MONTH)) %>" /><portlet:param name="day" value="<%= String.valueOf(cal.get(Calendar.DATE)) %>" /><portlet:param name="year" value="<%= String.valueOf(cal.get(Calendar.YEAR)) %>" /></portlet:renderURL>">
							<img border="0" src="<%= themeDisplay.getPathThemeImages() %>/arrows/01_plus.png" title="<liferay-ui:message key="add" />" />
							</a>
						</c:if>
					</td>
				</tr>
				</table>

				<table border="0" cellpadding="4" cellspacing="0" width="100%">

				<%
				List events = CalEventLocalServiceUtil.getEvents(scopeGroupId, cal, eventType);

				events = ListUtil.sort(events, new EventTimeComparator(timeZone, locale));

				for (int j = 0; j < events.size(); j++) {
					CalEvent event = (CalEvent)events.get(j);

					event = event.toEscapedModel();

					boolean allDay = CalUtil.isAllDay(event, timeZone, locale);

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

					if (MathUtil.isEven(j)) {
						className = "portlet-section-alternate results-row alt";
						classHoverName = "portlet-section-alternate-hover results-row alt hover";
					}
				%>

					<tr class="<%= className %>">
						<td class="lfr-top">
							<c:choose>
								<c:when test="<%= allDay %>">
									<liferay-ui:message key="all-day" />
								</c:when>
								<c:otherwise>
									<c:choose>
										<c:when test="<%= event.isTimeZoneSensitive() %>">
											<%= dateFormatTime.format(Time.getDate(event.getStartDate(), timeZone)) %>
										</c:when>
										<c:otherwise>
											<%= dateFormatTime.format(event.getStartDate()) %>
										</c:otherwise>
									</c:choose>
								</c:otherwise>
							</c:choose>

							<a href="<portlet:renderURL><portlet:param name="struts_action" value="/calendar/view_event" /><portlet:param name="redirect" value="<%= currentURL %>" /><portlet:param name="eventId" value="<%= String.valueOf(event.getEventId()) %>" /></portlet:renderURL>">
							<%= event.getTitle() %>
							</a>
						</td>
					</tr>

				<%
				}
				%>

				</table>
			</td>

		<%
		cal.add(Calendar.DATE, 1);
		}
		%>

	</tr>
</tbody>
</table>

<%
cal.add(Calendar.DATE, -7);

String pageSubtitle = LanguageUtil.get(pageContext, "week-of").concat(StringPool.SPACE).concat(dateFormatDate.format(Time.getDate(cal)));

PortalUtil.setPageSubtitle(pageSubtitle, request);
%>