/*
 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.editing.TemplatePicker"]){dojo._hasResource["esri.dijit.editing.TemplatePicker"]=true;dojo.provide("esri.dijit.editing.TemplatePicker");dojo.require("dojo.data.ItemFileReadStore");dojo.require("dojox.grid.DataGrid");(function(){var _1=[dojo.moduleUrl("esri.dijit.editing","css/TemplatePicker.css"),dojo.moduleUrl("dojox","grid/resources/Grid.css")];var _2=document.getElementsByTagName("head").item(0),_3;for(var i=0,il=_1.length;i<il;i++){_3=document.createElement("link");_3.type="text/css";_3.rel="stylesheet";_3.href=_1[i].toString();_2.appendChild(_3);}}());dojo.declare("esri.dijit.editing.TemplatePicker",[dijit._Widget,dijit._Templated],{widgetsInTemplate:true,templateString:"<div class=\"templatePicker\">\r\n\r\n  <table dojoType=\"dojox.grid.DataGrid\" noDataMessage=\"${emptyMessage}\" selectionMode=\"none\" autoHeight=\"${_rows}\" autoWidth=\"${_autoWidth}\"\r\n         query=\"{ query: '*' }\" dojoAttachPoint=\"grid\" class=\"grid\">\r\n  </table>\r\n  \r\n</div>\r\n",basePath:dojo.moduleUrl("esri.dijit.editing"),featureLayers:null,items:null,grouping:true,showTooltip:false,maxLabelLength:0,rows:4,columns:3,surfaceWidth:30,surfaceHeight:30,emptyMessage:"",_uniqueId:{id:0},_assumedCellWidth:90,_initialAutoWidth:300,_initialAutoHeight:200,_ieTimer:150,constructor:function(_4,_5){_4=_4||{};if(!_4.items&&!_4.featureLayers){console.error("TemplatePicker: please provide 'featureLayers' or 'items' parameter in the constructor");}this._dojo14x=(dojo.version.minor>=4);this._itemWidgets={};this.emptyMessage=_4.emptyMessage||dojo.getObject("bundle.widgets.templatePicker.creationDisabled",false,esri)||"";},postMixInProperties:function(){this.inherited(arguments);this._preprocess();},startup:function(){this.inherited(arguments);if(this.rows==="auto"&&this.columns==="auto"){var _6=dojo.contentBox(this.domNode);if(!_6.w){this.domNode.style.width=this._initialAutoWidth+"px";}if(!_6.h){this.domNode.style.height=this._initialAutoHeight+"px";}_6=dojo.contentBox(this.domNode);this._columns=Math.floor(_6.w/this._assumedCellWidth)||1;}this._applyGridPatches();this._setGridLayout();dojo.connect(this.grid,"onRowClick",this,this._rowClicked);this._setGridData();this._toggleTooltip();if(dojo.isIE<9){this._repaintItems=dojo.hitch(this,this._repaintItems);setTimeout(this._repaintItems,this._ieTimer);}},destroy:function(){this.showTooltip=false;this._toggleTooltip();this.featureLayers=this.items=this.grid=this._flItems=this._itItems=this._groupRowIndices=this._selectedCell=this._selectedInfo=this._selectedItem=null;this.inherited(arguments);},getSelected:function(){return this._selectedCell?this._selectedItem:null;},clearSelection:function(){var _7=this._selectedCell,_8=this._selectedInfo;if(_7){this._rowClicked({cellNode:_7,rowIndex:_8.selRow,cellIndex:_8.selCol});}},update:function(_9){var _a=(this.rows==="auto"&&this.columns==="auto"&&_9)?true:false;var _b=this.grid;if(_a){var _c=this.domNode,_d=dojo.contentBox(_c),id=_c.id;this._columns=Math.floor((_d.w-_b.views.views[0].getScrollbarWidth())/dojo.coords(dojo.query("#"+id+".templatePicker div.item")[0].parentNode).w);this._columns=this._columns||1;}var _e=this._rows;this._preprocess();var _f=this._rows;this._setGridLayout();this._setGridData();if(_f!==_e){_b.set("autoHeight",this._rows,false);}if(_a){_b._resize({w:_d.w,h:_d.h});_b.viewsHeaderNode.style.display="none";}else{_b.update();}this._toggleTooltip();var _10=this,_11=this.getSelected();if(_11){_b.store.fetch({onComplete:function(its){var _12=_10._locate(_11,_10._selectedInfo,its);var _13=_12&&_b.views.views[0].getCellNode(_12[0],_12[1]);if(_13){_10._rowClicked({cellNode:_13,rowIndex:_12[0],cellIndex:_12[1]},true);}}});}if(dojo.isIE<9){setTimeout(this._repaintItems,this._ieTimer);}var _14=this.featureLayers,_15=this.items;if((!_14||!_14.length)&&(!_15||!_15.length)&&_b&&this.emptyMessage){_b.showMessage(this.emptyMessage);}},onSelectionChange:function(){},_preprocess:function(){if(this.items){this.grouping=false;}this._autoWidth=false;if(this.rows==="auto"||this.columns==="auto"){this._autoWidth=true;}var _16;if(this.featureLayers){_16=this._flItems=this._getItemsFromLayers(this.featureLayers);}else{_16=this._itItems=this._getItemsFromItems(this.items);}if(this.rows==="auto"&&this.columns==="auto"){if(!this._started){this._rows=false;this._columns=null;this._autoWidth=false;}return;}var len=0;this._rows=this.rows;this._columns=this.columns;if(this.rows==="auto"){if(this.featureLayers){if(this.grouping){len=_16.length;dojo.forEach(_16,function(_17){len+=Math.ceil(_17.length/this.columns);},this);}else{dojo.forEach(_16,function(_18){len+=_18.length;},this);len=Math.ceil(len/this.columns);}}else{len=Math.ceil(_16.length/this.columns);}this._rows=len;}else{if(this.columns==="auto"){if(this.featureLayers){if(this.grouping){len=3;}else{dojo.forEach(_16,function(_19){len+=_19.length;},this);len=Math.ceil(len/this.rows);}}else{len=Math.ceil(_16.length/this.rows);}this._columns=len;}}},_applyGridPatches:function(){var _1a=this.grid;var _1b=_1a.adaptWidth;_1a.adaptWidth=function(){var _1c=this.views.views;for(var i=0,_1d;_1d=_1c[i];i++){dojo.style(_1d.headerNode,"display","block");}_1b.apply(this,arguments);for(var i=0,_1d;_1d=_1c[i];i++){dojo.style(_1d.headerNode,"display","none");}};if(this._dojo14x){if(this.rows!=="auto"&&this.columns!=="auto"){var _1e=dojo.connect(_1a,"_onFetchComplete",this,function(){dojo.disconnect(_1e);this.grid.set("autoHeight",this._rows);});}dojo.connect(_1a,"_onDelete",this,this._destroyItems);dojo.connect(_1a,"_clearData",this,this._destroyItems);dojo.connect(_1a,"destroy",this,this._destroyItems);var _1f=_1a.focus;if(_1f&&_1f.findAndFocusGridCell){_1f.findAndFocusGridCell=function(){return false;};}}},_setGridLayout:function(){var _20=function(_21){return function(_22,_23){return this._cellGet(_21,_22,_23);};};var _24=dojo.hitch(this,this._cellFormatter);var _25=[],_26=this._columns;for(var i=0;i<_26;i++){_25.push({field:"cell"+i,get:dojo.hitch(this,_20(i)),formatter:_24});}var _27={cells:[_25]};if(this.grouping){var _28={field:"groupName",colSpan:_26,get:dojo.hitch(this,this._cellGetGroup),formatter:dojo.hitch(this,this._cellGroupFormatter)};_27.cells.push([_28]);}var _29=this.grid;_29.set("structure",_27);},_setGridData:function(){var _2a=[];if(this.grouping){this._groupRowIndices=[];var _2b,_2c,_2d=this._columns;dojo.forEach(this._flItems,function(_2e,idx){_2a.push({});var _2f=(idx===0)?0:(_2b+_2c+1);this._groupRowIndices.push(_2f);_2b=_2f;_2c=Math.ceil(_2e.length/_2d);_2a=_2a.concat(this._getStoreItems(_2e));},this);}else{if(this.featureLayers){dojo.forEach(this._flItems,function(_30){_2a=_2a.concat(_30);});_2a=this._getStoreItems(_2a);}else{_2a=this._getStoreItems(this._itItems);}}var _31=new dojo.data.ItemFileReadStore({data:{items:_2a}});this.grid.setStore(_31);},_toggleTooltip:function(){if(this.showTooltip){if(this.tooltip){return;}this.tooltip=dojo.create("div",{"class":"tooltip"},this.domNode);this.tooltip.style.display="none";this.tooltip.style.position="fixed";var _32=this.grid;this._mouseOverConnect=dojo.connect(_32,"onCellMouseOver",this,this._cellMouseOver);this._mouseOutConnect=dojo.connect(_32,"onCellMouseOut",this,this._cellMouseOut);}else{if(this.tooltip){dojo.disconnect(this._mouseOverConnect);dojo.disconnect(this._mouseOutConnect);dojo.destroy(this.tooltip);this.tooltip=null;}}},_rowClicked:function(evt,_33){var _34=evt.cellNode,row=evt.rowIndex,col=evt.cellIndex;var _35=this._getCellInfo(_34,row,col);if(!_35){return;}if(this._selectedCell){dojo.removeClass(this._selectedCell,"selectedItem");}if(_34!==this._selectedCell){dojo.addClass(_34,"selectedItem");this._selectedCell=_34;var _36=this.grid.store;this._selectedItem={featureLayer:_36.getValue(_35,"layer"),type:_36.getValue(_35,"type"),template:_36.getValue(_35,"template"),symbolInfo:_36.getValue(_35,"symbolInfo"),item:this._getItem(_35)};this._selectedInfo={selRow:row,selCol:col,index1:_36.getValue(_35,"index1"),index2:_36.getValue(_35,"index2"),index:_36.getValue(_35,"index")};}else{this._selectedCell=this._selectedInfo=this._selectedItem=null;}if(!_33){this.onSelectionChange();}},_locate:function(_37,_38,_39){var _3a=this.grid.store,_3b=new Array(this._columns);var _3c,_3d=_38.index1,_3e=_38.index2,_3f=_38.index,_40=_37.item;dojo.some(_39,function(_41,_42){return dojo.some(_3b,function(_43,_44){var _45=_3a.getValue(_41,"cell"+_44);if(_45&&(_40?(_3f===_3a.getValue(_45,"index")):(_3d===_3a.getValue(_45,"index1")&&_3e===_3a.getValue(_45,"index2")))){_3c=[_42,_44];return true;}else{return false;}});});return _3c;},_getCellInfo:function(_46,row,col){if(!_46){return;}var _47=this.grid;var _48=_47.getItem(row);var _49=_47.store.getValue(_48,"cell"+col);return _49;},_getItem:function(_4a){var _4b=this.items;if(_4b){return _4b[this.grid.store.getValue(_4a,"index")];}},_cellMouseOver:function(evt){var _4c=this.tooltip;var _4d=evt.cellNode,row=evt.rowIndex,col=evt.cellIndex;var _4e=this._getCellInfo(_4d,row,col);if(_4c&&_4e){var _4f=this.grid.store;var _50=_4f.getValue(_4e,"template");var _51=_4f.getValue(_4e,"type");var _52=_4f.getValue(_4e,"symbolInfo");var _53=_4f.getValue(_4e,"layer");var _54=this._getItem(_4e);var _55=(_54&&(_54.label+(_54.description?(": "+_54.description):"")))||(_50&&(_50.name+(_50.description?(": "+_50.description):"")))||(_51&&_51.name)||(_52&&(_52.label+(_52.description?(": "+_52.description):"")))||((_53&&_53.name+": ")+"Default");_4c.style.display="none";_4c.innerHTML=_55;var _56=dojo.coords(_4d.firstChild);dojo.style(_4c,{left:(_56.x)+"px",top:(_56.y+_56.h+5)+"px"});_4c.style.display="";}},_cellMouseOut:function(){var _57=this.tooltip;if(_57){_57.style.display="none";}},_destroyItems:function(){var _58=this._itemWidgets;for(var w in _58){if(!_58[w]){continue;}_58[w].destroy();delete _58[w];}},_repaintItems:function(){var _59=this._itemWidgets;for(var w in _59){var _5a=_59[w];if(_5a){_5a._repaint(_5a._surface);}}},_getStoreItems:function(_5b){var uid=this._uniqueId;_5b=dojo.map(_5b,function(_5c){return dojo.mixin({"surfaceId":"tpick-surface-"+(uid.id++)},_5c);});var len=_5b.length,_5d=0,obj={},k=0,_5e,_5f=[],_60=true,_61=this._columns;while(_5d<len){_60=true;_5e="cell"+k;obj[_5e]=_5b[_5d];_5d++;k++;if(k%_61===0){_60=false;_5f.push(obj);obj={};k=0;}}if(_60){_5f.push(obj);}return _5f;},_getItemsFromLayers:function(_62){var _63=[];dojo.forEach(_62,function(_64,_65){_63.push(this._getItemsFromLayer(_64,_65));},this);return _63;},_getItemsFromLayer:function(_66,_67){var _68=[];var _69=[];_69=_69.concat(_66.templates);dojo.forEach(_66.types,function(_6a){var _6b=_6a.templates;dojo.forEach(_6b,function(_6c){_6c._type_=_6a;});_69=_69.concat(_6b);});_69=dojo.filter(_69,esri._isDefined);var _6d=_66.renderer;if(_6d){if(_69.length>0){dojo.forEach(_69,function(_6e){var _6f=_6e.prototype;if(_6f){var _70=_6d.getSymbol(_6f);if(_70){_68.push({label:this._trimLabel(_6e.name),symbol:_70,layer:_66,type:_6e._type_,template:_6e,index1:_67,index2:_68.length});}}delete _6e._type_;},this);}else{var _71=_6d.declaredClass.replace("esri.renderer.",""),_72=[];if(_71==="TemporalRenderer"){_6d=_6d.observationRenderer;if(_6d){_71=_6d.declaredClass.replace("esri.renderer.","");}}switch(_71){case "SimpleRenderer":_72=[{symbol:_6d.symbol,label:_6d.label,description:_6d.description}];break;case "UniqueValueRenderer":case "ClassBreaksRenderer":_72=_6d.infos;break;}_68=dojo.map(_72,function(_73,idx){return {label:this._trimLabel(_73.label),description:_73.description,symbolInfo:dojo.mixin({constructor:function(){}},_73),symbol:_73.symbol,layer:_66,index1:_67,index2:idx};},this);}}return _68;},_getItemsFromItems:function(_74){return dojo.map(_74,function(_75,idx){var _75=dojo.mixin({index:idx},_75);_75.label=this._trimLabel(_75.label);return _75;},this);},_trimLabel:function(_76){var max=this.maxLabelLength;if(max&&_76){if(_76.length>max){_76=_76.substr(0,max)+"...";}}return _76||"";},_cellGet:function(_77,_78,_79){if(!_79){return "";}return this.grid.store.getValue(_79,"cell"+_77);},_cellFormatter:function(_7a){if(_7a){var _7b=this._itemWidgets,_7c=this.grid.store;var sid=_7c.getValue(_7a,"surfaceId");var w=_7b[sid];if(!w){w=(_7b[sid]=new esri.dijit.editing.TemplatePickerItem({id:sid,label:_7c.getValue(_7a,"label"),symbol:_7c.getValue(_7a,"symbol"),surfaceWidth:this.surfaceWidth,surfaceHeight:this.surfaceHeight,template:_7c.getValue(_7a,"template")}));}return w||"";}else{return "";}},_cellGetGroup:function(_7d,_7e){if(!this._groupRowIndices){return "";}var _7f=dojo.indexOf(this._groupRowIndices,_7d);if(!_7e||_7f===-1){return "";}return (this.featureLayers[_7f]&&this.featureLayers[_7f].name)||"";},_cellGroupFormatter:function(_80){if(_80){return "<div class='groupLabel'>"+_80+"</div>";}else{return "";}}});dojo.declare("esri.dijit.editing.TemplatePickerItem",[dijit._Widget,dijit._Templated],{templateString:"<div class='item' style='text-align: center;'>"+"<div class='itemSymbol' dojoAttachPoint='_surfaceNode'></div>"+"<div class='itemLabel'>${label}</div>"+"</div>",startup:function(){if(this._started){return;}this.inherited(arguments);this._surface=this._draw(this._surfaceNode,this.symbol,this.surfaceWidth,this.surfaceHeight,this.template);},_draw:function(_81,_82,_83,_84,_85){var _86=dojox.gfx.createSurface(_81,_83,_84);if(dojo.isIE<9){var _87=_86.getEventSource();dojo.style(_87,"position","relative");dojo.style(_87.parentNode,"position","relative");}var _88=this._getDrawingToolShape(_82,_85)||esri.symbol.getShapeDescriptors(_82);var _89;try{_89=_86.createShape(_88.defaultShape).setFill(_88.fill).setStroke(_88.stroke);}catch(e){_86.clear();_86.destroy();return;}var dim=_86.getDimensions();var _8a={dx:dim.width/2,dy:dim.height/2};var _8b=_89.getBoundingBox(),_8c=_8b.width,_8d=_8b.height;if(_8c>_83||_8d>_84){var _8e=_8c>_8d?_8c:_8d;var _8f=_83<_84?_83:_84;var _90=(_8f-5)/_8e;dojo.mixin(_8a,{xx:_90,yy:_90});}_89.applyTransform(_8a);return _86;},_getDrawingToolShape:function(_91,_92){var _93,_94=_92?_92.drawingTool||null:null;switch(_94){case "esriFeatureEditToolArrow":_93={type:"path",path:"M 10,1 L 3,8 L 3,5 L -15,5 L -15,-2 L 3,-2 L 3,-5 L 10,1 E"};break;case "esriFeatureEditToolLeftArrow":_93={type:"path",path:"M -15,1 L -8,8 L -8,5 L 10,5 L 10,-2 L -8,-2 L -8,-5 L -15,1 E"};break;case "esriFeatureEditToolRightArrow":_93={type:"path",path:"M 10,1 L 3,8 L 3,5 L -15,5 L -15,-2 L 3,-2 L 3,-5 L 10,1 E"};break;case "esriFeatureEditToolUpArrow":_93={type:"path",path:"M 1,-10 L 8,-3 L 5,-3 L 5,15 L -2,15 L -2,-3 L -5,-3 L 1,-10 E"};break;case "esriFeatureEditToolDownArrow":_93={type:"path",path:"M 1,15 L 8,8 L 5,8 L 5,-10 L -2,-10 L -2,8 L -5,8 L 1,15 E"};break;case "esriFeatureEditToolTriangle":_93={type:"path",path:"M -10,14 L 2,-10 L 14,14 L -10,14 E"};break;case "esriFeatureEditToolRectangle":_93={type:"path",path:"M -10,-10 L 10,-10 L 10,10 L -10,10 L -10,-10 E"};break;case "esriFeatureEditToolCircle":_93={type:"circle",cx:0,cy:0,r:10};break;case "esriFeatureEditToolEllipse":_93={type:"ellipse",cx:0,cy:0,rx:10,ry:5};break;case "esriFeatureEditToolFreehand":if(_91.type==="simplelinesymbol"||_91.type==="cartographiclinesymbol"){_93={type:"path",path:"m -11, -7c-1.5,-3.75 7.25,-9.25 12.5,-7c5.25,2.25 6.75,9.75 3.75,12.75c-3,3 -3.25,2.5 -9.75,5.25c-6.5,2.75 -7.25,14.25 2,15.25c9.25,1 11.75,-4 13.25,-6.75c1.5,-2.75 3.5,-11.75 12,-6.5"};}else{_93={type:"path",path:"M 10,-13 c3.1,0.16667 4.42564,2.09743 2.76923,3.69231c-2.61025,2.87179 -5.61025,5.6718 -6.14358,6.20513c-0.66667,0.93333 -0.46667,1.2 -0.53333,1.93333c-0.00001,0.86666 0.6,1.66667 1.13334,2c1.03077,0.38462 2.8,0.93333 3.38974,1.70769c0.47693,0.42564 0.87693,0.75897 1.41026,1.75897c0.13333,1.06667 -0.46667,2.86667 -1.8,3.8c-0.73333,0.73333 -3.86667,2.66666 -4.86667,3.13333c-0.93333,0.8 -7.4,3.2 -7.6,3.06667c-1.06667,0.46667 -4.73333,1.13334 -5.2,1.26667c-1.6,0.33334 -4.6,0.4 -6.25128,0.05128c-1.41539,-0.18462 -2.34872,-2.31796 -1.41539,-4.45129c0.93333,-1.73333 1.86667,-3.13333 2.64615,-3.85641c1.28718,-1.47692 2.57437,-2.68204 3.88718,-3.54359c0.88718,-1.13845 1.8,-1.33333 2.26666,-2.45641c0.33334,-0.74359 0.37949,-1.7641 0.06667,-2.87692c-0.66666,-1.46666 -1.66666,-1.86666 -2.98975,-2.2c-1.27692,-0.26666 -2.12307,-0.64102 -3.27692,-1.46666c-0.66667,-1.00001 -1.01538,-3.01539 0.73333,-4.06667c1.73333,-1.2 3.6,-1.93333 4.93333,-2.2c1.33333,-0.46667 4.84104,-1.09743 5.84103,-1.23076c1.60001,-0.46667 6.02564,-0.50257 7.29231,-0.56924z"};}break;default:return null;}return {defaultShape:_93,fill:_91.getFill(),stroke:_91.getStroke()};},_repaint:function(_95){if(!_95){this._surface=this._draw(this._surfaceNode,this.symbol,this.surfaceWidth,this.surfaceHeight,this.template);return;}if(_95.getStroke&&_95.setStroke){_95.setStroke(_95.getStroke());}if(_95.getFill&&_95.setFill){_95.setFill(_95.getFill());}if(_95.children&&dojo.isArray(_95.children)){dojo.forEach(_95.children,this._repaint,this);}},destroy:function(){if(this._surface){this._surface.destroy();delete this._surface;}this.inherited(arguments);}});}