{* Smarty *}

{literal}
<script type="text/javascript">
//<![CDATA[
$(function () {
  //alert(e);
	$("#StructureOrganizationTree")
		.jstree({
			"plugins" : [ "themes", "json_data", "ui", "crrm", "dnd", "types", "hotkeys"],
      "themes" : {
        "theme" : "classic",
        "icons" : false
      },
			"json_data" : {
				"ajax" : {
          "url" : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureOrganizationSelect.ajax.php{literal}",
					"data" : function (n) {
						return {
							"operation" : "get_children",
							"id" : n.attr ? n.attr("id").replace("node_","") : 0
						};
					}
				}
			},
			"types" : {
				"max_depth" : -2,
				"max_children" : -2,
				"valid_children" : [ "default" ],
				"types" : {
					"default" : {
						"valid_children" : "default",
						"icon" : {
							"image" : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToJquery}/jsTree/_demo/file.png{literal}"
						}
					},
					"folder" : {
						"valid_children" : [ "default", "folder" ],
						"icon" : {
							"image" : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToJquery}/jsTree/_demo/folder.png{literal}"
						}
					},
					"drive" : {
						"valid_children" : [ "default", "folder" ],
						"icon" : {
							"image" : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToJquery}/jsTree/_demo/root.png{literal}"
						},
						"start_drag" : false,
						"move_node" : false,
						"delete_node" : false,
						"remove" : false
					}
				}
			},
			"core" : {
				"initially_open" : [ "node_1" ],
        "strings" : {
          "loading" : "Загрузка ...",
          "new_node" : "Новый элемент"
        }
			}
		})
    .bind("select_node.jstree", function (e, data) {
			data.rslt.obj.each(function () {
        var VarId = this.id.replace("node_","");
        //alert(VarId);
        var VarText = $("#node_"+VarId+" > a").text();
        StructureOrganizationSelectionSelect(VarId, VarText);
        //alert(':'+VarText+':');
//        $("#structureorganization_id").val(VarId);
//        $("#structureorganization_title").val(VarText);
          
        //$("#StructureOrganizationChoice").dialog("close");
        //StructureSelectionIns(this);
        //jQuery('#flex1').flexOptions({newp:1, query: this.id.replace("node_",""), qtype: "zes", url: '{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/SearchNdByMks.ajax.php{literal}'}).flexReload();

//				$.ajax({
//					async : false,
//					type: "GET",
//					url : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/DemandsUsersStepOne.ajax.php{literal}",
//					data : {
//						"UrlId" : this.id.replace("node_","")
//					},
//					success : function (answ) {
//            $("#StructureSvt").html(answ);
//					}
//				});
			});
		})
//		.bind("create.jstree", function (e, data) {
//        $.post(
//          "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureOrganizationAction.ajax.php{literal}",
//          {
//            "operation" : "create_node",
//            "id" : data.rslt.parent.attr("id").replace("node_",""),
//            "position" : data.rslt.position,
//            "title" : data.rslt.name,
//            "type" : data.rslt.obj.attr("rel")
//          },
//          function (r) {
//            if(r.status) {
//              $(data.rslt.obj).attr("id", "node_" + r.id);
//            }
//            else {
//              $.jstree.rollback(data.rlbk);
//            }
//          }
//        );
//      //}
//		})
//
//		.bind("remove.jstree", function (e, data) {
//			data.rslt.obj.each(function () {
//				$.ajax({
//					async : false,
//					type: "POST",
//					url : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureOrganizationAction.ajax.php{literal}",
//					data : {
//						"operation" : "remove_node",
//						"id" : this.id.replace("node_","")
//					},
//					success : function (r) {
//						if(!r.status) {
//							data.inst.refresh();
//						}
//					}
//				});
//			});
//		})
//		.bind("rename.jstree", function (e, data) {
//      if('folder' == data.rslt.obj.attr("rel")){
//        $.post(
//          "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureOrganizationAction.ajax.php{literal}",
//          {
//            "operation" : "rename_node",
//            "id" : data.rslt.obj.attr("id").replace("node_",""),
//            "title" : data.rslt.new_name
//          },
//          function (r) {
//            if(!r.status) {
//              $.jstree.rollback(data.rlbk);
//            }
//          }
//        );
//      }
//		})
//		.bind("move_node.jstree", function (e, data) {
//			data.rslt.o.each(function (i) {
//				$.ajax({
//					async : false,
//					type: "POST",
//					url : "{/literal}{$MS_SP->Sp_PathTop}{$MS_SP->Sp_PathToSite}/AjaxServer/StructureOrganizationAction.ajax.php{literal}",
//					data : {
//						"operation" : "move_node",
//						"id" : $(this).attr("id").replace("node_",""),
//						"ref" : data.rslt.np.attr("id").replace("node_",""),
//						"position" : data.rslt.cp + i,
//						"title" : data.rslt.name,
//						"copy" : data.rslt.cy ? 1 : 0
//					},
//					success : function (r) {
//						if(!r.status) {
//							$.jstree.rollback(data.rlbk);
//						}
//						else {
//							$(data.rslt.oc).attr("id", "node_" + r.id);
//							if(data.rslt.cy && $(data.rslt.oc).children("UL").length) {
//								data.inst.refresh(data.inst._get_parent(data.rslt.oc));
//							}
//						}
//					}
//				});
//			});
//		})
});

//function StructureSelectionIns(VarThis) {
//  var VarId = $(VarThis).parent('li').attr('id').toString().replace("node_", "");
//  var VarText = $(VarThis).text();
//  $("#structureorganization_id").val(VarId);
//  $("#structureorganization_title").val(VarText);
//  $("#StructureOrganizationChoice").dialog("close");
//}
//]]>
</script>
{/literal}

<div id="StructureOrganizationTree" class="demo"></div>

