<%
	import org.exoplatform.webui.core.UIPortletApplication;
	import org.exoplatform.portal.mop.user.UserNode;
	import javax.portlet.MimeResponse;
	import org.exoplatform.portal.application.PortalRequestContext;
	import org.exoplatform.portal.webui.util.Util;
	import org.exoplatform.portal.webui.navigation.TreeNode;
	import org.exoplatform.web.url.PortalURL;
	import org.exoplatform.web.url.navigation.NavigationResource;
	import javax.portlet.ResourceURL;

	PortalRequestContext pcontext = Util.getPortalRequestContext();

	UIPortletApplication siteMapPortlet = uicomponent.getParent();
	def useAJAX = siteMapPortlet.isUseAjax();

	def actionCollapseAll = uicomponent.event("CollapseAllNode");

	PortalURL nodeURL = nodeurl();

	public void renderNodes(TreeNode rootTree, PortalURL nodeURL, boolean useAjax) {
		List childrenNodes = rootTree.getChildren();
		int childrenSize = childrenNodes.size() ;
		int size = 0;
		for(treeNode in childrenNodes) {

			// count size;
			size++;

			def node = treeNode.getNode();
			def treePath = node.getId();
			String label = node.encodedResolvedLabel;

			def actionLink = "#";
			if(treeNode.getNode().getPageRef() != null) {
			   nodeURL.setNode(node);
			   nodeURL.setAjax(useAjax);
			   actionLink = nodeURL.toString();
			}

			lastNode = '';
			if (size == childrenSize) {
				lastNode = 'LastNode';
			}
			// if node have child

			if(treeNode.hasChild()) {
				if (treeNode.isExpanded()) {
				   def actionCollapse = uicomponent.url("CollapseNode", treePath);
					println """
						<div class="$lastNode Node">
							<div class="CollapseIcon ClearFix" title="Collapse Icon">
                        <input type="hidden" name="collapseURL" value="$actionCollapse"/>
					""";
					   println "<a class='NodeIcon DefaultPageIcon' href=\"$actionLink\" title=\"$label\">$label</a>";
					println """
							</div>
							<div class="ChildrenContainer" style="display: block">
					""";

					renderNodes(treeNode, nodeURL, useAjax);

				} else {
				    MimeResponse res = _ctx.getRequestContext().getResponse();
					ResourceURL resourceURL = res.createResourceURL();
					resourceURL.setResourceID(treePath);
					def actionExpand = resourceURL.toString();

					println """
						<div class="$lastNode Node">
							<div class="ExpandIcon ClearFix" title="Expand Icon">
                        <input type="hidden" name="expandURL" value="$actionExpand"/>
					""";
					      println "<a class='NodeIcon DefaultPageIcon' href=\"$actionLink\" title=\"$label\">$label</a>";
					println """
							</div>
							<div class="ChildrenContainer" style="display: none">
					""";

				    renderNodes(treeNode, nodeURL, useAjax);
				}


				println """
						</div>
					</div>
				""";
			} else {
				// if node doesn't have child
				println """
					<div class="$lastNode Node ClearFix">
						<div class="NullItem">
							<div class="ClearFix">
								<a class="NodeIcon DefaultPageIcon" href="$actionLink" title="$label">$label</a>
							</div>
						</div>
					</div>

				""";

			}
		}
	}
%>

<div id="$uicomponent.id" class="UISitemap" >
	<div class="ClearFix">
		<div class="CollapseAll FL" title="Collapse All">
			<a href="#" onclick="$actionCollapseAll"><%=_ctx.appRes(siteMapPortlet.getName() + ".label.CollapseAll")%></a>
		</div>
	</div>
	<div class="SitemapContent">
		<div class="UISiteTree">
			<% TreeNode treeNodeRoot = uicomponent.getTreeNodes() ;%>
			<% renderNodes(treeNodeRoot,nodeURL,useAJAX); %>
		</div>
	</div>
</div>
