/*
 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/dijit/Bookmarks",["dojo/_base/declare","dojo/_base/connect","dojo/_base/lang","dojo/_base/array","dojo/_base/kernel","dojo/has","dojo/keys","dojo/query","dojo/dom","dojo/dom-construct","dojo/dom-class","dojo/dom-geometry","dojo/i18n!esri/nls/jsapi","esri/kernel","esri/domUtils","esri/geometry/Extent","esri/dijit/BookmarkItem","esri/Evented"],function(_1,_2,_3,_4,_5,_6,_7,_8,_9,_a,_b,_c,_d,_e,_f,_10,_11,_12){var BM=_1(_12,{declaredClass:"esri.dijit.Bookmarks",_eventMap:{"click":true,"edit":true,"remove":true},constructor:function(_13,_14){this.map=_13.map;this.editable=_13.editable;this.initBookmarks=_13.bookmarks;this._clickHandlers=this._mouseOverHandlers=this._mouseOutHandlers=this._removeHandlers=this._editHandlers=[];this.bookmarkDomNode=_a.create("div");_b.add(this.bookmarkDomNode,"esriBookmarks");this.bookmarkTable=_a.create("table");_b.add(this.bookmarkTable,"esriBookmarkTable");this.bookmarkDomNode.appendChild(this.bookmarkTable);_14=_9.byId(_14);_14.appendChild(this.bookmarkDomNode);this._addInitialBookmarks();},onClick:function(){},onEdit:function(){},onRemove:function(){},addBookmark:function(_15){var _16;if(_15.declaredClass=="esri.dijit.BookmarkItem"){_16=_15;this.bookmarks.push(_16);}else{var _17=new _10(_15.extent);_16=new _11({name:_15.name,extent:_17});this.bookmarks.push(_16);}var _18;if(this.editable){var _19=_d.widgets.bookmarks;console.log(_19);var _1a=_19.NLS_bookmark_edit;var _1b=_19.NLS_bookmark_remove;_18=_a.create("div",{innerHTML:"<div class='esriBookmarkLabel'>"+_15.name+"</div><div title='"+_1b+"' class='esriBookmarkRemoveImage'><br/></div><div title='"+_1a+"' class='esriBookmarkEditImage'><br/></div>"});var _1c=_5.query(".esriBookmarkEditImage",_18)[0];var _1d=_5.query(".esriBookmarkRemoveImage",_18)[0];this._removeHandlers.push(_2.connect(_1d,"onclick",this,"_removeBookmark"));this._editHandlers.push(_2.connect(_1c,"onclick",this,"_editBookmarkLabel"));}else{_18=_a.create("div",{innerHTML:"<div class='esriBookmarkLabel' style='width: 210px;'>"+_15.name+"</div>"});}_b.add(_18,"esriBookmarkItem");var _1e;if(_15.extent.declaredClass=="esri.geometry.Extent"){_1e=_15.extent;}else{_1e=new _10(_15.extent);}var _1f=_5.query(".esriBookmarkLabel",_18)[0];this._clickHandlers.push(_2.connect(_1f,"onclick",_3.hitch(this,"_onClickHandler",_15)));this._mouseOverHandlers.push(_2.connect(_18,"onmouseover",function(){_b.add(this,"esriBookmarkHighlight");}));this._mouseOutHandlers.push(_2.connect(_18,"onmouseout",function(){_b.remove(this,"esriBookmarkHighlight");}));var _20=this.bookmarkTable;var _21;if(this.editable){_21=_20.rows.length-1;}else{_21=_20.rows.length;}var _22=_20.insertRow(_21);var _23=_22.insertCell(0);_23.appendChild(_18);},removeBookmark:function(_24){var i;var _25=_5.query(".esriBookmarkLabel",this.bookmarkDomNode);var _26=_4.filter(_25,function(_27){return _27.innerHTML==_24;});_4.forEach(_26,function(_28){_28.parentNode.parentNode.parentNode.parentNode.removeChild(_28.parentNode.parentNode.parentNode);});for(i=this.bookmarks.length-1;i>=0;i--){if(this.bookmarks[i].name==_24){this.bookmarks.splice(i,1);}}this.onRemove();},hide:function(){_f.hide(this.bookmarkDomNode);},show:function(){_f.show(this.bookmarkDomNode);},destroy:function(){this.map=null;_4.forEach(this._clickHandlers,function(_29,idx){_2.disconnect(_29);});_4.forEach(this._mouseOverHandlers,function(_2a,idx){_2.disconnect(_2a);});_4.forEach(this._mouseOutHandlers,function(_2b,idx){_2.disconnect(_2b);});_4.forEach(this._removeHandlers,function(_2c,idx){_2.disconnect(_2c);});_4.forEach(this._editHandlers,function(_2d,idx){_2.disconnect(_2d);});_a.destroy(this.bookmarkDomNode);},toJson:function(){var _2e=[];_4.forEach(this.bookmarks,function(_2f,idx){_2e.push(_2f.toJson());});return _2e;},_addInitialBookmarks:function(){if(this.editable){var _30=_d.widgets.bookmarks;var _31=_30.NLS_add_bookmark;var _32=_a.create("div",{innerHTML:"<div>"+_31+"</div>"});_b.add(_32,"esriBookmarkItem");_b.add(_32,"esriAddBookmark");this._clickHandlers.push(_2.connect(_32,"onclick",this,this._newBookmark));this._mouseOverHandlers.push(_2.connect(_32,"onmouseover",function(){_b.add(this,"esriBookmarkHighlight");}));this._mouseOutHandlers.push(_2.connect(_32,"onmouseout",function(){_b.remove(this,"esriBookmarkHighlight");}));var _33=this.bookmarkTable;var _34=_33.insertRow(0);var _35=_34.insertCell(0);_35.appendChild(_32);}this.bookmarks=[];_4.forEach(this.initBookmarks,function(_36,idx){this.addBookmark(_36);},this);},_removeBookmark:function(e){this.bookmarks.splice(e.target.parentNode.parentNode.parentNode.rowIndex,1);e.target.parentNode.parentNode.parentNode.parentNode.removeChild(e.target.parentNode.parentNode.parentNode);this.onRemove();},_editBookmarkLabel:function(e){var _37=e.target.parentNode;var _38=_c.position(_37,true);var y=_38.y;var _39=_a.create("div",{innerHTML:"<input type='text' class='esriBookmarkEditBox' style='left:"+_38.x+"px; top:"+y+"px;'/>"});this._inputBox=_5.query("input",_39)[0];this._label=_5.query(".esriBookmarkLabel",_37)[0];var _3a=_d.widgets.bookmarks;var _3b=_3a.NLS_new_bookmark;if(this._label.innerHTML==_3b){this._inputBox.value="";}else{this._inputBox.value=this._label.innerHTML;}_2.connect(this._inputBox,"onkeyup",this,function(key){switch(key.keyCode){case _7.ENTER:this._finishEdit();break;default:break;}});_2.connect(this._inputBox,"onblur",this,"_finishEdit");_37.appendChild(_39);this._inputBox.focus();},_finishEdit:function(){this._inputBox.parentNode.parentNode.removeChild(this._inputBox.parentNode);var _3c=_d.widgets.bookmarks;var _3d=_3c.NLS_new_bookmark;if(this._inputBox.value==""){this._label.innerHTML=_3d;}else{this._label.innerHTML=this._inputBox.value;}var _3e=_5.query(".esriBookmarkLabel",this.bookmarkDomNode);_4.forEach(this.bookmarks,function(_3f,idx){_3f.name=_3e[idx].innerHTML;});this.onEdit();},_newBookmark:function(){var map=this.map,_40=_d.widgets.bookmarks,_41=_40.NLS_new_bookmark,_42=map.extent,_43;if(map.spatialReference._isWrappable()){var _44=_10.prototype._normalizeX(_42.xmin,map.spatialReference._getInfo()).x;var _45=_10.prototype._normalizeX(_42.xmax,map.spatialReference._getInfo()).x;if(_44>_45){var _46=map.spatialReference.isWebMercator(),_47=_46?20037508.342788905:180,_48=_46?-20037508.342788905:-180,_49,_4a;if(Math.abs(_44-_47)>Math.abs(_45-_48)){_49=_44;_4a=_47;}else{_49=_48;_4a=_45;}_43=new _10(_49,_42.ymin,_4a,_42.ymax,map.spatialReference);}else{_43=new _10(_44,_42.ymin,_45,_42.ymax,map.spatialReference);}}else{_43=_42;}var _4b=new _11({"name":_41,"extent":_43});this.addBookmark(_4b);var _4c=_5.query(".esriBookmarkItem",this.bookmarkDomNode);var _4d=_4c[_4c.length-2];var e={"target":{"parentNode":null}};e.target.parentNode=_4d;this._editBookmarkLabel(e);},_onClickHandler:function(_4e){var _4f=_4e.extent;if(!_4e.extent.declaredClass){_4f=new _10(_4e.extent);}this.map.setExtent(_4f);this.onClick();}});if(_6("extend-esri")){_3.setObject("dijit.Bookmarks",BM,_e);}return BM;});