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