text: !function(e){e.fn.dragsort=function(t){if("destroy"!=t){var r=e.extend({},e.fn.dragsort.defaults,t),o=[],a=null,i=null;return this.each(function(t,n){e(n).is("table")&&1==e(n).children().length&&e(n).children().is("tbody")&&(n=e(n).children().get(0));var d={draggedItem:null,placeHolderItem:null,pos:null,offset:null,offsetLimit:null,scroll:null,container:n,init:function(){r.tagName=0==e(this.container).children().length?"li":e(this.container).children().get(0).tagName.toLowerCase(),r.itemSelector||(r.itemSelector=r.tagName),r.dragSelector||(r.dragSelector=r.tagName),r.placeHolderTemplate||(r.placeHolderTemplate="<"+r.tagName+">&nbsp;</"+r.tagName+">"),e(this.container).attr("data-listidx",t).bind("mousedown touchstart",this.grabItem).bind("dragsort-uninit",this.uninit),this.styleDragHandlers(!0)},uninit:function(){var t=o[e(this).attr("data-listidx")];e(t.container).unbind("mousedown touchstart",t.grabItem).unbind("dragsort-uninit"),t.styleDragHandlers(!1)},getItems:function(){return e(this.container).children(r.itemSelector)},styleDragHandlers:function(t){this.getItems().map(function(){return e(this).is(r.dragSelector)?this:e(this).find(r.dragSelector).get()}).css("cursor",t?r.cursor:"default")},grabItem:function(t){var a=o[e(this).attr("data-listidx")],i=e(t.target).closest("[data-listidx] > "+r.tagName).get(0),n=a.getItems().filter(function(){return this==i}).length>0;if(!(1!=t.which&&0!=t.which||e(t.target).is(r.dragSelectorExclude)||e(t.target).closest(r.dragSelectorExclude).length>0)&&n){t.preventDefault();for(var d=t.target;!e(d).is(r.dragSelector);){if(d==this)return;d=d.parentNode}e(d).attr("data-cursor",e(d).css("cursor")),e(d).css("cursor","move");var l=this,s=function(){a.dragStart.call(l,t),e(a.container).unbind("mousemove touchmove",s)};e(a.container).bind("mousemove touchmove",s).bind("mouseup touchend",function(){e(a.container).unbind("mousemove touchmove",s),e(d).css("cursor",e(d).attr("data-cursor"))})}},dragStart:function(t){null!=a&&null!=a.draggedItem&&a.dropItem(),t.changedTouches&&t.changedTouches[0]&&(t.pageX=t.changedTouches[0].pageX,t.pageY=t.changedTouches[0].pageY),(a=o[e(this).attr("data-listidx")]).draggedItem=e(t.target).closest("[data-listidx] > "+r.tagName),a.draggedItem.attr("data-origpos",e(this).attr("data-listidx")+"-"+e(a.container).children().index(a.draggedItem));var i=parseInt(a.draggedItem.css("marginTop")),n=parseInt(a.draggedItem.css("marginLeft"));if(a.offset=a.draggedItem.offset(),a.offset.top=t.pageY-a.offset.top+(isNaN(i)?0:i)-1,a.offset.left=t.pageX-a.offset.left+(isNaN(n)?0:n)-1,!r.dragBetween){var d=0==e(a.container).outerHeight()?Math.max(1,Math.round(.5+a.getItems().length*a.draggedItem.outerWidth()/e(a.container).outerWidth()))*a.draggedItem.outerHeight():e(a.container).outerHeight();a.offsetLimit=e(a.container).offset(),a.offsetLimit.right=a.offsetLimit.left+e(a.container).outerWidth()-a.draggedItem.outerWidth(),a.offsetLimit.bottom=a.offsetLimit.top+d-a.draggedItem.outerHeight()}var l=a.draggedItem.height(),s=a.draggedItem.width();if("tr"==r.tagName?(a.draggedItem.children().each(function(){e(this).width(e(this).width())}),a.placeHolderItem=a.draggedItem.clone().attr("data-placeholder",!0),a.draggedItem.after(a.placeHolderItem),a.placeHolderItem.children().each(function(){e(this).css({borderWidth:0,width:e(this).width()+1,height:e(this).height()+1}).html("&nbsp;")})):(a.draggedItem.after(r.placeHolderTemplate),a.placeHolderItem=a.draggedItem.next().css({height:l,width:s}).attr("data-placeholder",!0)),"td"==r.tagName){var c=a.draggedItem.closest("table").get(0);e('<table id="'+c.id+'" style="border-width: 0px;" class="dragSortItem '+c.className+'"><tr></tr></table>').appendTo("body").children().append(a.draggedItem)}var g=a.draggedItem.attr("style");a.draggedItem.attr("data-origstyle",g||""),a.draggedItem.css({position:"absolute",opacity:.8,"z-index":999,height:l,width:s}),a.scroll={moveX:0,moveY:0,maxX:e(document).width()-e(window).width(),maxY:e(document).height()-e(window).height()},a.scroll.scrollY=window.setInterval(function(){if(r.scrollContainer==window){var t=e(r.scrollContainer).scrollTop();(a.scroll.moveY>0&&t<a.scroll.maxY||a.scroll.moveY<0&&t>0)&&(e(r.scrollContainer).scrollTop(t+a.scroll.moveY),a.draggedItem.css("top",a.draggedItem.offset().top+a.scroll.moveY+1))}else e(r.scrollContainer).scrollTop(e(r.scrollContainer).scrollTop()+a.scroll.moveY)},10),a.scroll.scrollX=window.setInterval(function(){if(r.scrollContainer==window){var t=e(r.scrollContainer).scrollLeft();(a.scroll.moveX>0&&t<a.scroll.maxX||a.scroll.moveX<0&&t>0)&&(e(r.scrollContainer).scrollLeft(t+a.scroll.moveX),a.draggedItem.css("left",a.draggedItem.offset().left+a.scroll.moveX+1))}else e(r.scrollContainer).scrollLeft(e(r.scrollContainer).scrollLeft()+a.scroll.moveX)},10),e(o).each(function(e,t){t.createDropTargets(),t.buildPositionTable()}),a.setPos(t.pageX,t.pageY),e(document).bind("mousemove touchmove",a.swapItems),e(document).bind("mouseup touchend",a.dropItem),r.scrollContainer!=window&&e(window).bind("wheel",a.wheel)},setPos:function(t,o){var i=o-this.offset.top,n=t-this.offset.left;r.dragBetween||(i=Math.min(this.offsetLimit.bottom,Math.max(i,this.offsetLimit.top)),n=Math.min(this.offsetLimit.right,Math.max(n,this.offsetLimit.left)));var d=this.draggedItem.offsetParent().not("body").offset();if(null!=d&&(i-=d.top,n-=d.left),r.scrollContainer==window)o-=e(window).scrollTop(),t-=e(window).scrollLeft(),o=Math.max(0,o-e(window).height()+5)+Math.min(0,o-5),t=Math.max(0,t-e(window).width()+5)+Math.min(0,t-5);else{var l=e(r.scrollContainer),s=l.offset();o=Math.max(0,o-l.height()-s.top)+Math.min(0,o-s.top),t=Math.max(0,t-l.width()-s.left)+Math.min(0,t-s.left)}a.scroll.moveX=0==t?0:t*r.scrollSpeed/Math.abs(t),a.scroll.moveY=0==o?0:o*r.scrollSpeed/Math.abs(o),this.draggedItem.css({top:i,left:n})},wheel:function(t){if(a&&r.scrollContainer!=window){var o=e(r.scrollContainer),i=o.offset();if((t=t.originalEvent).clientX>i.left&&t.clientX<i.left+o.width()&&t.clientY>i.top&&t.clientY<i.top+o.height()){var n=(0==t.deltaMode?1:10)*t.deltaY;o.scrollTop(o.scrollTop()+n),t.preventDefault()}}},buildPositionTable:function(){var t=[];this.getItems().not([a.draggedItem[0],a.placeHolderItem[0]]).each(function(r){var o=e(this).offset();o.right=o.left+e(this).outerWidth(),o.bottom=o.top+e(this).outerHeight(),o.elm=this,t[r]=o}),this.pos=t},dropItem:function(){if(null!=a.draggedItem){var t=a.draggedItem.attr("data-origstyle");if(a.draggedItem.attr("style",t),""==t&&a.draggedItem.removeAttr("style"),a.draggedItem.removeAttr("data-origstyle"),a.styleDragHandlers(!0),a.placeHolderItem.before(a.draggedItem),a.placeHolderItem.remove(),e("[data-droptarget], .dragSortItem").remove(),window.clearInterval(a.scroll.scrollY),window.clearInterval(a.scroll.scrollX),a.draggedItem.attr("data-origpos")!=e(o).index(a)+"-"+e(a.container).children().index(a.draggedItem)&&0==r.dragEnd.apply(a.draggedItem)){var i=a.draggedItem.attr("data-origpos").split("-"),n=e(o[i[0]].container).children().not(a.draggedItem).eq(i[1]);n.length>0?n.before(a.draggedItem):0==i[1]?e(o[i[0]].container).prepend(a.draggedItem):e(o[i[0]].container).append(a.draggedItem)}return a.draggedItem.removeAttr("data-origpos"),a.draggedItem=null,e(document).unbind("mousemove touchmove",a.swapItems),e(document).unbind("mouseup touchend",a.dropItem),r.scrollContainer!=window&&e(window).unbind("wheel",a.wheel),!1}},swapItems:function(t){if(null==a.draggedItem)return!1;t.changedTouches&&t.changedTouches[0]&&(t.pageX=t.changedTouches[0].pageX,t.pageY=t.changedTouches[0].pageY),a.setPos(t.pageX,t.pageY);for(var n=a.findPos(t.pageX,t.pageY),d=a,l=0;-1==n&&r.dragBetween&&l<o.length;l++)n=o[l].findPos(t.pageX,t.pageY),d=o[l];if(-1==n)return!1;var s=function(){return e(d.container).children().not(d.draggedItem)},c=s().not(r.itemSelector).each(function(e){this.idx=s().index(this)});return null==i||i.top>a.draggedItem.offset().top||i.left>a.draggedItem.offset().left?e(d.pos[n].elm).before(a.placeHolderItem):e(d.pos[n].elm).after(a.placeHolderItem),c.each(function(){var t=s().eq(this.idx).get(0);this!=t&&s().index(this)<this.idx?e(this).insertAfter(t):this!=t&&e(this).insertBefore(t)}),e(o).each(function(e,t){t.createDropTargets(),t.buildPositionTable()}),i=a.draggedItem.offset(),!1},findPos:function(e,t){for(var r=0;r<this.pos.length;r++)if(this.pos[r].left<e&&this.pos[r].right>e&&this.pos[r].top<t&&this.pos[r].bottom>t)return r;return-1},createDropTargets:function(){r.dragBetween&&e(o).each(function(){var t=e(this.container).find("[data-placeholder]"),o=e(this.container).find("[data-droptarget]");t.length>0&&o.length>0?o.remove():0==t.length&&0==o.length&&("td"==r.tagName?e(r.placeHolderTemplate).attr("data-droptarget",!0).appendTo(this.container):e(this.container).append(a.placeHolderItem.removeAttr("data-placeholder").clone().attr("data-droptarget",!0)),a.placeHolderItem.attr("data-placeholder",!0))})}};d.init(),o.push(d)}),this}e(this).trigger("dragsort-uninit")},e.fn.dragsort.defaults={itemSelector:"",dragSelector:"",dragSelectorExclude:"input, textarea",dragEnd:function(){},dragBetween:!1,placeHolderTemplate:"",scrollContainer:window,scrollSpeed:5,cursor:"pointer"}}(jQuery);