/*
 * jQuery UI Nested Sortable
 * v 2.1a / 2016-02-04
 * https://github.com/ilikenwf/nestedSortable
 *
 * Depends on:
 *	 jquery.ui.sortable.js 1.10+
 *
 * Copyright (c) 2010-2016 Manuele J Sarfatti and contributors
 * Licensed under the MIT License
 * http://www.opensource.org/licenses/mit-license.php
 */
!function(e){"use strict";"function"==typeof define&&define.amd?define(["jquery","jquery-ui/ui/sortable"],e):e(window.jQuery)}(function(v){"use strict";function l(e,t,s){return t<e&&e<t+s}v.widget("mjs.nestedSortable",v.extend({},v.ui.sortable.prototype,{options:{disableParentChange:!1,doNotClear:!1,expandOnHover:700,isAllowed:function(){return!0},isTree:!1,listType:"ol",maxLevels:0,protectRoot:!1,rootID:null,rtl:!1,startCollapsed:!1,tabSize:20,branchClass:"mjs-nestedSortable-branch",collapsedClass:"mjs-nestedSortable-collapsed",disableNestingClass:"mjs-nestedSortable-no-nesting",errorClass:"mjs-nestedSortable-error",expandedClass:"mjs-nestedSortable-expanded",hoveringClass:"mjs-nestedSortable-hovering",leafClass:"mjs-nestedSortable-leaf",disabledClass:"mjs-nestedSortable-disabled"},_create:function(){var i=this;if(this.element.data("ui-sortable",this.element.data("mjs-nestedSortable")),!this.element.is(this.options.listType))throw new Error("nestedSortable: Please check that the listType option is set to your actual list type");this.options.isTree&&this.options.expandOnHover&&(this.options.tolerance="intersect"),v.ui.sortable.prototype._create.apply(this,arguments),this.options.isTree&&v(this.items).each(function(){var e=this.item,t=e.hasClass(i.options.collapsedClass),s=e.hasClass(i.options.expandedClass);e.children(i.options.listType).length?(e.addClass(i.options.branchClass),t||s||(i.options.startCollapsed?e.addClass(i.options.collapsedClass):e.addClass(i.options.expandedClass))):e.addClass(i.options.leafClass)})},_destroy:function(){return this.element.removeData("mjs-nestedSortable").removeData("ui-sortable"),v.ui.sortable.prototype._destroy.apply(this,arguments)},_mouseDrag:function(e){var t,s,i,o,l,r,n,a,h,p,d,c=this,u=this.options,f=!1,m=v(document);for(this.position=this._generatePosition(e),this.positionAbs=this._convertPositionTo("absolute"),this.lastPositionAbs||(this.lastPositionAbs=this.positionAbs),this.options.scroll&&(this.scrollParent[0]!==document&&"HTML"!==this.scrollParent[0].tagName?(this.overflowOffset.top+this.scrollParent[0].offsetHeight-e.pageY<u.scrollSensitivity?(f=this.scrollParent.scrollTop()+u.scrollSpeed,this.scrollParent.scrollTop(f)):e.pageY-this.overflowOffset.top<u.scrollSensitivity&&(f=this.scrollParent.scrollTop()-u.scrollSpeed,this.scrollParent.scrollTop(f)),this.overflowOffset.left+this.scrollParent[0].offsetWidth-e.pageX<u.scrollSensitivity?(f=this.scrollParent.scrollLeft()+u.scrollSpeed,this.scrollParent.scrollLeft(f)):e.pageX-this.overflowOffset.left<u.scrollSensitivity&&(f=this.scrollParent.scrollLeft()-u.scrollSpeed,this.scrollParent.scrollLeft(f))):(e.pageY-m.scrollTop()<u.scrollSensitivity?(f=m.scrollTop()-u.scrollSpeed,m.scrollTop(f)):v(window).height()-(e.pageY-m.scrollTop())<u.scrollSensitivity&&(f=m.scrollTop()+u.scrollSpeed,m.scrollTop(f)),e.pageX-m.scrollLeft()<u.scrollSensitivity?(f=m.scrollLeft()-u.scrollSpeed,m.scrollLeft(f)):v(window).width()-(e.pageX-m.scrollLeft())<u.scrollSensitivity&&(f=m.scrollLeft()+u.scrollSpeed,m.scrollLeft(f))),!1!==f&&v.ui.ddmanager&&!u.dropBehaviour&&v.ui.ddmanager.prepareOffsets(this,e)),this.positionAbs=this._convertPositionTo("absolute"),d=this.placeholder.offset().top,this.options.axis&&"y"===this.options.axis||(this.helper[0].style.left=this.position.left+"px"),this.options.axis&&"x"===this.options.axis||(this.helper[0].style.top=this.position.top+"px"),this.hovering=this.hovering||null,this.mouseentered=this.mouseentered||!1,function(){var e=this.placeholder.parent().parent();e&&e.closest(".ui-sortable").length&&(o=e)}.call(this),m=this._getLevel(this.placeholder),f=this._getChildLevels(this.helper),r=document.createElement(u.listType),this.dragDirection={vertical:this._getDragVerticalDirection(),horizontal:this._getDragHorizontalDirection()},t=this.items.length-1;0<=t;t--)if(s=this.items[t],i=s.item[0],a=this._intersectsWithPointer(s),a&&s.instance===this.currentContainer){if(-1!==i.className.indexOf(u.disabledClass))if(2===a){if((n=this.items[t+1])&&n.item.hasClass(u.disabledClass))continue}else if(1===a&&(l=this.items[t-1])&&l.item.hasClass(u.disabledClass))continue;if(n=1===a?"next":"prev",!(i===this.currentItem[0]||this.placeholder[n]()[0]===i||v.contains(this.placeholder[0],i)||"semi-dynamic"===this.options.type&&v.contains(this.element[0],i))){if(this.mouseentered||(v(i).mouseenter(),this.mouseentered=!0),u.isTree&&v(i).hasClass(u.collapsedClass)&&u.expandOnHover&&(this.hovering||(v(i).addClass(u.hoveringClass),this.hovering=window.setTimeout(function(){v(i).removeClass(u.collapsedClass).addClass(u.expandedClass),c.refreshPositions(),c._trigger("expand",e,[c._uiHash(),i])},u.expandOnHover))),this.direction=1===a?"down":"up","pointer"!==this.options.tolerance&&!this._intersectsWithSides(s))break;v(i).mouseleave(),this.mouseentered=!1,v(i).removeClass(u.hoveringClass),this.hovering&&window.clearTimeout(this.hovering),this.hovering=null,!u.protectRoot||this.currentItem[0].parentNode===this.element[0]&&i.parentNode!==this.element[0]?u.protectRoot||this._rearrange(e,s):this.currentItem[0].parentNode!==this.element[0]&&i.parentNode===this.element[0]?(v(i).children(u.listType).length||(i.appendChild(r),u.isTree&&v(i).removeClass(u.leafClass).addClass(u.branchClass+" "+u.expandedClass)),void 0!==(a=("down"===this.direction?v(i).prev():v(i)).children(u.listType))[0]&&this._rearrange(e,null,a)):this._rearrange(e,s),this._clearEmpty(i),this._trigger("change",e,this._uiHash());break}}if(function(){var e=this.placeholder.prev();h=e.length?e:null}.call(this),null!=h)for(;"li"!==h[0].nodeName.toLowerCase()||-1!==h[0].className.indexOf(u.disabledClass)||h[0]===this.currentItem[0]||h[0]===this.helper[0];){if(!h[0].previousSibling){h=null;break}h=v(h[0].previousSibling)}if(function(){var e=this.placeholder.next();p=e.length?e:null}.call(this),null!=p)for(;"li"!==p[0].nodeName.toLowerCase()||-1!==p[0].className.indexOf(u.disabledClass)||p[0]===this.currentItem[0]||p[0]===this.helper[0];){if(!p[0].nextSibling){p=null;break}p=v(p[0].nextSibling)}return this.beyondMaxLevels=0,null==o||null!=p||u.protectRoot&&o[0].parentNode==this.element[0]||!(u.rtl&&this.positionAbs.left+this.helper.outerWidth()>o.offset().left+o.outerWidth()||!u.rtl&&this.positionAbs.left<o.offset().left)?null==h||h.hasClass(u.disableNestingClass)||!(h.children(u.listType).length&&h.children(u.listType).is(":visible")||!h.children(u.listType).length)||u.protectRoot&&this.currentItem[0].parentNode===this.element[0]||!(u.rtl&&this.positionAbs.left+this.helper.outerWidth()<h.offset().left+h.outerWidth()-u.tabSize||!u.rtl&&this.positionAbs.left>h.offset().left+u.tabSize)?this._isAllowed(o,m,m+f):(this._isAllowed(h,m,m+f+1),h.children(u.listType).length||(h[0].appendChild(r),u.isTree&&h.removeClass(u.leafClass).addClass(u.branchClass+" "+u.expandedClass)),d&&d<=h.offset().top?h.children(u.listType).prepend(this.placeholder):h.children(u.listType)[0].appendChild(this.placeholder[0]),void 0!==o&&this._clearEmpty(o[0]),this._trigger("change",e,this._uiHash())):(o.after(this.placeholder[0]),d=!o.children(u.listItem).children("li:visible:not(.ui-sortable-helper)").length,u.isTree&&d&&o.removeClass(this.options.branchClass+" "+this.options.expandedClass).addClass(this.options.leafClass),void 0!==o&&this._clearEmpty(o[0]),this._trigger("change",e,this._uiHash())),this._contactContainers(e),v.ui.ddmanager&&v.ui.ddmanager.drag(this,e),this._trigger("sort",e,this._uiHash()),this.lastPositionAbs=this.positionAbs,!1},_mouseStop:function(e){this.beyondMaxLevels&&(this.placeholder.removeClass(this.options.errorClass),this.domPosition.prev?v(this.domPosition.prev).after(this.placeholder):v(this.domPosition.parent).prepend(this.placeholder),this._trigger("revert",e,this._uiHash())),v("."+this.options.hoveringClass).mouseleave().removeClass(this.options.hoveringClass),this.mouseentered=!1,this.hovering&&window.clearTimeout(this.hovering),this.hovering=null,this._relocate_event=e,this._pid_current=v(this.domPosition.parent).parent().attr("id"),this._sort_current=this.domPosition.prev?v(this.domPosition.prev).next().index():0,v.ui.sortable.prototype._mouseStop.apply(this,arguments)},_intersectsWithSides:function(e){var t=this.options.isTree?.8:.5,s=l(this.positionAbs.top+this.offset.click.top,e.top+e.height*t,e.height),i=l(this.positionAbs.top+this.offset.click.top,e.top-e.height*t,e.height),o=l(this.positionAbs.left+this.offset.click.left,e.left+e.width/2,e.width),t=this._getDragVerticalDirection(),e=this._getDragHorizontalDirection();return this.floating&&e?"right"===e&&o||"left"===e&&!o:t&&("down"===t&&s||"up"===t&&i)},_contactContainers:function(){this.options.protectRoot&&this.currentItem[0].parentNode===this.element[0]||v.ui.sortable.prototype._contactContainers.apply(this,arguments)},_clear:function(){var e,t;for(v.ui.sortable.prototype._clear.apply(this,arguments),this._pid_current===this._uiHash().item.parent().parent().attr("id")&&this._sort_current===this._uiHash().item.index()||this._trigger("relocate",this._relocate_event,this._uiHash()),e=this.items.length-1;0<=e;e--)t=this.items[e].item[0],this._clearEmpty(t)},serialize:function(e){var s=v.extend({},this.options,e),e=this._getItemsAsjQuery(s&&s.connected),i=[];return v(e).each(function(){var e=(v(s.item||this).attr(s.attribute||"id")||"").match(s.expression||/(.+)[-=_](.+)/),t=(v(s.item||this).parent(s.listType).parent(s.items).attr(s.attribute||"id")||"").match(s.expression||/(.+)[-=_](.+)/);e&&i.push((s.key||e[1])+"["+(s.key&&s.expression?e[1]:e[2])+"]="+(t?s.key&&s.expression?t[1]:t[2]:s.rootID))}),!i.length&&s.key&&i.push(s.key+"="),i.join("&")},toHierarchy:function(e){var l=v.extend({},this.options,e),t=[];return v(this.element).children(l.items).each(function(){var e=function t(e){var s,i=(v(e).attr(l.attribute||"id")||"").match(l.expression||/(.+)[-=_](.+)/);var o=v(e).data();o.nestedSortableItem&&delete o.nestedSortableItem;if(i)return s={id:i[2]},s=v.extend({},s,o),0<v(e).children(l.listType).children(l.items).length&&(s.children=[],v(e).children(l.listType).children(l.items).each(function(){var e=t(this);s.children.push(e)})),s}(this);t.push(e)}),t},toArray:function(e){var n=v.extend({},this.options,e),a=n.startDepthCount||0,h=[],t=1;return n.excludeRoot||(h.push({item_id:n.rootID,parent_id:null,depth:a,left:t,right:2*(v(n.items,this.element).length+1)}),t++),v(this.element).children(n.items).each(function(){t=function e(t,s,i){var o,l=i+1;0<v(t).children(n.listType).children(n.items).length&&(s++,v(t).children(n.listType).children(n.items).each(function(){l=e(v(this),s,l)}),s--);o=(v(t).attr(n.attribute||"id")||"").match(n.expression||/(.+)[-=_](.+)/);r=s===a?n.rootID:(r=v(t).parent(n.listType).parent(n.items).attr(n.attribute||"id").match(n.expression||/(.+)[-=_](.+)/),r[2]);{var r;o&&(t=v(t).children("div").data(),r=v.extend(t,{id:o[2],parent_id:r,depth:s,left:i,right:l}),h.push(r))}i=l+1;return i}(this,a,t)}),h=h.sort(function(e,t){return e.left-t.left})},_clearEmpty:function(e){var t,s,i,o=this.options,l=v(e).children(o.listType),r=l.has("li").length,n=o.doNotClear||r||o.protectRoot&&v(e)[0]===this.element[0];o.isTree&&(t=e,s=o.branchClass,i=o.leafClass,n&&(s=[i,i=s][0]),v(t).removeClass(s).addClass(i)),n||(l.parent().removeClass(o.expandedClass),l.remove())},_getLevel:function(e){var t,s=1;if(this.options.listType)for(t=e.closest(this.options.listType);t&&0<t.length&&!t.is(".ui-sortable");)s++,t=t.parent().closest(this.options.listType);return s},_getChildLevels:function(e,s){var i=this,t=this.options,o=0;return s=s||0,v(e).children(t.listType).children(t.items).each(function(e,t){o=Math.max(i._getChildLevels(t,s+1),o)}),s?o+1:o},_isAllowed:function(e,t,s){var i=this.options,o=this.placeholder.closest(".ui-sortable").nestedSortable("option","maxLevels"),l=this.currentItem.parent().parent();i.disableParentChange&&(void 0!==e&&!l.is(e)||void 0===e&&l.is("li"))||!i.isAllowed(this.placeholder,e,this.currentItem)?(this.placeholder.addClass(i.errorClass),this.beyondMaxLevels=o<s&&0!==o?s-o:1):o<s&&0!==o?(this.placeholder.addClass(i.errorClass),this.beyondMaxLevels=s-o):(this.placeholder.removeClass(i.errorClass),this.beyondMaxLevels=0)}})),v.mjs.nestedSortable.prototype.options=v.extend({},v.ui.sortable.prototype.options,v.mjs.nestedSortable.prototype.options)});