﻿
function cleanTree(inWrapper) {
   //$(inWrapper + "ul").hide();
   //jQuery.tree.reference(inWrapper).destroy();
   //document.getElementById(inWrapper.replace("#", "")).innerHTML = '';
   return false;
}

var nodeChangedID;

function treeCreate(inWrapper, inType, inAppend, inReasonID, inDisplayRootNode, inRootID, inIsMultiTree, inClickable, inDraggable, inCanRecycle, inCanDuplicate, inCanDelete, inNodeChanged, inDragMode) { 

   if (inReasonID == "") {
    inReasonID = "DFC4EDBBDFBA4402923C2A0BCB57FE95";
   }

  nodeChangedID = inNodeChanged; 

  if (inIsMultiTree == "") {
    inIsMultiTree = "none";
  }

	$(function () { 
		$(inWrapper).tree({
			data : { 
				type : "json",
				async : true,
				opts : {
					async : true,
					async_data : function (NODE) { return { id : $(NODE).attr("id") || 0 } },
					method : "GET",
					url : "/parts/treeJSON.aspx?appendID=" + inAppend + "&treeType=" + inType + "&reasonID=" + inReasonID + "&displayRootNode=" + inDisplayRootNode + "&rootid=" + inRootID
				}
			},
			selected	: false,		// FALSE or STRING or ARRAY
			opened		: [],			// ARRAY OF INITIALLY OPENED NODES
			languages	: [],			// ARRAY of string values (which will be used as CSS classes - so they must be valid)
			ui		: {
				dots		: true,		// BOOL - dots or no dots
				animation	: 0,		// INT - duration of open/close animations in miliseconds
				scroll_spd	: 4,
				theme_path	: false,	// Path to the theme CSS file - if set to false and theme_name is not false - will lookup jstree-path-here/themes/theme-name-here/style.css
				theme_name	: "default",// if set to false no theme will be loaded
				selected_parent_close	: "select_parent", // false, "deselect", "select_parent"
				selected_delete			: "select_previous" // false, "select_previous"
			},
			types	: {
				"default" : {
					clickable	: inClickable, // can be function
					renameable	: false, // can be function
					deletable	: false, // can be function
					creatable	: false, // can be function
					draggable	: inDraggable, // can be function
					max_children	: -1, // -1 - not set, 0 - no children, 1 - one child, etc // can be function
					max_depth		: -1, // -1 - not set, 0 - no children, 1 - one level of children, etc // can be function
					valid_children	: "all", // all, none, array of values // can be function
					icon : {
						image : true,
						position : false
					}
				}
			},
			rules	: {
				multiple	: false,	// FALSE | CTRL | ON - multiple selection off/ with or without holding Ctrl
				multitree	: inIsMultiTree,	// all, none, array of tree IDs to accept from
				type_attr	: "rel",	// STRING attribute name (where is the type stored as string)
				createat	: "bottom",	// STRING (top or bottom) new nodes get inserted at top or bottom
				drag_copy	: inDragMode,	// FALSE | CTRL | ON - drag to copy off/ with or without holding Ctrl
				drag_button	: "left",	// left, right or both
				use_max_children	: true,
				use_max_depth		: true,

				max_children: -1,
				max_depth	: -1,
				valid_children : "all"
			},
			lang : {
				new_node	: "New folder",
				loading		: "Loading ..."
			},
			callback	: {
//				beforechange: function(NODE,TREE_OBJ) { return true },
//				beforeopen	: function(NODE,TREE_OBJ) { return true },
//				beforeclose	: function(NODE,TREE_OBJ) { return true },
//				beforemove	: function(NODE,REF_NODE,TYPE,TREE_OBJ) { return true }, 
//				beforecreate: function(NODE,REF_NODE,TYPE,TREE_OBJ) { return true }, 
//				beforerename: function(NODE,LANG,TREE_OBJ) { return true }, 
//				beforedelete: function(NODE,TREE_OBJ) { return true }, 
//				beforedata	: function(NODE,TREE_OBJ) { return { id : $(NODE).attr("id") || 0 } }, // PARAMETERS PASSED TO SERVER
//				ondata		: function(DATA,TREE_OBJ) { return DATA; },		// modify data before parsing it
//				onparse		: function(STR,TREE_OBJ) { return STR; },		// modify string before visualizing it
//				onhover		: function(NODE,TREE_OBJ) { },				// node hovered
//				onselect	: function(NODE,TREE_OBJ) { },			  // node selected
//				ondeselect	: function(NODE,TREE_OBJ) { },			// node deselected
				onchange	: function(NODE,TREE_OBJ) {
				  treeActions(inType, $(NODE).attr("id"), NODE);
				},					// focus changed
				onrename	: function(NODE,TREE_OBJ,RB) { },				// node renamed
				onmove		: function(NODE,REF_NODE,TYPE,TREE_OBJ,RB) {
				 var nodeMovedID;
         var nodeMovedToID;
         var prevNodeID;
         var nextNodeID;
         var reasonTypeID;
         var modulrGroupPointerID; 
         var isRecursive;

         nodeMovedID = $(NODE).attr("id")
         nodeMovedToID = $(NODE).parents("li").attr("id")
         prevNodeID = $(NODE).prev().attr("id");
         nextNodeID = $(NODE).next().attr("id");
         reasonTypeID = $("#reasonSelect").val();
         moduleGroupPointerID = $("#moduleGroupSelect").val();
         isRecursive = $("#isRecursive").val();
         processTree(nodeMovedID, nodeMovedToID, prevNodeID, nextNodeID, reasonTypeID, isRecursive, "isMove", inType, moduleGroupPointerID);
				},	// move completed
				oncopy		: function(NODE,REF_NODE,TYPE,TREE_OBJ,RB) {
				
				 var nodeMovedID;
         var nodeMovedToID;
         var prevNodeID;
         var nextNodeID;
         var reasonTypeID;
         var modulrGroupPointerID; 
         var isRecursive;

         nodeMovedID = $(NODE).attr("id")
         nodeMovedToID = $(NODE).parents("li").attr("id")
         prevNodeID = $(NODE).prev().attr("id");
         nextNodeID = $(NODE).next().attr("id");
         reasonTypeID = $("#reasonSelect").val();
         moduleGroupPointerID = $("#moduleGroupSelect").val();
         isRecursive = $("#isRecursive").val();
         
         processTree(nodeMovedID, nodeMovedToID, prevNodeID, nextNodeID, reasonTypeID, isRecursive, "isCopy", inType, moduleGroupPointerID);        
				}//,	// copy completed
//				oncreate	: function(NODE,REF_NODE,TYPE,TREE_OBJ,RB) { },	// node created
//				ondelete	: function(NODE,TREE_OBJ,RB) { },				// node deleted
//				onopen		: function(NODE,TREE_OBJ) { },					// node opened
//				onopen_all	: function(TREE_OBJ) { },						// all nodes opened
//				onclose_all	: function(TREE_OBJ) { },						// all nodes closed
//				onclose		: function(NODE,TREE_OBJ) { },					// node closed
//				error		: function(TEXT,TREE_OBJ) { },					// error occured
//				ondblclk	: function(NODE,TREE_OBJ) { TREE_OBJ.toggle_branch.call(TREE_OBJ, NODE); TREE_OBJ.select_branch.call(TREE_OBJ, NODE); },
//				onrgtclk	: function(NODE,TREE_OBJ,EV) { },				// right click - to prevent use: EV.preventDefault(); EV.stopPropagation(); return false
//				onload		: function(TREE_OBJ) { },
//				oninit		: function(TREE_OBJ) { },
//				onfocus		: function(TREE_OBJ) { },
//				ondestroy	: function(TREE_OBJ) { },
//				onsearch	: function(NODES, TREE_OBJ) { NODES.addClass("search"); },
//				ondrop		: function(NODE,REF_NODE,TYPE,TREE_OBJ) { },
//				check		: function(RULE,NODE,VALUE,TREE_OBJ) { return VALUE; },
//				check_move	: function(NODE,REF_NODE,TYPE,TREE_OBJ) { return true; }
			},
		  plugins : { 
			  contextmenu : {
          items : {
            remove : false,
            create : false,
            rename : false,
            my_act : {
						  label	: "recycle", 
						  icon	: "/assets/images/common/delete_16.png",
						  visible	: function (NODE, TREE_OBJ) { 
						    if (NODE.attr("rel").indexOf("rec") == -1) {
						      return -1;
						    }
						    if (inCanRecycle) {
						      return 1;
						    }
						    else {
						      return -1;
						    }
							}, 
						  action	: function (NODE, TREE_OBJ) { 
							  remoteRecycle($(NODE).attr("id"));
						  }
            }
          }
			  }
		  }
		});
	});
}


