/*
 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/_Box",["dojo/_base/declare","dojo/_base/lang","dojo/_base/array","dojo/_base/connect","dojo/has","dojo/dom-style","dojox/gfx/Moveable","dojox/gfx/matrix","esri/kernel","esri/lang","esri/geometry/Point","esri/geometry/Polyline","esri/geometry/webMercatorUtils","esri/geometry/jsonUtils","esri/graphic"],function(_1,_2,_3,_4,_5,_6,_7,_8,_9,_a,_b,_c,_d,_e,_f){var BOX=_1(null,{declaredClass:"esri.toolbars._Box",constructor:function(_10,map,_11,_12,_13,_14){this._graphic=_10;this._map=map;this._toolbar=_11;this._scale=_12;this._rotate=_13;this._defaultEventArgs={};this._scaleEvent="Scale";this._rotateEvent="Rotate";this._uniformScaling=_14;var _15=_11._options;this._markerSymbol=_15.boxHandleSymbol;this._lineSymbol=_15.boxLineSymbol;this._moveStartHandler=_2.hitch(this,this._moveStartHandler);this._firstMoveHandler=_2.hitch(this,this._firstMoveHandler);this._moveStopHandler=_2.hitch(this,this._moveStopHandler);this._moveHandler=_2.hitch(this,this._moveHandler);this._init();},destroy:function(){this._cleanUp();this._graphic=this._map=this._toolbar=this._markerSymbol=this._lineSymbol=null;},refresh:function(){this._draw();},suspend:function(){_3.forEach(this._getAllGraphics(),function(g){g.hide();});},resume:function(){_3.forEach(this._getAllGraphics(),function(g){g.show();});this._draw();},_init:function(){this._draw();},_cleanUp:function(){if(this._connects){_3.forEach(this._connects,_4.disconnect);}var _16=this._toolbar._scratchGL;if(this._anchors){_3.forEach(this._anchors,function(_17){_16.remove(_17.graphic);var mov=_17.moveable;if(mov){mov.destroy();}});}if(this._box){_16.remove(this._box);}this._box=this._anchors=this._connects=null;},_draw:function(){if(!this._graphic.getDojoShape()){this._cleanUp();return;}var map=this._map,_18=this._toolbar._scratchGL;var _19=this._getBoxCoords();var _1a=new _c(map.spatialReference);var _1b=_2.clone(_3.filter(_19,function(pt,_1c){return (_1c!==8&&_1c%2===0);}));if(_1b[0]){_1b.push([_1b[0][0],_1b[0][1]]);}_1a.addPath(_1b);if(this._rotate){_1a.addPath([_19[1],_19[8]]);}if(this._box){this._box.setGeometry(_1a);}else{this._box=new _f(_1a,this._lineSymbol);_18.add(this._box);}if(this._anchors){_3.forEach(this._anchors,function(_1d,_1e){if(!this._scale){_1e=8;}var _1f=new _b(_19[_1e],map.spatialReference);_1d.graphic.setGeometry(_1f);var mov=_1d.moveable,_20=_1d.graphic.getDojoShape();if(_20){if(!mov){_1d.moveable=this._getMoveable(_1d.graphic,_1e);}else{if(_20!==mov.shape){mov.destroy();_1d.moveable=this._getMoveable(_1d.graphic,_1e);}}}},this);}else{this._anchors=[];this._connects=[];_3.forEach(_19,function(_21,_22){if(!this._scale&&_22<8){return;}_21=new _b(_21,map.spatialReference);var _23=new _f(_21,this._markerSymbol);_18.add(_23);this._anchors.push({graphic:_23,moveable:this._getMoveable(_23,_22)});},this);}},_getBoxCoords:function(_24){var _25=this._graphic,map=this._map,_26=this._getTransformedBoundingBox(_25),_27=[],pt,_28,_29;_3.forEach(_26,function(_2a,_2b,arr){pt=_2a;_28=arr[_2b+1];if(!_28){_28=arr[0];}_29={x:(pt.x+_28.x)/2,y:(pt.y+_28.y)/2};if(!_24){pt=map.toMap(pt);_29=map.toMap(_29);}_27.push([pt.x,pt.y]);_27.push([_29.x,_29.y]);});if(this._rotate){var _2c=_2.clone(_27[1]);_2c=_24?{x:_2c[0],y:_2c[1]}:map.toScreen({x:_2c[0],y:_2c[1],spatialReference:map.spatialReference});_2c.y-=this._toolbar._options.rotateHandleOffset;if(!_24){_2c=map.toMap(_2c);}_27.push([_2c.x,_2c.y]);}return _27;},_getTransformedBoundingBox:function(_2d){var map=this._map,_2e=_2d.geometry.getExtent(),_2f=_2d.geometry.spatialReference,_30=new _b(_2e.xmin,_2e.ymax,_2f),_31=new _b(_2e.xmax,_2e.ymin,_2f);_30=map.toScreen(_30);_31=map.toScreen(_31);return [{x:_30.x,y:_30.y},{x:_31.x,y:_30.y},{x:_31.x,y:_31.y},{x:_30.x,y:_31.y}];},_getAllGraphics:function(){var _32=[this._box];if(this._anchors){_3.forEach(this._anchors,function(_33){_32.push(_33.graphic);});}_32=_3.filter(_32,_a.isDefined);return _32;},_getMoveable:function(_34,_35){var _36=_34.getDojoShape();if(!_36){return;}var _37=new _7(_36);_37._index=_35;this._connects.push(_4.connect(_37,"onMoveStart",this._moveStartHandler));this._connects.push(_4.connect(_37,"onFirstMove",this._firstMoveHandler));this._connects.push(_4.connect(_37,"onMoveStop",this._moveStopHandler));_37.onMove=this._moveHandler;var _38=_36.getEventSource();if(_38){_6.set(_38,"cursor",this._toolbar._cursors["box"+_35]);}return _37;},_moveStartHandler:function(_39){this._toolbar["on"+(_39.host._index===8?this._rotateEvent:this._scaleEvent)+"Start"](this._graphic);},_firstMoveHandler:function(_3a){var _3b=_3a.host._index,_3c=(this._wrapOffset=_3a.host.shape._wrapOffsets[0]||0),_3d=this._graphic.getLayer()._div.getTransform(),mx=_8,_3e,_3f,_40,_41=_3.map(this._getBoxCoords(true),function(arr){return {x:arr[0]+_3c,y:arr[1]};});_40={x:_41[1].x,y:_41[3].y};this._centerCoord=mx.multiplyPoint(mx.invert(_3d),_40);if(_3b===8){_3e=mx.multiplyPoint(mx.invert(_3d),_41[1]);this._startLine=[this._centerCoord,_3e];this._moveLine=_2.clone(this._startLine);}else{_3e=mx.multiplyPoint(mx.invert(_3d),_41[_3b]);_3f=mx.multiplyPoint(mx.invert(_3d),_41[(_3b+4)%8]);this._firstMoverToCenter=Math.sqrt((_3e.x-this._centerCoord.x)*(_3e.x-this._centerCoord.x)+(_3e.y-this._centerCoord.y)*(_3e.y-this._centerCoord.y));this._startBox=_3f;this._startBox.width=(_41[4].x-_41[0].x);this._startBox.height=(_41[4].y-_41[0].y);this._moveBox=_2.clone(this._startBox);this._xfactor=_3e.x>_3f.x?1:-1;this._yfactor=_3e.y>_3f.y?1:-1;if(_3b===1||_3b===5){this._xfactor=0;}else{if(_3b===3||_3b===7){this._yfactor=0;}}}this._toolbar._beginOperation("BOX");this._toolbar["on"+(_3b===8?this._rotateEvent:this._scaleEvent)+"FirstMove"](this._graphic);},_moveHandler:function(_42,_43){var _44=_42.host._index,_45=this._defaultEventArgs,_46,_47,tx,pt,_48,_49,_4a,_4b=0,_4c=0,_4d,_4e,_4f;_45.angle=0;_45.scaleX=1;_45.scaleY=1;if(_44===8){_46=this._startLine;_47=this._moveLine;pt=_47[1];pt.x+=_43.dx;pt.y+=_43.dy;_48=this._getAngle(_46,_47);tx=_8.rotategAt(_48,_46[0]);this._graphic.getDojoShape().setTransform(tx);_45.transform=tx;_45.angle=_48;_45.around=_46[0];}else{_46=this._startBox;_47=this._moveBox;_47.width+=(_43.dx*this._xfactor);_47.height+=(_43.dy*this._yfactor);if(this._uniformScaling){_4d=_47.x+this._xfactor*_47.width;_4e=_47.y+this._yfactor*_47.height;_4f=Math.sqrt((_4d-this._centerCoord.x)*(_4d-this._centerCoord.x)+(_4e-this._centerCoord.y)*(_4e-this._centerCoord.y));_49=_4a=_4f/this._firstMoverToCenter;_4b=this._xfactor*_46.width/2;_4c=this._yfactor*_46.height/2;}else{_49=_47.width/_46.width;_4a=_47.height/_46.height;}if(isNaN(_49)||_49===Infinity||_49===-Infinity){_49=1;}if(isNaN(_4a)||_4a===Infinity||_4a===-Infinity){_4a=1;}tx=_8.scaleAt(_49,_4a,_46.x+_4b,_46.y+_4c);this._graphic.getDojoShape().setTransform(tx);_45.transform=tx;_45.scaleX=_49;_45.scaleY=_4a;_45.around={x:_46.x+_4b,y:_46.y+_4c};}this._toolbar["on"+(_44===8?this._rotateEvent:this._scaleEvent)](this._graphic,_45);},_moveStopHandler:function(_50){var _51=this._graphic,_52=this._toolbar,_53=_52._geo?_d.geographicToWebMercator(_51.geometry):_51.geometry,_54=_53.spatialReference,_55=_51.getDojoShape(),_56=_55.getTransform(),_57=_51.getLayer()._div.getTransform();_53=_53.toJson();this._updateSegments(_53.paths||_53.rings,_56,_57,_54);_55.setTransform(null);_53=_e.fromJson(_53);_51.setGeometry(_52._geo?_d.webMercatorToGeographic(_53,true):_53);this._draw();this._startBox=this._moveBox=this._xfactor=this._yfactor=null;this._startLine=this._moveLine=null;_52._endOperation("BOX");this._defaultEventArgs.transform=_56;_52["on"+(_50.host._index===8?this._rotateEvent:this._scaleEvent)+"Stop"](this._graphic,this._defaultEventArgs);},_updateSegments:function(_58,_59,_5a,_5b){var mx=_8,map=this._map,_5c=this._wrapOffset||0;_3.forEach(_58,function(_5d){_3.forEach(_5d,function(_5e){var _5f=map.toScreen({x:_5e[0],y:_5e[1],spatialReference:_5b},true);_5f.x+=_5c;_5f=mx.multiplyPoint([_5a,_59,mx.invert(_5a)],_5f);_5f.x-=_5c;var _60=map.toMap(_5f);_5e[0]=_60.x;_5e[1]=_60.y;});});},_getAngle:function(_61,_62){var _63=Math.atan2(_61[0].y-_61[1].y,_61[0].x-_61[1].x)*180/Math.PI,_64=Math.atan2(_62[0].y-_62[1].y,_62[0].x-_62[1].x)*180/Math.PI;return _64-_63;}});if(_5("extend-esri")){_2.setObject("toolbars._Box",BOX,_9);}return BOX;});