﻿
Partial Class ETGSubMenu
    Inherits System.Web.UI.UserControl

    Private Sub FinalizeMenu()

        'On Error Resume Next

        Dim objCmsSiteMapNode As Telerik.Sitefinity.Web.PageSiteNode
        Dim theItem As MenuItem
        Dim objMenuItem As MenuItem
        Dim x As Boolean

        'Find the menu item (if any) that corresponds to this page.
        theItem = FindMenuItem(Menu1.Items)

        'Now, figure out which parent item it belongs to.
        Dim theParent As MenuItem = GetItemParent(theItem)

        'Finally, remove all children from the other top-level menu items.
        If Not theParent Is Nothing Then
            RemoveChildrenOfOthers(Menu1.Items, theParent)
        Else
            RemoveChildrenOfOthers(Menu1.Items, theItem)
        End If

        ' End If

        'Add page if no menu items.
        If Menu1.Items.Count = 0 Then
            objCmsSiteMapNode = SiteMap.CurrentNode
            objMenuItem = New MenuItem
            If Not objCmsSiteMapNode Is Nothing Then
                objMenuItem.Text = objCmsSiteMapNode.Title
            Else
                objMenuItem.Text = "Page Title"
            End If
            objMenuItem.NavigateUrl = Request.Url.AbsolutePath
            objMenuItem.Selected = True
            Menu1.Items.Add(child:=objMenuItem)
        End If

    End Sub

    Private Function IsSamePage(ByVal targetUrl As String) As Boolean
        On Error Resume Next

        'Check if same page.
        Return Server.MapPath(Request.Url.LocalPath) = Server.MapPath(targetUrl)

    End Function

    Private Function FindMenuItem(ByVal mis As MenuItemCollection) As MenuItem
        On Error Resume Next

        'Loop menu items.
        For Each mi As MenuItem In mis

            'Check if same page.
            If IsSamePage(mi.NavigateUrl) Then
                Return mi
            Else
                Dim mi2 As MenuItem = FindMenuItem(mi.ChildItems)
                If mi2 IsNot Nothing Then
                    Return mi2
                End If
            End If

        Next

        'Set function return.
        Return Nothing

    End Function

    Private Function GetItemParent(ByVal mi As MenuItem) As MenuItem

        Try

            'Find parent.
            If mi.Parent Is Nothing Then
                Return mi
            Else
                Return GetItemParent(mi.Parent)
            End If

        Catch

        End Try

    End Function



    Private Sub RemoveChildrenOfOthers(ByVal all As MenuItemCollection, ByVal keep As MenuItem)
        On Error Resume Next

        'Loop menu items.
        For Each mi As MenuItem In all

            'Remove children.
            If mi.Equals(keep) = False Then
                mi.ChildItems.Clear()
            End If

            ''Remove children.
            'If mi.Equals(keep) = True Then

            '    'Loop menu items.
            '    For Each mi2 As MenuItem In mi.ChildItems
            '        'mi2.ChildItems.Clear()
            '    Next

            'End If

            'Response.Write(keep.Text & "<br>")

            'If mi.Text = "Thank You" Then
            'Response.Write("dddd")
            'End If

        Next

        Dim theItem As MenuItem = FindMenuItem(Menu1.Items)
        Dim objParent As MenuItem
        Dim objParent2 As MenuItem

        If theItem Is Nothing Then
            Exit Sub
        End If

        'Loop menu items.
        For Each mi2 As MenuItem In theItem.ChildItems
            mi2.ChildItems.Clear()
        Next

        If theItem.Depth = 1 Then

            'Loop menu items.
            For Each mi2 As MenuItem In theItem.Parent.ChildItems
                If mi2.Equals(theItem) = False Then
                    mi2.ChildItems.Clear()
                End If
            Next

        End If

        If theItem.Depth = 2 Then

            objParent = theItem.Parent
            objParent2 = objParent.Parent

            'Loop menu items.
            For Each mi2 As MenuItem In objParent2.ChildItems
                If mi2.Equals(objParent) = False Then
                    mi2.ChildItems.Clear()
                End If
            Next

        End If

    End Sub

    Protected Sub TreeView1_DataBound(sender As Object, e As EventArgs) Handles TreeView1.DataBound

        LoadMenu(menuItem:=Nothing, treeNode:=Nothing)
        FinalizeMenu()
        TreeView1.Visible = False

    End Sub

    Private Sub LoadMenu(menuItem As MenuItem, treeNode As TreeNode)

        Dim objTreeNode As TreeNode
        Dim objMenuItem As MenuItem
        Dim objTreeNodeCollection As TreeNodeCollection

        If treeNode Is Nothing Then
            objTreeNodeCollection = TreeView1.Nodes
        Else
            objTreeNodeCollection = treeNode.ChildNodes
        End If

        For Each objTreeNode In objTreeNodeCollection

	    If objTreeNode.Text <> "Thank You" Then

            objMenuItem = New MenuItem
            objMenuItem.Text = objTreeNode.Text
            objMenuItem.NavigateUrl = objTreeNode.NavigateUrl

            If menuItem Is Nothing Then
                Menu1.Items.Add(objMenuItem)
            Else
                menuItem.ChildItems.Add(objMenuItem)
            End If

            If objTreeNode.ChildNodes.Count > 0 Then
                LoadMenu(menuItem:=objMenuItem, treeNode:=objTreeNode)
            End If

            'Select the current page.
            If LCase(ResolveUrl(objMenuItem.NavigateUrl)) = LCase(Request.Url.AbsolutePath) Then
                objMenuItem.Selectable = True
                objMenuItem.Selected = True
            End If

	End If

        Next

    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        
    End Sub

    Protected Sub TreeView1_TreeNodeDataBound(sender As Object, e As TreeNodeEventArgs) Handles TreeView1.TreeNodeDataBound

        Dim objCmsSiteMapNode As Telerik.Sitefinity.Web.PageSiteNode = e.Node.DataItem

'Response.Write(e.Node.Text & " " & objCmsSiteMapNode.ShowInNavigation & "<br>")

        If objCmsSiteMapNode.ShowInNavigation = False Then
	     If e.Node.Parent Is Nothing Then
            	TreeView1.Nodes.Remove(e.Node)
	     Else
		'e.Node.Parent.Nodes.Remove(e.Node)
	     End If
        End If
    End Sub

End Class

            


