/*
 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.PrintTask"]){dojo._hasResource["esri.tasks.PrintTask"]=true;dojo.provide("esri.tasks.PrintTask");dojo.require("esri.tasks._task");dojo.require("esri.tasks.gp");dojo.declare("esri.tasks.PrintTask",esri.tasks._Task,{constructor:function(_1){this.url=_1;this._handler=dojo.hitch(this,this._handler);},_handler:function(_2,io,_3,_4,_5){try{var _6=_2[0].value;this._successHandler([_6],"onComplete",_3,_5);}catch(err){this._errorHandler(err,_4,_5);}},execute:function(_7,_8,_9){var _a=this._handler,_b=this._errorHandler;var _c=new esri.tasks.Geoprocessor(this._url.path);var _d=_7.template?_7.template:new esri.tasks.PrintTemplate();var _e=_d.exportOptions;var _f;if(_e){var _10=_e.width;var _11=_e.height;var dpi=_e.dpi;_f={outputSize:[_10,_11],dpi:dpi};}if(_d.preserveScale===false){this._preserveScale=false;}else{this._preserveScale=true;}var _12=_d.layoutOptions;var _13,_14=[];if(_12){this.legendAll=false;if(!_12.legendLayers){this.legendAll=true;}else{dojo.forEach(_12.legendLayers,function(_15,idx){_14.push({id:_15});});}var _16,_17;if(_12.scalebarUnit==="Miles"||_12.scalebarUnit==="Kilometers"){_16="Kilometers";_17="Miles";}else{if(_12.scalebarUnit==="Meters"||_12.scalebarUnit==="Feet"){_16="Meters";_17="Feet";}}var _18={Miles:"mi",Kilometers:"km",Yards:"yd",Feet:"ft",Meters:"m"};_13={titleText:_12.titleText,authorText:_12.authorText,copyrightText:_12.copyrightText,scaleBarOptions:{metricUnit:_16,metricLabel:_18[_16],nonMetricUnit:_17,nonMetricLabel:_18[_17]},legendOptions:{operationalLayers:_14}};}var map=_7.map;var _19=this._getPrintDefinition(map);dojo.mixin(_19,{exportOptions:_f,layoutOptions:_13});if(this.allLayerslegend){dojo.mixin(_19.layoutOptions,{legendOptions:{operationalLayers:this.allLayerslegend}});}var _1a=dojo.toJson(_19);var _1b=_d.format;var _1c=_d.layout;var _1d={Web_Map_as_JSON:_1a,Format:_1b,Layout_Template:_1c};var dfd=new dojo.Deferred(esri._dfdCanceller);var _1e=function(r,i){_a(r,i,_8,_9,dfd);};var _1f=function(r){_b(r,_9,dfd);};dfd._pendingDfd=_c.execute(_1d,_1e,_1f);return dfd;},onComplete:function(){},_multipointLayer:function(){this.layerDefinition={name:"multipointLayer",geometryType:"esriGeometryMultipoint",drawingInfo:{renderer:null},fields:[{name:"fake",type:"esriFieldTypeInteger"}]};this.featureSet={geometryType:"esriGeometryMultipoint",features:[]};},_polygonLayer:function(){this.layerDefinition={name:"polygonLayer",geometryType:"esriGeometryPolygon",drawingInfo:{renderer:null},fields:[{name:"fake",type:"esriFieldTypeInteger"}]};this.featureSet={geometryType:"esriGeometryPolygon",features:[]};},_pointLayer:function(){this.layerDefinition={name:"pointLayer",geometryType:"esriGeometryPoint",drawingInfo:{renderer:null},fields:[{name:"fake",type:"esriFieldTypeInteger"}]};this.featureSet={geometryType:"esriGeometryPoint",features:[]};},_polylineLayer:function(){this.layerDefinition={name:"polylineLayer",geometryType:"esriGeometryPolyline",drawingInfo:{renderer:null},fields:[{name:"fake",type:"esriFieldTypeInteger"}]};this.featureSet={geometryType:"esriGeometryPolyline",features:[]};},_createFeatureCollection:function(_20){var _21=new this._polygonLayer();var _22=new this._polylineLayer();var _23=new this._pointLayer();var _24=new this._multipointLayer();var _25,_26=0,_27=0,_28=0,_29=0;dojo.forEach(_20.graphics,function(g,idx){if(g.geometry.type==="polygon"){_28++;_25=g.toJson();if(!_25.attributes){_25.attributes={fake:_28};}_21.featureSet.features.push(_25);if(_20.renderer){_21.layerDefinition.drawingInfo.renderer=_20.renderer.toJson();}if(_20.fields){_21.layerDefinition.fields=_20.fields;}}else{if(g.geometry.type==="polyline"){_27++;_25=g.toJson();if(!_25.attributes){_25.attributes={fake:_27};}_22.featureSet.features.push(_25);if(_20.renderer){_21.layerDefinition.drawingInfo.renderer=_20.renderer.toJson();}if(_20.fields){_21.layerDefinition.fields=_20.fields;}}else{if(g.geometry.type==="point"){_26++;_25=g.toJson();if(!_25.attributes){_25.attributes={fake:_26};}_23.featureSet.features.push(_25);if(_20.renderer){_21.layerDefinition.drawingInfo.renderer=_20.renderer.toJson();}if(_20.fields){_21.layerDefinition.fields=_20.fields;}}else{if(g.geometry.type==="multipoint"){_29++;_25=g.toJson();if(!_25.attributes){_25.attributes={fake:_29};}_24.featureSet.features.push(_25);if(_20.renderer){_21.layerDefinition.drawingInfo.renderer=_20.renderer.toJson();}if(_20.fields){_21.layerDefinition.fields=_20.fields;}}}}}},this);var _2a=[];if(_23.featureSet.features.length>0){_2a.push(_23);}if(_22.featureSet.features.length>0){_2a.push(_22);}if(_21.featureSet.features.length>0){_2a.push(_21);}if(_24.featureSet.features.length>0){_2a.push(_24);}var _2b={layers:_2a};var _2c={id:_20.id,opacity:_20.opacity,featureCollection:_2b};return _2c;},_getPrintDefinition:function(map){var _2d=this._createOperationalLayers(map);var _2e={operationalLayers:_2d};var _2f={mapOptions:{extent:map.extent.toJson(),spatialReference:map.spatialReference.toJson()}};if(this._preserveScale){dojo.mixin(_2f.mapOptions,{scale:esri.geometry.getScale(map)});}if(map.timeExtent){dojo.mixin(_2f.mapOptions,{time:[map.timeExtent.startTime.getTime(),map.timeExtent.endTime.getTime()]});}var _30={};dojo.mixin(_30,_2f,_2e);return _30;},_createOperationalLayers:function(map){var i,_31,_32,_33,_34=[];if(this.legendAll){this.allLayerslegend=[];}else{this.allLayerslegend=null;}for(i=0;i<map.layerIds.length;i++){_31=map.getLayer(map.layerIds[i]);if(!_31.loaded||!_31.visible){continue;}if(_31.credential){_31.url+="?token="+_31.credential.token;}_32=_31.declaredClass;switch(_32){case "esri.layers.ArcGISDynamicMapServiceLayer":var _35=[];if(_31._params.dynamicLayers){var _36=dojo.fromJson(_31._params.dynamicLayers);dojo.forEach(_36,function(_37,idx){_35.push({id:_37.id,layerDefinition:_37});});}else{dojo.forEach(_31.layerInfos,function(_38,idx){var _39={id:_38.id,layerDefinition:{definitionExpression:null,layerTimeOptions:null}};if(_31.layerDefinitions&&_31.layerDefinitions[_38.id]){_39.layerDefinition.definitionExpression=_31.layerDefinitions[_38.id];}if(_31.layerTimeOptions&&_31.layerTimeOptions[_38.id]){_39.layerDefinition.layerTimeOptions=_31.layerTimeOptions[_38.id];}_35.push(_39);});}_33={id:_31.id,url:_31.url,title:_31.id,opacity:_31.opacity,visibleLayers:_31.visibleLayers,layers:_35};_34.push(_33);if(this.allLayerslegend){this.allLayerslegend.push({id:_31.id,subLayerIds:_31.visibleLayers});}break;case "esri.layers.ArcGISImageServiceLayer":_33={id:_31.id,url:_31.url,title:_31.id,opacity:_31.opacity,bandIds:_31.bandIds,compressionQuality:_31.compressionQuality,format:_31.format,interpolation:_31.interpolation};if(_31.mosaicRule){dojo.mixin(_33,{mosaicRule:_31.mosaicRule.toJson()});}if(_31.renderingRule){dojo.mixin(_33,{renderingRule:_31.renderingRule.toJson()});}_34.push(_33);if(this.allLayerslegend){this.allLayerslegend.push({id:_31.id});}break;case "esri.layers.WMSLayer":var _3a=_31.imageFormat.split("/")[1];if(_3a.toLowerCase()==="png"){_3a="png32";}_33={id:_31.id,url:_31.url,title:_31.title,opacity:_31.opacity,type:"wms",version:_31.version,format:_3a,transparentBackground:_31.imageTransparency,visibleLayers:_31.visibleLayers};_34.push(_33);if(this.allLayerslegend){this.allLayerslegend.push({id:_31.id,subLayerIds:_31.visibleLayers});}break;case "esri.virtualearth.VETiledLayer":_33={id:_31.id,visibility:_31.visible,type:"BingMaps"+_31.mapStyle,culture:_31.culture,opacity:_31.opacity};_34.push(_33);break;case "esri.layers.OpenStreetMapLayer":_33={id:_31.id,title:_31.id,type:"OpenStreetMap",opacity:_31.opacity,url:_31.tileServers[0]};_34.push(_33);break;case "esri.layers.WMTSLayer":_33={id:_31.id,url:_31.url,title:_31.id,opacity:_31.opacity,type:"wmts",layer:_31.layerInfos[0].identifier,style:_31.layerInfos[0].style,format:_31.layerInfos[0].format,tileMatrixSet:_31.layerInfos[0].tileMatrixSet};_34.push(_33);break;case "esri.layers.KMLLayer":_33={id:_31.id,title:_31.id,type:"KML",url:_31.url,opacity:_31.opacity};_34.push(_33);if(this.allLayerslegend){this.allLayerslegend.push({id:_31.id});}break;case "esri.layers.MapImageLayer":var _3b=_31.getImages();dojo.forEach(_3b,function(_3c,idx){_33={id:_31.id+"_image"+idx,type:"image",title:_31.id,opacity:_31.opacity,extent:_3c.extent.toJson(),url:_3c.href};_34.push(_33);});break;default:if(_31 instanceof esri.layers.TiledMapServiceLayer||_31 instanceof esri.layers.DynamicMapServiceLayer){_33={id:_31.id,url:_31.url,title:_31.id,opacity:_31.opacity};_34.push(_33);}}}for(i=0;i<map.graphicsLayerIds.length;i++){_31=map.getLayer(map.graphicsLayerIds[i]);if(!_31.loaded||!_31.visible){continue;}_32=_31.declaredClass;switch(_32){case "esri.layers.FeatureLayer":if(_31.url){_33={id:_31.id,url:_31.url,title:_31.id,opacity:_31.opacity,minScale:_31.minScale,maxScale:_31.maxScale,layerDefinition:{drawingInfo:{renderer:_31.renderer.toJson()}}};if(_31.getDefinitionExpression()){dojo.mixin(_33.layerDefinition,{definitionExpression:_31.getDefinitionExpression()});}if(_31.mode!==2){if(_31.getSelectedFeatures().length>0){var _3d=dojo.map(_31.getSelectedFeatures(),function(_3e){return _3e.attributes[_31.objectIdField];});dojo.mixin(_33,{selectionObjectIds:_3d,selectionSymbol:_31.getSelectionSymbol().toJson()});}}else{var _3f=dojo.map(_31.getSelectedFeatures(),function(_40){return _40.attributes[_31.objectIdField];});dojo.mixin(_33.layerDefinition,{objectIds:_3f});var _41=new esri.renderer.SimpleRenderer(_31.getSelectionSymbol());dojo.mixin(_33.layerDefinition.drawingInfo,{renderer:_41.toJson()});}}else{_33=this._createFeatureCollection(_31);}_34.push(_33);if(this.allLayerslegend){this.allLayerslegend.push({id:_31.id});}break;case "esri.layers.GraphicsLayer":_33=this._createFeatureCollection(_31);_34.push(_33);if(this.allLayerslegend){this.allLayerslegend.push({id:_31.id});}break;default:}}if(map.graphics&&map.graphics.graphics.length>0){_33=this._createFeatureCollection(map.graphics);_34.push(_33);}return _34;}});dojo.declare("esri.tasks.PrintParameters",null,{map:null,template:null});dojo.declare("esri.tasks.PrintTemplate",null,{label:null,exportOptions:{width:800,height:1100,dpi:96},layoutOptions:null,format:"PNG32",layout:"MAP_ONLY",preserveScale:true});}