/**
 *  $Id: autosuggest.js 130 2006-11-30 20:44:53Z wingedfox $
 *  $HeadURL: https://svn.debugger.ru/repos/jslibs/BrowserExtensions/tags/BrowserExtensions.001/dom/autosuggest.js $
 *
 *  Extends the selectbox interface
 *
 *  @author Ilya Lebedev <ilya@lebedev.net>
 *  @modified $Date: 2006-11-30 23:44:53 +0300 (Чтв, 30 Ноя 2006) $
 *  @version $Rev: 130 $
 *  @title Selectbox
 *  @license LGPL 2.1 or later
 */
var Autosuggest=function(i){this.$VERSION$=" $Rev: 130 $ ".replace(/\D/g,"");var I=this;var l={'root':'Autosuggest'};var o=null;var O=null;var Q={'minlength':2,'delay':50,'size':15,'list':[],'place':0,'match':'start'};var _=null;this.setPlace=function(z){if(!isString(z))return false;switch(z.toLowerCase()){case'top':Q.place=1;break;case'bottom':Q.place=0;break;default:return false}return true};this.setMinLength=function(z){if(!isNumeric(parseInt(z)))return false;Q.minlength=z;return true};this.setDelay=function(z){if(!isNumeric(parseInt(z)))return false;Q.delay=z;return true};this.setSuggestions=function(z){if(!isArray(z))return false;Q.list=z;Q.rebuildList=true};this.setFilter=function(z){if(!O)return false;return O.showOnlyMatchingOptions(z,Q.match);};this.setFilterMatch=function(z){Q.match=z};this.getDelay=function(){return Q.delay};this.getMinLength=function(){return Q.minlength};this.show=function(){if(!o)return false;if(!o.parentNode||o.parentNode.nodeType==11){document.body.appendChild(o);var z=c(i);o.style.left=z.x+'px';o.style.top=z.y+i.offsetHeight+'px';O=new Selectbox(o.firstChild.id);}if(O){if(Q.rebuildList){O.addOptionsList(Q.list);Q.rebuildList=false}O.showOnlyMatchingOptions(i.value,Q.match);var Z=O.getOptionsLength();if(Z==0)return false;else if(Z<2)o.firstChild.size=2;else if(Z<Q.size)o.firstChild.size=Z;else o.firstChild.size=Q.size}else{return false}setTimeout(C,Q.delay);return true};this.hide=function(z){if(!o)return false;_=setTimeout(function(){o.style.display='none';_=null},Q.delay);return true};this.isVisible=function(){return!o.style.display};this.isHidden=function(){return!I.isVisible();};var c=function(z){var Z={'x':z.offsetLeft,'y':z.offsetTop};if(z.offsetParent){var w=c(z.offsetParent);Z.x+=w.x;Z.y+=w.y}return Z};var C=function(){o.style.visibility='hidden';o.style.display='';var z=c(i)['y']-getBodyScrollTop(),Z=getClientHeight(),w=Z-(c(i)['y']+i.offsetHeight-getBodyScrollTop()),W=document.getElementById(O.getId()),s=c(i)['y'],S=s+i.offsetHeight+'px';if(z>W.parentNode.offsetHeight&&Q.place){W.parentNode.style.top=z-W.parentNode.offsetHeight+getBodyScrollTop()+'px'}else if(w>W.parentNode.offsetHeight&&!Q.place){W.parentNode.style.top=c(i)['y']+i.offsetHeight+'px'}else{while(W.size>2&&w<W.parentNode.offsetHeight&&z<W.parentNode.offsetHeight){W.size--}if(w>=W.parentNode.offsetHeight||w>=z){W.parentNode.style.top=c(i)['y']+i.offsetHeight+'px'}else if(z>=W.parentNode.offsetHeight){W.parentNode.style.top=z-W.parentNode.offsetHeight+getBodyScrollTop()+'px'}}o.style.visibility='';if(O.getSelectedIndex()<0)O.selectOption(0);_=1};var e=function(z){el=z.target||z.srcElement;if(I.isHidden()&&i.value.length>=Q.minlength&&27!=z.keyCode){if(13==z.keyCode)return;I.show();}switch(z.keyCode){case 33:case 34:case 36:case 35:case 38:case 40:case 27:if(!I.isHidden())x(z);break;case 13:if(I.isHidden())return;i.value=o.firstChild.value;I.hide();i.focus();return x(z);break;default:if(O){O.showOnlyMatchingOptions(i.value,Q.match);if(O.getSelectedIndex()<0)O.selectOption(0);}break}};var v=function(z){var Z=z.target||z.srcElement;if('click'==z.type)z.keyCode=13;switch(z.keyCode){case 13:i.value=o.firstChild.value;if(z.preventDefault)z.preventDefault();z.returnValue=false;I.hide();i.focus();return false;default:}i.focus();};var V=function(z){switch(z.keyCode){case 27:case 33:case 34:case 36:case 35:case 27:if(I.isHidden())return;case 38:case 40:case 37:case 39:case 13:case 9:x(z);}};var x=function(z){z.returnValue=false;if(z.preventDefault)z.preventDefault();};var X=function(){o=document.createElementExt('div',{'class':l.root});o.innerHTML='<select id="Autosuggest'+(new Date).valueOf()+'" size="10" autocomplete="off"></select>';if(!o)return false;o.style.display='none';if(isString(i))i=document.getElementById(i);i.attachEvent('onkeyup',e);i.attachEvent('onkeypress',V);i.attachEvent('onkeydown',function(z){switch(z.keyCode){case 27:if(!I.isHidden()){I.hide();x(z);}break;case 32:if(!z.ctrlKey)return;case 38:case 40:case 33:case 34:if(I.isHidden()){I.show();}else{if(38==z.keyCode)O.selectPrev(true);else if(40==z.keyCode)O.selectNext(true);else if(33==z.keyCode)O.selectOption(O.getSelectedIndex()-o.firstChild.size,true);else if(34==z.keyCode)O.selectOption(O.getSelectedIndex()-(-o.firstChild.size),true);}x(z);break;case 36:case 35:if(!I.isHidden()){if(36==z.keyCode)O.selectOption(0,true);else if(35==z.keyCode)O.selectOption(O.getOptionsLength(),true);x(z);}break}});i.attachEvent('onmousedown',function(){_=1});i.attachEvent('onblur',function(){_==1?I.hide():_=2});o.firstChild.attachEvent('onmousedown',function(){_=2});o.firstChild.attachEvent('onkeydown',function(){_=1});o.firstChild.attachEvent('onblur',function(){_==2?I.hide():_=1});i.autocomplete='off';i.setAttribute('autocomplete','off');o.firstChild.attachEvent('onclick',v);o.firstChild.attachEvent('onkeydown',v);};return X();};Autosuggest.controls={};function getClientWidth(){var i=0;if(self.innerHeight)i=self.innerWidth;else if(document.documentElement&&document.documentElement.clientWidth)i=document.documentElement.clientWidth;else if(document.body)i=document.body.clientWidth;return i}function getClientHeight(){var i=0;if(self.innerHeight)i=self.innerHeight;else if(document.documentElement&&document.documentElement.clientHeight)i=document.documentElement.clientHeight;else if(document.body)i=document.body.clientHeight;return i}function getBodyScrollTop(){return self.pageYOffset||(document.documentElement&&document.documentElement.scrollTop)||(document.body&&document.body.scrollTop);}function getBodyScrollLeft(){return self.pageXOffset||(document.documentElement&&document.documentElement.scrollLeft)||(document.body&&document.body.scrollLeft);}
