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

<%
NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
NumberFormat percentFormat = NumberFormat.getPercentInstance(locale);
%>

<table border="0" cellpadding="4" cellspacing="0" width="100%">
<tr class="portlet-section-header results-header" style="font-size: x-small; font-weight: bold;">
	<td>
		%
	</td>
	<td colspan="6">
		<liferay-ui:message key="votes" />
	</td>
</tr>

<%
int totalVotes = question.getVotesCount();

for (int i = 0; i < choices.size(); i++) {
	PollsChoice choice = (PollsChoice)choices.get(i);

	choice = choice.toEscapedModel();

	int choiceVotes = choice.getVotesCount();

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

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

	double votesPercent = 0.0;

	if (totalVotes > 0) {
		votesPercent = (double)choiceVotes / totalVotes;
	}

	int votesPixelWidth = 35;

	if (windowState.equals(WindowState.MAXIMIZED)) {
		votesPixelWidth = 100;
	}

	int votesPercentWidth = (int)Math.floor(votesPercent * votesPixelWidth);
%>

<tr class="<%= className %>" style="font-size: x-small;" onMouseEnter="this.className = '<%= classHoverName %>';" onMouseLeave="this.className = '<%= className %>';">
	<td>
		<%= percentFormat.format(votesPercent) %>
	</td>
	<td colspan="<%= votesPercentWidth > 0 ? "1" : "3" %>">
		<%= numberFormat.format(choiceVotes) %>
	</td>

	<c:if test="<%= votesPercentWidth > 0 %>">
		<td></td>
		<td>
			<table border="0" cellpadding="0" cellspacing="0">
			<tr>
				<td class="alpha"><img border="0" height="5" hspace="0" src="<%= themeDisplay.getPathThemeImages() %>/spacer.png" vspace="0" width="<%= votesPercentWidth %>" /></td>
				<td class="beta"><img border="0" height="5" hspace="0" src="<%= themeDisplay.getPathThemeImages() %>/spacer.png" vspace="0" width="<%= votesPixelWidth - votesPercentWidth %>" /></td>
			</tr>
			</table>
		</td>
	</c:if>

	<td></td>
	<td>
		<strong><%= choice.getName() %>.</strong>
	</td>
	<td width="99%">
		<%= choice.getDescription(locale) %>
	</td>
</tr>

<%
}
%>

</table>

<br />

<div>
	<strong><liferay-ui:message key="total-votes" />:</strong> <%= numberFormat.format(totalVotes) %>
</div>

<c:if test="<%= portletName.equals(PortletKeys.POLLS) %>">
	<br />

	<div>
		<strong><liferay-ui:message key="charts" />:</strong>

		<%
		for (int i = 0; i < _CHARTS.length; i++) {
			String chart = _CHARTS[i];

			String hrefTaglib = "javascript:var viewChartWindow = window.open('" + themeDisplay.getPathMain() + "/polls/view_chart?questionId=" + question.getQuestionId() +"&chartType=" + TextFormatter.format(chart, TextFormatter.N) + "', 'viewChart', 'directories=no,height=430,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no,width=420'); void(''); viewChartWindow.focus();";
		%>

			<aui:a href="<%= hrefTaglib %>"><liferay-ui:message key="<%= chart %>" /></aui:a><%= (i != (_CHARTS.length - 1)) ? StringPool.COMMA_AND_SPACE : StringPool.BLANK %>

		<%
			}
		%>

	</div>

	<c:if test="<%= PollsQuestionPermission.contains(permissionChecker, question, ActionKeys.UPDATE) %>">
		<br />

		<liferay-ui:header
			title="actual-votes"
		/>

		<%
		PortletURL portletURL = renderResponse.createRenderURL();

		portletURL.setParameter("struts_action", "/polls/view_question");
		portletURL.setParameter("redirect", redirect);
		portletURL.setParameter("questionId", String.valueOf(question.getQuestionId()));

		List<String> headerNames = new ArrayList<String>();

		headerNames.add("user");
		headerNames.add("choice");
		headerNames.add("vote-date");

		SearchContainer searchContainer = new SearchContainer(renderRequest, null, null, SearchContainer.DEFAULT_CUR_PARAM, SearchContainer.DEFAULT_DELTA, portletURL, headerNames, null);

		int total = PollsVoteLocalServiceUtil.getQuestionVotesCount(question.getQuestionId());

		searchContainer.setTotal(total);

		List results = PollsVoteLocalServiceUtil.getQuestionVotes(question.getQuestionId(), searchContainer.getStart(), searchContainer.getEnd());

		searchContainer.setResults(results);

		List resultRows = searchContainer.getResultRows();

		for (int i = 0; i < results.size(); i++) {
			PollsVote vote = (PollsVote)results.get(i);

			ResultRow row = new ResultRow(vote, vote.getVoteId(), i);

			// User

			row.addText(PortalUtil.getUserName(vote.getUserId(), String.valueOf(vote.getUserId()), request));

			// Choice

			PollsChoice choice = vote.getChoice();

			row.addText(choice.getName() + ". " + choice.getDescription());

			// Vote date

			row.addText(dateFormatDateTime.format(vote.getVoteDate()));

			// Add result row

			resultRows.add(row);
		}
		%>

		<liferay-ui:search-iterator searchContainer="<%= searchContainer %>" />
	</c:if>
</c:if>

<c:if test="<%= question.isExpired() %>">
	<br />

	<div style="font-size: xx-small;">
		<%= LanguageUtil.format(pageContext, "voting-is-disabled-because-this-poll-expired-on-x", dateFormatDateTime.format(question.getExpirationDate())) %>
	</div>
</c:if>

<%!
private static final String[] _CHARTS = {"area", "horizontal-bar", "line", "pie", "vertical-bar"};
%>