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

<c:choose>
	<c:when test="<%= results.isEmpty() %>">
		<div class="portlet-msg-info">
			<%= LanguageUtil.get(pageContext, "there-are-no-images-in-this-folder") %>
		</div>
	</c:when>
	<c:otherwise>
		<div class="taglib-search-iterator-page-iterator-top">
			<liferay-ui:search-paginator searchContainer="<%= searchContainer %>" />
		</div>
	</c:otherwise>
</c:choose>

<div>

	<%
	for (int i = 0; i < results.size(); i++) {
		IGImage image = null;

		if (useAssetEntryQuery) {
			AssetEntry assetEntry = (AssetEntry)results.get(i);

			image = IGImageLocalServiceUtil.getIGImage(assetEntry.getClassPK());
		}
		else {
			image = (IGImage)results.get(i);
		}
	%>

		<c:choose>
			<c:when test="<%= IGImagePermission.contains(permissionChecker, image, ActionKeys.VIEW) %>">

				<%
				Image largeImage = ImageLocalServiceUtil.getImage(image.getLargeImageId());
				Image smallImage = ImageLocalServiceUtil.getImage(image.getSmallImageId());

				long smallImageId = 0;
				int smallImageHeight = 100;
				int smallImageWidth = 100;

				if (smallImage != null) {
					smallImageId = smallImage.getImageId();
					smallImageHeight = smallImage.getHeight();
					smallImageWidth = smallImage.getWidth();
				}

				int topMargin = PrefsPropsUtil.getInteger(PropsKeys.IG_IMAGE_THUMBNAIL_MAX_DIMENSION) - smallImageHeight;
				int sideMargin = (PrefsPropsUtil.getInteger(PropsKeys.IG_IMAGE_THUMBNAIL_MAX_DIMENSION) - smallImageWidth) / 2;
				%>

				<a class="image-thumbnail" href="<%= themeDisplay.getPathImage() %>/image_gallery?img_id=<%= largeImage.getImageId() %>&t=<%= ImageServletTokenUtil.getToken(largeImage.getImageId()) %>" largeImageId="<%= largeImage.getImageId() %>" title="<%= HtmlUtil.escape(image.getName()) + " - " + HtmlUtil.escape(image.getDescription()) %>">
					<img alt="<%= HtmlUtil.escape(image.getName()) + " - " + HtmlUtil.escape(image.getDescription()) %>" border="no" src="<%= themeDisplay.getPathImage() %>/image_gallery?img_id=<%= smallImageId %>&igImageId=<%= image.getImageId() %>&igSmallImage=1&t=<%= ImageServletTokenUtil.getToken(smallImageId) %>" style="height: <%= smallImageHeight %>; margin: <%= topMargin %>px <%= sideMargin %>px 0px <%= sideMargin %>px; width: <%= smallImageWidth %>;" />

					<span class="image-title"><%= HtmlUtil.escape(image.getName()) %></span>

					<c:if test="<%= scores != null %>">
						<span class="image-score">

							<%
							double score = ((Double)scores.get(i)).doubleValue();

							score = MathUtils.round((score * 10) / 2, 1, BigDecimal.ROUND_HALF_UP);
							%>

							<liferay-ui:ratings-score score="<%= score %>" />
						</span>
					</c:if>
				</a>

				<div class="aui-helper-hidden" id="<portlet:namespace />buttonsContainer_<%= largeImage.getImageId() %>">
					<div class="buttons-container float-container" id="<portlet:namespace />buttons_<%= largeImage.getImageId() %>">
						<%@ include file="/html/portlet/image_gallery/image_action.jspf" %>
					</div>
				</div>

				<%
				List assetTags = AssetTagServiceUtil.getTags(IGImage.class.getName(), image.getImageId());
				%>

				<div id="<portlet:namespace />categorizacionContainer_<%= largeImage.getImageId() %>" style="display: none;">
					<span <%= !assetTags.isEmpty() ? "class=\"has-tags\"" : "" %>>
						<liferay-ui:asset-categories-summary
							className="<%= IGImage.class.getName() %>"
							classPK="<%= image.getImageId() %>"
						/>
					</span>

					<liferay-ui:asset-tags-summary
						className="<%= IGImage.class.getName() %>"
						classPK="<%= image.getImageId() %>"
					/>
				</div>
			</c:when>
			<c:otherwise>
				<div style="float: left; margin: 100px 10px 0px;">
					<img alt="<liferay-ui:message key="image" />" border="no" src="<%= themeDisplay.getPathThemeImages() %>/application/forbidden_action.png" />
				</div>
			</c:otherwise>
		</c:choose>

	<%
	}
	%>

</div>

<c:if test="<%= !results.isEmpty() %>">
	<div class="taglib-search-iterator-page-iterator-bottom">
		<liferay-ui:search-paginator searchContainer="<%= searchContainer %>" />
	</div>
</c:if>

<aui:script use="aui-image-viewer-gallery">
	var viewportRegion = A.getDoc().get('viewportRegion');
	var maxHeight = (viewportRegion.height * 0.5);
	var maxWidth = (viewportRegion.width * 0.5);

	var imageGallery = new A.ImageGallery(
		{
			after: {
				render: function(event) {
					var instance = this;
					var footerNode = instance.footerNode;

					instance._actions = A.Node.create('<div class="lfr-image-gallery-actions"></div>');

					if (footerNode) {
						footerNode.append(
							instance._actions
						);
					}
				},
				request: function(event) {
					var instance = this;

					var currentLink = instance.getCurrentLink();
					var largeImageId = currentLink.attr('largeImageId');
					var actions = instance._actions;

					if (actions) {
						var action = A.one('#<portlet:namespace />buttonsContainer_' + largeImageId);

						actions.empty();

						actions.append(
							action.clone().show()
						);
					}
				}
			},
			delay: 5000,
			links: '#imageGalleryPanelContainer .image-thumbnail',
			maxHeight: maxHeight,
			maxWidth: maxWidth,
			playingLabel: '(<liferay-ui:message key="playing" />)'
		}
	).render();
</aui:script>