<%
/*
<!--

    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;

  /** PLF-488 : */
  import org.exoplatform.web.application.JavascriptManager;
  import org.gatein.common.text.EntityEncoder;
  def groupNavigations = uicomponent.getGroupNavigations();
  /** FIN */


  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 findSpacesLink = portalURI + "publicSpace";
  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>
    """;
  }
   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;
		}

	}

	void renderSpacePageNavigation(PageNavigation navigation) {
            nodes = navigation.getNodes() ;
            if(nodes.size() < 1) return ;
            String navTitle = OrganizationUtils.getGroupLabel(navigation.ownerId);
            String spacesNavigations= navigation.getOwnerId();
            if(spacesNavigations.indexOf("spaces") > 0) {
                        print """
                        <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">
			""" ;
				/** PLF-1303 */
				for(int j = 0; j < node.getChildren().size(); j++) {
					def childNode = node.getChildren().get(j);
					if (uicomponent.isRender(node, childNode)) {
						renderPageNode(childNode, j%2 == 0);
					}
				}
			print """
					</div>
				</div>
			""" ;

		}
		print """
			</div>
		""" ;
	}

	/** FIN */

%>
<div class="UIUserToolBarGroupPortlet" id="$uicomponent.id" >
  <div class="UIHorizontalTabs">
    <div class="TabsContainer">
      <div class="UITab NormalToolbarTab">
       		<%
			if (renderSpacesLink()) {
		%>
	<div class="">
		  <a class="EditorIcon TBIcon" href="<%= spacesLink %>"><%=_ctx.appRes("UIMySpacePlatformToolBarPortlet.label")%></a>
	</div>

	  <!-- Merge exoadmin:  navigation-->

		<% if (!groupNavigations.isEmpty()) { %>

			<div style="display:none" class="MenuItemContainer">
				<% 
				def MY_SPACES_MAX_NUMBER = 10;
				def mySpacesDisplayed = 0;
				for(navigation in groupNavigations) {
					if (navigation.getOwnerId().indexOf("spaces") < 0 || mySpacesDisplayed == -1){
						break;
					}
					/** PLF-1521 */
						if(mySpacesDisplayed < MY_SPACES_MAX_NUMBER){
							mySpacesDisplayed += 1;
							renderSpacePageNavigation(navigation);
						}else{
							def portalContextPath = Util.getPortalRequestContext().getPortalURI().split("/")[1];
							def portalContextAccessPath = Util.getPortalRequestContext().getPortalURI().split("/")[2];
							mySpacesDisplayed = -1;
							def moreSpacesLabel = _ctx.appRes("UIMySpacePlatformToolBarPortlet.more");
							def moreSpacesLink = spacesLink;
							print """
							<div class="SubBlock">
								<div class="MenuItem">
									<a href="$moreSpacesLink">$moreSpacesLabel</a>
								</div>
							</div>
							""" ;
						}
					
					
				} %>
				<% if (renderSpacesLink()) { %>
				<div class="SubBlock">
						<div class="MenuItem">   
							<a class="FindPeopleIcon" href="<%= findSpacesLink %>"><%=_ctx.appRes("UIMySpacePlatformToolBarPortlet.find")%></a>
						</div>               
					</div>
				<% } %>

			</div>
			<% } %>
		<% } %>
	   <!-- FIN -->

      </div>
    </div>
  </div>
</div>
