/*
 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.dijit.editing.SelectionHelper"]){dojo._hasResource["esri.dijit.editing.SelectionHelper"]=true;dojo.provide("esri.dijit.editing.SelectionHelper");dojo.declare("esri.dijit.editing.SelectionHelper",null,{constructor:function(_1){this._settings=_1||{};this._sConnects=[];this._mapServiceCount=0;this._map=this._settings.map;this._tolerance=this._settings.singleSelectionTolerance;this._initMapServiceInfos(this._settings.layers);},destroy:function(){for(var _2 in this._sConnects){if(this._sConnects.hasOwnProperty(_2)){dojo.disconnect(this._sConnects[_2]);}}},selectFeatures:function(_3,_4,_5,_6){if(_5===esri.layers.FeatureLayer.SELECTION_NEW){this._resetMapServiceInfos();this.getSelection(_3);}var _7=[];dojo.forEach(_3,function(_8){if(_8.visible===true&&_8._isMapAtVisibleScale()===true){var _9=_5;if(_8._isSelOnly&&_9===esri.layers.FeatureLayer.SELECTION_NEW){_9=esri.layers.FeatureLayer.SELECTION_ADD;}_7.push(_8.selectFeatures(_4,_9));}});var _a=new dojo.DeferredList(_7);_a.addCallback(dojo.hitch(this,function(_b){var _c=[];dojo.forEach(_b,function(_d,_e){dojo.forEach(_d[1],function(_f){var _10=_f.attributes[_3[_e].objectIdField];_f=_3[_e]._mode._getFeature(_10)||null;if(_f){_c.push(_f);}},this);},this);if(!this._mapServiceCount){_6(_c);return;}var _11=_5===esri.layers.FeatureLayer.SELECTION_SUBTRACT;if(_11){this._resetMapServiceInfos();this._createLayerDefs(this._getLayerInfosFromSelection(_3));}else{this._createLayerDefs(this._getLayerInfosFromFeatures(_c));}this._updateLayerDefs(this._mapServiceInfos,false,!((_c&&_c.length)||_11),dojo.hitch(this,_6,_c));}));},selectFeaturesByGeometry:function(_12,_13,_14,_15){var _16=_13;if(_13.declaredClass.indexOf("Extent")!==-1){if(_13.xmax==_13.xmin&&_13.ymax==_13.ymin){_16=new esri.geometry.Point(_13.xmax,_13.ymax);}}_16=(_16.declaredClass.indexOf("Point")!==-1)?this._extentFromPoint(_16):_16;var _17=new esri.tasks.Query();_17.geometry=_16;this.selectFeatures(_12,_17,_14,_15);},clearSelection:function(_18){var _19=this._nonSelOnlyLayers;dojo.forEach(_19,"if (item.clearSelection){ item.clearSelection(); }");if(!this._mapServiceCount){return;}this._resetMapServiceInfos();var _1a=this._getLayerInfosFromSelection(this._settings.layers);var _1b=dojo.some(_1a,"return item.oids && item.oids.length");if(_1b){this._createLayerDefs(_1a);this._updateLayerDefs(this._mapServiceInfos,true,_18||false);}},findMapService:function(_1c){var map=this._map;var _1d=map.layerIds;var _1e=(_1c&&_1c._url)?_1c._url.path.toLowerCase():"";var _1f;for(var _20 in _1d){if(_1d.hasOwnProperty(_20)){_1f=map.getLayer(_1d[_20]);var _21=_1f._url?_1f._url.path.toLowerCase().replace("mapserver","featureserver"):"";if(_1e.substr(0,_21.length)===_21&&_1f.declaredClass==="esri.layers.ArcGISDynamicMapServiceLayer"){return _1f;}}}},getSelection:function(_22){var _23=[];dojo.forEach(_22,function(_24){if(_24._isSelOnly){_23.push(this._createLayerInfo(_24));}},this);dojo.forEach(_23,function(_25){var _26=this._createMapServiceInfo(this.findMapService(_25.layer));if(_26){_26.layerInfos[_25.layer.layerId]=_25;}},this);},_initMapServiceInfos:function(_27){this._nonSelOnlyLayers=[];this._mapServiceInfos=[];dojo.forEach(_27,function(_28){var _29=this.findMapService(_28);if(_29){this._mapServiceCount++;this._createMapServiceInfo(_29);if(_29){_29.setDisableClientCaching(true);}}else{this._nonSelOnlyLayers.push(_28);}},this);},_createMapServiceInfo:function(_2a){if(!_2a){return null;}var _2b=this._mapServiceInfos;var _2c=_2b[_2a.id];if(!_2c){_2c=_2b[_2a.id]={mapService:_2a,layerInfos:[],layerDefs:dojo.mixin([],_2a.layerDefinitions||[]),origLayerDefs:dojo.mixin([],_2a.layerDefinitions||[])};}return _2c;},_resetMapServiceInfo:function(_2d){dojo.forEach(_2d.layerInfos,this._resetLayerInfo);_2d.layerDefs=dojo.mixin([],_2d.origLayerDefs||[]);},_resetMapServiceInfos:function(){var _2e=this._mapServiceInfos;for(var _2f in _2e){if(_2e.hasOwnProperty(_2f)){this._resetMapServiceInfo(_2e[_2f]);}}},_createLayerInfo:function(_30,_31){var _32=_30.objectIdField;var _33=_31?[]:_30.getSelectedFeatures();return {layer:_30,selectedFeatures:_33||[],oids:dojo.map(_33,function(_34){return _34.attributes[_32];})};},_resetLayerInfo:function(_35){if(!_35){return;}_35.selectedFeatures=[];_35.oids=[];},_updateLayerDefs:function(_36,_37,_38,_39){for(var _3a in _36){if(_36.hasOwnProperty(_3a)){var _3b=_36[_3a];var _3c=_3b.mapService;var _3d=_3b.layerDefs=(_37?dojo.mixin([],_3b.origLayerDefs||[]):_3b.layerDefs);if(_3d){if(!_38){this._sConnects[_3c.id]=(dojo.connect(_3c,"onUpdateEnd",dojo.hitch(this,"_onMapServiceUpdate",_3b,_37,_39)));}else{if(_39){_39();}}_3c.setLayerDefinitions(_3d,_38||false);}else{if(_39){_39();}}}}},_onMapServiceUpdate:function(_3e,_3f,_40){dojo.disconnect(this._sConnects[_3e.mapService.id]);dojo.forEach(_3e.layerInfos,function(_41){if(_3f){if(_41){_41.layer.clearSelection();}}else{var _42=new esri.tasks.Query();_42.objectIds=_41?_41.oids:[];if(_42.objectIds.length){_41.layer.selectFeatures(_42,esri.layers.FeatureLayer.SELECTION_SUBTRACT);}}},this);if(_3f){this._resetMapServiceInfo(_3e);}if(_40){_40();}},_createLayerDefs:function(_43){dojo.forEach(_43,function(_44){var _45=_44.layer;var _46=this._createMapServiceInfo(this.findMapService(_44.layer));if(!_46){return;}var _47=_46.mapService;var _48=_46.layerDefs;var _49=_45.objectIdField;var _4a=_45.layerId;var _4b="(\""+_49+"\" NOT IN (";var _4c=_44.oids;if(_4c&&_4c.length){dojo.forEach(_44.oids,function(oid,idx){_4c=true;if(idx){_4b+=",";}_4b+="'"+oid+"'";});_4b+="))";if(_48.length&&(_48[_4a]&&_48[_4a].length)){_48[_4a]+=" AND"+_4b;}else{_48[_4a]=_4b;}}},this);},_getLayerInfosFromFeatures:function(_4d){var _4e=[];dojo.forEach(_4d,function(_4f){var _50=_4f.getLayer();if(_50&&_50._isSelOnly){if(!_4e[_50.id]){_4e[_50.id]=this._createLayerInfo(_50,true);}_4e[_50.id].selectedFeatures.push(_4f);_4e[_50.id].oids.push(_4f.attributes[_50.objectIdField]);}},this);var _51=[];for(var _52 in _4e){if(_4e.hasOwnProperty(_52)){_51.push(_4e[_52]);}}return _51;},_getLayerInfosFromSelection:function(_53){var _54=[];dojo.forEach(_53,function(_55){if(_55._isSelOnly){_54.push(this._createLayerInfo(_55,false));}},this);return _54;},_extentFromPoint:function(_56){var _57=this._tolerance;var map=this._map;var _58=map.toScreen(_56);var _59=new esri.geometry.Point(_58.x-_57,_58.y+_57);var _5a=new esri.geometry.Point(_58.x+_57,_58.y-_57);var _5b=map.toMap(_59);var _5c=map.toMap(_5a);return new esri.geometry.Extent(_5b.x,_5b.y,_5c.x,_5c.y,map.spatialReference);}});}