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

dateFormatDate = FastDateFormatFactoryUtil.getSimpleDateFormat("MMMMM d, yyyy", 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, -1);
			%>

			<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, 1);
			%>

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

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

			<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="today" />
	</c:if>
</div>

<br />

<%
List<CalEvent> results = CalEventLocalServiceUtil.getEvents(scopeGroupId, selCal, eventType);

List<CalEvent> allDayResults = new ArrayList<CalEvent>();
List<CalEvent> partDayResults = new ArrayList<CalEvent>();

Iterator<CalEvent> itr = results.iterator();

while (itr.hasNext()) {
	CalEvent event = itr.next();

	if (event.isAllDay()) {
		allDayResults.add(event);
	}
	else {
		partDayResults.add(event);
	}
}

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

List<List<CalEvent>> sortedResults = new ArrayList<List<CalEvent>>();

if (!partDayResults.isEmpty()) {
	sortedResults.add(new ArrayList<CalEvent>());

	sortedResults.get(0).add(partDayResults.get(0));

	for (int i = 1; i < partDayResults.size(); i++) {
		CalEvent event = partDayResults.get(i);

		Date startDate = event.getStartDate();

		if (event.isTimeZoneSensitive()) {
			startDate = Time.getDate(event.getStartDate(), timeZone);
		}

		int startDateScore = (startDate.getHours() * 60) + startDate.getMinutes();

		for (int j = 0; j < sortedResults.size(); j++) {
			List<CalEvent> curEvents = sortedResults.get(j);

			CalEvent curEvent = (CalEvent)curEvents.get(curEvents.size() - 1);

			Date curStartDate = curEvent.getStartDate();

			if (curEvent.isTimeZoneSensitive()) {
				curStartDate = Time.getDate(curEvent.getStartDate(), timeZone);
			}

			int curDisplayDuration = Math.max(50, (curEvent.getDurationHour() * 60) + curEvent.getDurationMinute());

			int curStartDateScore = (curStartDate.getHours() * 60) + curStartDate.getMinutes() + curDisplayDuration;

			if (curStartDateScore <= startDateScore) {
				sortedResults.get(j).add(event);

				break;
			}
			else if (sortedResults.size() <= (j + 1)) {
				sortedResults.add(new ArrayList<CalEvent>());

				sortedResults.get(j + 1).add(event);

				break;
			}
		}
	}
}
%>

<div class="day-grid" style="position: relative;">
	<div class="hour all-day">
		<span><liferay-ui:message key="all-day" /></span>

		<%
		for (int i = 0; i < allDayResults.size(); i++) {
			CalEvent event = allDayResults.get(i);

			event = event.toEscapedModel();
		%>

			<div class="event-box-wrapper" style="margin: 0 auto; width: 90%;">
				<div class="event-box">
					<div class="event-edit" style="float: right;">
						<c:if test="<%= CalEventPermission.contains(permissionChecker, event, ActionKeys.UPDATE) %>">
							<portlet:renderURL var="editAllDayEventURL">
								<portlet:param name="struts_action" value="/calendar/edit_event" />
								<portlet:param name="redirect" value="<%= currentURL %>" />
								<portlet:param name="eventId" value="<%= String.valueOf(event.getEventId()) %>" />
							</portlet:renderURL>

							<liferay-ui:icon
								image="edit"
								url="<%= editAllDayEventURL %>"
							/>
						</c:if>
					</div>

					<div class="event-title">
						<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(), 65) %></a>
					</div>
				</div>
			</div>

		<%
		}
		%>

	</div>
</div>

<div class="day-grid" style="position: relative;">

	<%
	for (int i = 0; i < 24; i++) {
		String displayTime = ((i % 12) == 0 ? "12" : (i % 12)) + ( i < 12 ? " AM" : " PM");
		String businessHours = ((i >= 9) && (i < 17)) ? "business-hour" : "night-hour";
	%>

		<div class="hour <%= businessHours %>">
			<span><%= displayTime %></span>
		</div>

		<div class="half-hour <%= businessHours %>"><!-- --></div>

	<%
	}
	%>

	<%
	int eventWidth = 90 / (sortedResults.isEmpty() ? 1 : sortedResults.size());

	for (int i = 0; i < sortedResults.size(); i++) {
		List<CalEvent> displayResults = sortedResults.get(i);

		for (CalEvent event : displayResults) {
			event = event.toEscapedModel();

			Date startDate = event.getStartDate();

			if (event.isTimeZoneSensitive()) {
				startDate = Time.getDate(event.getStartDate(), timeZone);
			}

			int eventStartPos = (int)(((((startDate.getHours() * 60) + (float)startDate.getMinutes() ) / 60) * 50) - 2);

			int eventDisplayDuration = Math.max(50, (event.getDurationHour() * 60) + event.getDurationMinute());

			int eventBoxLength = (int)(((eventDisplayDuration / 60) * 50) - 12);

			if ((eventBoxLength + eventStartPos) > 1186) {
				eventBoxLength -= (eventBoxLength + eventStartPos) - 1186; // = (24 * 50) - 4
			}
		%>

			<div class="event-box-wrapper" style="height: <%= eventBoxLength %>px; left: <%= 5 + (i * eventWidth) %>%; position: absolute; top: <%= eventStartPos %>px; width: <%= eventWidth %>%;">
				<div class="event-box" style="height: 100%;">
					<div class="event-edit" style="float: right;">
						<c:if test="<%= CalEventPermission.contains(permissionChecker, event, ActionKeys.UPDATE) %>">
							<portlet:renderURL var="editDayEventURL">
								<portlet:param name="struts_action" value="/calendar/edit_event" />
								<portlet:param name="redirect" value="<%= currentURL %>" />
								<portlet:param name="eventId" value="<%= String.valueOf(event.getEventId()) %>" />
							</portlet:renderURL>

							<liferay-ui:icon
								image="edit"
								url="<%= editDayEventURL %>"
							/>
						</c:if>
					</div>

					<div class="event-time">
						<c:choose>
							<c:when test="<%= event.isTimeZoneSensitive() %>">
								<%= dateFormatTime.format(Time.getDate(event.getStartDate(), timeZone)) %>
								-
								<%= dateFormatTime.format(Time.getDate(CalUtil.getEndTime(event), timeZone)) %>
							</c:when>
							<c:otherwise>
								<%= dateFormatTime.format(event.getStartDate()) %>
								-
								<%= dateFormatTime.format(CalUtil.getEndTime(event)) %>
							</c:otherwise>
						</c:choose>
					</div>

					<div class="event-title">
						<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(), 65) %>
						</a>
					</div>

					<c:if test="<%= eventBoxLength >= 100 %>">
						<div class="event-description">
							<%= StringUtil.shorten(event.getDescription(), 100) %>
						</div>
					</c:if>
				</div>
			</div>

	<%
		}
	}
	%>

</div>

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

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