function treeActions(treeType, inNODEid, NODE) {
    switch (treeType)
    {
        case "assetPickerSource":
            AjaxLoad('#ajaxAssetPicker', '/parts/ajaxAssetPicker.aspx?isDialogue=true&valueOveride=' + $("#temp").val() + '&lid=' + inNODEid + '&formRowPointerID=' + nodeChangedID,'','updating asset','','');
            return true;
        case "copyElement":
            AjaxLoad('#copyElementCheck','/parts/ajaxCopyElement.aspx?isDialogue=true&lid=' + inNODEid,'','selecting element','','');
            return true;
        case "assetLocation":
            AjaxLoad('#copyAssetCheck','/parts/ajaxCopyAsset.aspx?isDialogue=true&lid=' + inNODEid,'','selecting asset','','');
            return true;
        case "editorInsertLink":
            var passURL = "";
            switch(whichLinkDialog) {
              
              case "docs":
                passURL = "/file.axd?pointerID=" + inNODEid.replace("Lnk_", "");
                break;
              case "page":
                passURL = "/page.aspx?pointerID=" + inNODEid.replace("Lnk_", "");
                break;
            }
            useLink($(NODE).attr("alt"), passURL);
            //AjaxLoad('#editorLinkDetails','/parts/editorLinkDetails.aspx?isDialogue=true&lnkid=' + inNODEid,'','selecting asset','','');
            return true;
        case "userTree":
            AjaxLoad('#blockAjaxID_Permissions','/parts/ajaxPermissions.aspx?lnkid=' + inNODEid + '&permView=' + $("input[name='permView']:checked").val(),'','reading permissions','','');
            return true;
        default:
            Tr(inNODEid);
            return true;
    }
    return false;
}
