<%
/**
 * 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:if test="<%= DLFileEntryPermission.contains(permissionChecker, fileEntry, ActionKeys.VIEW) %>">

	<%
	String fileUrl = themeDisplay.getPortalURL() + themeDisplay.getPathContext() + "/documents/" + themeDisplay.getScopeGroupId() + StringPool.SLASH + folderId + StringPool.SLASH + HttpUtil.encodeURL(HtmlUtil.unescape(fileEntry.getTitle()));
	String webDavUrl = StringPool.BLANK;

	if (portletDisplay.isWebDAVEnabled()) {
		StringBuilder sb = new StringBuilder();

		if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
			DLFolder curFolder = DLFolderLocalServiceUtil.getFolder(folderId);

			while (true) {
				sb.insert(0, HttpUtil.encodeURL(curFolder.getName(), true));
				sb.insert(0, StringPool.SLASH);

				if (curFolder.getParentFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
					break;
				}
				else {
					curFolder = DLFolderLocalServiceUtil.getFolder(curFolder.getParentFolderId());
				}
			}
		}

		sb.append(StringPool.SLASH);
		sb.append(HttpUtil.encodeURL(HtmlUtil.unescape(fileEntry.getTitle()), true));

		Group group = themeDisplay.getScopeGroup();

		webDavUrl = themeDisplay.getPortalURL() + "/tunnel-web/secure/webdav" + group.getFriendlyURL() + "/document_library" + sb.toString();
	}

	String officeDoc = null;

	String curExtension = fileEntry.getExtension();

	if (portletDisplay.isWebDAVEnabled() && BrowserSnifferUtil.isIe(request)) {
		if (curExtension.equalsIgnoreCase("doc") ||
			curExtension.equalsIgnoreCase("dot") ||
			curExtension.equalsIgnoreCase("ppt") ||
			curExtension.equalsIgnoreCase("xls")) {

			officeDoc = "SharePoint.OpenDocuments.1";
		}
		else if (curExtension.equalsIgnoreCase("docx") ||
				 curExtension.equalsIgnoreCase("pptx") ||
				 curExtension.equalsIgnoreCase("xlsx")) {

			officeDoc = "SharePoint.OpenDocuments.2";
		}
	}
	%>

	<c:if test="<%= officeDoc != null %>">
		<liferay-ui:icon
			image="../document_library/msoffice"
			message='<%= LanguageUtil.get(pageContext, "open-in-ms-office") %>'
			url='<%= "javascript:" + renderResponse.getNamespace() + "openDocument();" %>'
		/>
	</c:if>

	<aui:script>
		function <portlet:namespace />openDocument() {
			var fileUrl = "<%= fileUrl %>";
			var webDavUrl = "<%= webDavUrl %>";

			<c:choose>
				<c:when test="<%= officeDoc != null %>">
					var officeDoc = new ActiveXObject("<%= officeDoc %>");

					if (officeDoc) {
						officeDoc.EditDocument(webDavUrl);
					}
					else {
						window.location.href = fileUrl;
					}
				</c:when>
				<c:otherwise>
					window.location.href = fileUrl;
				</c:otherwise>
			</c:choose>
		}
	</aui:script>
</c:if>