/*
 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.AttributeInspector"]){dojo._hasResource["esri.dijit.AttributeInspector"]=true;dojo.provide("esri.dijit.AttributeInspector");dojo.require("dojo.fx");dojo.require("dojox.gfx");dojo.require("dijit._Widget");dojo.require("dijit._Templated");dojo.require("dijit.Editor");dojo.require("dijit._editor.plugins.LinkDialog");dojo.require("dijit._editor.plugins.TextColor");dojo.require("esri.dijit.editing.AttachmentEditor");dojo.require("esri.dijit.editing.Util");dojo.require("esri.tasks.query");dojo.require("dijit.form.DateTextBox");dojo.require("dijit.form.TextBox");dojo.require("dijit.form.NumberTextBox");dojo.require("dijit.form.FilteringSelect");dojo.require("dijit.form.NumberSpinner");dojo.require("dijit.form.Button");dojo.require("dijit.form.SimpleTextarea");dojo.require("dijit.Tooltip");dojo.require("dojo.data.ItemFileReadStore");(function(){var _1=document.createElement("link");_1.type="text/css";_1.rel="stylesheet";_1.href=dojo.moduleUrl("esri","dijit/css/AttributeInspector.css");document.getElementsByTagName("head").item(0).appendChild(_1);})();dojo.declare("esri.dijit.AttributeInspector",[dijit._Widget,dijit._Templated],{widgetsInTemplate:true,templateString:"<div class=\"esriAttributeInspector\">\r\n    <div class=\"atiLayerName\" dojoAttachPoint=\"layerName\"></div>\r\n    <div class=\"atiAttributes\" dojoAttachPoint=\"attributeTable\"></div>\r\n    <div dojoAttachPoint=\"attachmentEditor\"></div>\r\n    <div class=\"atiEditorTrackingInfo\" dojoAttachPoint=\"editorTrackingInfoDiv\"></div>\r\n    <div class=\"atiButtons\" dojoAttachPoint=\"editButtons\">\r\n        <button  dojoType=\"dijit.form.Button\" class=\"atiButton atiDeleteButton\"  dojoAttachPoint=\"deleteBtn\" dojoAttachEvent=\"onClick: onDeleteBtn\" showLabel=\"true\" type=\"button\">${NLS_deleteFeature}</button>\r\n        <div class=\"atiNavButtons\" dojoAttachPoint=\"navButtons\">\r\n            <div class=\"atiNavMessage\" dojoAttachPoint=\"navMessage\"></div>\r\n            <button  dojoType=\"dijit.form.Button\" iconClass=\"atiButton atiFirstIcon\" dojoAttachPoint=\"firstFeatureButton\" dojoAttachEvent=\"onClick: onFirstFeature\" showLabel=\"false\" type=\"button\">${NLS_first}</button>\r\n            <button  dojoType=\"dijit.form.Button\" iconClass=\"atiButton atiPrevIcon\" dojoAttachPoint=\"prevFeatureButton\" dojoAttachEvent=\"onClick: onPreviousFeature\" showLabel=\"false\" type=\"button\">${NLS_previous}</button>\r\n            <button  dojoType=\"dijit.form.Button\" iconClass=\"atiButton atiNextIcon\" dojoAttachPoint=\"nextFeatureButton\" dojoAttachEvent=\"onClick: onNextFeature\" showLabel=\"false\" type=\"button\">${NLS_next}</button>\r\n            <button  dojoType=\"dijit.form.Button\" iconClass=\"atiButton atiLastIcon\" dojoAttachPoint=\"lastFeatureButton\" dojoAttachEvent=\"onClick: onLastFeature\" showLabel=\"false\" type=\"button\">${NLS_last}</button>\r\n        </div>\r\n    </div>\r\n</div>\r\n",_navMessage:"( ${idx} ${of} ${numFeatures} )",onUpdate:function(){},onDelete:function(){},onAttributeChange:function(){},onNext:function(){},onReset:function(){},onCancel:function(){},constructor:function(_2,_3){dojo.mixin(this,esri.bundle.widgets.attributeInspector);_2=_2||{};if(!_2.featureLayer&&!_2.layerInfos){console.error("esri.AttributeInspector: please provide correct parameter in the constructor");}this._userIds={};if(_2.featureLayer&&_2.featureLayer.credential){var _4=_2.featureLayer.id;this._userIds[_4]=_2.featureLayer.credential.userId;}else{if(_2.layerInfos){var _5=_2.layerInfos;dojo.forEach(_5,function(_6){if(_6.featureLayer){var _7=_6.featureLayer.id;if(_6.featureLayer.credential){this._userIds[_7]=_6.featureLayer.credential.userId;}if(_6.userId){this._userIds[_7]=_6.userId;}}},this);}}this._datePackage=this._getDatePackage(_2);this._layerInfos=_2.layerInfos||[{featureLayer:_2.featureLayer,options:_2.options||[]}];this._aiConnects=[];this._selection=[];this._toolTips=[];this._numFeatures=0;this._featureIdx=0;this._currentLInfo=null;this._currentFeature=null;this._hideNavButtons=_2.hideNavButtons||false;},postCreate:function(){this._initLayerInfos();this._createAttachmentEditor();this.onFirstFeature();},destroy:function(){this._destroyAttributeTable();dojo.forEach(this._aiConnects,dojo.disconnect);delete this._aiConnects;if(this._attachmentEditor){this._attachmentEditor.destroy();delete this._attachmentEditor;}delete this._layerInfos;this._selection=this._currentFeature=this._currentLInfo=this._attributes=this._layerInfos=null;this.inherited(arguments);},refresh:function(){this._updateSelection();},first:function(){this.onFirstFeature();},last:function(){this.onLastFeature();},next:function(){this.onNextFeature();},previous:function(){this.onPreviousFeature();},showFeature:function(_8,_9){if(_9){this._createOnlyFirstTime=true;}this._updateSelection([_8],_9);this._updateUI();},onLayerSelectionChange:function(_a,_b,_c){this._createOnlyFirstTime=false;this._featureIdx=(_c===esri.layers.FeatureLayer.SELECTION_NEW)?0:this._featureIdx;this._updateSelection();this._updateUI();},onLayerSelectionClear:function(){if(!this._selection||this._selection.length<=0){return;}this._numFeatures=0;this._featureIdx=0;this._selection=[];this._currentFeature=null;this._currentLInfo=null;this._updateUI();},onLayerEditsComplete:function(_d,_e,_f,_10){_10=_10||[];if(_10.length){var _11=this._selection;var _12=_d.featureLayer.objectIdField;dojo.forEach(_10,dojo.hitch(this,function(del){dojo.some(_11,dojo.hitch(this,function(_13,idx){if(_13.attributes[_12]!==del.objectId){return false;}this._selection.splice(idx,1);return true;}));}));}_e=_e||[];if(_e.length){this._selection=esri.dijit.editing.Util.LayerHelper.findFeatures(_e,_d.featureLayer);this._featureIdx=0;}var _14=this._numFeatures=this._selection?this._selection.length:0;if(_e.length){var _15=_14?this._selection[this._featureIdx]:null;var _16=_15.getLayer();var _17=_16.getEditCapabilities();if(!(_17.canCreate&&!_17.canUpdate)){this._showFeature(_15);}}this._updateUI();},onFieldValueChange:function(_18,_19){_19=(typeof _19==="undefined")?null:_19;var _1a=_18.field;if(_1a.type==="esriFieldTypeDate"){_19=(_19&&_19.getTime)?_19.getTime():(_19&&_19.toGregorian?_19.toGregorian().getTime():_19);}if(this._currentFeature.attributes[_1a.name]===_19){return;}var _1b=this._currentLInfo;var _1c=this._currentFeature;var _1d=_1a.name;if(_1d===_1b.typeIdField){var _1e=this._findFirst(_1b.types,"id",_19);var _1f=_1b.fieldInfos;dojo.forEach(_1f,function(_20){_1a=_20.field;if(!_1a||_1a.name===_1b.typeIdField){return;}var _21=_20.dijit;var _22=this._setFieldDomain(_21,_1e,_1a);if(_22&&_21){this._setValue(_21,_1c.attributes[_1a.name]+"");if(_21.isValid()===false){this._setValue(_21,null);}}},this);}this.onAttributeChange(_1c,_1d,_19);},onDeleteBtn:function(evt){this._deleteFeature();},onNextFeature:function(evt){this._onNextFeature(1);},onPreviousFeature:function(evt){this._onNextFeature(-1);},onFirstFeature:function(evt){this._onNextFeature(this._featureIdx*-1);},onLastFeature:function(evt){this._onNextFeature((this._numFeatures-1)-this._featureIdx);},_initLayerInfos:function(){var _23=this._layerInfos;this._editorTrackingInfos={};dojo.forEach(_23,this._initLayerInfo,this);},_initLayerInfo:function(_24){var _25=_24.featureLayer;this._connect(_25,"onSelectionComplete",dojo.hitch(this,"onLayerSelectionChange",_24));this._connect(_25,"onSelectionClear",dojo.hitch(this,"onLayerSelectionClear",_24));this._connect(_25,"onEditsComplete",dojo.hitch(this,"onLayerEditsComplete",_24));_24.showAttachments=_25.hasAttachments?(esri._isDefined(_24.showAttachments)?_24.showAttachments:true):false;_24.hideFields=_24.hideFields||[];_24.htmlFields=_24.htmlFields||[];_24.isEditable=_25.isEditable()?(esri._isDefined(_24.isEditable)?_24.isEditable:true):false;_24.typeIdField=_25.typeIdField;_24.layerId=_25.id;_24.types=_25.types;if(!_24.showGlobalID&&_25.globalIdField){_24.hideFields.push(_25.globalIdField);}if(!_24.showObjectID){_24.hideFields.push(_25.objectIdField);}var _26=this._getFields(_24.featureLayer);if(!_26){return;}var _27=_24.fieldInfos||[];_27=dojo.map(_27,function(_28){return dojo.mixin({},_28);});if(!_27.length){_26=dojo.filter(_26,dojo.hitch(this,function(_29){return !this._isInFields(_29.name,_24.hideFields);}));_24.fieldInfos=dojo.map(_26,dojo.hitch(this,function(_2a){var _2b=(this._isInFields(_2a.name,_24.htmlFields)?esri.dijit.AttributeInspector.STRING_FIELD_OPTION_RICHTEXT:esri.dijit.AttributeInspector.STRING_FIELD_OPTION_TEXTBOX);return {"fieldName":_2a.name,"field":_2a,"stringFieldOption":_2b};}));}else{_24.fieldInfos=dojo.filter(dojo.map(_27,dojo.hitch(this,function(_2c){var _2d=_2c.stringFieldOption||(this._isInFields(_2c.fieldName,_24.htmlFields)?esri.dijit.AttributeInspector.STRING_FIELD_OPTION_RICHTEXT:esri.dijit.AttributeInspector.STRING_FIELD_OPTION_TEXTBOX);return dojo.mixin(_2c,{"field":this._findFirst(_26,"name",_2c.fieldName),"stringFieldOption":_2d});})),"return item.field;");}var _2e=[];if(_25.editFieldsInfo){if(_25.editFieldsInfo.creatorField){_2e.push(_25.editFieldsInfo.creatorField);}if(_25.editFieldsInfo.creationDateField){_2e.push(_25.editFieldsInfo.creationDateField);}if(_25.editFieldsInfo.editorField){_2e.push(_25.editFieldsInfo.editorField);}if(_25.editFieldsInfo.editDateField){_2e.push(_25.editFieldsInfo.editDateField);}}this._editorTrackingInfos[_25.id]=_2e;},_createAttachmentEditor:function(){this._attachmentEditor=null;var _2f=this._layerInfos;var _30=dojo.filter(_2f,"return item.showAttachments");if(!_30||!_30.length){return;}this._attachmentEditor=new esri.dijit.editing.AttachmentEditor({"class":"atiAttachmentEditor"},this.attachmentEditor);this._attachmentEditor.startup();},_setCurrentLInfo:function(_31){var _32=this._currentLInfo?this._currentLInfo.featureLayer:null;var _33=_31.featureLayer;if(_32&&_32.id===_33.id&&!_32.ownershipBasedAccessControlForFeatures){var _34=_33.getEditCapabilities();if(!(_34.canCreate&&!_34.canUpdate)){return;}}this._currentLInfo=_31;this._createTable();},_updateSelection:function(_35,_36){this._selection=_35||[];var _37=this._layerInfos;dojo.forEach(_37,this._getSelection,this);var _38=this._numFeatures=this._selection.length;var _39=_38?this._selection[this._featureIdx]:null;this._showFeature(_39,_36);},_getSelection:function(_3a){var _3b=_3a.featureLayer.getSelectedFeatures();this._selection=this._selection.concat(_3b);},_updateUI:function(){var _3c=this._numFeatures;var _3d=this._currentLInfo;this.layerName.innerHTML=(!_3d||_3c===0)?this.NLS_noFeaturesSelected:(_3d.featureLayer?_3d.featureLayer.name:"");dojo.style(this.attributeTable,"display",_3c?"":"none");dojo.style(this.editButtons,"display",_3c?"":"none");dojo.style(this.navButtons,"display",(!this._hideNavButtons&&(_3c>1)?"":"none"));this.navMessage.innerHTML=esri.substitute({idx:this._featureIdx+1,of:this.NLS_of,numFeatures:this._numFeatures},this._navMessage);if(this._attachmentEditor){dojo.style(this._attachmentEditor.domNode,"display",((_3d&&_3d.showAttachments)&&_3c)?"":"none");}var _3e=((_3d&&_3d.showDeleteButton===false)||!this._canDelete)?false:true;dojo.style(this.deleteBtn.domNode,"display",_3e?"":"none");if(this.domNode.parentNode&&this.domNode.parentNode.scrollTop>0){this.domNode.parentNode.scrollTop=0;}},_onNextFeature:function(_3f){this._featureIdx+=_3f;if(this._featureIdx<0){this._featureIdx=this._numFeatures-1;}else{if(this._featureIdx>=this._numFeatures){this._featureIdx=0;}}var _40=this._selection.length?this._selection[this._featureIdx]:null;this._showFeature(_40);this._updateUI();this.onNext(_40);},_deleteFeature:function(){this.onDelete(this._currentFeature);},_showFeature:function(_41,_42){if(!_41){return;}this._currentFeature=_41;var _43=_42?_42:_41.getLayer();var _44=_43.getEditCapabilities({feature:_41,userId:this._userIds[_43.id]});this._canUpdate=_44.canUpdate;this._canDelete=_44.canDelete;var _45=this._getLInfoFromFeatureLayer(_43);if(!_45){return;}this._setCurrentLInfo(_45);var _46=_41.attributes;var _47=this._findFirst(_45.types,"id",_46[_45.typeIdField]);var _48,_49=null;var _4a=_45.fieldInfos;dojo.forEach(_4a,function(_4b){_49=_4b.field;_48=_4b.dijit||null;if(!_48){return;}var _4c=this._setFieldDomain(_48,_47,_49);var _4d=_46[_49.name];_4d=(_4d&&_4c&&_4c.codedValues&&_4c.codedValues.length)?(_4c.codedValues[_4d]?_4c.codedValues[_4d].name:_4d):_4d;if(!esri._isDefined(_4d)){_4d="";}if(_48.declaredClass==="dijit.form.DateTextBox"){_4d=(_4d==="")?null:new Date(_4d);}else{if(_48.declaredClass==="dijit.form.FilteringSelect"){_48._lastValueReported=null;_4d=_46[_49.name]+"";}}try{this._setValue(_48,_4d);if(_48.declaredClass==="dijit.form.FilteringSelect"&&_48.isValid()===false){this._setValue(_48,null);}}catch(error){_48.set("displayedValue",this.NLS_errorInvalid,false);}},this);if(this._attachmentEditor&&_45.showAttachments){this._attachmentEditor.showAttachments(this._currentFeature);}var _4e=_43.getEditSummary(_41);if(_4e){this.editorTrackingInfoDiv.innerHTML=_4e;esri.show(this.editorTrackingInfoDiv);}else{esri.hide(this.editorTrackingInfoDiv);}},_setFieldDomain:function(_4f,_50,_51){if(!_4f){return null;}var _52=_51.domain;if(_50&&_50.domains){if(_50.domains[_51.name]&&_50.domains[_51.name] instanceof esri.layers.InheritedDomain===false){_52=_50.domains[_51.name];}}if(!_52){return null;}if(_52.codedValues&&_52.codedValues.length>0){_4f.store=this._toStore(dojo.map(_52.codedValues,"return { id: item.code += '', name: item.name };"));this._setValue(_4f,_52.codedValues[0].code);}else{_4f.constraints={min:esri._isDefined(_52.minValue)?_52.minValue:Number.MIN_VALUE,max:esri._isDefined(_52.maxValue)?_52.maxValue:Number.MAX_VALUE};this._setValue(_4f,_4f.constraints.min);}return _52;},_setValue:function(_53,_54){if(!_53.set){return;}_53._onChangeActive=false;_53.set("value",_54,true);_53._onChangeActive=true;},_getFields:function(_55){var _56=_55._getOutFields();if(!_56){return null;}var _57=_55.fields;return (_56=="*")?_57:dojo.filter(dojo.map(_56,dojo.hitch(this,"_findFirst",_57,"name")),esri._isDefined);},_isInFields:function(_58,_59){if(!_58||!_59&&!_59.length){return false;}return dojo.some(_59,function(_5a){return _5a.toLowerCase()===_58.toLowerCase();});},_findFirst:function(_5b,_5c,_5d){var _5e=dojo.filter(_5b,function(_5f){return _5f.hasOwnProperty(_5c)&&_5f[_5c]===_5d;});return (_5e&&_5e.length)?_5e[0]:null;},_getLInfoFromFeatureLayer:function(_60){var _61=_60?_60.id:null;return this._findFirst(this._layerInfos,"layerId",_61);},_createTable:function(){this._destroyAttributeTable();this.attributeTable.innerHTML="";this._attributes=dojo.create("table",{cellspacing:"0",cellpadding:"0"},this.attributeTable);var _62=dojo.create("tbody",null,this._attributes);var _63=this._currentFeature;var _64=this._currentLInfo;var _65=this._findFirst(_64.types,"id",_63.attributes[_64.typeIdField]);var _66=_64.fieldInfos;dojo.forEach(_66,dojo.hitch(this,"_createField",_65,_62),this);this._createOnlyFirstTime=false;},_createField:function(_67,_68,_69){var _6a=this._currentLInfo;var _6b=_69.field;if(this._isInFields(_6b.name,_6a.hideFields)){return;}if(this._isInFields(_6b.name,this._editorTrackingInfos[_6a.featureLayer.id])){return;}var _6c=dojo.create("tr",null,_68);var td=dojo.create("td",{innerHTML:_69.label||_6b.alias||_6b.name,"class":"atiLabel"},_6c);td=dojo.create("td",null,_6c);var _6d=null;var _6e=false;if(_69.customField){dojo.place(_69.customField.domNode||_69.customField,dojo.create("div",null,td),"first");_6d=_69.customField;}else{if(_6a.isEditable===false||_6b.editable===false||_69.isEditable===false||_6b.type==="esriFieldTypeOID"||_6b.type==="esriFieldTypeGlobalID"||(!this._canUpdate&&!this._createOnlyFirstTime)){_6e=true;}}if(!_6d&&(_6a.typeIdField&&_6b.name.toLowerCase()==_6a.typeIdField.toLowerCase())){_6d=this._createTypeField(_6b,_69,td);}else{if(!_6d){_6d=this._createDomainField(_6b,_69,_67,td);}}if(!_6d){switch(_6b.type){case "esriFieldTypeString":_6d=this._createStringField(_6b,_69,td);break;case "esriFieldTypeDate":_6d=this._createDateField(_6b,_69,td);break;case "esriFieldTypeInteger":case "esriFieldTypeSmallInteger":_6d=this._createIntField(_6b,_69,td);break;case "esriFieldTypeSingle":case "esriFieldTypeDouble":_6d=this._createFltField(_6b,_69,td);break;default:_6d=this._createStringField(_6b,_69,td);break;}}if(_69.tooltip&&_69.tooltip.length){this._toolTips.push(new dijit.Tooltip({connectId:[_6d.id],label:_69.tooltip}));}_6d.onChange=dojo.hitch(this,"onFieldValueChange",_69);_6d.set("disabled",_6e);_69.dijit=_6d;},_createTypeField:function(_6f,_70,_71){return new dijit.form.FilteringSelect({"class":"atiField",name:_6f.alias||_6f.name,store:this._toStore(dojo.map(this._currentLInfo.types,"return { id: item.id, name: item.name };")),searchAttr:"name"},dojo.create("div",null,_71));},_createDomainField:function(_72,_73,_74,_75){var _76=_72.domain;if(_74&&_74.domains){if(_74.domains[_72.name]&&_74.domains[_72.name] instanceof esri.layers.InheritedDomain===false){_76=_74.domains[_72.name];}}if(!_76){return null;}if(_76.codedValues){return new dijit.form.FilteringSelect({"class":"atiField",name:_72.alias||_72.name,store:null,searchAttr:"name",required:_72.nullable||false},dojo.create("div",null,_75));}else{return new dijit.form.NumberSpinner({"class":"atiField"},dojo.create("div",null,_75));}},_createStringField:function(_77,_78,_79){var _7a={"class":"atiField",trim:true,maxLength:_77.length};if(_78.stringFieldOption===esri.dijit.AttributeInspector.STRING_FIELD_OPTION_TEXTAREA){_7a["class"]+=" atiTextAreaField";return new dijit.form.SimpleTextarea(_7a,dojo.create("div",null,_79));}else{if(_78.stringFieldOption===esri.dijit.AttributeInspector.STRING_FIELD_OPTION_RICHTEXT){_7a["class"]+=" atiRichTextField";_7a.height="100%";_7a.width="100%";_7a.plugins=_78.richTextPlugins||["bold","italic","underline","foreColor","hiliteColor","|","justifyLeft","justifyCenter","justifyRight","justifyFull","|","insertOrderedList","insertUnorderedList","indent","outdent","|","createLink"];return new dijit.Editor(_7a,dojo.create("div",null,_79));}else{return new dijit.form.TextBox(_7a,dojo.create("div",null,_79));}}},_createDateField:function(_7b,_7c,_7d){var _7e={"class":"atiField","trim":true};if(this._datePackage){_7e.datePackage=this._datePackage;}return new dijit.form.DateTextBox(_7e,dojo.create("div",null,_7d));},_createIntField:function(_7f,_80,_81){return new dijit.form.NumberTextBox({"class":"atiField",constraints:{places:0},invalidMessage:this.NLS_validationInt,trim:true},dojo.create("div",null,_81));},_createFltField:function(_82,_83,_84){return new dijit.form.NumberTextBox({"class":"atiField",trim:true,invalidMessage:this.NLS_validationFlt},dojo.create("div",null,_84));},_toStore:function(_85){return new dojo.data.ItemFileReadStore({data:{identifier:"id",label:"name",items:_85}});},_connect:function(_86,evt,_87){this._aiConnects.push(dojo.connect(_86,evt,_87));},_getDatePackage:function(_88){if(_88.datePackage===null){return null;}else{if(_88.datePackage){return _88.datePackage;}}return null;},_destroyAttributeTable:function(){var _89=this.layerInfos;dojo.forEach(_89,function(_8a){var _8b=_8a.fieldInfos;dojo.forEach(_8b,function(_8c){var _8d=_8c.dijit;if(_8d){_8d._onChangeHandle=null;if(_8c.customField){return;}if(_8d.destroyRecursive){_8d.destroyRecursive();}else{if(_8d.destroy){_8d.destroy();}}}_8c.dijit=null;},this);},this);var _8e=this._toolTips;dojo.forEach(_8e,"item.destroy(); delete item;");this._toolTips=[];if(this._attributes){dojo.destroy(this._attributes);}}});dojo.mixin(esri.dijit.AttributeInspector,{STRING_FIELD_OPTION_RICHTEXT:"richtext",STRING_FIELD_OPTION_TEXTAREA:"textarea",STRING_FIELD_OPTION_TEXTBOX:"textbox"});}