/**
* Joint 0.4 - JavaScript diagramming library.
* Copyright (c) David Durman 2009 - 2011
* Licensed under the MIT license: (http://www.opensource.org/licenses/mit-license.php)
*/(function(g){var e=g.Joint,k=e.point,j=e.rect,c=e.dia={_currentDrag:false,_currentZoom:false,_registeredObjects:{},_registeredJoints:{},Joint:function(){var a=e.apply(null,arguments);this.registerJoint(a);return a},registeredElements:function(){return this._registeredObjects[e.paper().euid()]||(this._registeredObjects[e.paper().euid()]=[])},registeredJoints:function(){return this._registeredJoints[e.paper().euid()]||(this._registeredJoints[e.paper().euid()]=[])},register:function(a){(this._registeredObjects[e.paper().euid()]|| (this._registeredObjects[e.paper().euid()]=[])).push(a)},unregister:function(a){for(var b=this._registeredObjects[e.paper().euid()]||(this._registeredObjects[e.paper().euid()]=[]),d=b.length;d--;)b[d]===a&&b.splice(d,1)},registerJoint:function(a){(this._registeredJoints[e.paper().euid()]||(this._registeredJoints[e.paper().euid()]=[])).push(a)},unregisterJoint:function(a){for(var b=this._registeredJoints[e.paper().euid()]||(this._registeredJoints[e.paper().euid()]=[]),d=b.length;d--;)b[d]===a&&b.splice(d, 1)}};g=c.Element=function(){};g.create=function(a){var b=new this(a);b.init&&b.init(a);b.defaults(b.properties);b.paper.safari();return b};g.extend=function(a){var b=a.constructor=function(f){this.construct(f)};b.base=this;var d=b.prototype=new this;e.Mixin(d,a);e.Supplement(b,this);return b};g.prototype={parentElement:null,toolbox:null,_isElement:true,lastScaleX:1,lastScaleY:1,dx:undefined,dy:undefined,origBBox:undefined,construct:function(a){this.properties={dx:0,dy:0,rot:0,sx:1,sy:1,module:this.module, object:this.object,parent:a.parent};this.shadow=this.wrapper=null;this.shadowAttrs={stroke:"none",fill:"#999",translation:"7,7",opacity:0.5};this.inner=[];this.ghostAttrs={opacity:0.5,"stroke-dasharray":"-",stroke:"black"};this._opt={draggable:true,ghosting:false,toolbox:false};this.paper=e.paper();c.register(this)},defaults:function(a){if(a.shadow){e.Mixin(this.shadowAttrs,a.shadow);this.createShadow()}},euid:function(){return e.generateEuid.call(this)},joints:function(){return this.wrapper.joints()}, yourself:function(){return this.wrapper},updateJoints:function(){var a=this.wrapper.joints();if(a)for(var b=0,d=a.length;b<d;b++)a[b].update()},toggleGhosting:function(){this._opt.ghosting=!this._opt.ghosting;return this},createGhost:function(){this.ghost=this.cloneWrapper(this.ghostAttrs)},createShadow:function(){this.shadowAttrs.rotation=this.wrapper.attrs.rotation;this.shadow=this.cloneWrapper(this.shadowAttrs);this.shadow.toBack()},cloneWrapper:function(a){var b=this.wrapper.attrs,d=this.wrapper.paper, f;switch(this.wrapper.type){case "rect":f=d.rect(b.x,b.y,b.width,b.height,b.r);break;case "circle":f=d.circle(b.cx,b.cy,b.r);break;case "ellipse":f=d.ellipse(b.cx,b.cy,b.rx,b.ry)}f.attr(a);return f},objPos:function(a){switch(this[a].type){case "rect":return k(this[a].attr("x"),this[a].attr("y"));case "circle":case "ellipse":return k(this[a].attr("cx"),this[a].attr("cy"))}},wrapperPos:function(){return this.objPos("wrapper")},ghostPos:function(){return this.objPos("ghost")},toFront:function(){this.shadow&& this.shadow.toFront();this.wrapper&&this.wrapper.toFront();for(var a=0,b=this.inner.length;a<b;a++)this.inner[a].toFront();return this},toBack:function(){for(var a=this.inner.length-1;a>=0;--a)this.inner[a].toBack();this.wrapper&&this.wrapper.toBack();this.shadow&&this.shadow.toBack();return this},dragger:function(a){if(this.wholeShape._opt.draggable){c._currentDrag=this.wholeShape;if(c._currentDrag._opt.ghosting){c._currentDrag.createGhost();c._currentDrag.ghost.toFront()}else c._currentDrag.toFront(); c._currentDrag.removeToolbox();c._currentDrag.translate(1,1);c._currentDrag.dx=a.clientX;c._currentDrag.dy=a.clientY;a.preventDefault&&a.preventDefault()}},zoomer:function(a){c._currentZoom=this;c._currentZoom.toFront();c._currentZoom.removeToolbox();var b=j(c._currentZoom.origBBox);c._currentZoom.dx=a.clientX;c._currentZoom.dy=a.clientY;c._currentZoom.dWidth=b.width*c._currentZoom.lastScaleX;c._currentZoom.dHeight=b.height*c._currentZoom.lastScaleY;a.preventDefault&&a.preventDefault()},translate:function(a, b){this.properties.dx+=a;this.properties.dy+=b;this.wrapper.translate(a,b);this.shadow&&this.shadow.translate(a,b);for(var d=this.inner.length-1;d>=0;--d)this.inner[d].translate(a,b);this.translateToolbox(a,b);this.paper.safari()},setWrapper:function(a){this.wrapper=a;this.wrapper.wholeShape=this;this.type=this.wrapper.type;this.origBBox=this.wrapper.getBBox();if(this._opt&&this._opt.draggable){this.wrapper.mousedown(this.dragger);this.wrapper.node.style.cursor="move"}if(!this.wrapper.joints){this.wrapper._joints= [];this.wrapper.joints=function(){return this._joints}}this.addToolbox();return this},addInner:function(a){this.inner.push(a);a.wholeShape=this;a.parentElement=this;if(a._isElement)a.properties.parent=this.euid();if(!a._isElement&&this._opt&&this._opt.draggable){a.mousedown(this.dragger);a.node.style.cursor="move"}a.toFront();return this},delInner:function(a){for(var b=0,d=this.inner.length;b<d;b++)if(this.inner[b]==a)break;if(b<d){this.inner.splice(b,1);a.parentElement=null;if(a._isElement)a.properties.parent= undefined}return this},addToolbox:function(){if(!this._opt.toolbox)return this;var a=this,b=this.wrapper.getBBox(),d=b.x-10;b=b.y-22;this.toolbox=[];this.toolbox.push(this.paper.rect(d,b,33,22,5).attr({fill:"white"}));this.toolbox.push(this.paper.image("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAALCAYAAACprHcmAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAE5SURBVHjaYvz//z8DsQAggFhARGRkpBETE1M/kGkOxIz//v078+HDh4odO3acBPJ//4eaCBBADCA6Kirq4JlzJ978/vPrNwifOHX4fUhIyFmgvDQQs4HUgDBAALFAbTDX1zNiZmFmBfONDM14WFlZdYFMCSD+AsS/QOIAAcQEVcyIw5m8IJNhHIAAAisGufHMuZNfgE74A8Knzx7/LiLO91tfXx9kOgsjEIDUAQQQ2FqQZ3q7Jk6AWs2gqCbOkZDn8l9AiLuNi4vrxfHjx7cC1X8HCCCwYqiv/aBu5NXQ0FD9+/dfr4uf/te7N1/Mu337ttmbN2/uAwQQzIO/gfg11DNsN4BA/LD4n8f33swF8v8DFQoAaS6AAGLEFilQN3JCbQLhH0B8HyCAGHHFIFQDB1QTSNEXgAADAEQ2gYZ9CcycAAAAAElFTkSuQmCC", d,b,11,11));this.toolbox[this.toolbox.length-1].toFront();e.addEvent(this.toolbox[this.toolbox.length-1].node,"mousedown",function(f){c.Element.prototype.zoomer.apply(a,[f])});this.toolbox.push(this.paper.image("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAALCAYAAACprHcmAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEJSURBVHjaYvj//z8DFGOAnz9/rjl27Jg0AwMDExAzAAQQI0ghFPz/8usZjM3ACJTnYBEC0iyfmZmZZYBCXwECiAkm+evXL4bff34w/P33C4z//PvB8O33awYmJiZeoDQ/ELMBBBALSKGJiQkPOzs7AxsbC8OaTXMZWFhZoEb8g5nFDsTMAAHEBFIIZLwCuo/hy5dvDCF+yQx/fv+BuAvhRDAACCCQM0AO5YRJfv78lSE+Ko/h79+/DP8RJoMBQACheHDv4wYGdOAs28DAyMioCmS+AAggJgYSAEAAoZiMUxHUZIAAYkES4AJSQjD3o4HvQPwXIIDgJgMVM4PCEhREWBT/BUUFQIABAMuFbgea+o0EAAAAAElFTkSuQmCC", d+22,b,11,11));this.toolbox[this.toolbox.length-1].toFront();this.toolbox[this.toolbox.length-1].node.onclick=function(){a.embed()};this.toolbox.push(this.paper.image("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAALCAYAAACprHcmAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEJSURBVHjaYvj//z8DFGOAnz9/rjl27Jg0AwMDExAzAAQQI0ghFPz/8usZjM3ACJTnYBEC0iyfmZmZZYBCXwECiIkBCfz99wuO//z7wfDt92sGJiYmXqAUPxCzAQQQi4mJyX0gQwFZExcXJ8OaTXMYODmZYULsQMwMEEAgk9WB+D0jIyNElJ2NYdXG2QzsHOwMSE4EA4AAYjpz5swvIC3By8sLVrh2yzygiRwQTzD8Q1EMEEBwD/779+//7gcNDCysKN5gcJZtYADaqgpkvgAIILgM0CMYCtEBQAChBB1ORVCTAQKIBUmAC0gJATEnFvXfQSELEEBwk4GKQeHEBgoiLIr/AvEvgAADAH4mYO9cg5S2AAAAAElFTkSuQmCC", d+11,b,11,11));this.toolbox[this.toolbox.length-1].toFront();this.toolbox[this.toolbox.length-1].node.onclick=function(){a.unembed()};this.toolbox.push(this.paper.path("M24.778,21.419 19.276,15.917 24.777,10.415 21.949,7.585 16.447,13.087 10.945,7.585 8.117,10.415 13.618,15.917 8.116,21.419 10.946,24.248 16.447,18.746 21.948,24.248").attr({fill:"#000",stroke:"none"}).translate(d,b).scale(0.5));this.toolbox[this.toolbox.length-1].toFront();this.toolbox[this.toolbox.length-1].node.onclick=function(){a.remove()}; this.toolbox.push(this.paper.image("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAALCAYAAACprHcmAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEjSURBVHjaYvz//z8DsQAggJjwSaanpwsBMReMDxBATAQMO/zv379eRkZGdiBmAgggJiymqaWlpS0GSrIAFZ4A0h5AYR4gZgEIICaoAg6ggolACea/f/9aAulAoDD3169fNwPZ0kA2B0gxQADBTBYECuYCaa7bt2/vACkEYs4zZ84cA9KsQAwKBUaAAGIBqfzz5w8jExPTRiCTXUFBwQ9IfwP5x8TExAJI/4IpBgggsOJ58+Y9B1JRQMwGdOdjoFP2ghRwcnL6A4P2KUghiA8QQGDFQIH/QGf8BDJ/L1myZC8fHx/IeiZmZmbr379/H4ApBgggFlgoANX/A1L/gJoYP336BHIG47Nnz1zu3r0LUvgD5FqAAGLEF4Og0EHy4G+AAAMAho1gqqugDLgAAAAASUVORK5CYII=", d,b+11,11,11));this.toolbox[this.toolbox.length-1].toFront();this.toolbox[this.toolbox.length-1].node.onmousedown=function(){c._currentDrag=a.clone()[0];console.log(c._currentDrag[0])};return this},removeToolbox:function(){if(this.toolbox)for(var a=this.toolbox.length-1;a>=0;--a)this.toolbox[a].remove();this.toolbox=null;return this},toggleToolbox:function(){this._opt.toolbox=!this._opt.toolbox;this._opt.toolbox?this.addToolbox():this.removeToolbox();return this},translateToolbox:function(a,b){if(this.toolbox)for(var d= this.toolbox.length-1;d>=0;--d)this.toolbox[d].translate(a,b)},disconnect:function(){for(var a=this.joints(),b=a.length,d;b--;){d=a[b];if(d.endObject().wholeShape===this){d.freeJoint(d.endObject());d.draw(["dummyEnd"]);d.update()}if(d.startObject().wholeShape===this){d.freeJoint(d.startObject());d.draw(["dummyStart"]);d.update()}}},unregisterFromJoints:function(){for(var a=this.joints(),b=a.length;b--;)a[b].unregister(this);return this},remove:function(){var a=this.inner,b=a.length;this.unregisterFromJoints(); this.disconnect();this.removeToolbox();for(this.unembed();b--;)a[b].remove();this.wrapper.remove();c.unregister(this);this.removed=true;return null},liquidate:function(){for(var a=this.joints(),b=a.length,d,f=this.inner;b--;){d=a[b];d.freeJoint(d.startObject());d.freeJoint(d.endObject());d.clean(["connection","startCap","endCap","handleStart","handleEnd","label"]);c.unregisterJoint(d);d.unregister(this)}this.removeToolbox();this.unembed();for(b=f.length;b--;)f[b].liquidate?f[b].liquidate():f[b].remove(); this.wrapper.remove();c.unregister(this);this.removed=true;return null},draggable:function(a){this._opt.draggable=a;this.wrapper.node.style.cursor=a?"move":null;for(var b=this.inner.length;b--;)this.inner[b].node.style.cursor=a?"move":null;return this},highlight:function(){this.wrapper.attr("stroke","red");return this},unhighlight:function(){this.wrapper.attr("stroke",this.properties.attrs.stroke||"#000");return this},embed:function(){for(var a=c._registeredObjects[this.paper.euid()],b=j(this.wrapper.getBBox()), d=null,f=0,h=a.length;f<h;f++){var i=a[f];if(j(i.getBBox()).containsPoint(b.origin()))d=i;if(i==this.parentElement){i.delInner(this);if(d)break}}d&&d.addInner(this);return this},unembed:function(){if(this.parentElement){this.parentElement.delInner(this);this.parentElement=null;this.properties.parent=undefined}return this},scale:function(a,b){this.properties.sx=a;this.properties.sy=b;this.shadow&&this.shadow.scale.apply(this.shadow,arguments);this.wrapper.scale.apply(this.wrapper,arguments);this.zoom.apply(this, arguments);for(var d=0,f=this.inner.length;d<f;d++){var h=this.inner[d];h._isElement&&h.scale.apply(h,arguments)}if(!this._doNotRedrawToolbox){this.removeToolbox();this.addToolbox()}},zoom:function(){},getBBox:function(){return this.wrapper.getBBox()},joint:function(a,b){var d=this.wrapper.joint.apply(this.wrapper,[a._isElement?a.wrapper:a,b]);e.dia.registerJoint(d);return d},attr:function(){return Raphael.el.attr.apply(this.wrapper,arguments)}};g.mouseMove=function(a){a=a||window.event;if(c._currentDrag){c._currentDrag._opt.ghosting? c._currentDrag.ghost.translate(a.clientX-c._currentDrag.dx,a.clientY-c._currentDrag.dy):c._currentDrag.translate(a.clientX-c._currentDrag.dx,a.clientY-c._currentDrag.dy);c._currentDrag.dx=a.clientX;c._currentDrag.dy=a.clientY}if(c._currentZoom){var b=a.clientY-c._currentZoom.dy;c._currentZoom.dWidth-=a.clientX-c._currentZoom.dx;c._currentZoom.dHeight-=b;if(c._currentZoom.dWidth<1)c._currentZoom.dWidth=1;if(c._currentZoom.dHeight<1)c._currentZoom.dHeight=1;b=c._currentZoom.dWidth/c._currentZoom.origBBox.width; var d=c._currentZoom.dHeight/c._currentZoom.origBBox.height;c._currentZoom._doNotRedrawToolbox=true;c._currentZoom.scale(b,d);r.safari();c._currentZoom.dx=a.clientX;c._currentZoom.dy=a.clientY;c._currentZoom.lastScaleX=b;c._currentZoom.lastScaleY=d}};g.mouseUp=function(){if(c._currentDrag&&c._currentDrag._opt.ghosting){var a=c._currentDrag.ghostPos(),b=c._currentDrag.wrapperPos();c._currentDrag.translate(a.x-b.x,a.y-b.y);c._currentDrag.ghost.remove();c._currentDrag.updateJoints()}if(c._currentDrag){c._currentDrag.addToolbox(); c._currentDrag.toFront();c._currentDrag.translate(1,1)}if(c._currentZoom){c._currentZoom.removeToolbox();c._currentZoom.addToolbox();c._currentZoom.toFront()}c._currentDrag=false;c._currentZoom=false};e.addEvent(document,"mousemove",g.mouseMove);e.addEvent(document,"mouseup",g.mouseUp)})(this);