
var selectedElement=0;var currentX=0;var currentY=0;var currentMatrix=0;function selectElement(evt){selectedElement=evt.target;currentX=evt.clientX;currentY=evt.clientY;currentMatrix=selectedElement.getAttributeNS(null,"transform").slice(7,-1).split(',');for(var i=0;i<currentMatrix.length;i++){currentMatrix[i]=parseFloat(currentMatrix[i]);}
selectedElement.setAttributeNS(null,"onmousemove","moveElement(evt)");selectedElement.setAttributeNS(null,"onmouseout","deselectElement(evt)");selectedElement.setAttributeNS(null,"onmouseup","deselectElement(evt)");selectedElement.toFront()}
function moveElement(evt){var dx=evt.clientX-currentX;var dy=evt.clientY-currentY;currentMatrix[4]+=dx;currentMatrix[5]+=dy;newMatrix="matrix("+currentMatrix.join(',')+")";selectedElement.setAttributeNS(null,"transform",newMatrix);currentX=evt.clientX;currentY=evt.clientY;}
function deselectElement(evt){if(selectedElement!=0){selectedElement.removeAttributeNS(null,"onmousemove");selectedElement.removeAttributeNS(null,"onmouseout");selectedElement.removeAttributeNS(null,"onmouseup");selectedElement=0;}}
var start=function(){this.ox=this.attr("x");this.oy=this.attr("y");this.animate({opacity:.5},500,">");this.toFront();},move=function(dx,dy){this.attr({x:this.ox+dx,y:this.oy+dy});this.bpmnObject.renderLines();},up=function(){this.animate({opacity:1},500,">");};dragStart=function(){this.moved=false;var parent=this;this.sdx=0;this.sdy=0;if(!this.children)
parent=this.parent;parent.bpmnObject.chart.clearLineSelection();var c=parent.bpmnObject.getCenter();for(var snapIdx=0;snapIdx<snapPoints.length;snapIdx++){var snapPoint=snapPoints[snapIdx];if(snapPoint.x==c.x&&snapPoint.y==c.y){snapPoints.splice(snapIdx,1);break;}}
var els=$.extend([],parent.children);els.push(parent);$.each(els,function(e,el){if(el.type=='rect'||el.type=='image'||el.type=='text'){el.ox=el.attr('x');el.oy=el.attr('y');}
else if(el.type=='path'){el.ox=0;el.oy=0;}
else{el.ox=el.attr('cx');el.oy=el.attr('cy');}});parent.animate({'opacity':.5},500);};function snap(value,gridSize,roundFunction){if(roundFunction===undefined)roundFunction=Math.round;return gridSize*roundFunction(value/gridSize);}
function snapToCentrePoint(c,rangeSize){var retOb={x:0,y:0};for(var snapIdx=0;snapIdx<snapPoints.length;snapIdx++){var snapPoint=snapPoints[snapIdx];if(Math.abs(snapPoint.x-c.x)<=rangeSize){retOb.x=snapPoint.x-c.x;}
if(Math.abs(snapPoint.y-c.y)<=rangeSize){retOb.y=snapPoint.y-c.y;}
if(retOb.x!=0||retOb.y!=0)
break;}
return retOb;}
var snapPoints=[];addSnapPoint=function(point){snapPoints.push(point);}
dragMove=function(dx,dy){this.sdx=dx;this.sdy=dy;var parent=moveItem.call(this,dx,dy);if(parent.bpmnObject){parent.bpmnObject.redrawConnectingLines();parent.bpmnObject.chart.findNearbyConnections(parent.bpmnObject);}};moveItem=function(dx,dy)
{dx=snap(dx,10);dy=snap(dy,10);if(dx!=0||dy!=0)
this.moved=true;var parent=this;if(!this.children)
parent=this.parent;var els=$.extend([],parent.children);els.push(parent);$.each(els,function(i,el){if(el.type=='rect'||el.type=='image'||el.type=='text'){att={x:el.ox+dx,y:el.oy+dy};el.attr(att);}
else if(el.type=='path'){el.translate(dx-el.ox,dy-el.oy);el.ox=dx;el.oy=dy;}
else{att={cx:el.ox+dx,cy:el.oy+dy};el.attr(att);}});return parent;}
dragEnd=function(){dragEndInternal.call(this,true);};dragEndInternal=function(realDrag){var parent=this;if(!this.children)
parent=this.parent;parent.animate({'opacity':1},500);var c=parent.bpmnObject.getCenter();snapPoints.push(c);var deleted=false;if(realDrag)
{if(!this.moved&&parent.bpmnObject.clicked)
parent.bpmnObject.clicked();else
parent.bpmnObject.chart.selectedSymbol=null;if((this.bpmnObject)&&(typeof this.bpmnObject.dropped==='undefined'))
{if(this.bpmnObject.hitTest()){if(this.bpmnObject)
{this.bpmnObject.dropped=true;this.bpmnObject.chart.selectedSymbol=null;}}
else
{this.bpmnObject.chart.deleteSymbol(this.bpmnObject);deleted=true;}}
else if(parent.bpmnObject)
{var dropHitResult=parent.bpmnObject.existingHitTest();if(dropHitResult&&dropHitResult.snapBack){parent.bpmnObject.nudgeXY(0-this.sdx,0-this.sdy);parent.bpmnObject.chart.redrawAllLines();parent.bpmnObject.chart.fixParallels();}}}
if(!deleted)
{if(parent.bpmnObject.chart)
{parent.bpmnObject.chart.stopLineAnimation(parent.bpmnObject);}}};