<%
/*
<!--

    Copyright (C) 2003-2007 eXo Platform SAS.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU Affero General Public License
    as published by the Free Software Foundation; either version 3
    of the License, or (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, see<http://www.gnu.org/licenses/>.

-->
*/
%>
<%
	import org.exoplatform.portal.config.model.PageNavigation;
	import org.exoplatform.portal.config.model.PageNode;
	import org.exoplatform.portal.webui.util.Util;
	import org.exoplatform.webui.organization.OrganizationUtils;

	import org.exoplatform.web.application.JavascriptManager;
	import org.gatein.common.text.EntityEncoder;
	def groupNavigations = uicomponent.getGroupNavigations();


	def rcontext = _ctx.getRequestContext();
	def jsManager = rcontext.getJavascriptManager();
	jsManager.importJavascript('eXo.portal.UIPortalNavigation');
	jsManager.importJavascript('eXo.portal.UIAdminToolbar');
	jsManager.addCustomizedOnLoadScript('eXo.portal.UIAdminToolbar.onLoad("' + uicomponent.id + '");');
	def portalURI = Util.getPortalRequestContext().getPortalURI();
	boolean hasPermission = uicomponent.hasPermission();

	void renderPageNode(PageNode node, boolean flag) {
		PageNode selectedNode = uicomponent.getSelectedPageNode();
		def tabStyleNavigation = "";
		if(selectedNode != null && node.getUri() == selectedNode.getUri()) {
		  tabStyleNavigation = "SelectedItem";
		}
		def hasChild = (node.getChildren() != null && node.getChildren().size() > 0);
		def clazz = "";
		if(hasChild) clazz = "ArrowIcon";
		def href = Util.getPortalRequestContext().getPortalURI() + node.getUri();
		def icon = node.getIcon();
		if(icon == null) icon = "DefaultPageIcon";
		def toolong = (node.resolvedLabel.length() > 60);
		def label = (toolong ? node.resolvedLabel.substring(0, 57) + "..." : node.resolvedLabel);
		def title = "";
		if(toolong == true) {
		  title = "title='$node.resolvedLabel'";
		} else {
		  title = "";
		}
		print """
		  <div class="MenuItem $tabStyleNavigation">
			<div class="$clazz">
		""";
		if(node.pageReference != null) {
		  print """<a class="ItemIcon $icon" href="$href" $title>$label</a>""";
		} else {
		  print """<a class="ItemIcon $icon" href="#" $title>$label</a>""";
		}
		print """
			</div>
		""";
		if(hasChild) {
		  print """
			<div class="MenuItemContainer" style="position: absolute; display:none">
			  <div class="SubBlock">
		  """;
			for(int j = 0; j < node.getChildren().size(); j++) {
			  PageNode childNode = node.getChildren().get(j);
			  renderPageNode(childNode, j%2 == 0);
			}
		  print """
			  </div>
			</div>
		  """;
		}
		print """
		  </div>
		""";
	}

	void renderGroupPageNavigation(PageNavigation navigation) {
		nodes = navigation.getNodes() ;
		if(nodes.size() < 1) return ;
		String navTitle = OrganizationUtils.getGroupLabel(navigation.ownerId);
		String groupsNavigations= navigation.getOwnerId();
		print """
		<div class="TitleBar"><div style="width: 99%" title="$navigation.ownerId">$navTitle</div></div>
		<div class="SubBlock">
			""" ;
			for(int i = 0; i < nodes.size(); i++) {
				renderPageNodeGroup(nodes.get(i), i%2 == 0 ) ;
			}
			print """
		</div>
		""" ;
	}
	
	void renderPageNodeGroup(PageNode node, boolean flag) {
		PageNode selectedNode = uicomponent.getSelectedPageNode();
		String tabStyleNavigation = "";
		if(selectedNode != null && node.getUri() == selectedNode.getUri()) {
				tabStyleNavigation = "SelectedItem";
		}
		boolean hasChild = (node.getChildren() != null && node.getChildren().size() > 0);
		String clazz = "";
		if(hasChild) clazz = "ArrowIcon";
		String	href = Util.getPortalRequestContext().getPortalURI() + node.getUri();

		String icon = node.getIcon();
		if(icon == null) icon = "DefaultPageIcon";
		boolean toolong = (node.resolvedLabel.length() > 60);
		String label = ( toolong ? node.resolvedLabel.substring(0, 57) + "..." : node.resolvedLabel);
		String title = "";
		if(toolong) title = "title='$node.resolvedLabel'";
		else title = "";
		EntityEncoder entityEncoder = EntityEncoder.FULL;
		label = entityEncoder.encode(label);
		print """
			<div class="MenuItem $tabStyleNavigation">
				<div class="$clazz">
					""";
					if(node.pageReference != null) {
						print """<a class="ItemIcon $icon" href="$href" $title>$label</a>""";

					} else {
						print """<a class="ItemIcon $icon" href="#" $title>$label</a>""";
					}
					print """
				</div>
		""" ;
		if(hasChild) {
			print """
				<div class="MenuItemContainer" style="position: absolute; display:none">
					<div class="SubBlock">
			""" ;
				for(int j = 0; j < node.getChildren().size(); j++) {
					renderPageNode(node.getChildren().get(j), j%2 == 0);
				}
			print """
					</div>
				</div>
			""" ;

		}
		print """
			</div>
		""" ;
	}
%>
<div class="UIUserToolBarGroupPortlet" id="$uicomponent.id" >
  <div class="UIHorizontalTabs">
    <div class="TabsContainer">
      <div class="UITab NormalToolbarTab">
        <div class="">
			<% if(hasPermission){ %>
				<a class="EditorIcon TBIcon" href="<%= portalURI + "groupnavigation" %>">
			<% }else{ %>
				<a class="EditorIcon TBIcon" href="#">
			<% } %>
			<%=_ctx.appRes("UIMyGroupsPlatformToolBarPortlet.label")%></a>
		</div>
		<% if (!groupNavigations.isEmpty()) { %>
			<div style="display:none" class="MenuItemContainer">
				<% 
				for(navigation in groupNavigations) {
					renderGroupPageNavigation(navigation);
				} 
				if(hasPermission){ %>
					<div class="SubBlock">
						<div class="MenuItem">   
							<a class="ManageSite" href="<%= portalURI + "groupnavigation" %>"><%=_ctx.appRes("UIMyGroupsPlatformToolBarPortlet.manage")%></a>
						</div>               
					</div>
				<% } %>
			</div>
		<% } %>
      </div>
    </div>
  </div>
</div>
