<%@ Reference VirtualPath="~/bitrix/components/bitrix/system.PublicMenu/component.ascx" %>
<%@ Control Language="C#" ClassName="template" Inherits="Bitrix.Main.Components.SystemPublicMenuTemplate" %>
<%@ Import Namespace="Bitrix" %>

<script runat="server">
	public void RenderMenuItem(BXPublicMenuItem item, int depth, bool last, HtmlTextWriter writer)
	{
		bool hasChildren = item.Children != null && item.Children.Count != 0;
		
		if (depth == 0)
			writer.AddAttribute(HtmlTextWriterAttribute.Class, item.IsSelected ? "root-item-selected" : "root-item");
		else if (item.IsAccessible)
		{
			string cssClass = (item.IsSelected ? "item-selected" : "") + (last ? " item-last" : "");
			cssClass = cssClass.Trim();
			if (cssClass != "")
				writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClass.ToString());
		}
		writer.RenderBeginTag(HtmlTextWriterTag.Li);
		{
			if (depth == 0)
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Class, "root-separator");
				writer.RenderBeginTag(HtmlTextWriterTag.Div);
				writer.RenderEndTag();
			}

			if (depth == 0)
				writer.AddAttribute(HtmlTextWriterAttribute.Class, item.IsSelected ? "root-item-selected" : "root-item");
			else if (hasChildren)
				writer.AddAttribute(HtmlTextWriterAttribute.Class, "parent");
			else if (!item.IsAccessible)
				writer.AddAttribute(HtmlTextWriterAttribute.Class, "denied");

			if (!item.IsAccessible)
				writer.AddAttribute(HtmlTextWriterAttribute.Title, "Access denied");
			writer.AddAttribute(HtmlTextWriterAttribute.Href, (hasChildren || item.IsAccessible) ? item.Href : "");
			writer.RenderBeginTag(HtmlTextWriterTag.A);
			writer.WriteEncodedText(item.Title);
			writer.RenderEndTag();

			if (hasChildren)
			{
				writer.RenderBeginTag(HtmlTextWriterTag.Ul);
				for (int i = 0; i < item.Children.Count; i++)
					RenderMenuItem(item.Children[i], depth + 1, i == item.Children.Count - 1, writer);
				writer.RenderEndTag();
			}
		}
		writer.RenderEndTag();
	}
	public override string Title
	{
		get
		{
			return "Portal template (primary menu)";
		}
	}
</script>

<%
	if (Component.Menu == null || Component.Menu.Count == 0)
		return;
%>

<ul id="horizontal-multilevel-menu">
	<li class="left-margin">&nbsp;</li>
	<% 
		for (int i = 0; i < Component.Menu.Count; i++)
			RenderMenuItem(Component.Menu[i], 0, i == Component.Menu.Count - 1, CurrentWriter);
	%>
</ul>
<div class="menu-clear-left"></div>