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

