/*
 jQuery UI Nested Sortable 1.2.1

 Copyright 2010, Manuele J Sarfatti

 http://mjsarfatti.com/sandbox/nestedSortable

 Depends:
 jquery.ui.core.js 1.8+
 jquery.ui.widget.js 1.8+
 jquery.ui.sortable.js 1.8+
*/
(function(a){a.widget("ui.nestedSortable",a.extend({},a.ui.sortable.prototype,{options:{tabSize:20,disableNesting:"ui-nestedSortable-no-nesting",errorClass:"ui-nestedSortable-error",listType:"ol"},_create:function(){this.element.data("sortable",this.element.data("sortableTree"));return a.ui.sortable.prototype._create.apply(this,arguments)},_mouseDrag:function(b){this.position=this._generatePosition(b);this.positionAbs=this._convertPositionTo("absolute");if(!this.lastPositionAbs)this.lastPositionAbs=
this.positionAbs;if(this.options.scroll){var c=this.options,d=!1;if(this.scrollParent[0]!=document&&this.scrollParent[0].tagName!="HTML"){if(this.overflowOffset.top+this.scrollParent[0].offsetHeight-b.pageY<c.scrollSensitivity)this.scrollParent[0].scrollTop=d=this.scrollParent[0].scrollTop+c.scrollSpeed;else if(b.pageY-this.overflowOffset.top<c.scrollSensitivity)this.scrollParent[0].scrollTop=d=this.scrollParent[0].scrollTop-c.scrollSpeed;if(this.overflowOffset.left+this.scrollParent[0].offsetWidth-
b.pageX<c.scrollSensitivity)this.scrollParent[0].scrollLeft=d=this.scrollParent[0].scrollLeft+c.scrollSpeed;else if(b.pageX-this.overflowOffset.left<c.scrollSensitivity)this.scrollParent[0].scrollLeft=d=this.scrollParent[0].scrollLeft-c.scrollSpeed}else b.pageY-a(document).scrollTop()<c.scrollSensitivity?d=a(document).scrollTop(a(document).scrollTop()-c.scrollSpeed):a(window).height()-(b.pageY-a(document).scrollTop())<c.scrollSensitivity&&(d=a(document).scrollTop(a(document).scrollTop()+c.scrollSpeed)),
b.pageX-a(document).scrollLeft()<c.scrollSensitivity?d=a(document).scrollLeft(a(document).scrollLeft()-c.scrollSpeed):a(window).width()-(b.pageX-a(document).scrollLeft())<c.scrollSensitivity&&(d=a(document).scrollLeft(a(document).scrollLeft()+c.scrollSpeed));d!==!1&&a.ui.ddmanager&&!c.dropBehaviour&&a.ui.ddmanager.prepareOffsets(this,b)}this.positionAbs=this._convertPositionTo("absolute");if(!this.options.axis||this.options.axis!="y")this.helper[0].style.left=this.position.left+"px";if(!this.options.axis||
this.options.axis!="x")this.helper[0].style.top=this.position.top+"px";for(d=this.items.length-1;d>=0;d--){var f=this.items[d],e=f.item[0],h=this._intersectsWithPointer(f);if(h&&e!=this.currentItem[0]&&this.placeholder[h==1?"next":"prev"]()[0]!=e&&!a.ui.contains(this.placeholder[0],e)&&(this.options.type=="semi-dynamic"?!a.ui.contains(this.element[0],e):1)){this.direction=h==1?"down":"up";if(this.options.tolerance=="pointer"||this._intersectsWithSides(f))this._rearrange(b,f);else break;this._clearEmpty(e);
this._trigger("change",b,this._uiHash());break}}for(itemBefore=this.placeholder[0].previousSibling;itemBefore!=null;)if(itemBefore.nodeType==1&&itemBefore!=this.currentItem[0])break;else itemBefore=itemBefore.previousSibling;parentItem=this.placeholder[0].parentNode.parentNode;newList=document.createElement(c.listType);parentItem!=null&&parentItem.nodeName=="LI"&&this.positionAbs.left<a(parentItem).offset().left?(a(parentItem).after(this.placeholder[0]),this._clearEmpty(parentItem)):itemBefore!=null&&
itemBefore.nodeName=="LI"&&this.positionAbs.left>a(itemBefore).offset().left+this.options.tabSize?a(itemBefore).hasClass(this.options.disableNesting)?a(this.placeholder[0]).addClass(this.options.errorClass).css("marginLeft",this.options.tabSize):(a(this.placeholder[0]).hasClass(this.options.errorClass)&&a(this.placeholder[0]).css("marginLeft",0).removeClass(this.options.errorClass),itemBefore.children[1]==null&&itemBefore.appendChild(newList),itemBefore.children[1].appendChild(this.placeholder[0])):
itemBefore!=null?(a(this.placeholder[0]).hasClass(this.options.errorClass)&&a(this.placeholder[0]).css("marginLeft",0).removeClass(this.options.errorClass),a(itemBefore).after(this.placeholder[0])):a(this.placeholder[0]).hasClass(this.options.errorClass)&&a(this.placeholder[0]).css("marginLeft",0).removeClass(this.options.errorClass);this._contactContainers(b);a.ui.ddmanager&&a.ui.ddmanager.drag(this,b);this._trigger("sort",b,this._uiHash());this.lastPositionAbs=this.positionAbs;return!1},serialize:function(b){var c=
this._getItemsAsjQuery(b&&b.connected),d=[],b=b||{};a(c).each(function(){var c=(a(b.item||this).attr(b.attribute||"id")||"").match(b.expression||/(.+)[-=_](.+)/),e=(a(b.item||this).parent(b.listType).parent("li").attr(b.attribute||"id")||"").match(b.expression||/(.+)[-=_](.+)/);c&&d.push((b.key||c[1]+"["+(b.key&&b.expression?c[1]:c[2])+"]")+"="+(e?b.key&&b.expression?e[1]:e[2]:"root"))});!d.length&&b.key&&d.push(b.key+"=");return d.join("&")},toArray:function(b){function c(e,g,i){right=i+1;a(e).children(b.listType).children("li").length>
0&&(g++,a(e).children(b.listType).children("li").each(function(){right=c(a(this),g,right)}),g--);id=a(e).attr("id").match(b.expression||/(.+)[-=_](.+)/);g===d+1?pid="root":(parentItem=a(e).parent(b.listType).parent("li").attr("id").match(b.expression||/(.+)[-=_](.+)/),pid=parentItem[2]);f.push({item_id:id[2],parent_id:pid,depth:g,left:i,right:right});return i=right+1}var b=b||{},d=b.startDepthCount||0,f=[],e=2;f.push({item_id:"root",parent_id:"none",depth:d,left:"1",right:(a("li",this.element).length+
1)*2});a(this.element).children("li").each(function(){e=c(a(this),d+1,e)});return f},_createPlaceholder:function(b){var c=b||this,d=c.options;if(!d.placeholder||d.placeholder.constructor==String){var f=d.placeholder;d.placeholder={element:function(){var b=a(document.createElement(c.currentItem[0].nodeName)).addClass(f||c.currentItem[0].className+" ui-sortable-placeholder").removeClass("ui-sortable-helper")[0];if(!f)b.style.visibility="hidden";return b},update:function(b,a){if(!f||d.forcePlaceholderSize)(!a.height()||
a.css("height")=="auto")&&a.height(c.currentItem.height()),a.width()||a.width(c.currentItem.width())}}}c.placeholder=a(d.placeholder.element.call(c.element,c.currentItem));c.currentItem.after(c.placeholder);d.placeholder.update(c,c.placeholder)},_clear:function(){a.ui.sortable.prototype._clear.apply(this,arguments);for(var b=this.items.length-1;b>=0;b--)this._clearEmpty(this.items[b].item[0]);return!0},_clearEmpty:function(a){a.children[1]&&a.children[1].children.length==0&&a.removeChild(a.children[1])}}));
a.ui.nestedSortable.prototype.options=a.extend({},a.ui.sortable.prototype.options,a.ui.nestedSortable.prototype.options)})(jQuery);
