<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Employee>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
    <script type="text/javascript">
        
        function closeWindow() {
            $('#Window').close();
            removeSelection();
        }

        function removeSelection() {
            $('#AjaxTreeView .t-state-selected').removeClass('t-state-selected');
        }

        function onSelect(e) {
            var urlFormat = '<%= HttpUtility.UrlDecode(Url.Action("TestView", "Home", new { id = "{0}" })) %>';

            var specificUrl = $.telerik.formatString(urlFormat, $('#AjaxTreeView').data('tTreeView').getItemValue(e.item));
            
            var window = $('#Window').data('tWindow');
            window.ajaxRequest(specificUrl);
            window.center().refresh();
            window.open();
        }
        
	</script>
    
    <%= Html.Telerik().TreeView()
            .Name("AjaxTreeView")
            .ShowCheckBox(true)
            .HtmlAttributes(new { style = "width:250px; height:550px;" })
            .BindTo(Model, (item, employee) =>
            {
                // bind initial data - can be omitted if there is none
                item.Text = employee.FirstName + " " + employee.LastName;
                item.Value = employee.EmployeeID.ToString();
                item.LoadOnDemand = employee.Employees.Count > 0;
            })
            .ClientEvents(events => events.OnSelect("onSelect"))    
            .DataBinding(dataBinding => dataBinding
                    .Ajax().Select("_AjaxLoading", "Home")
            )
    %>
    
    <%= Html.Telerik().Window()
            .Name("Window")
            .Buttons(buttons => buttons.Refresh().Close())
		    .Height(700)
		    .Width(300)
            .Visible(false)   
		    .Resizable()
            .Draggable(true)
            .ClientEvents(events => events.OnClose("removeSelection"))
    %>
    
</asp:Content>