(function(d){d.widget("ui.draggable",d.extend({},d.ui.mouse,{init:function(){var c=this.options;if(c.helper=='original'&&!(/(relative|absolute|fixed)/).test(this.element.css('position')))this.element.css('position','relative');this.element.addClass('ui-draggable');(c.disabled&&this.element.addClass('ui-draggable-disabled'));this.mouseInit()},mouseStart:function(c){var a=this.options;if(this.helper||a.disabled||d(c.target).is('.ui-resizable-handle'))return false;var b=!this.options.handle||!d(this.options.handle,this.element).length?true:false;d(this.options.handle,this.element).find("*").andSelf().each(function(){if(this==c.target)b=true});if(!b)return false;if(d.ui.ddmanager)d.ui.ddmanager.current=this;this.helper=d.isFunction(a.helper)?d(a.helper.apply(this.element[0],[c])):(a.helper=='clone'?this.element.clone():this.element);if(!this.helper.parents('body').length)this.helper.appendTo((a.appendTo=='parent'?this.element[0].parentNode:a.appendTo));if(this.helper[0]!=this.element[0]&&!(/(fixed|absolute)/).test(this.helper.css("position")))this.helper.css("position","absolute");this.margins={left:(parseInt(this.element.css("marginLeft"),10)||0),top:(parseInt(this.element.css("marginTop"),10)||0)};this.cssPosition=this.helper.css("position");this.offset=this.element.offset();this.offset={top:this.offset.top-this.margins.top,left:this.offset.left-this.margins.left};this.offset.click={left:c.pageX-this.offset.left,top:c.pageY-this.offset.top};this.offsetParent=this.helper.offsetParent();var e=this.offsetParent.offset();if(this.offsetParent[0]==document.body&&d.browser.mozilla)e={top:0,left:0};this.offset.parent={top:e.top+(parseInt(this.offsetParent.css("borderTopWidth"),10)||0),left:e.left+(parseInt(this.offsetParent.css("borderLeftWidth"),10)||0)};var f=this.element.position();this.offset.relative=this.cssPosition=="relative"?{top:f.top-(parseInt(this.helper.css("top"),10)||0)+this.offsetParent[0].scrollTop,left:f.left-(parseInt(this.helper.css("left"),10)||0)+this.offsetParent[0].scrollLeft}:{top:0,left:0};this.originalPosition=this.generatePosition(c);this.helperProportions={width:this.helper.outerWidth(),height:this.helper.outerHeight()};if(a.cursorAt){if(a.cursorAt.left!=undefined)this.offset.click.left=a.cursorAt.left+this.margins.left;if(a.cursorAt.right!=undefined)this.offset.click.left=this.helperProportions.width-a.cursorAt.right+this.margins.left;if(a.cursorAt.top!=undefined)this.offset.click.top=a.cursorAt.top+this.margins.top;if(a.cursorAt.bottom!=undefined)this.offset.click.top=this.helperProportions.height-a.cursorAt.bottom+this.margins.top}if(a.containment){if(a.containment=='parent')a.containment=this.helper[0].parentNode;if(a.containment=='document'||a.containment=='window')this.containment=[0-this.offset.relative.left-this.offset.parent.left,0-this.offset.relative.top-this.offset.parent.top,d(a.containment=='document'?document:window).width()-this.offset.relative.left-this.offset.parent.left-this.helperProportions.width-this.margins.left-(parseInt(this.element.css("marginRight"),10)||0),(d(a.containment=='document'?document:window).height()||document.body.parentNode.scrollHeight)-this.offset.relative.top-this.offset.parent.top-this.helperProportions.height-this.margins.top-(parseInt(this.element.css("marginBottom"),10)||0)];if(!(/^(document|window|parent)$/).test(a.containment)){var g=d(a.containment)[0];var i=d(a.containment).offset();this.containment=[i.left+(parseInt(d(g).css("borderLeftWidth"),10)||0)-this.offset.relative.left-this.offset.parent.left,i.top+(parseInt(d(g).css("borderTopWidth"),10)||0)-this.offset.relative.top-this.offset.parent.top,i.left+Math.max(g.scrollWidth,g.offsetWidth)-(parseInt(d(g).css("borderLeftWidth"),10)||0)-this.offset.relative.left-this.offset.parent.left-this.helperProportions.width-this.margins.left-(parseInt(this.element.css("marginRight"),10)||0),i.top+Math.max(g.scrollHeight,g.offsetHeight)-(parseInt(d(g).css("borderTopWidth"),10)||0)-this.offset.relative.top-this.offset.parent.top-this.helperProportions.height-this.margins.top-(parseInt(this.element.css("marginBottom"),10)||0)]}}this.propagate("start",c);this.helperProportions={width:this.helper.outerWidth(),height:this.helper.outerHeight()};if(d.ui.ddmanager&&!a.dropBehaviour)d.ui.ddmanager.prepareOffsets(this,c);this.helper.addClass("ui-draggable-dragging");this.mouseDrag(c);return true},convertPositionTo:function(c,a){if(!a)a=this.position;var b=c=="absolute"?1:-1;return{top:(a.top+this.offset.relative.top*b+this.offset.parent.top*b-(this.cssPosition=="fixed"||(this.cssPosition=="absolute"&&this.offsetParent[0]==document.body)?0:this.offsetParent[0].scrollTop)*b+(this.cssPosition=="fixed"?d(document).scrollTop():0)*b+this.margins.top*b),left:(a.left+this.offset.relative.left*b+this.offset.parent.left*b-(this.cssPosition=="fixed"||(this.cssPosition=="absolute"&&this.offsetParent[0]==document.body)?0:this.offsetParent[0].scrollLeft)*b+(this.cssPosition=="fixed"?d(document).scrollLeft():0)*b+this.margins.left*b)}},generatePosition:function(c){var a=this.options;var b={top:(c.pageY-this.offset.click.top-this.offset.relative.top-this.offset.parent.top+(this.cssPosition=="fixed"||(this.cssPosition=="absolute"&&this.offsetParent[0]==document.body)?0:this.offsetParent[0].scrollTop)-(this.cssPosition=="fixed"?d(document).scrollTop():0)),left:(c.pageX-this.offset.click.left-this.offset.relative.left-this.offset.parent.left+(this.cssPosition=="fixed"||(this.cssPosition=="absolute"&&this.offsetParent[0]==document.body)?0:this.offsetParent[0].scrollLeft)-(this.cssPosition=="fixed"?d(document).scrollLeft():0))};if(!this.originalPosition)return b;if(this.containment){if(b.left<this.containment[0])b.left=this.containment[0];if(b.top<this.containment[1])b.top=this.containment[1];if(b.left>this.containment[2])b.left=this.containment[2];if(b.top>this.containment[3])b.top=this.containment[3]}if(a.grid){var e=this.originalPosition.top+Math.round((b.top-this.originalPosition.top)/a.grid[1])*a.grid[1];b.top=this.containment?(!(e<this.containment[1]||e>this.containment[3])?e:(!(e<this.containment[1])?e-a.grid[1]:e+a.grid[1])):e;var f=this.originalPosition.left+Math.round((b.left-this.originalPosition.left)/a.grid[0])*a.grid[0];b.left=this.containment?(!(f<this.containment[0]||f>this.containment[2])?f:(!(f<this.containment[0])?f-a.grid[0]:f+a.grid[0])):f}return b},mouseDrag:function(c){this.position=this.generatePosition(c);this.positionAbs=this.convertPositionTo("absolute");this.position=this.propagate("drag",c)||this.position;if(!this.options.axis||this.options.axis!="y")this.helper[0].style.left=this.position.left+'px';if(!this.options.axis||this.options.axis!="x")this.helper[0].style.top=this.position.top+'px';if(d.ui.ddmanager)d.ui.ddmanager.drag(this,c);return false},mouseStop:function(c){var a=false;if(d.ui.ddmanager&&!this.options.dropBehaviour)var a=d.ui.ddmanager.drop(this,c);if((this.options.revert=="invalid"&&!a)||(this.options.revert=="valid"&&a)||this.options.revert===true){var b=this;d(this.helper).animate(this.originalPosition,parseInt(this.options.revert,10)||500,function(){b.propagate("stop",c);b.clear()})}else{this.propagate("stop",c);this.clear()}return false},clear:function(){this.helper.removeClass("ui-draggable-dragging");if(this.options.helper!='original'&&!this.cancelHelperRemoval)this.helper.remove();this.helper=null;this.cancelHelperRemoval=false},plugins:{},uiHash:function(c){return{helper:this.helper,position:this.position,absolutePosition:this.positionAbs,options:this.options}},propagate:function(c,a){d.ui.plugin.call(this,c,[a,this.uiHash()]);if(c=="drag")this.positionAbs=this.convertPositionTo("absolute");return this.element.triggerHandler(c=="drag"?c:"drag"+c,[a,this.uiHash()],this.options[c])},destroy:function(){if(!this.element.data('draggable'))return;this.element.removeData("draggable").unbind(".draggable").removeClass('ui-draggable');this.mouseDestroy()}}));d.extend(d.ui.draggable,{defaults:{appendTo:"parent",axis:false,cancel:":input",delay:0,distance:1,helper:"original"}});d.ui.plugin.add("draggable","cursor",{start:function(c,a){var b=d('body');if(b.css("cursor"))a.options._cursor=b.css("cursor");b.css("cursor",a.options.cursor)},stop:function(c,a){if(a.options._cursor)d('body').css("cursor",a.options._cursor)}});d.ui.plugin.add("draggable","zIndex",{start:function(c,a){var b=d(a.helper);if(b.css("zIndex"))a.options._zIndex=b.css("zIndex");b.css('zIndex',a.options.zIndex)},stop:function(c,a){if(a.options._zIndex)d(a.helper).css('zIndex',a.options._zIndex)}});d.ui.plugin.add("draggable","opacity",{start:function(c,a){var b=d(a.helper);if(b.css("opacity"))a.options._opacity=b.css("opacity");b.css('opacity',a.options.opacity)},stop:function(c,a){if(a.options._opacity)d(a.helper).css('opacity',a.options._opacity)}});d.ui.plugin.add("draggable","iframeFix",{start:function(c,a){d(a.options.iframeFix===true?"iframe":a.options.iframeFix).each(function(){d('<div class="ui-draggable-iframeFix" style="background: #fff;"></div>').css({width:this.offsetWidth+"px",height:this.offsetHeight+"px",position:"absolute",opacity:"0.001",zIndex:1000}).css(d(this).offset()).appendTo("body")})},stop:function(c,a){d("div.DragDropIframeFix").each(function(){this.parentNode.removeChild(this)})}});d.ui.plugin.add("draggable","scroll",{start:function(a,b){var e=b.options;var f=d(this).data("draggable");e.scrollSensitivity=e.scrollSensitivity||20;e.scrollSpeed=e.scrollSpeed||20;f.overflowY=function(c){do{if(/auto|scroll/.test(c.css('overflow'))||(/auto|scroll/).test(c.css('overflow-y')))return c;c=c.parent()}while(c[0].parentNode);return d(document)}(this);f.overflowX=function(c){do{if(/auto|scroll/.test(c.css('overflow'))||(/auto|scroll/).test(c.css('overflow-x')))return c;c=c.parent()}while(c[0].parentNode);return d(document)}(this);if(f.overflowY[0]!=document&&f.overflowY[0].tagName!='HTML')f.overflowYOffset=f.overflowY.offset();if(f.overflowX[0]!=document&&f.overflowX[0].tagName!='HTML')f.overflowXOffset=f.overflowX.offset()},drag:function(c,a){var b=a.options;var e=d(this).data("draggable");if(e.overflowY[0]!=document&&e.overflowY[0].tagName!='HTML'){if((e.overflowYOffset.top+e.overflowY[0].offsetHeight)-c.pageY<b.scrollSensitivity)e.overflowY[0].scrollTop=e.overflowY[0].scrollTop+b.scrollSpeed;if(c.pageY-e.overflowYOffset.top<b.scrollSensitivity)e.overflowY[0].scrollTop=e.overflowY[0].scrollTop-b.scrollSpeed}else{if(c.pageY-d(document).scrollTop()<b.scrollSensitivity)d(document).scrollTop(d(document).scrollTop()-b.scrollSpeed);if(d(window).height()-(c.pageY-d(document).scrollTop())<b.scrollSensitivity)d(document).scrollTop(d(document).scrollTop()+b.scrollSpeed)}if(e.overflowX[0]!=document&&e.overflowX[0].tagName!='HTML'){if((e.overflowXOffset.left+e.overflowX[0].offsetWidth)-c.pageX<b.scrollSensitivity)e.overflowX[0].scrollLeft=e.overflowX[0].scrollLeft+b.scrollSpeed;if(c.pageX-e.overflowXOffset.left<b.scrollSensitivity)e.overflowX[0].scrollLeft=e.overflowX[0].scrollLeft-b.scrollSpeed}else{if(c.pageX-d(document).scrollLeft()<b.scrollSensitivity)d(document).scrollLeft(d(document).scrollLeft()-b.scrollSpeed);if(d(window).width()-(c.pageX-d(document).scrollLeft())<b.scrollSensitivity)d(document).scrollLeft(d(document).scrollLeft()+b.scrollSpeed)}}});d.ui.plugin.add("draggable","snap",{start:function(b,e){var f=d(this).data("draggable");f.snapElements=[];d(e.options.snap===true?'.ui-draggable':e.options.snap).each(function(){var c=d(this);var a=c.offset();if(this!=f.element[0])f.snapElements.push({item:this,width:c.outerWidth(),height:c.outerHeight(),top:a.top,left:a.left})})},drag:function(c,a){var b=d(this).data("draggable");var e=a.options.snapTolerance||20;var f=a.absolutePosition.left,g=f+b.helperProportions.width,i=a.absolutePosition.top,h=i+b.helperProportions.height;for(var n=b.snapElements.length-1;n>=0;n--){var j=b.snapElements[n].left,l=j+b.snapElements[n].width,k=b.snapElements[n].top,m=k+b.snapElements[n].height;if(!((j-e<f&&f<l+e&&k-e<i&&i<m+e)||(j-e<f&&f<l+e&&k-e<h&&h<m+e)||(j-e<g&&g<l+e&&k-e<i&&i<m+e)||(j-e<g&&g<l+e&&k-e<h&&h<m+e)))continue;if(a.options.snapMode!='inner'){var o=Math.abs(k-h)<=20;var p=Math.abs(m-i)<=20;var q=Math.abs(j-g)<=20;var r=Math.abs(l-f)<=20;if(o)a.position.top=b.convertPositionTo("relative",{top:k-b.helperProportions.height,left:0}).top;if(p)a.position.top=b.convertPositionTo("relative",{top:m,left:0}).top;if(q)a.position.left=b.convertPositionTo("relative",{top:0,left:j-b.helperProportions.width}).left;if(r)a.position.left=b.convertPositionTo("relative",{top:0,left:l}).left}if(a.options.snapMode!='outer'){var o=Math.abs(k-i)<=20;var p=Math.abs(m-h)<=20;var q=Math.abs(j-f)<=20;var r=Math.abs(l-g)<=20;if(o)a.position.top=b.convertPositionTo("relative",{top:k,left:0}).top;if(p)a.position.top=b.convertPositionTo("relative",{top:m-b.helperProportions.height,left:0}).top;if(q)a.position.left=b.convertPositionTo("relative",{top:0,left:j}).left;if(r)a.position.left=b.convertPositionTo("relative",{top:0,left:l-b.helperProportions.width}).left}}}});d.ui.plugin.add("draggable","connectToSortable",{start:function(a,b){var e=d(this).data("draggable");e.sortables=[];d(b.options.connectToSortable).each(function(){if(d.data(this,'sortable')){var c=d.data(this,'sortable');e.sortables.push({instance:c,shouldRevert:c.options.revert});c.refreshItems();c.propagate("activate",a,e)}})},stop:function(c,a){var b=d(this).data("draggable");d.each(b.sortables,function(){if(this.instance.isOver){this.instance.isOver=0;b.cancelHelperRemoval=true;this.instance.cancelHelperRemoval=false;if(this.shouldRevert)this.instance.options.revert=true;this.instance.mouseStop(c);this.instance.element.triggerHandler("sortreceive",[c,d.extend(this.instance.ui(),{sender:b.element})],this.instance.options["receive"]);this.instance.options.helper=this.instance.options._helper}else{this.instance.propagate("deactivate",c,b)}})},drag:function(g,i){var h=d(this).data("draggable"),n=this;var j=function(c){var a=c.left,b=a+c.width,e=c.top,f=e+c.height;return(a<(this.positionAbs.left+this.offset.click.left)&&(this.positionAbs.left+this.offset.click.left)<b&&e<(this.positionAbs.top+this.offset.click.top)&&(this.positionAbs.top+this.offset.click.top)<f)};d.each(h.sortables,function(c){if(j.call(h,this.instance.containerCache)){if(!this.instance.isOver){this.instance.isOver=1;this.instance.currentItem=d(n).clone().appendTo(this.instance.element).data("sortable-item",true);this.instance.options._helper=this.instance.options.helper;this.instance.options.helper=function(){return i.helper[0]};g.target=this.instance.currentItem[0];this.instance.mouseCapture(g,true);this.instance.mouseStart(g,true,true);this.instance.offset.click.top=h.offset.click.top;this.instance.offset.click.left=h.offset.click.left;this.instance.offset.parent.left-=h.offset.parent.left-this.instance.offset.parent.left;this.instance.offset.parent.top-=h.offset.parent.top-this.instance.offset.parent.top;h.propagate("toSortable",g)}if(this.instance.currentItem)this.instance.mouseDrag(g)}else{if(this.instance.isOver){this.instance.isOver=0;this.instance.cancelHelperRemoval=true;this.instance.options.revert=false;this.instance.mouseStop(g,true);this.instance.options.helper=this.instance.options._helper;this.instance.currentItem.remove();if(this.instance.placeholder)this.instance.placeholder.remove();h.propagate("fromSortable",g)}}})}});d.ui.plugin.add("draggable","stack",{start:function(b,e){var f=d.makeArray(d(e.options.stack.group)).sort(function(c,a){return(parseInt(d(c).css("zIndex"),10)||e.options.stack.min)-(parseInt(d(a).css("zIndex"),10)||e.options.stack.min)});d(f).each(function(c){this.style.zIndex=e.options.stack.min+c});this[0].style.zIndex=e.options.stack.min+f.length}})})(jQuery);
