
jQuery.iNestedSortable={checkHover:function(e,o){if(e.isNestedSortable){jQuery.iNestedSortable.scroll(e);return jQuery.iNestedSortable.newCheckHover(e);}else{return jQuery.iNestedSortable.oldCheckHover(e,o);}},oldCheckHover:jQuery.iSort.checkhover,newCheckHover:function(e){if(!jQuery.iDrag.dragged){return;}
if(!(e.dropCfg.el.size()>0)){return;}
if(!e.nestedSortCfg.remeasured){jQuery.iSort.measure(e);e.nestedSortCfg.remeasured=true;}
var precItem=jQuery.iNestedSortable.findPrecedingItem(e);var shouldNest=jQuery.iNestedSortable.shouldNestItem(e,precItem);var touchingFirst=(!precItem)?jQuery.iNestedSortable.isTouchingFirstItem(e):false;var quit=false;if(precItem){if(e.nestedSortCfg.lastPrecedingItem===precItem&&e.nestedSortCfg.lastShouldNest===shouldNest){quit=true;}}
else if(e.nestedSortCfg.lastPrecedingItem===precItem&&e.nestedSortCfg.lastTouchingFirst===touchingFirst){quit=true;}
e.nestedSortCfg.lastPrecedingItem=precItem;e.nestedSortCfg.lastShouldNest=shouldNest;e.nestedSortCfg.lastTouchingFirst=touchingFirst;if(quit){return;}
if(precItem!==null){if(shouldNest){jQuery.iNestedSortable.nestItem(e,precItem);}else{jQuery.iNestedSortable.appendItem(e,precItem);}}else if(touchingFirst){jQuery.iNestedSortable.insertOnTop(e);}},scroll:function(e){if(!e.nestedSortCfg.autoScroll){return false;}
var sensitivity=e.nestedSortCfg.scrollSensitivity;var speed=e.nestedSortCfg.scrollSpeed;var pointer=jQuery.iDrag.dragged.dragCfg.currentPointer;var docDim=jQuery.iUtil.getScroll();if((pointer.y-docDim.ih)-docDim.t>-sensitivity){window.scrollBy(0,speed);}
if(pointer.y-docDim.t<sensitivity){window.scrollBy(0,-speed);}},check:function(dragged){jQuery.iNestedSortable.newCheck(dragged);return jQuery.iNestedSortable.oldCheck(dragged);},oldCheck:jQuery.iSort.check,newCheck:function(dragged){if(jQuery.iNestedSortable.latestNestingClass&&jQuery.iNestedSortable.currentNesting)
{jQuery.iNestedSortable.currentNesting.removeClass(jQuery.iNestedSortable.latestNestingClass);jQuery.iNestedSortable.currentNesting=null;jQuery.iNestedSortable.latestNestingClass="";}
if(jQuery.iDrop.overzone.isNestedSortable){jQuery.iDrop.overzone.nestedSortCfg.remeasured=false;}},serialize:function(s){if(jQuery('#'+s).get(0).isNestedSortable){return jQuery.iNestedSortable.newSerialize(s);}else{return jQuery.iNestedSortable.oldSerialize(s);}},oldSerialize:jQuery.iSort.serialize,newSerialize:function(s){var i;var h='';var currentPath='';var o={};var e;var buildHierarchySer=function(context){var retVal=[];thisChildren=jQuery(context).children('.'+jQuery.iSort.collected[s]);thisChildren.each(function(i){var serId=jQuery.attr(this,'id');if(serId&&serId.match){serId=serId.match(e.nestedSortCfg.serializeRegExp)[0];}
if(h.length>0){h+='&';}
h+=s+currentPath+'['+i+'][id]='+serId;retVal[i]={id:serId};var newContext=jQuery(this).children(e.nestedSortCfg.nestingTag+"."+e.nestedSortCfg.nestingTagClass.split(" ").join(".")).get(0);var oldPath=currentPath;currentPath+='['+i+'][children]';var thisChildren=buildHierarchySer(newContext);if(thisChildren.length>0){retVal[i].children=thisChildren;}
currentPath=oldPath;});return retVal;};if(s){if(jQuery.iSort.collected[s]){e=jQuery('#'+s).get(0);o[s]=buildHierarchySer(e);}else{for(a in s){if(jQuery.iSort.collected[s[a]]){e=jQuery('#'+s[a]).get(0);o[s[a]]=buildHierarchySer(e);}}}}else{for(i in jQuery.iSort.collected){e=jQuery('#'+i).get(0);o[i]=buildHierarchySer(e);}}
return{hash:h,o:o};},findPrecedingItem:function(e){var largestY=0;var preceding=jQuery.grep(e.dropCfg.el,function(i){var isOnTop=(i.pos.y<jQuery.iDrag.dragged.dragCfg.ny)&&(i.pos.y>largestY);if(!isOnTop){return false;}
var isSameLevel;if(e.nestedSortCfg.rightToLeft){isSameLevel=(i.pos.x+i.pos.wb+e.nestedSortCfg.snapTolerance>jQuery.iDrag.dragged.dragCfg.nx+jQuery.iDrag.dragged.dragCfg.oC.wb);}else{isSameLevel=(i.pos.x-e.nestedSortCfg.snapTolerance<jQuery.iDrag.dragged.dragCfg.nx);}
if(!isSameLevel){return false;}
var isBeingDragged=jQuery.iNestedSortable.isBeingDragged(e,i);if(isBeingDragged){return false;}
largestY=i.pos.y;return true;});if(preceding.length>0){return preceding[(preceding.length-1)];}else{return null;}},isTouchingFirstItem:function(e){var lowestY;var firstItem=jQuery.grep(e.dropCfg.el,function(i){var isBefore=(lowestY===undefined||i.pos.y<lowestY);if(!isBefore){return false;}
var isBeingDragged=jQuery.iNestedSortable.isBeingDragged(e,i);if(isBeingDragged){return false;}
lowestY=i.pos.y;return true;});if(firstItem.length>0){firstItem=firstItem[(firstItem.length-1)];return firstItem.pos.y<jQuery.iDrag.dragged.dragCfg.ny+jQuery.iDrag.dragged.dragCfg.oC.hb&&firstItem.pos.y>jQuery.iDrag.dragged.dragCfg.ny;}else{return false;}},isBeingDragged:function(e,elem){var dragged=jQuery.iDrag.dragged;if(!dragged){return false;}
if(elem==dragged){return true;}
if(jQuery(elem).parents("."+e.sortCfg.accept.split(" ").join(".")).filter(function(){return this==dragged;}).length!==0){return true;}else{return false;}},shouldNestItem:function(e,precedingItem){if(!precedingItem){return false;}
if(e.nestedSortCfg.noNestingClass&&jQuery(precedingItem).filter("."+e.nestedSortCfg.noNestingClass).get(0)===precedingItem)
{return false;}
if(e.nestedSortCfg.rightToLeft){return precedingItem.pos.x+precedingItem.pos.wb-(e.nestedSortCfg.nestingPxSpace-e.nestedSortCfg.snapTolerance)>jQuery.iDrag.dragged.dragCfg.nx+jQuery.iDrag.dragged.dragCfg.oC.wb;}else{return precedingItem.pos.x+(e.nestedSortCfg.nestingPxSpace-e.nestedSortCfg.snapTolerance)<jQuery.iDrag.dragged.dragCfg.nx;}},nestItem:function(e,parent){var parentNesting=jQuery(parent).children(e.nestedSortCfg.nestingTag+"."+e.nestedSortCfg.nestingTagClass.split(" ").join("."));var helper=jQuery.iSort.helper;styleHelper=helper.get(0).style;styleHelper.width='auto';if(!parentNesting.size()){var newUl="<"+e.nestedSortCfg.nestingTag+" class='"+e.nestedSortCfg.nestingTagClass+"'></"+e.nestedSortCfg.nestingTag+">";parentNesting=jQuery(parent).append(newUl).children(e.nestedSortCfg.nestingTag).css(e.nestedSortCfg.styleToAttach);}
jQuery.iNestedSortable.updateCurrentNestingClass(e,parentNesting);jQuery.iNestedSortable.beforeHelperRemove(e);parentNesting.prepend(helper.get(0));jQuery.iNestedSortable.afterHelperInsert(e);},appendItem:function(e,itemBefore){jQuery.iNestedSortable.updateCurrentNestingClass(e,jQuery(itemBefore).parent());jQuery.iNestedSortable.beforeHelperRemove(e);jQuery(itemBefore).after(jQuery.iSort.helper.get(0));jQuery.iNestedSortable.afterHelperInsert(e);},insertOnTop:function(e){jQuery.iNestedSortable.updateCurrentNestingClass(e,e);jQuery.iNestedSortable.beforeHelperRemove(e);jQuery(e).prepend(jQuery.iSort.helper.get(0));jQuery.iNestedSortable.afterHelperInsert(e);},beforeHelperRemove:function(e){var parent=jQuery.iSort.helper.parent(e.nestedSortCfg.nestingTag+"."+e.nestedSortCfg.nestingTagClass.split(" ").join("."));var numSiblings=parent.children("."+e.sortCfg.accept.split(" ").join(".")+":visible").size();if(numSiblings===0&&parent.get(0)!==e){parent.hide();}},afterHelperInsert:function(e){var parent=jQuery.iSort.helper.parent();if(parent.get(0)!==e){parent.show();}
e.nestedSortCfg.remeasured=false;},updateCurrentNestingClass:function(e,nestingElem){var nesting=jQuery(nestingElem);if((e.nestedSortCfg.currentNestingClass)&&(!jQuery.iNestedSortable.currentNesting||nesting.get(0)!=jQuery.iNestedSortable.currentNesting.get(0))){if(jQuery.iNestedSortable.currentNesting){jQuery.iNestedSortable.currentNesting.removeClass(e.nestedSortCfg.currentNestingClass);}
if(nesting.get(0)!=e){jQuery.iNestedSortable.currentNesting=nesting;nesting.addClass(e.nestedSortCfg.currentNestingClass);jQuery.iNestedSortable.latestNestingClass=e.nestedSortCfg.currentNestingClass;}else{jQuery.iNestedSortable.currentNesting=null;jQuery.iNestedSortable.latestNestingClass="";}}},destroy:function(){return this.each(function(){if(this.isNestedSortable){this.nestedSortCfg=null;this.isNestedSortable=null;jQuery(this).SortableDestroy();}});},build:function(conf){if(conf.accept&&jQuery.iUtil&&jQuery.iDrag&&jQuery.iDrop&&jQuery.iSort)
{this.each(function(){this.isNestedSortable=true;this.nestedSortCfg={noNestingClass:conf.noNestingClass?conf.noNestingClass:false,rightToLeft:conf.rightToLeft?true:false,nestingPxSpace:parseInt(conf.nestingPxSpace,10)||30,currentNestingClass:conf.currentNestingClass?conf.currentNestingClass:"",nestingLimit:conf.nestingLimit?conf.nestingLimit:false,autoScroll:conf.autoScroll!==undefined?conf.autoScroll==true:true,scrollSensitivity:conf.scrollSensitivity?conf.scrollSensitivity:20,scrollSpeed:conf.scrollSpeed?conf.scrollSpeed:20,serializeRegExp:conf.serializeRegExp?conf.serializeRegExp:/[^\-]*$/};this.nestedSortCfg.snapTolerance=parseInt(this.nestedSortCfg.nestingPxSpace*0.4,10);this.nestedSortCfg.nestingTag=this.tagName;this.nestedSortCfg.nestingTagClass=this.className;this.nestedSortCfg.styleToAttach=(this.nestedSortCfg.rightToLeft)?{"padding-left":0,"padding-right":this.nestedSortCfg.nestingPxSpace+'px'}:{"padding-left":this.nestedSortCfg.nestingPxSpace+'px',"padding-right":0};jQuery(this.nestedSortCfg.nestingTag,this).css(this.nestedSortCfg.styleToAttach);});jQuery.iSort.checkhover=jQuery.iNestedSortable.checkHover;jQuery.iSort.check=jQuery.iNestedSortable.check;jQuery.iSort.serialize=jQuery.iNestedSortable.serialize;}
return this.Sortable(conf);}};jQuery.fn.extend({NestedSortable:jQuery.iNestedSortable.build,NestedSortableDestroy:jQuery.iNestedSortable.destroy});jQuery.iUtil.getScroll=function(e)
{var t,l,w,h,iw,ih;if(e&&e.nodeName.toLowerCase()!='body'){t=e.scrollTop;l=e.scrollLeft;w=e.scrollWidth;h=e.scrollHeight;iw=0;ih=0;}else{if(document.documentElement&&document.documentElement.scrollTop){t=document.documentElement.scrollTop;l=document.documentElement.scrollLeft;w=document.documentElement.scrollWidth;h=document.documentElement.scrollHeight;}else if(document.body){t=document.body.scrollTop;l=document.body.scrollLeft;w=document.body.scrollWidth;h=document.body.scrollHeight;}
iw=self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth||0;ih=self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight||0;}
return{t:t,l:l,w:w,h:h,iw:iw,ih:ih};};
