/*
 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.tasks.gp"]){dojo._hasResource["esri.tasks.gp"]=true;dojo.provide("esri.tasks.gp");dojo.require("esri.tasks._task");dojo.require("esri.layers.agsdynamic");dojo.require("dojo.date.locale");dojo.declare("esri.tasks.Geoprocessor",esri.tasks._Task,{constructor:function(_1){this._jobUpdateHandler=dojo.hitch(this,this._jobUpdateHandler);this._getJobStatus=dojo.hitch(this,this._getJobStatus);this._getResultDataHandler=dojo.hitch(this,this._getResultDataHandler);this._getResultImageHandler=dojo.hitch(this,this._getResultImageHandler);this._executeHandler=dojo.hitch(this,this._executeHandler);this._updateTimers=[];},updateDelay:1000,processSpatialReference:null,outputSpatialReference:null,outSpatialReference:null,setUpdateDelay:function(_2){this.updateDelay=_2;},setProcessSpatialReference:function(sr){this.processSpatialReference=sr;},setOutputSpatialReference:function(sr){this._setOutSR(sr);},setOutSpatialReference:function(sr){this._setOutSR(sr);},__msigns:[{n:"execute",c:3,a:[{i:0,p:["*"]}],e:2,f:1},{n:"submitJob",c:4,a:[{i:0,p:["*"]}],e:3}],_setOutSR:function(sr){this.outSpatialReference=this.outputSpatialReference=sr;},_getOutSR:function(){return this.outSpatialReference||this.outputSpatialReference;},_gpEncode:function(_3,_4,_5){for(var i in _3){var _6=_3[i];if(dojo.isArray(_6)){_3[i]=dojo.toJson(dojo.map(_6,function(_7){return this._gpEncode({item:_7},true).item;},this));}else{if(_6 instanceof Date){_3[i]=_6.getTime();}}}return this._encode(_3,_4,_5);},_decode:function(_8){var _9=_8.dataType,_a,_b=new esri.tasks.ParameterValue(_8);if(dojo.indexOf(["GPBoolean","GPDouble","GPLong","GPString"],_9)!==-1){return _b;}if(_9==="GPLinearUnit"){_b.value=new esri.tasks.LinearUnit(_b.value);}else{if(_9==="GPFeatureRecordSetLayer"||_9==="GPRecordSet"){_b.value=new esri.tasks.FeatureSet(_b.value);}else{if(_9==="GPDataFile"){_b.value=new esri.tasks.DataFile(_b.value);}else{if(_9==="GPDate"){_a=_b.value;if(dojo.isString(_a)){_b.value=new esri.tasks.Date({date:_a});}else{_b.value=new Date(_a);}}else{if(_9==="GPRasterData"||_9==="GPRasterDataLayer"){var _c=_8.value.mapImage;if(_c){_b.value=new esri.layers.MapImage(_c);}else{_b.value=new esri.tasks.RasterData(_b.value);}}else{if(_9.indexOf("GPMultiValue:")!==-1){var _d=_9.split(":")[1];_a=_b.value;_b.value=dojo.map(_a,function(_e){return this._decode({paramName:"_name",dataType:_d,value:_e}).value;},this);}else{console.log(this.declaredClass+" : "+esri.bundle.tasks.gp.gpDataTypeNotHandled+" : "+_b.dataType);_b=null;}}}}}}return _b;},submitJob:function(_f,_10,_11,_12,_13){var _14=this._getOutSR();var _15=_13.assembly,_16=this._gpEncode(dojo.mixin({},this._url.query,{f:"json","env:outSR":(_14?(_14.wkid||dojo.toJson(_14.toJson())):null),"env:processSR":(this.processSpatialReference?(this.processSpatialReference.wkid||dojo.toJson(this.processSpatialReference.toJson())):null)},_f),null,_15&&_15[0]),_17=this._jobUpdateHandler,_18=this._errorHandler;return esri.request({url:this._url.path+"/submitJob",content:_16,callbackParamName:"callback",load:function(r,i){_17(r,i,false,_10,_11,_13.dfd);},error:function(r){_18(r,_12,_13.dfd);}});},_jobUpdateHandler:function(_19,io,_1a,_1b,_1c,dfd){var _1d=_19.jobId,_1e=new esri.tasks.JobInfo(_19);this._successHandler([_1e],"onStatusUpdate",_1c,_1a&&dfd);if(!_1a){clearTimeout(this._updateTimers[_1d]);this._updateTimers[_1d]=null;if(dfd){dfd.progress(_1e);}switch(_19.jobStatus){case esri.tasks.JobInfo.STATUS_SUBMITTED:case esri.tasks.JobInfo.STATUS_EXECUTING:case esri.tasks.JobInfo.STATUS_WAITING:case esri.tasks.JobInfo.STATUS_NEW:var _1f=this._getJobStatus;this._updateTimers[_1d]=setTimeout(function(){_1f(_1d,_1a,_1b,_1c,dfd);},this.updateDelay);break;default:this._successHandler([_1e],"onJobComplete",_1b,dfd);}}},_getJobStatus:function(_20,_21,_22,_23,dfd){var _24=this._jobUpdateHandler;esri.request({url:this._url.path+"/jobs/"+_20,content:dojo.mixin({},this._url.query,{f:"json"}),callbackParamName:"callback",load:function(){_24(arguments[0],arguments[1],_21,_22,_23,dfd);},error:this._errorHandler});},_getResultDataHandler:function(_25,io,_26,_27,dfd){try{var _28=this._decode(_25);this._successHandler([_28],"onGetResultDataComplete",_26,dfd);}catch(err){this._errorHandler(err,_27,dfd);}},getResultData:function(_29,_2a,_2b,_2c){var _2d=this._getResultDataHandler,_2e=this._errorHandler;var dfd=new dojo.Deferred(esri._dfdCanceller);dfd._pendingDfd=esri.request({url:this._url.path+"/jobs/"+_29+"/results/"+_2a,content:dojo.mixin({},this._url.query,{f:"json",returnType:"data"}),callbackParamName:"callback",load:function(r,i){_2d(r,i,_2b,_2c,dfd);},error:function(r){_2e(r,_2c,dfd);}});return dfd;},checkJobStatus:function(_2f,_30,_31){var _32=this._jobUpdateHandler,_33=this._errorHandler;var dfd=new dojo.Deferred(esri._dfdCanceller);dfd._pendingDfd=esri.request({url:this._url.path+"/jobs/"+_2f,content:dojo.mixin({},this._url.query,{f:"json"}),callbackParamName:"callback",load:function(r,i){_32(r,i,true,null,_30,dfd);},error:function(r){_33(r,_31,dfd);}});return dfd;},execute:function(_34,_35,_36,_37){var _38=this._getOutSR();var _39=_37.assembly,_3a=this._gpEncode(dojo.mixin({},this._url.query,{f:"json","env:outSR":(_38?(_38.wkid||dojo.toJson(_38.toJson())):null),"env:processSR":(this.processSpatialReference?(this.processSpatialReference.wkid||dojo.toJson(this.processSpatialReference.toJson())):null)},_34),null,_39&&_39[0]),_3b=this._executeHandler,_3c=this._errorHandler;return esri.request({url:this._url.path+"/execute",content:_3a,callbackParamName:"callback",load:function(r,i){_3b(r,i,_35,_36,_37.dfd);},error:function(r){_3c(r,_36,_37.dfd);}});},_executeHandler:function(_3d,io,_3e,_3f,dfd){try{var _40=_3d.results,i,il,_41=_3d.messages;for(i=0,il=_40.length;i<il;i++){_40[i]=this._decode(_40[i]);}for(i=0,il=_41.length;i<il;i++){_41[i]=new esri.tasks.GPMessage(_41[i]);}this._successHandler([_40,_41],"onExecuteComplete",_3e,dfd);}catch(err){this._errorHandler(err,_3f,dfd);}},_getResultImageHandler:function(_42,io,_43,_44,dfd){try{var _45=this._decode(_42);this._successHandler([_45],"onGetResultImageComplete",_43,dfd);}catch(err){this._errorHandler(err,_44,dfd);}},getResultImage:function(_46,_47,_48,_49,_4a){var _4b=this._getResultImageHandler,_4c=this._errorHandler,_4d=this._gpEncode(dojo.mixin({},this._url.query,{f:"json"},_48.toJson()));var dfd=new dojo.Deferred(esri._dfdCanceller);dfd._pendingDfd=esri.request({url:this._url.path+"/jobs/"+_46+"/results/"+_47,content:_4d,callbackParamName:"callback",load:function(r,i){_4b(r,i,_49,_4a,dfd);},error:function(r){_4c(r,_4a,dfd);}});return dfd;},cancelJobStatusUpdates:function(_4e){clearTimeout(this._updateTimers[_4e]);this._updateTimers[_4e]=null;},getResultImageLayer:function(_4f,_50,_51,_52){var url=this._url.path+"/jobs/"+_4f+"/results/"+_50;if(this._url.query){url+="?"+dojo.objectToQuery(this._url.query);}var _53=new esri.tasks._GPResultImageLayer(url,{imageParameters:_51},true);this.onGetResultImageLayerComplete(_53);if(_52){_52(_53);}return _53;},onStatusUpdate:function(){},onJobComplete:function(){},onExecuteComplete:function(){},onGetResultDataComplete:function(){},onGetResultImageComplete:function(){},onGetResultImageLayerComplete:function(){}});esri._createWrappers("esri.tasks.Geoprocessor");dojo.declare("esri.tasks.JobInfo",null,{constructor:function(_54){this.messages=[];dojo.mixin(this,_54);var _55=this.messages;for(var i=0,il=_55.length;i<il;i++){_55[i]=new esri.tasks.GPMessage(_55[i]);}},jobId:"",jobStatus:""});dojo.mixin(esri.tasks.JobInfo,{STATUS_CANCELLED:"esriJobCancelled",STATUS_CANCELLING:"esriJobCancelling",STATUS_DELETED:"esriJobDeleted",STATUS_DELETING:"esriJobDeleting",STATUS_EXECUTING:"esriJobExecuting",STATUS_FAILED:"esriJobFailed",STATUS_NEW:"esriJobNew",STATUS_SUBMITTED:"esriJobSubmitted",STATUS_SUCCEEDED:"esriJobSucceeded",STATUS_TIMED_OUT:"esriJobTimedOut",STATUS_WAITING:"esriJobWaiting"});dojo.declare("esri.tasks.GPMessage",null,{constructor:function(_56){dojo.mixin(this,_56);}});dojo.mixin(esri.tasks.GPMessage,{TYPE_INFORMATIVE:"esriJobMessageTypeInformative",TYPE_PROCESS_DEFINITION:"esriJobMessageTypeProcessDefinition",TYPE_PROCESS_START:"esriJobMessageTypeProcessStart",TYPE_PROCESS_STOP:"esriJobMessageTypeProcessStop",TYPE_WARNING:"esriJobMessageTypeWarning",TYPE_ERROR:"esriJobMessageTypeError",TYPE_EMPTY:"esriJobMessageTypeEmpty",TYPE_ABORT:"esriJobMessageTypeAbort"});dojo.declare("esri.tasks.LinearUnit",null,{constructor:function(_57){if(_57){dojo.mixin(this,_57);}},distance:0,units:null,toJson:function(){var _58={};if(this.distance){_58.distance=this.distance;}if(this.units){_58.units=this.units;}return _58;}});dojo.declare("esri.tasks.DataFile",null,{constructor:function(_59){if(_59){dojo.mixin(this,_59);}},url:null,toJson:function(){if(this.url){return {url:this.url};}return null;}});dojo.declare("esri.tasks.RasterData",null,{constructor:function(_5a){if(_5a){dojo.mixin(this,_5a);}},url:null,format:null,toJson:function(){var _5b={};if(this.url){_5b.url=this.url;}if(this.format){_5b.format=this.format;}return _5b;}});dojo.declare("esri.tasks.Date",null,{constructor:function(_5c){if(_5c){if(_5c.format){this.format=_5c.format;}this.date=dojo.date.locale.parse(_5c.date,{selector:"date",datePattern:this.format});}},date:new Date(),format:"EEE MMM dd HH:mm:ss zzz yyyy",toJson:function(){return {date:dojo.date.locale.format(this.date,{selector:"date",datePattern:this.format}),format:this.format};}});dojo.declare("esri.tasks.ParameterValue",null,{constructor:function(_5d){dojo.mixin(this,_5d);}});dojo.declare("esri.tasks._GPResultImageLayer",esri.layers.ArcGISDynamicMapServiceLayer,{constructor:function(url,_5e){if(_5e&&_5e.imageParameters&&_5e.imageParameters.extent){this.initialExtent=(this.fullExtent=_5e.imageParameters.extent);this.spatialReference=this.initialExtent.spatialReference;}this.getImageUrl=dojo.hitch(this,this.getImageUrl);this.loaded=true;this.onLoad(this);},getImageUrl:function(_5f,_60,_61,_62){var _63=this._url.path+"?",_64=this._params,sr=_5f.spatialReference.wkid;_62(_63+dojo.objectToQuery(dojo.mixin(_64,{f:"image",bbox:dojo.toJson(_5f.toJson()),bboxSR:sr,imageSR:sr,size:_60+","+_61})));}});}