(function(t,e){var r={};t.fn.tableDrag=function(e){e=t.extend({draggableClass:"draggable",cookiePath:"/",group:{fieldClass:"row-depth",depthLimit:3},weight:{fieldClass:"row-weight",hidden:true},parent:{fieldClass:"row-parent",sourceFieldClass:"person-id",hidden:true},rtl:false},e||{});this.each(function(){if(e.rtl){t(this).addClass("tabledrag-rtl")}var a=new r.tableDrag(this,e);t("tr."+e.draggableClass,this).each(function(){var a=t(this);if(e.group.fieldClass){var i=t("."+e.group.fieldClass,a);if(i.length){for(var n=0,s=i.val();n").addClass(this.tableSettings.draggableClass).appendTo(e);var l=t("").appendTo(s).prepend(n).prepend(n);this.indentAmount=t(".indentation",l).get(1).offsetLeft-t(".indentation",l).get(0).offsetLeft;s.remove()}t("> tr."+this.tableSettings.draggableClass+", > tbody > tr."+this.tableSettings.draggableClass,e).each(function(){i.makeDraggable(this)});this.$table.before(t('').attr("title",r.t("Re-order rows by numerical weight/parent instead of dragging.")).click(function(){if(t.cookie("Drupal.tableDrag.showWeight")==1){i.hideColumns()}else{i.showColumns()}return false}).wrap('
').parent());i.initColumns();t(document).bind("mousemove",function(t){return i.dragRow(t,i)});t(document).bind("mouseup",function(t){return i.dropRow(t,i)})};r.tableDrag.prototype.initColumns=function(){for(var r in{group:1,weight:1,parent:1}){if(this.tableSettings[r].fieldClass!==e){var a=t("."+this.tableSettings[r].fieldClass+":first",this.table);if(a.length&&this.tableSettings[r].hidden){var i=this.tableSettings[r].hidden;var n=a.closest("td")}}if(i&&n[0]){var s=t("> td",n.parent()).index(n.get(0))+1;t("> thead > tr, > tbody > tr, > tr",this.table).each(function(){var e=s;var r=t(this).children();r.each(function(t){if(t1){e-=this.colSpan-1}});if(e>0){n=r.filter(":nth-child("+e+")");if(n[0].colSpan&&n[0].colSpan>1){n.addClass("tabledrag-has-colspan")}else{n.addClass("tabledrag-hide")}}})}}if(t.cookie("Drupal.tableDrag.showWeight")===null){t.cookie("Drupal.tableDrag.showWeight",0,{path:this.tableSettings.cookiePath,expires:365});this.hideColumns()}else{if(t.cookie("Drupal.tableDrag.showWeight")==1){this.showColumns()}else{this.hideColumns()}}};r.tableDrag.prototype.hideColumns=function(){t(".tabledrag-hide",this.table).css("display","none");t(".tabledrag-handle",this.table).css("display","");t(".tabledrag-has-colspan",this.table).each(function(){this.colSpan=this.colSpan-1});t(".tabledrag-toggle-weight").text(r.t("Show row weights/parents"));t.cookie("Drupal.tableDrag.showWeight",0,{path:this.tableSettings.cookiePath,expires:365});t(this.table).trigger("columnschange","hide")};r.tableDrag.prototype.showColumns=function(){t(".tabledrag-hide",this.table).css("display","");t(".tabledrag-handle",this.table).css("display","none");t(".tabledrag-has-colspan",this.table).each(function(){this.colSpan=this.colSpan+1});t(".tabledrag-toggle-weight").text(r.t("Hide row weights/parents"));t.cookie("Drupal.tableDrag.showWeight",1,{path:this.tableSettings.cookiePath,expires:365});t(this.table).trigger("columnschange","show")};r.tableDrag.prototype.rowSettings=function(r,a){if(this.tableSettings[r].fieldClass!==e){var i=t("."+this.tableSettings[r].fieldClass,a);if(i.length){var n=this.tableSettings[r];n.relationship=r=="group"?"group":r=="parent"?"parent":r=="weight"?"sibling":"self";n.action=r=="group"?"depth":r=="parent"?"match":r=="weight"?"order":"order";return n}}};r.tableDrag.prototype.makeDraggable=function(e){var a=this;var i=t('
 
').attr("title",r.t("Drag to re-order"));if(t("td:first .indentation:last",e).length){t("td:first .indentation:last",e).after(i);a.indentCount=Math.max(t(".indentation",e).length,a.indentCount)}else{t("td:first",e).prepend(i)}i.hover(function(){a.dragObject==null?t(this).addClass("tabledrag-handle-hover"):null},function(){a.dragObject==null?t(this).removeClass("tabledrag-handle-hover"):null});i.mousedown(function(r){a.dragObject={};a.dragObject.initMouseOffset=a.getMouseOffset(e,r);a.dragObject.initMouseCoords=a.mouseCoords(r);if(a.indentEnabled){a.dragObject.indentMousePos=a.dragObject.initMouseCoords}if(a.rowObject){t("a.tabledrag-handle",a.rowObject.element).blur()}a.rowObject=new a.row(e,"mouse",a.indentEnabled,a.maxDepth,true,a.tableSettings.draggableClass);a.table.topY=t(a.table).offset().top;a.table.bottomY=a.table.topY+a.table.offsetHeight;t(this).addClass("tabledrag-handle-hover");t(e).addClass("drag");t("body").addClass("drag");if(a.oldRowElement){t(a.oldRowElement).removeClass("drag-previous")}if(navigator.userAgent.indexOf("MSIE 6.")!=-1){t("select",this.table).css("display","none")}a.safeBlur=false;a.onDrag();return false});i.click(function(){return false});i.focus(function(){t(this).addClass("tabledrag-handle-hover");a.safeBlur=true});i.blur(function(e){t(this).removeClass("tabledrag-handle-hover");if(a.rowObject&&a.safeBlur){a.dropRow(e,a)}});i.keydown(function(r){if(r.keyCode!=9&&!a.rowObject){a.rowObject=new a.row(e,"keyboard",a.indentEnabled,a.maxDepth,true,a.tableSettings.draggableClass)}var n=false;switch(r.keyCode){case 37:case 63234:n=true;a.rowObject.indent(-1*a.rtl);break;case 38:case 63232:var s=t(a.rowObject.element).prev("tr").get(0);while(s&&t(s).is(":hidden")){s=t(s).prev("tr").get(0)}if(s){a.safeBlur=false;a.rowObject.direction="up";n=true;if(t(e).is(".tabledrag-root")){var l=0;while(s&&t(".indentation",s).length){s=t(s).prev("tr").get(0);l+=t(s).is(":hidden")?0:s.offsetHeight}if(s){a.rowObject.swap("before",s);window.scrollBy(0,-l)}}else if(a.table.tBodies[0].rows[0]!=s||t(s).is("."+a.tableSettings.draggableClass)){a.rowObject.swap("before",s);a.rowObject.interval=null;a.rowObject.indent(0);window.scrollBy(0,-parseInt(e.offsetHeight,10))}i.get(0).focus()}break;case 39:case 63235:n=true;a.rowObject.indent(1*a.rtl);break;case 40:case 63233:var o=t(a.rowObject.group).filter(":last").next("tr").get(0);while(o&&t(o).is(":hidden")){o=t(o).next("tr").get(0)}if(o){a.safeBlur=false;a.rowObject.direction="down";n=true;if(t(e).is(".tabledrag-root")){var l=0;var d=new a.row(o,"keyboard",a.indentEnabled,a.maxDepth,false,a.tableSettings.draggableClass);if(d){t(d.group).each(function(){l+=t(this).is(":hidden")?0:this.offsetHeight});var h=t(d.group).filter(":last").get(0);a.rowObject.swap("after",h);window.scrollBy(0,parseInt(l,10))}}else{a.rowObject.swap("after",o);a.rowObject.interval=null;a.rowObject.indent(0);window.scrollBy(0,parseInt(e.offsetHeight,10))}i.get(0).focus()}break}if(a.rowObject&&a.rowObject.changed==true){t(e).addClass("drag");if(a.oldRowElement){t(a.oldRowElement).removeClass("drag-previous")}a.oldRowElement=e;a.restripeTable();a.onDrag()}if(n){return false}});i.keypress(function(t){switch(t.keyCode){case 37:case 38:case 39:case 40:return false}})};r.tableDrag.prototype.dragRow=function(t,e){if(e.dragObject){e.currentMouseCoords=e.mouseCoords(t);var r=e.currentMouseCoords.y-e.dragObject.initMouseOffset.y;var a=e.currentMouseCoords.x-e.dragObject.initMouseOffset.x;if(r!=e.oldY){e.rowObject.direction=r>e.oldY?"down":"up";e.oldY=r;var i=e.checkScroll(e.currentMouseCoords.y);clearInterval(e.scrollInterval);if(i>0&&e.rowObject.direction=="down"||i<0&&e.rowObject.direction=="up"){e.setScroll(i)}var n=e.findDropTargetRow(a,r);if(n){if(e.rowObject.direction=="down"){e.rowObject.swap("after",n,e)}else{e.rowObject.swap("before",n,e)}e.restripeTable()}}if(e.indentEnabled){var s=e.currentMouseCoords.x-e.dragObject.indentMousePos.x;var l=Math.round(s/e.indentAmount*e.rtl);var o=e.rowObject.indent(l);e.dragObject.indentMousePos.x+=e.indentAmount*o*e.rtl;e.indentCount=Math.max(e.indentCount,e.rowObject.indents)}return false}};r.tableDrag.prototype.dropRow=function(e,r){if(r.rowObject!=null){var a=r.rowObject.element;if(r.rowObject.changed==true){r.updateFields(a);if(!!r.tableSettings.group.fieldClass){for(var i in r.rowObject.children){r.updateField(r.rowObject.children[i],"group")}}if(r.changed==false){r.changed=true}}if(r.indentEnabled){r.rowObject.removeIndentClasses()}if(r.oldRowElement){t(r.oldRowElement).removeClass("drag-previous")}t(a).removeClass("drag").addClass("drag-previous");r.oldRowElement=a;r.onDrop();r.rowObject=null}if(r.dragObject!=null){t(".tabledrag-handle",a).removeClass("tabledrag-handle-hover");r.dragObject=null;t("body").removeClass("drag");clearInterval(r.scrollInterval);if(navigator.userAgent.indexOf("MSIE 6.")!=-1){t("select",this.table).css("display","block")}}};r.tableDrag.prototype.mouseCoords=function(t){if(t.pageX||t.pageY){return{x:t.pageX,y:t.pageY}}return{x:t.clientX+document.body.scrollLeft-document.body.clientLeft,y:t.clientY+document.body.scrollTop-document.body.clientTop}};r.tableDrag.prototype.getMouseOffset=function(e,r){var a=t(e).offset();var i=this.mouseCoords(r);return{x:i.x-a.left,y:i.y-a.top}};r.tableDrag.prototype.findDropTargetRow=function(e,r){var a=t(this.table.tBodies[0].rows).not(":hidden");for(var i=0;il-o&&r=this.rowObject.indents){n=n.prev("tr")}if(n.length){i=n[0]}else{i=t(this.table).find("tr."+this.tableSettings.draggableClass+":first").get(0);if(i==this.rowObject.element){i=t(this.rowObject.group[this.rowObject.group.length-1]).next("tr."+this.tableSettings.draggableClass).get(0)}var l=true}}this.copyDragClasses(i,e,r);a=this.rowSettings(r,e);if(l){a.relationship="sibling";a.source=a.fieldClass}var o="."+a.fieldClass;var d=t(o,e);if(d){var h="."+(a.relationship=="parent"?a.sourceFieldClass:a.fieldClass);var g=t(h,i);switch(a.action){case"depth":d.val(t(".indentation",g.closest("tr")).length);break;case"match":d.val(g.val());break;case"order":var c=this.rowObject.findSiblings(a);if(d.is("select")){var f=[];t("option",d).each(function(){f.push(this.value)});var p=f[f.length-1];t(o,c).each(function(){if(f.length>0){this.value=f.shift()}else{this.value=p}})}else{var b=parseInt(t(o,c[0]).val(),10)||0;t(o,c).each(function(){this.value=b;b++})}break}}};r.tableDrag.prototype.copyDragClasses=function(e,r,a){var i=t("."+a,e);var n=t("."+a,r);if(i.length&&n.length){n[0].className=i[0].className}};r.tableDrag.prototype.checkScroll=function(t){var e=document.documentElement;var r=document.body;var a=this.windowHeight=window.innerHeight||(e.clientHeight&&e.clientWidth!=0?e.clientHeight:r.offsetHeight);var i=this.scrollY=document.all?!e.scrollTop?r.scrollTop:e.scrollTop:window.pageYOffset?window.pageYOffset:window.scrollY;var n=this.scrollSettings.trigger;var s=0;if(t-i>a-n){s=n/(a+i-t);s=s>0&&s0&&se.table.topY;var a=e.scrollY+e.windowHeight0&&a||t<0&&r){window.scrollBy(0,t)}},this.scrollSettings.interval)};r.tableDrag.prototype.restripeTable=function(){t("> tbody > tr."+this.tableSettings.draggableClass+":visible, > tr."+this.tableSettings.draggableClass+":visible",this.table).removeClass("odd even").filter(":odd").addClass("even").end().filter(":even").addClass("odd")};r.tableDrag.prototype.onDrag=function(){this.$table.trigger("tabledrag:dragrow",this);return null};r.tableDrag.prototype.onDrop=function(){this.$table.trigger("tabledrag:droprow",this);return null};r.tableDrag.prototype.row=function(e,r,a,i,n,s){this.element=e;this.method=r;this.group=[e];this.groupDepth=t(".indentation",e).length;this.changed=false;this.table=t(e).closest("table").get(0);this.indentEnabled=a;this.maxDepth=i;this.direction="";this.draggableClass=s;if(this.indentEnabled){this.indents=t(".indentation",e).length;this.children=this.findChildren(n);this.group=t.merge(this.group,this.children);for(var l=0;lr){n++;i.push(a[0]);if(e){t(".indentation",a).each(function(e){if(n==1&&e==r){t(this).addClass("tree-child-first")}if(e==r){t(this).addClass("tree-child")}else if(e>r){t(this).addClass("tree-child-horizontal")}})}}else{break}a=a.next("tr."+this.draggableClass)}if(e&&i.length){t(".indentation:nth-child("+(r+1)+")",i[i.length-1]).addClass("tree-child-last")}return i};r.tableDrag.prototype.row.prototype.isValidSwap=function(e){if(this.indentEnabled){var r,a;if(this.direction=="down"){r=e;a=t(e).next("tr").get(0)}else{r=t(e).prev("tr").get(0);a=e}this.interval=this.validIndentInterval(r,a);if(this.interval.min>this.interval.max){return false}}if(this.table.tBodies[0].rows[0]==e&&t(e).is(":not(."+this.draggableClass+")")){return false}return true};r.tableDrag.prototype.row.prototype.swap=function(e,r){t(r)[e](this.group);this.changed=true;this.onSwap(r)};r.tableDrag.prototype.row.prototype.validIndentInterval=function(e,r){var a,i;a=r?t(".indentation",r).length:0;if(!e||t(e).is(":not(."+this.draggableClass+")")||t(this.element).is(".tabledrag-root")){i=0}else{i=t(".indentation",e).length+(t(e).is(".tabledrag-leaf")?0:1);if(this.maxDepth){i=Math.min(i,this.maxDepth-(this.groupDepth-this.indents))}}return{min:a,max:i}};r.tableDrag.prototype.row.prototype.indent=function(e){if(!this.interval){var a=t(this.element).prev("tr").get(0);var i=t(this.group).filter(":last").next("tr").get(0);this.interval=this.validIndentInterval(a,i)}var n=this.indents+e;n=Math.max(n,this.interval.min);n=Math.min(n,this.interval.max);e=n-this.indents;for(var s=1;s<=Math.abs(e);s++){if(e<0){t(".indentation:first",this.group).remove();this.indents--}else{t("td:first",this.group).prepend(r.theme("tableDragIndentation"));this.indents++}}if(e){this.changed=true;this.groupDepth+=e;this.onIndent()}return e};r.tableDrag.prototype.row.prototype.findSiblings=function(e){var r=[];var a=["prev","next"];var i=this.indents;for(var n=0;n*'};r.theme.prototype.tableDragIndentation=function(){return'
 
'}})(jQuery);