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