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

<%@ include file="/html/portal/layout/view/portlet_js.jspf" %>

<aui:script position="inline">
	Liferay.Util.addInputFocus();

	<c:if test="<%= !themeDisplay.isStatePopUp() %>">

		<%
		String scroll = ParamUtil.getString(request, "scroll");
		%>

		<c:if test="<%= Validator.isNotNull(scroll) %>">
			Liferay.on(
				'allPortletsReady',
				function(event) {
					document.getElementById('<%= HtmlUtil.escape(scroll) %>').scrollIntoView();
				}
			);
		</c:if>
	</c:if>
</aui:script>

<aui:script use="aui-base">
	Liferay.Util.addInputType();

	Liferay.Portlet.ready(
		function(portletId, node) {
			Liferay.Util.addInputType(node);
		}
	);
</aui:script>

<%
Group group = null;

if (layout != null) {
	group = layout.getGroup();
}
%>

<c:if test="<%= (layout != null) && (!group.hasStagingGroup() || group.isStagingGroup()) && LayoutPermissionUtil.contains(permissionChecker, layout, ActionKeys.UPDATE) %>">
	<aui:script use="liferay-layout,liferay-navigation">
		var layoutOptions = {};

		if (!themeDisplay.isFreeformLayout()) {
			layoutOptions = {
				dragNodes: '#main-content .portlet-boundary',
				dropContainer: '.portlet-dropzone',
				dropNodes: '.portlet-column',
				emptyColumnClass: 'empty',
				handles: ['.portlet-title', '.portlet-title-default'],
				invalid: '.portlet-static',
				nestedPortletId: '_<%= PortletKeys.NESTED_PORTLETS %>_INSTANCE',
				portletBoundary: '.portlet-boundary'
			};
		}
		else {
			layoutOptions = {
				dragNodes: '#main-content .portlet-boundary',
				dropContainer: '.portlet-dropzone',
				dropNodes: '.portlet-column',
				emptyColumnClass: 'empty',
				freeForm: true,
				freeformPlaceholderClass: 'lfr-freeform-layout-drag-indicator',
				handles: ['.portlet-header-bar', '.portlet-title-default', '.portlet-topper'],
				nestedPortletId: '_<%= PortletKeys.NESTED_PORTLETS %>_INSTANCE',
				portletBoundary: '.portlet-boundary'
			};
		}

		<c:if test="<%= layout.isTypePortlet() %>">
			Liferay.once(
				'initLayout',
				function() {
					Liferay.Layout.init(layoutOptions);
				}
			);

			var mainContent = A.one('#main-content');

			if (mainContent) {
				mainContent.once(
					'mousemove',
					function() {
						Liferay.fire('initLayout');
					}
				);
			}
		</c:if>

		Liferay.once(
			'initNavigation',
			function() {
				new Liferay.Navigation(
					{
						layoutIds: [<%= ListUtil.toString(layouts, "layoutId") %>],
						navBlock: navBlock,
						hasPermission: <%= GroupPermissionUtil.contains(permissionChecker, scopeGroupId, ActionKeys.MANAGE_LAYOUTS) %>
					}
				);
			}
		);

		var navBlock = A.one('#navigation');

		if (navBlock) {
			navBlock.once(
				'mousemove',
				function() {
					Liferay.fire('initNavigation');
				}
			);
		}
	</aui:script>
</c:if>

<aui:script use="liferay-menu,liferay-notice,liferay-poller">
	new Liferay.Menu();

	Liferay.Notice.prototype.setClosing();

	<c:if test="<%= themeDisplay.isSignedIn() %>">
		Liferay._editControlsState = '<%= GetterUtil.getString(SessionClicks.get(request, "liferay_toggle_controls", null), "visible") %>';

		if (Liferay.Util) {
			Liferay.Util.toggleControls();
		}

		Liferay.Poller.init(
			{
				encryptedUserId: '<%= Encryptor.encrypt(company.getKeyObj(), String.valueOf(themeDisplay.getUserId())) %>',
				supportsComet: <%= ServerDetector.isSupportsComet() %>
			}
		);
	</c:if>
</aui:script>