<%
/*
<!--

    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.platform.webui.NavigationURLUtils;
  
  
  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 spacesLink = NavigationURLUtils.getURLInCurrentPortal("spaces");
  def spaceNavigations = uicomponent.getSpaceNavigations();
  
  void renderSpacePageNavigation(PageNavigation navigation) {
    nodes = navigation.getNodes();
    if(nodes.size() < 1) return;
    for(int i = 0; i < nodes.size(); i++) {
      renderPageNode(nodes.get(i), i%2 == 0);
    }
  }
  
  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 = NavigationURLUtils.getURL(node);
    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 """
      <li class="MenuItem $clazz $tabStyleNavigation">
    """;
    if(node.pageReference != 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">
      """;
        for(int j = 0; j < node.getChildren().size(); j++) {
          PageNode childNode = node.getChildren().get(j);
          def isRender = uicomponent.isRender(node, childNode);
          if (isRender) renderPageNode(childNode, j%2 == 0);
        }
      print """
        </ul>
      """;
    }
    print """
      </li>
    """;
  }
   boolean renderSpacesLink(){
 
 def navigations = uicomponent.getNavigations();
	if(navigations != null) {
				 for(nav in navigations) {
					 def pageNodes = nav.getNodes();
				
					 for(PageNode node in pageNodes) {
					 if (node.getURI() == "spaces") {
			        return true;
			       }
					  }
					 
				  }
				    return false;
				}
				
				}
%> 
<ul class="UIUserToolBarGroupPortlet UIHorizontalTabs" id="$uicomponent.id">
  <li class="UITab NormalToolbarTab">
	 <% if (renderSpacesLink()) { %>
	  <a class="TBIcon" href="<%= spacesLink %>">
		<%=_ctx.appRes("UISpacePlatformToolBarPortlet.label")%>
	  </a>
   
	 <% } %>
	 <a class="TBIcon" href="#">
	   
	  </a>
	<% if (!spaceNavigations.isEmpty()) { %>
	<ul style="display:none" class="MenuItemContainer">
	<% for(navigation in spaceNavigations) {
	  renderSpacePageNavigation(navigation);
	   } %>
	</ul>
   <% } %>
  </li>
</ul>
