/*
 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.arcgis.csv"]){dojo._hasResource["esri.arcgis.csv"]=true;dojo.provide("esri.arcgis.csv");dojo.require("dojox.data.CsvStore");(function(){var _1=esri.arcgis.csv;_1.latFieldStrings=["lat","latitude","y","ycenter","latitude83","latdecdeg","POINT-Y"];_1.longFieldStrings=["lon","lng","long","longitude","x","xcenter","longitude83","longdecdeg","POINT-X"];_1.buildCSVFeatureCollection=function(_2){var _3=new dojo.Deferred();var _4=function(_5){_3.callback(_5);};var _6=esri.request({url:_2.url,handleAs:"text",load:function(_7){_1._processCsvData(_7,_2,dojo.hitch(this,_4));},error:function(_8){console.error("error: "+_8);}},{usePost:false});return _3;};_1.projectFeatureCollection=function(_9,_a){var _b=new dojo.Deferred();var _c=function(_d){_b.callback(_d);};_1._projectFeatureSet(_9,new esri.SpatialReference({wkid:4326}),_a,dojo.hitch(this,_c));return _b;};_1.generateDefaultPopupInfo=function(_e){var _f=_e.layerDefinition.fields;var _10={"esriFieldTypeDouble":1,"esriFieldTypeSingle":1};var _11={"esriFieldTypeInteger":1,"esriFieldTypeSmallInteger":1};var dt={"esriFieldTypeDate":1};var _12=null;var _13=dojo.map(_f,dojo.hitch(this,function(_14){if(_14.name.toUpperCase()==="NAME"){_12=_14.name;}var _15=(_14.type!=="esriFieldTypeOID"&&_14.type!=="esriFieldTypeGlobalID"&&_14.type!=="esriFieldTypeGeometry");var _16=null;if(_15){var f=_14.name.toLowerCase();var _17=",stretched value,fnode_,tnode_,lpoly_,rpoly_,poly_,subclass,subclass_,rings_ok,rings_nok,";if(_17.indexOf(","+f+",")>-1||f.indexOf("area")>-1||f.indexOf("length")>-1||f.indexOf("shape")>-1||f.indexOf("perimeter")>-1||f.indexOf("objectid")>-1||f.indexOf("_")===f.length-1||(f.indexOf("_i")===f.length-2&&f.length>1)){_15=false;}if(_14.type in _11){_16={places:0,digitSeparator:true};}else{if(_14.type in _10){_16={places:2,digitSeparator:true};}else{if(_14.type in dt){_16={dateFormat:"shortDateShortTime"};}}}}return dojo.mixin({},{fieldName:_14.name,label:_14.alias,isEditable:true,tooltip:"",visible:_15,format:_16,stringFieldOption:"textbox"});}));var _18={title:_12?"{"+_12+"}":"",fieldInfos:_13,description:null,showAttachments:false,mediaInfos:[]};return _18;};_1._processCsvData=function(_19,_1a,_1b){var _1c=_19.indexOf("\n");var _1d=dojo.trim(_19.substr(0,_1c));var _1e=_1a.columnDelimiter;if(!_1e){_1e=_1._getSeparator(_1d);}var _1f=new dojox.data.CsvStore({data:_19,separator:_1e});var _20=(dojo.isIE<9)?750:1001;_1f.fetch({start:0,count:_20,onComplete:function(_21,_22){var _23=0;var _24={"layerDefinition":_1a.layerDefinition,"featureSet":{"features":[],"geometryType":"esriGeometryPoint"}};var _25=_24.layerDefinition.objectIdField;if(!_25){if(!dojo.some(_24.layerDefinition.fields,function(_26){if(_26.type=="esriFieldTypeOID"){_25=_26.name;return true;}return false;})){_24.layerDefinition.fields.push({"name":"__OBJECTID","alias":"__OBJECTID","type":"esriFieldTypeOID","editable":false,"domain":null});_25="__OBJECTID";}}var _27,_28;var _29=_1f._attributes;var _2a=[];var _2b=[];dojo.forEach(_24.layerDefinition.fields,function(_2c,_2d){if(_2c.type==="esriFieldTypeDate"){_2a.push(_2c.name);}else{if(_2c.type==="esriFieldTypeDouble"||_2c.type==="esriFieldTypeInteger"){_2b.push(_2c.name);}}});if(_1a.locationInfo&&_1a.locationInfo.locationType==="coordinates"){_27=_1a.locationInfo.latitudeFieldName;_28=_1a.locationInfo.longitudeFieldName;}else{dojo.forEach(_29,function(_2e){var _2f;_2f=dojo.indexOf(_1.latFieldStrings,_2e.toLowerCase());if(_2f!==-1){_27=_2e;}_2f=dojo.indexOf(_1.longFieldStrings,_2e.toLowerCase());if(_2f!==-1){_28=_2e;}},this);}if(!_27||!_28){setTimeout(function(){console.error("File does not seem to contain fields with point coordinates.");},1);return;}var i=0,il=_21.length;for(i;i<il;i++){if(_24.featureSet.features.length>=1000){setTimeout(function(){console.error("1000 feature limit reached. Unable to load any more data.");},1);break;}var _30=_21[i];var _31=_1f.getAttributes(_30),_32={};dojo.forEach(_31,function(_33,_34){if(dojo.some(_2a,function(a){return a===_33;})){var val=_1f.getValue(_30,_33),_35=new Date(val);_32[_33]=_1._isValidDate(_35,val)?_35.getTime():null;}else{if(dojo.some(_2b,function(a){return a===_33;})){var _36=dojo.number.parse(_1f.getValue(_30,_33));if((_33==_27||_33==_28)&&(isNaN(_36)||Math.abs(_36)>181)){_36=parseFloat(_1f.getValue(_30,_33));if(isNaN(_36)){_32[_33]=null;}else{_32[_33]=_36;}}else{if(isNaN(_36)){_32[_33]=null;}else{_32[_33]=_36;}}}else{_32[_33]=_1f.getValue(_30,_33);}}});_32[_25]=_23;_23++;var _37=_32[_27];var _38=_32[_28];if(_38==null||_37==null||isNaN(_37)||isNaN(_38)){continue;}var _39=new esri.geometry.Point(_38,_37,new esri.SpatialReference({wkid:4326}));var _3a={"geometry":_39.toJson(),"attributes":_32};_24.featureSet.features.push(_3a);}_24.layerDefinition.name="csv";if(_1b){_1b(_24);}},onError:function(_3b){console.error("Error fetching items from CSV store: ",_3b);}});return true;};_1._getSeparator=function(_3c){var _3d=[","," ",";","|","\t"];var _3e=0;var _3f="";dojo.forEach(_3d,function(_40){var _41=_3c.split(_40).length;if(_41>_3e){_3e=_41;_3f=_40;}});return _3f;};_1._isValidDate=function(d,_42){if(!d||Object.prototype.toString.call(d)!=="[object Date]"||isNaN(d.getTime())){return false;}var _43=true;if(dojo.isChrome&&/\d+\W*$/.test(_42)){var _44=_42.match(/[a-zA-Z]{2,}/);if(_44){var _45=false,i=0,len=_44.length,_46=/^((jan(uary)?)|(feb(ruary)?)|(mar(ch)?)|(apr(il)?)|(may)|(jun(e)?)|(jul(y)?)|(aug(ust)?)|(sep(tember)?)|(oct(ober)?)|(nov(ember)?)|(dec(ember)?)|(am)|(pm)|(gmt)|(utc))$/i;while(!_45&&(i<=len)&&!(_45=!_46.test(_44[i]))){i++;}_43=!_45;}}return _43;};_1._projectFeatureSet=function(_47,_48,_49,_4a){if(!_47.featureSet||_47.featureSet.length===0){return;}if(_1._sameSpatialReference(_49,_48)){_4a(_47);return;}var _4b=function(_4c){var _4d=[];dojo.forEach(_47.featureSet.features,function(_4e,i){if(_4c[i]){_4e.geometry=_4c[i];_4d.push(_4e);}},this);_4a(_47);};var _4f=function(_50,_51){console.error("error projecting featureSet ("+_47.layerDefinition.name+"). Try one more time.");_1._projectGeometries(_52,_47.featureSet.geometryType,_48,_49,dojo.hitch(this,_4b),dojo.hitch(this,_53));};var _53=function(_54,_55){console.error("error projecting featureSet ("+_47.layerDefinition.name+"). Final try.");_4a(_47);};if(_47.featureSet.features&&_47.featureSet.features.length>0){var _52=[];dojo.forEach(_47.featureSet.features,function(_56){_52.push(_56.geometry);});_1._projectGeometries(_52,_47.featureSet.geometryType,_48,_49,dojo.hitch(this,_4b),dojo.hitch(this,_4f));}else{_4a(_47);}};_1._projectGeometries=function(_57,_58,_59,_5a,_5b,_5c){if(_57.length===0){_5b(null);}var _5d=esri.geometry.getGeometryType(_58);var _5e=[];dojo.forEach(_57,function(_5f){var _60=new _5d(_5f);_60.spatialReference=_59;_5e.push(_60);},this);var _61=[102113,102100,3857];if(_59.wkid&&_59.wkid===4326&&_5a.wkid&&dojo.indexOf(_61,_5a.wkid)>-1){dojo.forEach(_5e,function(_62){if(_62.xmin){_62.xmin=Math.max(_62.xmin,-180);_62.xmax=Math.min(_62.xmax,180);_62.ymin=Math.max(_62.ymin,-89.99);_62.ymax=Math.min(_62.ymax,89.99);}else{if(_62.rings){dojo.forEach(_62.rings,function(_63){dojo.forEach(_63,function(_64){_64[0]=Math.min(Math.max(_64[0],-180),180);_64[1]=Math.min(Math.max(_64[1],-89.99),89.99);},this);},this);}else{if(_62.paths){dojo.forEach(_62.paths,function(_65){dojo.forEach(_65,function(_66){_66[0]=Math.min(Math.max(_66[0],-180),180);_66[1]=Math.min(Math.max(_66[1],-89.99),89.99);},this);},this);}else{if(_62.x){_62.x=Math.min(Math.max(_62.x,-180),180);_62.y=Math.min(Math.max(_62.y,-89.99),89.99);}}}}},this);_57=[];dojo.forEach(_5e,function(_67){var _68=esri.geometry.geographicToWebMercator(_67);if(_5a.wkid!==102100){_68.spatialReference=_5a;}_57.push(_68.toJson());},this);_5b(_57);}else{if(_59.wkid!==null&&dojo.indexOf(_61,_59.wkid)>-1&&_5a.wkid!==null&&_5a.wkid===4326){_57=[];dojo.forEach(_5e,function(_69){_57.push(esri.geometry.webMercatorToGeographic(_69).toJson());},this);_5b(_57);}else{var _6a=function(_6b,_6c){if(_6b&&_6b.length===_57.length){_57=[];dojo.forEach(_6b,function(_6d){if(_6d&&((_6d.rings&&_6d.rings.length>0&&_6d.rings[0].length>0&&_6d.rings[0][0].length>0&&!isNaN(_6d.rings[0][0][0])&&!isNaN(_6d.rings[0][0][1]))||(_6d.paths&&_6d.paths.length>0&&_6d.paths[0].length>0&&_6d.paths[0][0].length>0&&!isNaN(_6d.paths[0][0][0])&&!isNaN(_6d.paths[0][0][1]))||(_6d.xmin&&!isNaN(_6d.xmin)&&_6d.ymin&&!isNaN(_6d.ymin))||(_6d.x&&!isNaN(_6d.x)&&_6d.y&&!isNaN(_6d.y)))){_57.push(_6d.toJson());}else{_57.push(null);}},this);_5b(_57);}else{_5c(_6b,_6c);}};if(esri.config.defaults.geometryService){esri.config.defaults.geometryService.project(_5e,_5a,dojo.hitch(this,_6a),_5c);}else{_5b(null);}}}};_1._sameSpatialReference=function(sp1,sp2){var _6e=[102113,102100,3857];if(sp1&&sp2&&sp1.wkid===sp2.wkid&&sp1.wkt===sp2.wkt){return true;}else{if(sp1&&sp2&&sp1.wkid&&sp2.wkid&&dojo.indexOf(_6e,sp1.wkid)>-1&&dojo.indexOf(_6e,sp2.wkid)>-1){return true;}}return false;};}());}