/*

 jQuery UI Draggable

 Copyright (c) 2008 Paul Bakaus
 Dual licensed under the MIT (MIT-LICENSE.txt)
 and GPL (GPL-LICENSE.txt) licenses.

 http://docs.jquery.com/UI/Draggables

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

