/* See license.txt for terms of usage */ define("tabs/search",["domplate/domplate","core/lib","i18n!nls/search","domplate/toolbar","domplate/popupMenu","core/cookies","core/dragdrop"],function(Domplate,Lib,Strings,Toolbar,Menu,Cookies,DragDrop){with(Domplate){var Search={},caseSensitiveOption="searchCaseSensitive";return Search.Box=domplate({tag:SPAN({"class":"searchBox"},SPAN({"class":"toolbarSeparator resizer"}," "),SPAN({"class":"searchTextBox"},INPUT({"class":"searchInput",type:"text",placeholder:Strings.search,onkeydown:"$onKeyDown"}),SPAN({"class":"arrow",onclick:"$onOpenOptions"}," "))),onKeyDown:function(e){var t=$.event.fix(e||window.event),n=Lib.getAncestorByClass(t.target,"tabBody"),r=Lib.getElementByClass(n,"searchInput");setTimeout(Lib.bindFixed(this.search,this,n,t.keyCode,r.value))},initialize:function(e){var t=Lib.getElementByClass(e,"searchInput"),n=Lib.getElementByClass(e,"resizer");Search.Resizer.initialize(t,n)},search:function(e,t,n){var r=Lib.getElementByClass(e,"searchBox"),i=Lib.getElementByClass(e,"searchInput");i.removeAttribute("status");var s=i.value;if(s==n&&t!=13)return;if(t!=13&&Lib.isWebkit)return;var o=e.repObject.onSearch(s,t);o||i.setAttribute("status","notfound")},onOpenOptions:function(e){var t=Lib.fixEvent(e);Lib.cancelEvent(e);if(!Lib.isLeftClick(e))return;var n=t.target,r=this.getMenuItems(n),i=new Menu({id:"searchOptions",items:r});i.showPopup(n)},getMenuItems:function(e){var t=Lib.getAncestorByClass(e,"tabBody"),n=t.repObject.getSearchOptions();return n.push("-"),n.push({label:Strings.caseSensitive,checked:Cookies.getBooleanCookie(caseSensitiveOption),command:Lib.bindFixed(this.onOption,this,caseSensitiveOption)}),n},onOption:function(e){Cookies.toggleCookie(e);var t=Lib.getElementByClass(document.documentElement,"searchInput");t.removeAttribute("status")}}),Search.ObjectSearch=function(e,t,n,r){this.text=e,this.reverse=n,this.caseSensitive=r,this.stack=[],this.stack.push({object:t,propIndex:0,startOffset:-1}),this.matches=[]},Search.ObjectSearch.prototype={findNext:function(e){while(this.stack.length>0){var t=this.getCurrentScope(),n=this.find(t);if(n)return n}return!1},find:function(e){var t=0;for(var n in e.object){t++;if(e.propIndex>=t)continue;var r=e.object[n];if(!r)continue;e.propIndex=t;if(typeof r=="object")return this.stack.push({propIndex:0,object:r,startOffset:-1}),!1;var i=this.text,s=r+"";Cookies.getBooleanCookie(caseSensitiveOption)||(s=s.toLowerCase(),i=i.toLowerCase());var o=e.startOffset<0?0:e.startOffset,u=s.indexOf(i,o);if(u>=0)return e.propIndex+=-1,e.startOffset=u+i.length,this.matches.push({value:r,startOffset:u}),!0}return this.stack.pop(),!1},getCurrentScope:function(){return this.stack[this.stack.length-1]},getCurrentMatch:function(){return this.matches[this.matches.length-1]},selectText:function(e){var t=this.getCurrentMatch();Lib.selectElementText(e,t.startOffset,t.startOffset+this.text.length)}},Search.Resizer=domplate({initialize:function(e,t){this.searchInput=e,this.tracker=new DragDrop.Tracker(t,{onDragStart:Lib.bind(this.onDragStart,this),onDragOver:Lib.bind(this.onDragOver,this),onDrop:Lib.bind(this.onDrop,this)})},onDragStart:function(e){var t=Lib.getBody(this.searchInput.ownerDocument);t.setAttribute("vResizing","true"),this.startWidth=this.searchInput.clientWidth-20},onDragOver:function(e,t){var n=this.startWidth-e.x,r=Lib.getAncestorByClass(this.searchInput,"toolbar");if(n>r.clientWidth-40)return;this.searchInput.style.width=n+"px"},onDrop:function(e){var t=Lib.getBody(this.searchInput.ownerDocument);t.removeAttribute("vResizing")}}),Search}});