<%
/**
 * 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();

cal.set(Calendar.DATE, 1);

int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);

int maxDayOfMonth = cal.getActualMaximum(Calendar.DATE);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat("MMMM, yyyy", locale);
Format timeFormat = FastDateFormatFactoryUtil.getSimpleDateFormat("h:mma", 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>
			<a href="<%= portletURL.toString() %>&<portlet:namespace />month=<%= cal.get(Calendar.MONTH) - 1 %>&<portlet:namespace />day=<%= selDay %>&<portlet:namespace />year=<%= selYear %>&<portlet:namespace />eventType=<%= HtmlUtil.escape(eventType) %>">
			<img border="0" src="<%= themeDisplay.getPathThemeImages() %>/arrows/01_left.png" />
			</a>
		</td>
		<td>
			<%= dateFormat.format(Time.getDate(cal)) %>
		</td>
		<td>
			<a href="<%= portletURL.toString() %>&<portlet:namespace />month=<%= cal.get(Calendar.MONTH) + 1 %>&<portlet:namespace />day=<%= selDay %>&<portlet:namespace />year=<%= selYear %>&<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) || (curYear != selYear) %>">
		<aui:button onClick="<%= viewURL %>" value="current-month" />
	</c:if>
</div>

<br />

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

		<%
		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" %>%">
				<%= LanguageUtil.get(pageContext, days[daysIndex]) %>
			</td>

		<%
		}
		%>

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

		<%
		if (((selCal.getFirstDayOfWeek()) == Calendar.MONDAY)) {
			if (dayOfWeek == 1) {
				dayOfWeek += 6;
			}
			else {
				dayOfWeek --;
			}
		}

		for (int i = 1; i < dayOfWeek; i++) {
		%>

			<td height="100"></td>

		<%
		}

		for (int i = 1; i <= maxDayOfMonth; i++) {
			if (dayOfWeek > 7) {
		%>

				</tr>
				<tr>

			<%
				dayOfWeek = 1;
			}

			dayOfWeek++;

			Calendar tempCal = (Calendar)selCal.clone();

			tempCal.set(Calendar.MONTH, month);
			tempCal.set(Calendar.DATE, i);
			tempCal.set(Calendar.YEAR, year);

			String className = StringPool.BLANK;

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

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

				className = "selected-day";
			}
			%>

			<td class="lfr-top <%= className %>" height="100">
				<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr>
					<td>
						<table border="0" cellpadding="4" 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(month) %>" /><portlet:param name="day" value="<%= String.valueOf(i) %>" /><portlet:param name="year" value="<%= String.valueOf(year) %>" /></portlet:renderURL>"><%= i %></a>

								<c:if test="<%= tempCal.get(Calendar.DAY_OF_WEEK) == cal.getFirstDayOfWeek() %>">
									[<a href="<portlet:renderURL><portlet:param name="struts_action" value="/calendar/view" /><portlet:param name="tabs1" value="week" /><portlet:param name="month" value="<%= String.valueOf(month) %>" /><portlet:param name="day" value="<%= String.valueOf(i) %>" /><portlet:param name="year" value="<%= String.valueOf(year) %>" /></portlet:renderURL>"><liferay-ui:message key="week" /> <%= tempCal.get(Calendar.WEEK_OF_YEAR) %></a>]
								</c:if>
							</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(tempCal.get(Calendar.MONTH)) %>" /><portlet:param name="day" value="<%= String.valueOf(tempCal.get(Calendar.DATE)) %>" /><portlet:param name="year" value="<%= String.valueOf(tempCal.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>
					</td>
				</tr>

				<%
				List events = CalEventLocalServiceUtil.getEvents(scopeGroupId, tempCal, 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";

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

					<tr class="<%= className %>">
						<td>
							<c:if test="<%= !allDay %>">
								<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:if>

							<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>">
							<%= StringUtil.shorten(event.getTitle(), 80) %>
							</a>
						</td>
					</tr>

				<%
				}
				%>

				</table>
			</td>

		<%
		}

		for (int i = 7; i >= dayOfWeek; i--) {
		%>
			<td height="100"></td>

		<%
		}
		%>

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

<%
PortalUtil.setPageSubtitle(dateFormat.format(Time.getDate(cal)), request);
%>