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

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