<%	
	import java.util.List;

	import org.exoplatform.portal.mop.user.UserNavigation;
	import org.exoplatform.portal.mop.user.UserNode;
	import org.exoplatform.web.application.JavascriptManager;
	import org.exoplatform.portal.webui.util.Util ;
	import org.exoplatform.webui.organization.OrganizationUtils;
	import org.gatein.common.text.EntityEncoder;
	import org.exoplatform.platform.webui.NavigationURLUtils;
	
	def rcontext = _ctx.getRequestContext() ;
	JavascriptManager jsmanager = rcontext.getJavascriptManager();
	jsmanager.importJavascript('eXo.portal.UIPortalNavigation') ;
	jsmanager.importJavascript('eXo.portal.UIAdminToolbar') ;
	jsmanager.addCustomizedOnLoadScript('eXo.portal.UIAdminToolbar.onLoad("' + uicomponent.id + '");');
	
	List allPortalNames = uicomponent.getAllPortalNames();
	boolean hasPermission = uicomponent.hasEditOrCreatePortalPermission();
	void renderPortalNavigations(List allPortalNames, boolean hasPermission) {
			
			print """
					<ul style="position: absolute; display:none" class="MenuItemContainer">
			""";
			
			boolean isCurrent = false;
			String clazz = "";
			String href = "#";
			for(int i = 0; i < allPortalNames.size(); i++) {
				String portal = allPortalNames.get(i);
 				if(portal.equals(uicomponent.getCurrentPortal())) {
					isCurrent = true;
			 	} else isCurrent = false;
				if(isCurrent) clazz = "ArrowIcon";
				else clazz = "";
				href = NavigationURLUtils.getPortalURL(portal, null);
                EntityEncoder entityEncoder = EntityEncoder.FULL;
				portal = entityEncoder.encode(portal);
				print """
					<li class="MenuItem $clazz">
							<a href="$href" class="ItemIcon SiteIcon">$portal</a>
				""";
						if(isCurrent) {
							renderCurrentPortal();
						}
				print """
					</li>
				""";
			}
			print """
				</ul>
			""";
	}
	
	void renderCurrentPortal() {
		def nodes = uicomponent.getUserNodes(uicomponent.getCurrentPortalNavigation());
		print """
			<ul style="position: absolute; display:none" class="MenuItemContainer">
		""";
		int idx = 0;
		for(UserNode node in nodes) {
			renderPageNode(node, idx++ % 2 == 0);
		}
		print """
			</ul>
		""";
	}
	
	void renderPageNode(UserNode node, boolean flag) {
		UserNode 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 = NavigationURLUtils.getURL(node);
		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 """
			<li class="MenuItem $tabStyleNavigation $clazz">
		""";
						if(node.pageRef != null) {
								print """<a class="ItemIcon $icon" href="$href" $title>$label</a>""";
						} else {
								print """<a class="ItemIcon $icon" href="#" $title>$label</a>""";
						}
		if(hasChild) {
			print """
				<ul class="MenuItemContainer" style="position: absolute; display:none">			
			""" ;
				int idx = 0;
				for(UserNode child : node.getChildren()) {
					renderPageNode(child, idx++ % 2 == 0);
				}
			print """
				</ul>
			""" ;
			
		}
		print """
			</li>
		""" ;			
	}
%>

<% if(allPortalNames.isEmpty()) { %>
	<ul class="UIUserToolBarSitePortlet UIHorizontalTabs" id="$uicomponent.id">
		<li class="UITab NormalToolbarTab">
			<a class="ArrowIcon TBIcon" href="#">
					 <%=_ctx.appRes("UIUserPlatformToolBarSitePortlet.title")%>
			</a>
		</li>
	</ul>
<% } else if(allPortalNames.size() > 1) { %>
	<ul class="UIUserToolBarSitePortlet UIHorizontalTabs" id="$uicomponent.id">
		<li class="UITab NormalToolbarTab">
			<a class="ArrowIcon TBIcon" href="#">
					 <%=_ctx.appRes("UIUserPlatformToolBarSitePortlet.title")%>
			</a>
			<% try{
				renderPortalNavigations(allPortalNames, hasPermission);
			}catch(Exception exp) {
			  exp.printStackTrace();
			}
			%>
		</li>
	</ul>
<% } else if (allPortalNames.size() == 1){
	String portal = allPortalNames.get(0);
	href = NavigationURLUtils.getPortalURL(portal, null);
	%>
		<ul class="UIUserToolBarSitePortlet UIHorizontalTabs" id="$uicomponent.id">
		<li class="UITab NormalToolbarTab">
			<a class="ArrowIcon TBIcon" href="$href" style="text-transform: capitalize;">
					 <%=portal%>
			</a>
			<%
			renderCurrentPortal();
			%>

		</li>
	</ul>
	<%} %>
