/**
 * @param containerId the id of jstree container
 * @param data 
 * @param name the name of new node
 * @param id the id of new node
 * @param pos the position of new node to the parent node,maybe "last" which means the childnode of the selected 
 * 		  	  node,"after" or "before" means the sibling node of the selected node
 */
function jstreeCreateNode(containerId,data,name,id,pos,parentNode){
	var treeObj = $("#"+containerId).jstree(true);
	if(parentNode == null || parentNode == undefined){
		parentNode = treeObj.get_selected();
		if(!parentNode.length){
			parentNode=null;
		}
		else{
			parentNode = parentNode[0];
		}
	}
	
	var childNode = null;
	var type = data;
	
	if(type!="page" && type!="batch")
		type = "default";
	childNode = treeObj.create_node(parentNode,{text:name,id:id,data:data,type:type},pos);
	if(childNode) {
		treeObj.open_node(parentNode);
	}
}
/**
 * delete current node
 * @param containerId the id of jstree container
 */
function jstreeDeleteNode(containerId,nodeId){
	var treeObj = $("#"+containerId).jstree(true);
//	var parentNode = treeObj.get_selected();
//	if(!parentNode.length){return false;}
	treeObj.delete_node(nodeId);
	return treeObj.element.context.id;
}

/**
 * delete all node
 * @param containerId the id of jstree container
 */
function jstreeClearNode(containerId){
	var treeObj = $("#"+containerId).jstree(true);
	var nodes = treeObj.get_children_dom("#");
	while(nodes.length){
		treeObj.delete_node(nodes);
		nodes = treeObj.get_children_dom("#");
	}
}

/**
 * @param containerId the id of jstree container
 * @param node the id of to be selected node
 */
function jstreeSelectNode(containerId,node){
	var treeObj = $("#"+containerId).jstree(true);
	treeObj.deselect_all();
	treeObj.select_node(node);
}

/**
 * @param containerId the id of jstree container
 */
function jstreeSelectedNodes(containerId){
	var treeObj = $("#"+containerId).jstree(true);
	return treeObj.get_selected(true);
}

function jstreeRenameNode(containerId, node, text){
	var treeObj = $("#"+containerId).jstree(true);
	treeObj.rename_node(node,text);
}

/**
 * @param containerId the id of jstree container
 */
function jstreeCreate(containerId){
	$("#"+containerId).jstree({
		"core" : {
			"animation" : 0,
			"check_callback" : true,
			"themes" : { "stripes" : true }
		},
		"types" : {
			"#" : { "max_depth" : 4, "valid_children" : ["page","default"] },
			"page" : { "icon" : false, "valid_children" : ["batch"] },
			"batch" : { "icon" : false, "valid_children" : ["default"] },
			"default" : { "icon" : false, "valid_children" : [] }
		},
		"contextmenu" : {
			"items" : function(node) {
				var tmp = $.jstree.defaults.contextmenu.items();
				delete tmp.create;
				delete tmp.ccp;
				delete tmp.remove;
				if(containerId == "useBehaviorList"){
					delete tmp.rename;
				}
				return tmp;
			}
		},
		"plugins" : [ "contextmenu","types","dnd","wholerow" ]
	})
	return $("#"+containerId).jstree(true);
}
