/*
 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
 */

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