
var zoom=20.0;var graphWidth;var graphHeight;var offsetCentreX;var offsetCentreY;Vector.prototype.screenX=function(){return(this.x*zoom)+offsetCentreX;};Vector.prototype.screenY=function(){return(this.y*zoom)+offsetCentreY;};var g=new Graph();var paper=null;var graphContainer=null;var drawIntervalId=-1;var containerOffset;function unloadRelationshipGraph(){if(drawIntervalId>-1){$(window).unbind('resize',resizeGraph);for(var i=0;i<g.nodes.length;i++){var node=g.nodes[i];for(var e=0;e<node.outEdges.length;e++){var edge=node.outEdges[e];edge.uiElement.remove();if(edge.uiArrowElement!=null)
edge.uiArrowElement.remove();edge.uiElement=undefined;delete node.outEdges[e];edge=undefined;}
node.uiElement.remove();node.uiElement=undefined;delete g.nodes[i];g.nodes[i]=undefined;}
g.nodes.length=0;g.nodes=[];g.edges.length=0;g.edges=[];paper.clear();paper.remove();paper=undefined;g=undefined;g=new Graph();paper=null;graphContainer.onmousemove=null;graphContainer.onmouseup=null;drawIntervalId=-1;}};function wordwrap(str,width,brk,cut){brk=brk||'\n';width=Math.floor(width)||75;if(width<15)
width=15;cut=cut||false;if(!str){return str;}
var regex='.{1,'+width+'}(\\s|$)'+(cut?'|.{'+width+'}|.+$':'|\\S+?(\\s|$)');return str.match(RegExp(regex,'g')).join(brk);}
function loadRelationshipList(parentEleId,isUses){var thisImage=$('#'+parentEleId).attr('data-typeicon');var thisText=$('#'+parentEleId).attr('data-name');thisText=wordwrap(thisText,(thisText.length/2));var thisNode=new Node(Vector.random(),1.0,1.0,1.0,{image:thisImage,text:thisText,id:1,optional:'',dir:'',uses:isUses,expanded:true});g.addNode(thisNode);loadNodesList('#'+parentEleId,thisNode,isUses);$('#'+parentEleId).hide();showRelationshipGraph('editRelGraph');}
function loadNodesList(elementId,thisNode,isUses){$(elementId).find('li').each(function(){var childImage=$(this).find('a > img:first').attr('src');var childText=$(this).text().trim();childText=wordwrap(childText,(childText.length/2));childText+='\n('+$(this).closest('ul').attr('data-section')+')';var linkedNode=new Node(Vector.random(),1.0,1.0,1.0,{image:childImage,text:childText,id:$(this).attr('data-processId'),optional:$(this).attr('data-optional'),dir:$(this).attr('data-direction'),uses:isUses,expanded:false});g.addNode(linkedNode);g.addEdge(new Edge(thisNode,linkedNode,4.0,20.0));});}
function loadExplorerData(data,parentEleId){var nodeArray=[];var explorerNodes=$(data).find('.step');explorerNodes.each(function(){nodeArray[$(this).attr('nodeId')]=g.addNode(new Node(Vector.random(),8.0,8.0,1.0));});explorerNodes.each(function(){var thisNode=nodeArray[$(this).attr('nodeId')];if(thisNode){$.each($(this).attr('targetId').split(','),function(index,value){var otherNode=nodeArray[value];if(otherNode)
g.addEdge(new Edge(thisNode,otherNode,4.0,12.5));});}});showRelationshipGraph(parentEleId);}
function onGotRelationships(data,thisNode,isUses){for(var i=0;i<g.nodes.length;i++){var node=g.nodes[i];node.velocity=new Vector(0.0,0.0);}
loadNodesList(data.d,thisNode,isUses);}
var maxIcon;var minIcon;var hoverNode;var lockGraph;var unlockGraph;var snapshotGraph;var fdl;function getGraphSizes(){graphWidth=$(window).width()-350;graphHeight=$(window).height()-containerOffset.top;offsetCentreX=(graphWidth/2);offsetCentreY=(graphHeight/2);}
function resizeGraph(){getGraphSizes();if(paper&&paper!=undefined)
paper.setSize(graphWidth,graphHeight);}
Array.prototype.clean=function(deleteValue){for(var i=0;i<this.length;i++){if(this[i]==deleteValue){this.splice(i,1);i--;}}
return this;};var inRemoveNode=false;function showRelationshipGraph(parentEleId){if(paper==null){graphContainer=document.getElementById(parentEleId);containerOffset=$('#'+parentEleId).offset();getGraphSizes();$(window).bind('resize',resizeGraph);paper=Raphael(graphContainer,graphWidth,graphHeight);maxIcon=paper.path("M22.646,19.307c0.96-1.583,1.523-3.435,1.524-5.421C24.169,8.093,19.478,3.401,13.688,3.399C7.897,3.401,3.204,8.093,3.204,13.885c0,5.789,4.693,10.481,10.484,10.481c1.987,0,3.839-0.563,5.422-1.523l7.128,7.127l3.535-3.537L22.646,19.307zM13.688,20.369c-3.582-0.008-6.478-2.904-6.484-6.484c0.006-3.582,2.903-6.478,6.484-6.486c3.579,0.008,6.478,2.904,6.484,6.486C20.165,17.465,17.267,20.361,13.688,20.369zM15.687,9.051h-4v2.833H8.854v4.001h2.833v2.833h4v-2.834h2.832v-3.999h-2.833V9.051z").attr({fill:"#fff",stroke:"#777"});maxIcon.click(function(e){hoverNode.userData.expanded=true;$.ajax({type:'POST',contentType:"application/json; charset=utf-8",dataType:"json",data:"{processId:"+hoverNode.userData.id+"}",url:_reposRoot+'/ProcessDefinition.asmx/GetProcessRelationshipsHtml',success:function(data){onGotRelationships(data,hoverNode,true);}});maxIcon.hide();e.preventDefault();});maxIcon.hover(function(){this.show();this.attr("fill","#ccc");},function(){this.attr("fill","#fff");this.hide();});maxIcon.hide();minIcon=paper.path("M22.646,19.307c0.96-1.583,1.523-3.435,1.524-5.421C24.169,8.093,19.478,3.401,13.688,3.399C7.897,3.401,3.204,8.093,3.204,13.885c0,5.789,4.693,10.481,10.484,10.481c1.987,0,3.839-0.563,5.422-1.523l7.128,7.127l3.535-3.537L22.646,19.307zM13.688,20.369c-3.582-0.008-6.478-2.904-6.484-6.484c0.006-3.582,2.903-6.478,6.484-6.486c3.579,0.008,6.478,2.904,6.484,6.486C20.165,17.465,17.267,20.361,13.688,20.369zM8.854,11.884v4.001l9.665-0.001v-3.999L8.854,11.884z").attr({fill:"#fff",stroke:"#777"});minIcon.click(function(e){if(!inRemoveNode)
{inRemoveNode=true;setTimeout(function(){doNodeRemove(hoverNode);},200);}
e.preventDefault();});minIcon.hover(function(){this.show();this.attr("fill","#ccc");},function(){this.attr("fill","#fff");this.hide();});minIcon.hide();graphContainer.onmousemove=function(e){e=e||window.event;if(nodeDragging){var posx=0.0;var posy=0.0;if(!e)var e=window.event;if(e.pageX||e.pageY){posx=e.pageX;posy=e.pageY;}
else if(e.clientX||e.clientY){posx=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;posy=e.clientY+document.body.scrollTop+document.documentElement.scrollTop;}
posx-=containerOffset.left;posy-=containerOffset.top;nodeDragging.graphNode.position.x=(posx-offsetCentreX)/zoom;nodeDragging.graphNode.position.y=(posy-offsetCentreY)/zoom;if(fdl.stable){var v1=nodeDragging.graphNode.getCenter();for(var e=0;e<nodeDragging.graphNode.inEdges.length;e++){var edge=nodeDragging.graphNode.inEdges[e];var v2=edge.getSourceNode().getCenter();moveRelationshipEdge(v2,v1,edge,10);}}
paper.safari();}};graphContainer.onmouseup=function(){if(nodeDragging){nodeDragging.graphNode.inManualMove=false;}
nodeDragging=false;};fdl=new ForceDirectedLayout(g);addGraphButtons();fdl.startLayout();drawIntervalId=0;setTimeout(function(){drawRelationshipGraph(paper,g);},500);}};function doNodeRemove(removeNode){for(var i=0;i<g.nodes.length;i++){var node=g.nodes[i];if(node==removeNode)
{for(var e=0;e<node.inEdges.length;e++){var edge=node.inEdges[e];edge.uiElement.remove();if(edge.uiArrowElement!=null)
edge.uiArrowElement.remove();edge.uiElement=undefined;for(var eT=0;eT<edge.sourceNode.outEdges.length;eT++){var edgeOut=edge.sourceNode.outEdges[eT];if(edgeOut==edge){delete edge.sourceNode.outEdges[eT];edge.sourceNode.outEdges[eT]=undefined;}}
delete node.inEdges[e];edge=undefined;}
for(var e=0;e<node.outEdges.length;e++){var edge=node.outEdges[e];edge.uiElement.remove();if(edge.uiArrowElement!=null)
edge.uiArrowElement.remove();edge.uiElement=undefined;for(var eT=0;eT<edge.targetNode.inEdges.length;eT++){var edgeIn=edge.targetNode.inEdges[eT];if(edgeIn==edge){delete edge.targetNode.inEdges[eT];edge.targetNode.inEdges[eT]=undefined;}}
delete node.inEdges[e];edge=undefined;}
node.uiElement.unhover(nodeHoverIn,nodeHoverOut);node.uiElement.remove();node.uiElement=undefined;delete g.nodes[i];g.nodes[i]=undefined;break;}}
g.nodes.clean(undefined);for(var i=0;i<g.nodes.length;i++){var node=g.nodes[i];node.outEdges.clean(undefined);node.inEdges.clean(undefined);}
minIcon.hide();maxIcon.hide();inRemoveNode=false;return false;}
var allElements;function addGraphButtons(){unlockGraph=paper.path("M20.375,12.833h-2.209V10c0,0,0,0,0-0.001c0-2.389,1.945-4.333,4.334-4.333c2.391,0,4.335,1.944,4.335,4.333c0,0,0,0,0,0v2.834h2V9.999h-0.001c-0.001-3.498-2.836-6.333-6.334-6.333S16.166,6.502,16.166,10v2.833H3.125V25h17.25V12.833z").attr({fill:"#fff",stroke:"#777"});lockGraph=paper.path("M22.335,12.833V9.999h-0.001C22.333,6.501,19.498,3.666,16,3.666S9.666,6.502,9.666,10h0v2.833H7.375V25h17.25V12.833H22.335zM11.667,10C11.667,10,11.667,10,11.667,10c0-2.39,1.944-4.334,4.333-4.334c2.391,0,4.335,1.944,4.335,4.333c0,0,0,0,0,0v2.834h-8.668V10z").attr({fill:"#ccc",stroke:"#777"});lockGraph.translate(-4,0);lockGraph.hide();paper.set(unlockGraph,lockGraph).click(function(){if(fdl.stable){fdl.stable=false;lockGraph.hide();unlockGraph.show();}
else{fdl.stable=true;lockGraph.show();unlockGraph.hide();}});if(isProcessAuthor){snapshotGraph=paper.path("M24.25,10.25H20.5v-1.5h-9.375v1.5h-3.75c-1.104,0-2,0.896-2,2v10.375c0,1.104,0.896,2,2,2H24.25c1.104,0,2-0.896,2-2V12.25C26.25,11.146,25.354,10.25,24.25,10.25zM15.812,23.499c-3.342,0-6.06-2.719-6.06-6.061c0-3.342,2.718-6.062,6.06-6.062s6.062,2.72,6.062,6.062C21.874,20.78,19.153,23.499,15.812,23.499zM15.812,13.375c-2.244,0-4.062,1.819-4.062,4.062c0,2.244,1.819,4.062,4.062,4.062c2.244,0,4.062-1.818,4.062-4.062C19.875,15.194,18.057,13.375,15.812,13.375z").attr({fill:"#ccc",stroke:"#777"});snapshotGraph.translate(30,1);allElements=paper.set();snapshotGraph.click(function(e){lockGraph.remove();unlockGraph.remove();maxIcon.remove();minIcon.remove();snapshotGraph.remove();var bot=paper.bottom;while(bot){if(bot&&bot.type!="path")
allElements.push(bot);bot=bot.next;}
var allBB=allElements.getBBox();allElements.translate(-allBB.x+5,-allBB.y+5);bot=paper.bottom;while(bot){if(bot&&bot.type=="path")
bot.translate(-allBB.x+5,-allBB.y+5);bot=bot.next;}
graphJson=paper.serialize.json();var dParams={processId:processId,json:graphJson};$.ajax({type:'POST',contentType:"application/json; charset=utf-8",dataType:"json",data:JSON.stringify(dParams),url:_reposRoot+'/ProcessDefinition.asmx/SetProcessRelationshipsJson',success:function(){$tabs.tabs('select','#tabsDesc');}});});}}
var nodeHoverIn=function(){hoverNode=this.graphNode;if(!hoverNode.userData.expanded)
maxIcon.show();minIcon.show();positionMaxMinIcons();}
function positionMaxMinIcons(){if(maxIcon!=undefined&&maxIcon.node.style.display!="none")
{var bb=hoverNode.uiElement.getBBox();var iconBB=maxIcon.getBBox();maxIcon.translate(bb.x-iconBB.x+bb.width-iconBB.width,bb.y-iconBB.y);maxIcon.toFront();}
if(minIcon!=undefined&&minIcon.node.style.display!="none")
{var bb=hoverNode.uiElement.getBBox();var iconBB=minIcon.getBBox();minIcon.translate(bb.x-iconBB.x,bb.y-iconBB.y);minIcon.toFront();}}
var nodeHoverOut=function(){maxIcon.hide();minIcon.hide();}
var nodeDragging=false;var dragger=function(e){nodeDragging=this;nodeDragging.graphNode.inManualMove=true;e.preventDefault&&e.preventDefault();};function genRandomColorHex(){var colors=[14];colors[0]="0";colors[1]="1";colors[2]="2";colors[3]="3";colors[4]="4";colors[5]="5";colors[5]="6";colors[6]="7";colors[7]="8";;colors[8]="9";colors[9]="a";colors[10]="b";colors[11]="c";colors[12]="d";colors[13]="e";colors[14]="f";var digit=new Array(5);var color="";for(i=0;i<6;i++){digit[i]=colors[Math.round(Math.random()*14)]
color=color+digit[i]}
return color;};var elapsed=500;var inGraphDraw=false;function drawRelationshipGraph(paper,g){if(inRemoveNode)
{setTimeout(function(){drawRelationshipGraph(paper,g);},75);return;}
if(drawIntervalId==-1)
return;inGraphDraw=true;var start=new Date().getTime();var anyMovement=false;for(var i=0;i<g.nodes.length;i++){var node=g.nodes[i];var v1=node.getCenter();if(node.uiElement==null){if(node.userData!=undefined){var nodeSet=paper.set();var myCaption=paper.text(v1.screenX(),v1.screenY(),node.userData.text).attr({"fill":"#777","font-size":11});var captBBox=myCaption.getBBox();var c=paper.image(node.userData.image,captBBox.x+(captBBox.width/2)-9,captBBox.y-16,18,18);nodeSet.push(myCaption);nodeSet.push(c);captBBox=nodeSet.getBBox();var myCaptionRectangle=paper.rect(captBBox.x-5,captBBox.y-5,captBBox.width+10,captBBox.height+10,5).attr({"stroke":"#777","fill":"#fff","stroke-width":1});nodeSet.push(myCaptionRectangle);myCaption.graphNode=node;myCaptionRectangle.graphNode=node;c.graphNode=node;c.toFront();myCaption.toFront();nodeSet.graphNode=node;node.uiElement=nodeSet;node.mass=captBBox.width;nodeSet.attr("cursor","move");nodeSet.mousedown(dragger);if(node.userData.id>0){nodeSet.hover(nodeHoverIn,nodeHoverOut);}}
else{var c=paper.circle(v1.screenX(),v1.screenY(),node.width);c.attr("fill",'#'+genRandomColorHex());c.attr("cursor","move");c.graphNode=node;node.uiElement=c;c.mousedown(dragger);}}
else{var c=node.uiElement;var bb=c.getBBox();var ox=bb.x+(bb.width/2.0);var oy=bb.y+(bb.height/2.0);var moveX=v1.screenX()-ox;var moveY=v1.screenY()-oy;if(Math.abs(moveX+moveY)>0.2){c.translate(moveX,moveY);if(hoverNode==node)
positionMaxMinIcons();anyMovement=true;}}
if(anyMovement){for(var e=0;e<node.outEdges.length;e++){var edge=node.outEdges[e];var v2=edge.getTargetNode().getCenter();moveRelationshipEdge(v1,v2,edge,70);}}}
if(anyMovement)
elapsed=new Date().getTime()-start;else
elapsed=200;if(elapsed<75)
elapsed=75;if(fdl.stable)
elapsed=-20;inGraphDraw=false;setTimeout(function(){drawRelationshipGraph(paper,g);},elapsed+50);paper.safari();};function moveRelationshipEdge(v1,v2,edge,speed){var x1=Math.floor(v1.screenX());var y1=Math.floor(v1.screenY());var x2=Math.floor(v2.screenX());var y2=Math.floor(v2.screenY());var lineAttrs={"stroke":"#999","fill-opacity":.3};if(edge.uiElement==null){if(edge.getTargetNode().userData!=undefined){if(edge.getTargetNode().userData.optional=='true')
lineAttrs={"stroke-dasharray":"- ","stroke":"#999","fill-opacity":.3};}
var c=paper.path('M'+x1+','+y1+','+'L'+x2+','+y2).attr(lineAttrs);edge.uiElement=c;c.toBack();}
else
edge.uiElement.animate({path:'M'+x1+','+y1+','+'L'+x2+','+y2},speed);if(edge.uiArrowElement!=null){edge.uiArrowElement.remove();edge.uiArrowElement=undefined;}
if(edge.getTargetNode().userData!=undefined){var realDir=0;if(edge.getTargetNode().userData.dir=='in'){if(edge.getTargetNode().userData.uses)
realDir=1;else
realDir=2;}else if(edge.getTargetNode().userData.dir=='out'){if(edge.getTargetNode().userData.uses)
realDir=2;else
realDir=1;}
if(realDir==1){var arrowHeadLength=8;var lineAngle=Math.atan2(y2-y1,x2-x1);var cosAngle=Math.cos(lineAngle);var sinAngle=Math.sin(lineAngle);x1=((x1+x2)/2);y1=((y1+y2)/2);var y3=y1+(arrowHeadLength*sinAngle+(arrowHeadLength*cosAngle));var x3=x1+(arrowHeadLength*cosAngle-(arrowHeadLength*sinAngle));var y4=y1-(arrowHeadLength*cosAngle-(arrowHeadLength*sinAngle));var x4=x1+(arrowHeadLength*cosAngle+(arrowHeadLength*sinAngle));edge.uiArrowElement=paper.path('M'+x1+' '+y1+' L'+x3+' '+y3+' M'+x1+' '+y1+' L'+x4+' '+y4).attr(lineAttrs);}
else if(realDir==2){var arrowHeadLength=8;var lineAngle=Math.atan2(y2-y1,x2-x1);var cosAngle=Math.cos(lineAngle);var sinAngle=Math.sin(lineAngle);x2=((x1+x2)/2);y2=((y1+y2)/2);var y3=y2+(-arrowHeadLength*sinAngle+(arrowHeadLength*cosAngle));var x3=x2+(-arrowHeadLength*cosAngle-(arrowHeadLength*sinAngle));var y4=y2-(arrowHeadLength*cosAngle+(arrowHeadLength*sinAngle));var x4=x2+(-arrowHeadLength*cosAngle+(arrowHeadLength*sinAngle));edge.uiArrowElement=paper.path('M'+x2+' '+y2+' L'+x3+' '+y3+' M'+x2+' '+y2+' L'+x4+' '+y4).attr(lineAttrs);}
if(edge.uiArrowElement!=null)
edge.uiArrowElement.toBack();}}