/*
 COPYRIGHT 2009 ESRI

 TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
 Unpublished material - all rights reserved under the
 Copyright Laws of the United States and applicable international
 laws, treaties, and conventions.

 For additional information, contact:
 Environmental Systems Research Institute, Inc.
 Attn: Contracts and Legal Services Department
 380 New York Street
 Redlands, California, 92373
 USA

 email: contracts@esri.com
 */
//>>built
define("esri/toolbars/_VertexEditor",["dojo/_base/declare","dojo/_base/lang","dojo/_base/connect","dojo/_base/array","dojo/has","dijit/Menu","dijit/MenuItem","esri/kernel","esri/toolbars/_VertexMover","esri/geometry/Point","esri/geometry/jsonUtils","dojo/i18n!esri/nls/jsapi"],function(_1,_2,_3,_4,_5,_6,_7,_8,_9,_a,_b,_c){var _d=_1(null,{declaredClass:"esri.toolbars._GraphicVertexEditor",constructor:function(_e,_f,_10){this.graphic=_e;this.map=_f;this.toolbar=_10;var _11=_10._options;this._symbol1=_11.vertexSymbol;this._symbol2=_11.ghostVertexSymbol;var _12=_11.ghostLineSymbol;this._lineStroke={style:_12.style,width:_12.width,color:_12.color};this._canDel=_11.allowDeleteVertices;this._canAdd=_11.allowAddVertices;this._addControllers();},destroy:function(){this._removeControllers();},refresh:function(_13){if(_13){this._removeControllers();this._addControllers();}else{this._refresh(this._vertexMovers);this._refresh(this._mpVertexMovers);}},suspend:function(){if(!this._suspended){this._removeControllers();}this._suspended=true;},resume:function(){if(this._suspended){this._addControllers();}this._suspended=false;},_addControllers:function(){this._firstMoveHandle=_3.connect(_9,"onFirstMove",this,this._firstMoveHandler);this._moveStopHandle=_3.connect(_9,"onMoveStop",this,this._moveStopHandler);this._vertexMovers=this._add(this._getSegments(this.graphic.geometry),this._symbol1);if(this._canAdd){this._mpVertexMovers=this._add(this._getMidpointSegments(this.graphic.geometry),this._symbol2,true);}var _14=this._getGraphicsLayer();this._mouseOverHandle=_3.connect(_14,"onMouseOver",this,this._mouseOverHandler);this._mouseOutHandle=_3.connect(_14,"onMouseOut",this,this._mouseOutHandler);if(this._canDel){this._ctxMenu=new _6({style:"font-size: 12px; margin-left: 5px; margin-top: 5px;"});var _15=(this._ctxDelete=new _7({label:_c.toolbars.edit.deleteLabel,iconClass:"vertexDeleteIcon",style:"outline: none;"}));this._deleteHandle=_3.connect(_15,"onClick",this,this._deleteHandler);this._ctxMenu.addChild(_15);this._ctxMenu.startup();}},_removeControllers:function(){_3.disconnect(this._firstMoveHandle);_3.disconnect(this._moveStopHandle);_3.disconnect(this._mouseOverHandle);_3.disconnect(this._mouseOutHandle);_3.disconnect(this._deleteHandle);if(this._ctxMenu){this._ctxDelete=null;this._unbindCtxNode();this._ctxMenu.destroyRecursive();}this._remove(this._vertexMovers);this._remove(this._mpVertexMovers);this._vertexMovers=this._mpVertexMovers=null;},_add:function(_16,_17,_18){var i,j,_19=this.graphic,_1a=[];for(i=0;i<_16.length;i++){var _1b=_16[i],_1c=[];for(j=0;j<_1b.length;j++){_1c.push(new _9(_1b[j],_17,_19,i,j,_1b.length,this,_18));}_1a.push(_1c);}return _1a;},_remove:function(_1d){if(_1d){_4.forEach(_1d,function(_1e){_4.forEach(_1e,function(_1f){_1f.destroy();});});}},_refresh:function(_20){if(_20){_4.forEach(_20,function(_21){_4.forEach(_21,function(_22){_22.refresh();});});}},_isNew:function(_23){return (_4.indexOf(this._vertexMovers[_23.segIndex],_23)===-1)?true:false;},_getGraphicsLayer:function(){return this.toolbar._scratchGL;},_deleteHandler:function(evt){var _24=this._selectedMover,_25=_24.ptIndex;this._updateRelatedGraphic(_24,_24.relatedGraphic,_24.graphic.geometry,_24.segIndex,_24.ptIndex,_24.segLength,false,true);if(this._canAdd){this._deleteMidpoints(_24);}this._deleteVertex(_24);this.toolbar._endOperation("VERTICES");},_mouseOverHandler:function(evt){var _26=evt.graphic,_27=this._findMover(_26);if(_27){this.toolbar.onVertexMouseOver(this.graphic,_27._getInfo());if(!_27._placeholder){this._selectedMover=_27;if(this._canDel){this._bindCtxNode(_26.getDojoShape().getNode());}}}},_mouseOutHandler:function(evt){var _28=evt.graphic,_29=this._findMover(_28);if(_29){this.toolbar.onVertexMouseOut(this.graphic,_29._getInfo());}},_bindCtxNode:function(_2a){this._unbindCtxNode();this._ctxDelete.set("disabled",(this._selectedMover.segLength<=this.minLength)?true:false);this._ctxMenu.bindDomNode(_2a);this._bindNode=_2a;},_unbindCtxNode:function(){var _2b=this._bindNode;if(_2b){this._ctxMenu.unBindDomNode(_2b);}},_findMover:function(_2c){var i,_2d=[],_2e=this._mpVertexMovers;_4.forEach(this._vertexMovers,function(_2f){_2d=_2d.concat(_2f);});if(_2e){_4.forEach(_2e,function(_30){_2d=_2d.concat(_30);});}for(i=0;i<_2d.length;i++){var _31=_2d[i];if(_31.graphic===_2c){return _31;}}},_firstMoveHandler:function(_32){if(!this._isNew(_32)&&this._canAdd){this._hideRelatedMidpoints(_32);}this.toolbar._beginOperation("VERTICES");},_moveStopHandler:function(_33,tx){var add=this._isNew(_33);if(!tx||!tx.dx&&!tx.dy){if(!add&&this._canAdd){this._showRelatedMidpoints(_33);}return;}this._updateRelatedGraphic(_33,_33.relatedGraphic,_33.graphic.geometry,_33.segIndex,_33.ptIndex,_33.segLength,add);if(this._canAdd){if(add){this._addMidpoints(_33);}else{this._repositionRelatedMidpoints(_33);this._showRelatedMidpoints(_33);}}this.toolbar._endOperation("VERTICES");},_showRelatedMidpoints:function(_34){var i,_35=this._getAdjacentMidpoints(_34.ptIndex,_34.segLength),_36=this._mpVertexMovers[_34.segIndex];for(i=0;i<_35.length;i++){var mvr=_36[_35[i]];mvr.graphic.show();mvr.refresh();}},_hideRelatedMidpoints:function(_37){var i,_38=this._getAdjacentMidpoints(_37.ptIndex,_37.segLength),_39=this._mpVertexMovers[_37.segIndex];for(i=0;i<_38.length;i++){_39[_38[i]].graphic.hide();}},_repositionRelatedMidpoints:function(_3a){var i,_3b=this._getAdjacentMidpoints(_3a.ptIndex,_3a.segLength),_3c=this._mpVertexMovers[_3a.segIndex];for(i=0;i<_3b.length;i++){var _3d=this._getAdjacentVertices(_3b[i],_3a.segLength);var _3e=_3a.relatedGraphic.geometry.getPoint(_3a.segIndex,_3d[0]),_3f=_3a.relatedGraphic.geometry.getPoint(_3a.segIndex,_3d[1]);var _40=new _a({x:(_3e.x+_3f.x)/2,y:(_3e.y+_3f.y)/2,spatialReference:_3e.spatialReference.toJson()});_3c[_3b[i]].graphic.setGeometry(_40);}},_addMidpoints:function(_41){var _42=_41.segIndex,_43=_41.ptIndex,_44=_41.segLength;var _45=_43+1;var i,_46=_44+1;this._mpVertexMovers[_42].splice(_43,1);var _47=this._vertexMovers[_42];for(i=0;i<_45;i++){_47[i].segLength+=1;}for(i=_45;i<_47.length;i++){_47[i].ptIndex+=1;_47[i].segLength+=1;}_41.ptIndex=_45;_41.segLength=_47.length+1;_47.splice(_45,0,_41);_41.graphic.setSymbol(this._symbol1);_47=this._mpVertexMovers[_42];for(i=0;i<_43;i++){_47[i].segLength+=1;}for(i=_43;i<_44-1;i++){_47[i].ptIndex+=1;_47[i].segLength+=1;}var _48=this._getAdjacentVertices(_43,_46);var _49=this._getAdjacentVertices(_43+1,_46);var _4a,_4b;_4a=_41.relatedGraphic.geometry.getPoint(_41.segIndex,_48[0]);_4b=_41.relatedGraphic.geometry.getPoint(_41.segIndex,_48[1]);var _4c=new _a({x:(_4a.x+_4b.x)/2,y:(_4a.y+_4b.y)/2,spatialReference:_4a.spatialReference.toJson()});_4a=_41.relatedGraphic.geometry.getPoint(_41.segIndex,_49[0]);_4b=_41.relatedGraphic.geometry.getPoint(_41.segIndex,_49[1]);var _4d=new _a({x:(_4a.x+_4b.x)/2,y:(_4a.y+_4b.y)/2,spatialReference:_4a.spatialReference.toJson()});var _4e=new _9(_4c,this._symbol2,this.graphic,_41.segIndex,_43,_46,this,true);var _4f=new _9(_4d,this._symbol2,this.graphic,_41.segIndex,_43+1,_46,this,true);_47.splice(_43,0,_4e,_4f);},_deleteVertex:function(_50){var i,_51=_50.segIndex,_52=_50.ptIndex;var _53=this._vertexMovers[_51];for(i=0;i<_52;i++){_53[i].segLength-=1;}for(i=_52+1;i<_53.length;i++){var mvr=_53[i];mvr.ptIndex-=1;mvr.segLength-=1;}_53.splice(_52,1);var _54=_50._getInfo();_50.destroy();this.toolbar.onVertexDelete(this.graphic,_54);}});_2.mixin(_d,{create:function(_55,map,_56){var _57=_55.geometry.type;switch(_57){case "multipoint":return new _d.MultipointVertexEditor(_55,map,_56);break;case "polyline":return new _d.PolylineVertexEditor(_55,map,_56);break;case "polygon":return new _d.PolygonVertexEditor(_55,map,_56);break;}}});_d.MultipointVertexEditor=_1(_d,{declaredClass:"esri.toolbars._MultipointVertexEditor",minLength:1,constructor:function(){this._moveStartHandle=_3.connect(_9,"onMoveStart",this,this._moveStartHandler);_3.disconnect(this._firstMoveHandle);},destroy:function(){this.inherited(arguments);_3.disconnect(this._moveStartHandle);},_getSegments:function(_58){var i,_59=_58.points,_5a=[],sr=_58.spatialReference;for(i=0;i<_59.length;i++){var _5b=_59[i];_5a.push(new _a({x:_5b[0],y:_5b[1],spatialReference:sr.toJson()}));}return [_5a];},_getMidpointSegments:function(_5c){return [];},_getControlPoints:function(_5d,_5e,_5f,_60,_61){return [];},_getGraphicsLayer:function(){return this.graphic._graphicsLayer;},_mouseOverHandler:function(evt){var _62=evt.graphic,_63=this._findMover(evt);if(_63){this.toolbar.onVertexMouseOver(_62,_63._getInfo());this._selectedMover=_63;if(this._canDel){this._bindCtxNode(_63.graphic.getDojoShape().getNode());}}},_mouseOutHandler:function(evt){var _64=evt.graphic,_65=this._findMover(evt);if(_65){this.toolbar.onVertexMouseOut(_64,_65._getInfo());}},_findMover:function(evt){var i,_66=[].concat(this._vertexMovers[0]),_67=evt.target;for(i=0;i<_66.length;i++){var _68=_66[i];if(_68.graphic.getDojoShape().getNode()===_67){return _68;}}},_moveStartHandler:function(_69){var _6a=_69.relatedGraphic.geometry,_6b=_69.ptIndex;var _6c=_69.segLength-1;var _6d=_6a.points;var _6e=_6d.splice(_6b,1);_6d.push(_6e[0]);var j,_6f=this._vertexMovers[0];for(j=_6c;j>_6b;j--){_6f[j].ptIndex-=1;}_6e=_6f.splice(_6b,1);_6f.push(_6e[0]);_6e[0].ptIndex=_6c;},_moveStopHandler:function(_70){this._updateRelatedGraphic(_70,_70.relatedGraphic,_70.graphic.geometry,_70.segIndex,_70.ptIndex,_70.segLength);this.toolbar._endOperation("VERTICES");},_updateRelatedGraphic:function(_71,_72,_73,_74,_75,_76,add,del){var _77=_72.geometry;if(del){_77.removePoint(_75);}else{_77.setPoint(_75,_73);}_72.setGeometry(_77);},_deleteMidpoints:function(_78){}});_d.PolylineVertexEditor=_1(_d,{declaredClass:"esri.toolbars._PolylineVertexEditor",minLength:2,_getSegments:function(_79){var i,j,_7a=_79.paths,_7b=[];for(i=0;i<_7a.length;i++){var _7c=_7a[i],_7d=[];for(j=0;j<_7c.length;j++){_7d.push(_79.getPoint(i,j));}_7b.push(_7d);}return _7b;},_getMidpointSegments:function(_7e){var i,j,_7f=_7e.paths,_80=[],sr=_7e.spatialReference;for(i=0;i<_7f.length;i++){var _81=_7f[i],_82=[];for(j=0;j<_81.length-1;j++){var _83=_7e.getPoint(i,j),_84=_7e.getPoint(i,j+1);var _85=(_83.x+_84.x)/2,_86=(_83.y+_84.y)/2;var _87=new _a({x:_85,y:_86,spatialReference:sr.toJson()});_82.push(_87);}_80.push(_82);}return _80;},_getControlPoints:function(_88,_89,_8a,_8b,_8c){var map=this.map,_8d,_8e,pt1,pt2;if(this._isNew(_88)){_8d=_8b;_8e=_8b+1;if(_8d>=0){pt1=map.toScreen(_89.getPoint(_8a,_8d));}if(_8e<=_8c){pt2=map.toScreen(_89.getPoint(_8a,_8e));}}else{_8d=_8b-1;_8e=_8b+1;if(_8d>=0){pt1=map.toScreen(_89.getPoint(_8a,_8d));}if(_8e<_8c){pt2=map.toScreen(_89.getPoint(_8a,_8e));}}return [pt1,pt2];},_getAdjacentMidpoints:function(_8f,_90){var _91=[];var _92=_8f-1;if(_92>=0){_91.push(_92);}var _93=_8f;if(_93<_90-1){_91.push(_93);}return _91;},_getAdjacentVertices:function(_94,_95){return [_94,_94+1];},_deleteMidpoints:function(_96){var _97=_96.segIndex,_98=_96.ptIndex,_99=_96.segLength;var _9a=this._mpVertexMovers[_97],_9b=_9a.length-1;var _9c=this._getAdjacentMidpoints(_98,_99).sort();var i,min=_9c[0];for(i=0;i<min;i++){_9a[i].segLength-=1;}for(i=min+1;i<_9a.length;i++){var mvr=_9a[i];mvr.ptIndex-=1;mvr.segLength-=1;}if(_9c.length===1){_9a.splice(min,1)[0].destroy();}else{var _9d=this._getAdjacentVertices(min,_9b);var _9e=_96.relatedGraphic.geometry.getPoint(_96.segIndex,_9d[0]);var _9f=_96.relatedGraphic.geometry.getPoint(_96.segIndex,_9d[1]);var _a0=new _a({x:(_9e.x+_9f.x)/2,y:(_9e.y+_9f.y)/2,spatialReference:_9e.spatialReference.toJson()});var _a1=new _9(_a0,this._symbol2,this.graphic,_96.segIndex,min,_9b,this,true);var _a2=_9a.splice(min,_9c.length,_a1);for(i=0;i<_a2.length;i++){_a2[i].destroy();}}},_updateRelatedGraphic:function(_a3,_a4,_a5,_a6,_a7,_a8,add,del){var _a9=_a4.geometry;if(add){_a9.insertPoint(_a6,_a7+1,_b.fromJson(_a5.toJson()));}else{if(del){_a9.removePoint(_a6,_a7);}else{_a9.setPoint(_a6,_a7,_b.fromJson(_a5.toJson()));}}_a4.setGeometry(_a9);}});_d.PolygonVertexEditor=_1(_d,{declaredClass:"esri.toolbars._PolygonVertexEditor",minLength:3,_getSegments:function(_aa){var i,j,_ab=_aa.rings,_ac=[];for(i=0;i<_ab.length;i++){var _ad=_ab[i],_ae=[];for(j=0;j<_ad.length-1;j++){_ae.push(_aa.getPoint(i,j));}_ac.push(_ae);}return _ac;},_getMidpointSegments:function(_af){var i,j,_b0=_af.rings,_b1=[],sr=_af.spatialReference;for(i=0;i<_b0.length;i++){var _b2=_b0[i],_b3=[];for(j=0;j<_b2.length-1;j++){var _b4=_af.getPoint(i,j),_b5=_af.getPoint(i,j+1);var _b6=(_b4.x+_b5.x)/2,_b7=(_b4.y+_b5.y)/2;var _b8=new _a({x:_b6,y:_b7,spatialReference:sr.toJson()});_b3.push(_b8);}_b1.push(_b3);}return _b1;},_getControlPoints:function(_b9,_ba,_bb,_bc,_bd){var map=this.map,_be,_bf,pt1,pt2;if(this._isNew(_b9)){_be=_bc;_bf=(_bc+1)%_bd;}else{_be=_bc-1;_be=_be<0?(_bd+_be)%_bd:_be;_bf=(_bc+1)%_bd;}pt1=map.toScreen(_ba.getPoint(_bb,_be));pt2=map.toScreen(_ba.getPoint(_bb,_bf));return [pt1,pt2];},_getAdjacentMidpoints:function(_c0,_c1){var _c2=_c0-1;_c2=_c2<0?(_c1+_c2)%_c1:_c2;var _c3=_c0;return [_c2,_c3];},_getAdjacentVertices:function(_c4,_c5){return [_c4,(_c4+1)%_c5];},_deleteMidpoints:function(_c6){var _c7=_c6.segIndex,_c8=_c6.ptIndex,_c9=_c6.segLength;var _ca=this._mpVertexMovers[_c7],_cb=_ca.length-1,_cc=this._getAdjacentMidpoints(_c8,_c9).sort(),_cd,_ce,_cf,_d0,i,_d1,mvr,min=_cc[0],max=_cc[_cc.length-1];if(_c8===0){_cd=this._getAdjacentVertices(_cb-1,_cb);_ce=_c6.relatedGraphic.geometry.getPoint(_c6.segIndex,_cd[0]);_cf=_c6.relatedGraphic.geometry.getPoint(_c6.segIndex,_cd[1]);_d0=new _a({x:(_ce.x+_cf.x)/2,y:(_ce.y+_cf.y)/2,spatialReference:_ce.spatialReference.toJson()});_d1=new _9(_d0,this._symbol2,this.graphic,_c6.segIndex,_cb-1,_cb,this,true);_ca.splice(max,1,_d1)[0].destroy();_ca.splice(min,1)[0].destroy();for(i=0;i<_ca.length-1;i++){mvr=_ca[i];mvr.ptIndex-=1;mvr.segLength-=1;}}else{_cd=this._getAdjacentVertices(min,_cb);_ce=_c6.relatedGraphic.geometry.getPoint(_c6.segIndex,_cd[0]);_cf=_c6.relatedGraphic.geometry.getPoint(_c6.segIndex,_cd[1]);_d0=new _a({x:(_ce.x+_cf.x)/2,y:(_ce.y+_cf.y)/2,spatialReference:_ce.spatialReference.toJson()});_d1=new _9(_d0,this._symbol2,this.graphic,_c6.segIndex,min,_cb,this,true);var _d2=_ca.splice(min,_cc.length,_d1);for(i=0;i<_d2.length;i++){_d2[i].destroy();}for(i=0;i<min;i++){_ca[i].segLength-=1;}for(i=min+1;i<_ca.length;i++){mvr=_ca[i];mvr.ptIndex-=1;mvr.segLength-=1;}}},_updateRelatedGraphic:function(_d3,_d4,_d5,_d6,_d7,_d8,add,del){var _d9=_d4.geometry;if(add){_d9.insertPoint(_d6,_d7+1,_b.fromJson(_d5.toJson()));}else{if(del){_d9.removePoint(_d6,_d7);if(_d7===0){_d9.setPoint(_d6,_d8-1,_b.fromJson(_d9.getPoint(_d6,0).toJson()));}}else{_d9.setPoint(_d6,_d7,_b.fromJson(_d5.toJson()));if(_d7===0){_d9.setPoint(_d6,_d8,_b.fromJson(_d5.toJson()));}}}_d4.setGeometry(_d9);}});if(_5("extend-esri")){_2.setObject("toolbars._GraphicVertexEditor",_d,_8);_2.setObject("toolbars._MultipointVertexEditor",_d.MultipointVertexEditor,_8);_2.setObject("toolbars._PolylineVertexEditor",_d.PolylineVertexEditor,_8);_2.setObject("toolbars._PolygonVertexEditor",_d.PolygonVertexEditor,_8);}return _d;});