(function(A){function B(G,F){var H=this;var E=A.extend({dragAndDrop:false},F);this.selectedNode=null;G.addClass("tree");var D=G.children("ul");if(D.length==0){D=A.createElement("ul");G.append(D)}var C=function(){D.find("li").each(function(){H.childrenChanged(A(this))});G.removeClass("drag-over")};if(E.dragAndDrop){G.droppable({accept:"li",hoverClass:"drag-over",tolerance:"pointer",drop:function(J,K){var I=A(K.draggable);D.append(I);C()}})}this.callback=function(I,J){if(typeof I==="string"){I=E[I]}if(I){return I.apply(H,J)}else{return undefined}};this.setupList=function(I){I.children().each(function(){H.setupNode(A(this))})};this.setupNode=function(J){J.click(function(){H.toggleNode(J);return false});J.children("a").click(function(){H.selectNode(J);if(this.href){window.location=this.href}return false}).dblclick(function(){H.callback("dblclick",[J])});if(E.dragAndDrop){J.draggable({helper:"clone",revert:"invalid",revertDuration:250,opacity:0.8,zIndex:5,delay:50,start:function(){H.selectNode(J);J.find("li").andSelf().droppable("disable");J.css("opacity",0.5)},stop:function(){if(H.autoscrollInterval){window.clearInterval(H.autoscrollInterval);H.autoscrollInterval=undefined}J.find("li").andSelf().droppable("enable");J.css("opacity","")},scroll:true});J.droppable({accept:"li",hoverClass:"drag-over",tolerance:"pointer",greedy:true,drop:function(L,M){var K=A(M.draggable);J.children("ul").append(K);C();H.setExpanded(J);window.setTimeout(function(){H.scrollTo(K)},0)}})}var I=J.children("ul");if(I.length==0){I=A.createElement("ul");J.append(I)}I.hide();H.setupList(I);if(I.children().length==0){J.addClass("leaf").removeClass("expanded").removeClass("collapsed")}else{if(J.hasClass("expanded")){I.show()}else{J.addClass("collapsed");I.hide()}}};this.addNode=function(L,I,J){var K=A.createElement("li #"+L,[A.createElement("a",I)]);if(J){J.children("ul").append(K);H.childrenChanged(J)}else{D.append(K)}H.setupNode(K);return K};this.removeNode=function(J){var I=H.parentOf(J);J.remove();if(I!==null){H.childrenChanged(I)}};this.childrenChanged=function(K){var J=K.children("ul");var I=J.children().length;if(I==0){K.removeClass("expanded").removeClass("collapsed").addClass("leaf");J.hide()}else{if(H.isLeaf(K)){K.removeClass("leaf").addClass("collapsed");J.hide()}}};this.toggleNode=function(I){if(H.isExpanded(I)){H.setCollapsed(I)}else{if(H.isCollapsed(I)){H.setExpanded(I)}}};this.isExpanded=function(I){return I.hasClass("expanded")};this.isCollapsed=function(I){return I.hasClass("collapsed")};this.isLeaf=function(I){return I.hasClass("leaf")};this.setExpanded=function(J,I){if(H.isCollapsed(J)){H.callback("willExpand",[J]);J.removeClass("collapsed").addClass("expanded");if(I&&I.animate===false){J.children("ul").show()}else{J.children("ul").slideDown("fast")}H.callback("didExpand",[J])}};this.setCollapsed=function(I){if(H.isExpanded(I)){H.callback("willCollapse",[I]);I.removeClass("expanded").addClass("collapsed");I.children("ul").slideUp("fast");H.callback("didCollapse",[I])}};this.selectNode=function(L,K){var J=H.selectedNode;var I=L;if(J!==I){H.callback("selectionWillChange",[J,I]);if(J){J.removeClass("selected")}if(I&&I.length>0){H.selectedNode=I;I.addClass("selected");H.expandPathTo(I,K);H.scrollTo(I)}H.callback("selectionDidChange",[J,I])}};this.expandPathTo=function(K,I){var J=H.parentOf(K);if(J!==null){H.expandPathTo(J,I);H.setExpanded(J,I)}};this.isPathExpandedTo=function(J){var I=H.parentOf(J);if(I==null){return true}else{return H.isExpanded(I)&&H.isPathExpandedTo(I)}};this.scrollTo=function(N){if(E.scrollable&&H.isPathExpandedTo(N)){var M=N.position();var O=N.children("a");var P=O.outerWidth();var I=O.outerHeight();var J=E.scrollable.scrollTop();var K=E.scrollable.scrollLeft();var Q=E.scrollable.height();var L=E.scrollable.width();if(M.top<J){E.scrollable.scrollTop(M.top)}else{if(M.top+I>J+Q){E.scrollable.scrollTop(M.top+I-Q)}}if(M.left<K){E.scrollable.scrollLeft(M.left)}else{if(M.left+P>K+L){E.scrollable.scrollLeft(M.left+P-L)}}}};this.isVisible=function(N){if(!H.isPathExpandedTo(N)){return false}else{if(E.scrollable){return true}else{var I=N.position();var L=E.scrollable.scrollTop();var M=E.scrollable.scrollLeft();var K=E.scrollable.height();var J=E.scrollable.width();return I.top>=L&&I.top<L+K&&I.left>=M&&I.left<M+J}}};this.parentOf=function(J){var I=J.parent().parent();if(I.is("li")){return I}else{return null}};this.childrenOf=function(I){return I.children("ul").children("li")};this.rootNodes=function(){return D.children("li")};this.selectionPath=function(){function I(K){var J=H.parentOf(K);if(J===null){return[K]}else{var L=I(J);L.push(K);return L}}if(H.selectedNode){return I(H.selectedNode)}else{return[]}};this.nodeLabel=function(I){if(arguments.length==2){return I.children("a").html(arguments[1])}else{return I.children("a").html()}};this.nodeID=function(I){if(arguments.length==2){return I.attr("id",arguments[1])}else{return I.attr("id")}};this.badge=function(K){if(arguments.length==2){var I=K.children("span.badge");if(I.length==0){var I=A.createElement("span .badge");K.prepend(I)}var J=arguments[1];I.html(J);if(J.length==0){I.hide()}else{I.show()}}else{return K.children("span.badge").html()}};this.setupList(D);window.setTimeout(function(){D.hide().show()},10)}A.fn.makeTree=function(C){return new B(this,C)}})(jQuery);