<%
/*
<!--

    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;
  
  
  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();
  def spacesLink = portalURI + "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 = 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);
          def isRender = uicomponent.isRender(node, childNode);
          if (isRender) renderPageNode(childNode, j%2 == 0);
        }
      print """
          </div>
        </div>
      """;
    }
    print """
      </div>
    """;
  }
   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;
				}
				
				}
%> 
<div class="UIUserToolBarGroupPortlet" id="$uicomponent.id" >
  <div class="UIHorizontalTabs">
    <div class="TabsContainer">
      <div class="UITab NormalToolbarTab">
        <div class="">
		 <% if (renderSpacesLink()) { %>
          <a class="TBIcon" href="<%= spacesLink %>">
            <%=_ctx.appRes("UISpacePlatformToolBarPortlet.label")%>
          </a>
       
		 <% } %>
		 <a class="TBIcon" href="#">
           
          </a>
		
		  </div>
        <% if (!spaceNavigations.isEmpty()) { %>
        <div style="display:none" class="MenuItemContainer">
        <% for(navigation in spaceNavigations) {
          renderSpacePageNavigation(navigation);
           } %>
        </div>
       <% } %>
      </div>
    </div>
  </div>
</div>
