(function(a){jQuery.fn.sortableTable=function(l){function w(f,g){function x(b,c){h.offset({top:Math.min(a(document).height(),Math.max(0,c-h.height()/2)),left:Math.min(a(document).width(),Math.max(0,b-h.width()/2))})}function p(b,c,m){var e=b.offset();return m>=e.top&&c>=e.left&&c<e.left+b.width()&&m<e.top+b.height()}function y(b,c){if(j){var m=j=false;a(f).find("td").each(function(){if(a(this).children().first().attr("class")!=a(k).children().first().attr("class")&&p(a(this),b,c)){var e=k,r={left:a(e).children().first().offset().left-
a(this).offset().left,top:a(e).children().first().offset().top-a(this).offset().top},n=null;if(a(this).children().length>0)n={left:a(this).children().first().offset().left-a(e).offset().left,top:a(this).children().first().offset().top-a(e).offset().top};a(e).append(a(this).children().first()).children().stop(true,true).bind("mouseup",o);n&&a(e).append(a(this).children().first()).children().css("left",n.left+"px").css("top",n.top+"px");a(this).append(a(e).children().first()).children().bind("mouseup",
o).css("left",r.left+"px").css("top",r.top+"px");i(a(this).children().first(),{duration:100});i(a(e).children().first(),{duration:100});if(g.events&&g.events.drop){colIdx=a(this).prevAll().length;rowIdx=a(this).parent().prevAll().length;g.events.drop(e,this,{col:colIdx,row:rowIdx})}m=true}});if(!m){d&&i(d);i(h)}d=null}}function i(b,c){c||(c={});if(!c.pos)c.pos={left:0,top:0};if(!c.duration)c.duration=200;a(b).css("position","relative");a(b).animate({top:c.pos.top,left:c.pos.left},{duration:c.duration,
complete:function(){c.pos.left==0&&c.pos.top==0&&a(b).css("position","").css("left","").css("top","")}})}var j=false,h,k,d,s;g||(g={});var o=function(b){y(b.pageX,b.pageY)},t=function(b){h=a(this).children();if(h.length!=0){var c;if(c=g.ignoreRect){b={x:b.pageX-h.offset().left,y:b.pageY-h.offset().top};c=g.ignoreRect;c=b.y>c.top&&b.x>c.left&&b.y<c.top+c.height&&b.x<c.left+c.width}if(!c){j=true;k=this;g.events&&g.events.start&&g.events.start(this);return false}}},u=function(b){if(j){x(b.pageX,b.pageY);
if(p(a(k),b.pageX,b.pageY)){if(d!=null){i(d);d=null}}else a(f).find("td").each(function(){if(p(a(this),b.pageX,b.pageY)){if(a(d).attr("class")!=a(this).children().first().attr("class")){d!=null&&i(d);d=a(this).children().first();d.length>0&&i(a(d),{pos:{top:a(k).offset().top-a(d).parent().offset().top,left:a(k).offset().left-a(d).parent().offset().left}})}return false}})}return false},v=function(){if(j){j=false;d&&i(d);i(h);d=null}};this.init=function(){s=1;a(f).find("td").children().each(function(){a(this).attr("class",
a(this).attr("class").replace(/\s*draggable\-\d+/g,""));a(this).addClass("draggable-"+s++)});a(f).find("td").bind("mouseup",o);a(f).find("td").bind("mousedown",t);a(document).mousemove(u);a(document).bind("mouseleave",v)};this.refresh=function(){this.destroy();this.init()};this.destroy=function(){a(f).find("td").children().each(function(){a(this).attr("class",a(this).attr("class").replace(/\s*draggable\-\d+/g,""))});a(f).find("td").unbind("mouseup",o);a(f).find("td").unbind("mousedown",t);a(document).unbind("mousemove",
u);a(document).unbind("mouseleave",v)}}var q={init:function(f){f=new w(this,f);f.init();a(this).data("sortableTable",f)},refresh:function(){a(this).data("sortableTable").refresh()},destroy:function(){a(this).data("sortableTable").destroy()}};if(q[l])return q[l].apply(this,Array.prototype.slice.call(arguments,1));else if(typeof l==="object"||!l)return q.init.apply(this,arguments);else a.error("Method "+l+" does not exist on jQuery.sortableTable")}})(jQuery);
