/*
 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/tasks/PrintTask",["dojo/_base/declare","dojo/_base/lang","dojo/_base/array","dojo/_base/json","dojo/_base/Deferred","dojo/has","esri/kernel","esri/lang","esri/deferredUtils","esri/tasks/Task","esri/renderers/SimpleRenderer","esri/geometry/scaleUtils","esri/tasks/Geoprocessor","esri/tasks/PrintTemplate","dojo/has!extend-esri?esri/tasks/PrintParameters","dojo/has!extend-esri?esri/tasks/LegendLayer"],function(_1,_2,_3,_4,_5,_6,_7,_8,_9,_a,_b,_c,_d,_e){var _f=_1(_a,{declaredClass:"esri.tasks.PrintTask",constructor:function(url,_10){this.url=url;this.printGp=new _d(this.url);this._handler=_2.hitch(this,this._handler);if(_10&&_10.async){this.async=_10.async;}},_handler:function(_11,io,_12,_13,dfd){try{var _14;if(this.async){if(_11.jobStatus==="esriJobSucceeded"){this.printGp.getResultData(_11.jobId,"Output_File",_2.hitch(this,function(_15){_14=_15.value;this._successHandler([_14],"onComplete",_12,dfd);}));}}else{_14=_11[0].value;this._successHandler([_14],"onComplete",_12,dfd);}}catch(err){this._errorHandler(err,_13,dfd);}},execute:function(_16,_17,_18){var _19=this._handler,_1a=this._errorHandler;var _1b=_16.template||new _e();var _1c=_1b.exportOptions;var _1d;if(_1c){var _1e=_1c.width;var _1f=_1c.height;var dpi=_1c.dpi;_1d={outputSize:[_1e,_1f],dpi:dpi};}if(_1b.preserveScale===false){this._preserveScale=false;}else{this._preserveScale=true;}var _20=_1b.layoutOptions;var _21,_22=[];if(_20){this.legendAll=false;if(!_20.legendLayers){this.legendAll=true;}else{_3.forEach(_20.legendLayers,function(_23,idx){var _24={};_24.id=_23.layerId;if(_23.subLayerIds){_24.subLayerIds=_23.subLayerIds;}_22.push(_24);});}var _25,_26;if(_20.scalebarUnit==="Miles"||_20.scalebarUnit==="Kilometers"){_25="Kilometers";_26="Miles";}else{if(_20.scalebarUnit==="Meters"||_20.scalebarUnit==="Feet"){_25="Meters";_26="Feet";}}var _27={Miles:"mi",Kilometers:"km",Yards:"yd",Feet:"ft",Meters:"m"};_21={titleText:_20.titleText,authorText:_20.authorText,copyrightText:_20.copyrightText,customTextElements:_20.customTextElements,scaleBarOptions:{metricUnit:_25,metricLabel:_27[_25],nonMetricUnit:_26,nonMetricLabel:_27[_26]},legendOptions:{operationalLayers:_22}};}var map=_16.map;var _28=this._getPrintDefinition(map);if(_16.outSpatialReference){_28.mapOptions.spatialReference=_16.outSpatialReference.toJson();}if(_16.template&&_8.isDefined(_16.template.showAttribution)){_28.mapOptions.showAttribution=_16.template.showAttribution;}_2.mixin(_28,{exportOptions:_1d,layoutOptions:_21});if(this.allLayerslegend){_2.mixin(_28.layoutOptions,{legendOptions:{operationalLayers:this.allLayerslegend}});}var _29=_4.toJson(_8.fixJson(_28));var _2a=_1b.format;var _2b=_1b.layout;var _2c={Web_Map_as_JSON:_29,Format:_2a,Layout_Template:_2b};if(_16.extraParameters){_2c=_2.mixin(_2c,_16.extraParameters);}var dfd=new _5(_9._dfdCanceller);var _2d=function(r,i){_19(r,i,_17,_18,dfd);};var _2e=function(r){_1a(r,_18,dfd);};if(this.async){dfd._pendingDfd=this.printGp.submitJob(_2c,_2d,null,_2e);}else{dfd._pendingDfd=this.printGp.execute(_2c,_2d,_2e);}return dfd;},onComplete:function(){},_multipointLayer:function(){this.layerDefinition={name:"multipointLayer",geometryType:"esriGeometryMultipoint",drawingInfo:{renderer:null}};this.featureSet={geometryType:"esriGeometryMultipoint",features:[]};},_polygonLayer:function(){this.layerDefinition={name:"polygonLayer",geometryType:"esriGeometryPolygon",drawingInfo:{renderer:null}};this.featureSet={geometryType:"esriGeometryPolygon",features:[]};},_pointLayer:function(){this.layerDefinition={name:"pointLayer",geometryType:"esriGeometryPoint",drawingInfo:{renderer:null}};this.featureSet={geometryType:"esriGeometryPoint",features:[]};},_polylineLayer:function(){this.layerDefinition={name:"polylineLayer",geometryType:"esriGeometryPolyline",drawingInfo:{renderer:null}};this.featureSet={geometryType:"esriGeometryPolyline",features:[]};},_createFeatureCollection:function(_2f){var _30=new this._polygonLayer();var _31=new this._polylineLayer();var _32=new this._pointLayer();var _33=new this._multipointLayer();if(_2f.declaredClass==="esri.layers.FeatureLayer"){_30.layerDefinition.name=_31.layerDefinition.name=_32.layerDefinition.name=_33.layerDefinition.name=_2f.name||_2f.id;}if(_2f.renderer&&!_2.isFunction(_2f.renderer.attributeField)){_30.layerDefinition.drawingInfo.renderer=_2f.renderer.toJson();_31.layerDefinition.drawingInfo.renderer=_2f.renderer.toJson();_32.layerDefinition.drawingInfo.renderer=_2f.renderer.toJson();_33.layerDefinition.drawingInfo.renderer=_2f.renderer.toJson();}else{delete _30.layerDefinition.drawingInfo;delete _31.layerDefinition.drawingInfo;delete _32.layerDefinition.drawingInfo;delete _33.layerDefinition.drawingInfo;}if(_2f.fields){_30.layerDefinition.fields=_2f.fields;_31.layerDefinition.fields=_2f.fields;_32.layerDefinition.fields=_2f.fields;_33.layerDefinition.fields=_2f.fields;}var _34,i;for(i=0;i<_2f.graphics.length;i++){var g=_2f.graphics[i];if(g.visible===false||!g.geometry){continue;}_34=g.toJson();if(_34.symbol&&_34.symbol.outline&&_34.symbol.outline.color&&_34.symbol.outline.color[3]){_34.symbol.outline.color[3]=255;}if(_2f.renderer&&_2.isFunction(_2f.renderer.attributeField)&&!_34.symbol){_34.symbol=_2f.renderer.getSymbol(g)?_2f.renderer.getSymbol(g).toJson():_34.symbol;}switch(g.geometry.type){case "polygon":_30.featureSet.features.push(_34);break;case "polyline":_31.featureSet.features.push(_34);break;case "point":_32.featureSet.features.push(_34);break;case "multipoint":_33.featureSet.features.push(_34);break;}}var _35=[];if(_32.featureSet.features.length>0){_35.push(_32);}if(_31.featureSet.features.length>0){_35.push(_31);}if(_30.featureSet.features.length>0){_35.push(_30);}if(_33.featureSet.features.length>0){_35.push(_33);}var _36={layers:_35};var _37={id:_2f.id,opacity:_2f.opacity,minScale:_2f.minScale||0,maxScale:_2f.maxScale||0,featureCollection:_36};return _37;},_getPrintDefinition:function(map){var _38=this._createOperationalLayers(map);var _39={operationalLayers:_38};var _3a=map.extent,sr=map.spatialReference;if(map.spatialReference._isWrappable()){_3a=_3a._normalize(true);sr=_3a.spatialReference;}var _3b={mapOptions:{showAttribution:map.showAttribution,extent:_3a.toJson(),spatialReference:sr.toJson()}};if(this._preserveScale){_2.mixin(_3b.mapOptions,{scale:_c.getScale(map)});}if(map.timeExtent){_2.mixin(_3b.mapOptions,{time:[map.timeExtent.startTime.getTime(),map.timeExtent.endTime.getTime()]});}var _3c={};_2.mixin(_3c,_3b,_39);return _3c;},_createOperationalLayers:function(map){var i,_3d,_3e,_3f,_40=[],_41=[];if(this.legendAll){this.allLayerslegend=[];}else{this.allLayerslegend=null;}for(i=0;i<map.layerIds.length;i++){_3d=map.getLayer(map.layerIds[i]);if(!_3d.loaded||!_3d.visible||_3.indexOf(_40,_3d.id)!==-1){continue;}if(_3d.credential&&_3d.url.indexOf("token=")===-1){_3d.url+="?token="+_3d.credential.token;}_3e=_3d.declaredClass;_3f={id:_3d.id,title:_3d.id,opacity:_3d.opacity,minScale:_3d.minScale||0,maxScale:_3d.maxScale||0};switch(_3e){case "esri.layers.ArcGISDynamicMapServiceLayer":var _42=[];if(_3d._params.dynamicLayers){var _43=_4.fromJson(_3d._params.dynamicLayers);_3.forEach(_43,function(_44,idx){_42.push({id:_44.id,layerDefinition:_44});});}else{_3.forEach(_3d.layerInfos,function(_45,idx){var _46={id:_45.id,layerDefinition:{definitionExpression:null,layerTimeOptions:null}};if(_3d.layerDefinitions&&_3d.layerDefinitions[_45.id]){_46.layerDefinition.definitionExpression=_3d.layerDefinitions[_45.id];}if(_3d.layerTimeOptions&&_3d.layerTimeOptions[_45.id]){_46.layerDefinition.layerTimeOptions=_3d.layerTimeOptions[_45.id];}if(_46.layerDefinition.definitionExpression||_46.layerDefinition.layerTimeOptions){_42.push(_46);}});}var _47=_3d._params.layers?_3d.visibleLayers:null;_3f=_2.mixin(_3f,{url:_3d.url,visibleLayers:_47,layers:_42});_41.push(_3f);if(this.allLayerslegend){this.allLayerslegend.push({id:_3d.id,subLayerIds:_3d.visibleLayers});}break;case "esri.layers.ArcGISImageServiceLayer":_3f=_2.mixin(_3f,{url:_3d.url,bandIds:_3d.bandIds,compressionQuality:_3d.compressionQuality,format:_3d.format,interpolation:_3d.interpolation});if(_3d.mosaicRule){_2.mixin(_3f,{mosaicRule:_3d.mosaicRule.toJson()});}if(_3d.renderingRule){_2.mixin(_3f,{renderingRule:_3d.renderingRule.toJson()});}_41.push(_3f);if(this.allLayerslegend){this.allLayerslegend.push({id:_3d.id});}break;case "esri.layers.WMSLayer":_3f=_2.mixin(_3f,{url:_3d.url,title:_3d.title,type:"wms",version:_3d.version,transparentBackground:_3d.imageTransparency,visibleLayers:_3d.visibleLayers});_41.push(_3f);if(this.allLayerslegend){this.allLayerslegend.push({id:_3d.id,subLayerIds:_3d.visibleLayers});}break;case "esri.virtualearth.VETiledLayer":var _48=_3d.mapStyle;if(_48==="aerialWithLabels"){_48="Hybrid";}_3f=_2.mixin(_3f,{visibility:_3d.visible,type:"BingMaps"+_48,culture:_3d.culture,key:_3d.bingMapsKey});_41.push(_3f);break;case "esri.layers.OpenStreetMapLayer":_3f=_2.mixin(_3f,{type:"OpenStreetMap",url:_3d.tileServers[0]});_41.push(_3f);break;case "esri.layers.WMTSLayer":_3f=_2.mixin(_3f,{url:_3d.url,type:"wmts",layer:_3d.layerInfos[0].identifier,style:_3d.layerInfos[0].style,format:_3d.layerInfos[0].format,tileMatrixSet:_3d.layerInfos[0].tileMatrixSet});_41.push(_3f);break;case "esri.layers.MapImageLayer":var _49=_3d.getImages();_3.forEach(_49,function(_4a,idx){if(_4a.href){_3f={id:_3d.id+"_image"+idx,type:"image",title:_3d.id,minScale:_3d.minScale||0,maxScale:_3d.maxScale||0,opacity:_3d.opacity,extent:_4a.extent.toJson(),url:_4a.href};_41.push(_3f);}});break;case "esri.layers.WebTiledLayer":var _4b=_3d.url.replace(/\$\{/g,"{");_3f=_2.mixin(_3f,{type:"WebTiledLayer",urlTemplate:_4b,credits:_3d.copyright});if(_3d.subDomains&&_3d.subDomains.length>0){_3f.subDomains=_3d.subDomains;}_41.push(_3f);break;default:if(_3d.getTileUrl||_3d.getImageUrl){_3f=_2.mixin(_3f,{url:_3d.url});_41.push(_3f);}}}for(i=0;i<map.graphicsLayerIds.length;i++){_3d=map.getLayer(map.graphicsLayerIds[i]);if(!_3d.loaded||!_3d.visible||_3.indexOf(_40,_3d.id)!==-1){continue;}_3e=_3d.declaredClass;switch(_3e){case "esri.layers.FeatureLayer":if(_3d.url&&_3d.renderer&&_2.isString(_3d.renderer.attributeField)&&_3d._getField(_3d.renderer.attributeField,true)){if(_3d.credential&&_3d.url.indexOf("token=")===-1){_3d.url+="?token="+_3d.credential.token;}_3f={id:_3d.id,url:_3d.url,title:_3d.id,opacity:_3d.opacity,minScale:_3d.minScale||0,maxScale:_3d.maxScale||0,layerDefinition:{drawingInfo:{renderer:_3d.renderer.toJson()}}};if(_3d._params.source){var _4c=_3d._params.source.toJson();_2.mixin(_3f.layerDefinition,{source:_4c});}if(_3d.getDefinitionExpression()){_2.mixin(_3f.layerDefinition,{definitionExpression:_3d.getDefinitionExpression()});}if(_3d.mode!==2){if(_3d.getSelectedFeatures().length>0){var _4d=_3.map(_3d.getSelectedFeatures(),function(_4e){return _4e.attributes[_3d.objectIdField];});if(_4d.length>0&&_3d.getSelectionSymbol()){_2.mixin(_3f,{selectionObjectIds:_4d,selectionSymbol:_3d.getSelectionSymbol().toJson()});}}}else{var _4f=_3.map(_3d.getSelectedFeatures(),function(_50){return _50.attributes[_3d.objectIdField];});if(_4f.length===0||!_3d._params.drawMode){break;}_2.mixin(_3f.layerDefinition,{objectIds:_4f});var _51=null;if(_3d.getSelectionSymbol()){_51=new _b(_3d.getSelectionSymbol());}_2.mixin(_3f.layerDefinition.drawingInfo,{renderer:_51&&_51.toJson()});}}else{_3f=this._createFeatureCollection(_3d);}_41.push(_3f);if(this.allLayerslegend){this.allLayerslegend.push({id:_3d.id});}break;case "esri.layers.GraphicsLayer":_3f=this._createFeatureCollection(_3d);_41.push(_3f);if(this.allLayerslegend){this.allLayerslegend.push({id:_3d.id});}break;default:}}if(map.graphics&&map.graphics.graphics.length>0){_3f=this._createFeatureCollection(map.graphics);_41.push(_3f);}return _41;}});if(_6("extend-esri")){_2.setObject("tasks.PrintTask",_f,_7);}return _f;});