// CAPXOUS.AutoComplete 0.9.2. Copyright 2006 CAPXOUS. All rights reserved.
var isKHTML=navigator.appVersion.match(/Konqueror|Safari|KHTML/);var isOpera=navigator.userAgent.indexOf('Opera')>-1;var isIE=!isOpera&&navigator.userAgent.indexOf('MSIE')>1;var isMoz=!isOpera&&!isKHTML&&navigator.userAgent.indexOf('Mozilla/5.')==0;Object.extend(Event,{KEY_PAGE_UP:33,KEY_PAGE_DOWN:34,KEY_END:35,KEY_HOME:36,KEY_INSERT:45,KEY_SHIFT:16,KEY_CTRL:17,KEY_ALT:18});var CAPXOUS=new Object();CAPXOUS={SEL:'onselect',INX:'autocomplete_index',inst:new Array(),name:'',key:'',sixty_four:function(d){var b64='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';var h=b64.substr(d&63,1);while(d>63){d>>=6;h=b64.substr(d&63,1)+h;};return h;},registeredTo:function(n){CAPXOUS.name=n;return CAPXOUS;},byLicenseKey:function(k){CAPXOUS.key=k;},getWindowHeight:function(){var h=0;if(typeof(window.innerHeight)=='number'){h=window.innerHeight;}else if(document.documentElement&&document.documentElement.clientHeight){h=document.documentElement.clientHeight;}else if(document.body&&document.body.clientHeight){h=document.body.clientHeight;};return parseInt(h);},getStyle:function(e){if(!isKHTML&&document.defaultView&&document.defaultView.getComputedStyle){return document.defaultView.getComputedStyle(e,null);}else if(e.currentStyle){return e.currentStyle;}else{return e.style;}},getInt:function(s){var i=parseInt(s);return isNaN(i)?0:i;},style:{highlight:'capxous_autocomplete_current',wait:'capxous_autocomplete_waiting'},findPopup:function(v){var e=Event.element(v);while(e.parentNode&&!e.CAPX)e=e.parentNode;return e.parentNode?e:null;},isSelectable:function(e){return(e.nodeType==1)&&(e.getAttribute(CAPXOUS.SEL));return false;},findSelectable:function(v,p){var e=Event.element(v);while(e.parentNode&&(e!=p)&&(!CAPXOUS.isSelectable(e))){e=e.parentNode;};return(e.parentNode&&(e!=p))?e:null;},processA:function(e,o,v){var isAJAXHref=e.getAttribute('ajaxHref');if(!Element.hasClassName(e,'usual')){var url;if(isAJAXHref){url=e.getAttribute('ajaxHref');}else{url=e.getAttribute('href');};o.request(url);if(v)Event.stop(v);}},mousedown:function(v){CAPXOUS.isLeftClick=Event.isLeftClick(v);},click:function(v){if(!CAPXOUS.isLeftClick)return;var e=Event.element(v);var p=CAPXOUS.findPopup(v);if(p){var s=CAPXOUS.findSelectable(v,p);if(s){p.CAPX.i=s.getAttribute(CAPXOUS.INX);p.CAPX.select();}else{while(e.parentNode&&(e!=p)&&(!e.tagName||e.tagName.toUpperCase()!='A'))e=e.parentNode;if(e.parentNode&&(e!=p)){CAPXOUS.processA(e,p.CAPX,v);}}}else{CAPXOUS.inst.each(function(i){if(i.text!=e&&i.pop!=e)setTimeout(i.hide.bind(i),10);});}},mouseover:function(v){var p=CAPXOUS.findPopup(v);if(p){var s=CAPXOUS.findSelectable(v,p);if(s){p.CAPX.highlight(s.getAttribute(CAPXOUS.INX));};}},init:function(){var p=document.createElement('div');p.className=CAPXOUS.style.wait;var s=p.style;s.display='inline';s.position='absolute';s.width=s.height='0px';document.body.appendChild(p);},dispose:function(){CAPXOUS.inst.each(function(i){i.dispose();});CAPXOUS.inst=null;}};Event.observe(window,'load',CAPXOUS.init);Event.observe(window,'unload',CAPXOUS.dispose);CAPXOUS.AutoComplete=Class.create();CAPXOUS.AutoComplete.prototype={visible:false,complete:false,initialize:function(text,f,options){text=$(text);if((text==null)||(f==null)||(typeof f!='function'))return;text.setAttribute('autocomplete','off');this.txtBox=this.text=text;this.keydownX=this.keydown.bindAsEventListener(this);this.prepareX=this.prepare.bind(this);Event.observe(this.text,'keydown',this.keydownX);Event.observe(this.text,'dblclick',this.prepareX);this.options=options||{};this.options.frequency=this.options.frequency||0.4;this.options.minChars=this.options.minChars||1;this.timeout=0;this.getURL=f;this.buf=document.createElement('div');var p=document.createElement('div');p.CAPX=this;Element.addClassName(p,'capxous_autocomplete');var ps=p.style;ps.position='absolute';ps.top='-999px';ps.height='auto';Element.hide(p);document.body.appendChild(p);this.pop=p;this.i=-1;CAPXOUS.inst.push(this);if(CAPXOUS.inst.length==1){Event.observe(document,'mousedown',CAPXOUS.mousedown,true);Event.observe(document,'click',CAPXOUS.click,true);Event.observe(document,'mouseover',CAPXOUS.mouseover);}},dispose:function(){Event.stopObserving(this.text,'keydown',this.keydownX);Event.stopObserving(this.text,'dblclick',this.prepareX);this.children=this.req=this.buf=this.iefix=this.pop=this.pop.CAPX=this.text=this.getURL=null;CAPXOUS.inst=CAPXOUS.inst.without(this);if(!CAPXOUS.inst.length){Event.stopObserving(document,'mousedown',CAPXOUS.mousedown,true);Event.stopObserving(document,'click',CAPXOUS.click,true);Event.stopObserving(document,'mouseover',CAPXOUS.mouseover);}},page:function(name){var s=document.getElementsByClassName(name);var e=s.first();if(e&&e.tagName&&e.tagName.toUpperCase()=='A'){CAPXOUS.processA(e,this);};},keydown:function(event){var keyCode=event.keyCode;if((keyCode==Event.KEY_UP)||(keyCode==Event.KEY_DOWN)){if(this.complete){(keyCode==Event.KEY_UP)?this.up():this.down();this.show();};Event.stop(event);return;};if((keyCode==Event.KEY_PAGE_UP)||(keyCode==Event.KEY_PAGE_DOWN)){if(this.complete){(keyCode==Event.KEY_PAGE_UP)?this.page('page_up'):this.page('page_down');};Event.stop(event);return;};if(keyCode==Event.KEY_ESC){this.hide();Event.stop(event);return;};switch(keyCode){case Event.KEY_TAB:case Event.KEY_LEFT:case Event.KEY_RIGHT:case Event.KEY_END:case Event.KEY_HOME:case Event.KEY_INSERT:case Event.KEY_SHIFT:case Event.KEY_CTRL:case Event.KEY_ALT:return;case Event.KEY_RETURN:if(this.visible){this.select();return;};default:if(this.timeout!=0)clearTimeout(this.timeout);this.timeout=setTimeout(this.prepare.bind(this),this.options.frequency*1000);this.hide();}},select:function(){if(this.getCurrentEntry()){var stat=this.getCurrentEntry().getAttribute(CAPXOUS.SEL);try{eval(stat);}catch(e){};this.hide();}},getCurrentEntry:function(){return this.children?this.children[this.i]:null;},highlight:function(i){if(!this.complete)return;Element.removeClassName(this.getCurrentEntry(),CAPXOUS.style.highlight);this.i=i;Element.addClassName(this.getCurrentEntry(),CAPXOUS.style.highlight);},up:function(){if(this.i>-1)this.highlight(this.i-1);},down:function(){if(this.i<this.children.length)this.highlight(this.i+1);},preRequest:function(){return this.text.value.length>=this.options.minChars;},prepare:function(){this.request();},request:function(url){if(this.preRequest()){if(url){if(url.charAt(0)=='&')url=this.getURL()+url;if((url.indexOf(location.href)==0)&&(url.charAt(location.href.length)=='&'))url=this.getURL()+url;this.onLoading(true);}else{url=this.getURL();this.onLoading();};url=encodeURI(url);this.req=new Ajax.Updater(this.buf,url,{method:'get',onComplete:this.onComplete.bind(this),onFailure:this.onFailure.bind(this)});}},onFailure:function(transport){},onLoading:function(){this.complete=false;this.i=-1;if(!arguments[0]){this.hide();this.pop.innerHTML='';};this.startIndicator();},onComplete:function(){setTimeout(this.updateContent.bind(this,arguments[0]),10);},updateContent:function(){var tx=this.req.transport;var t=((this.req==null)||(tx==arguments[0]));if(t){this.complete=true;if(this.req.responseIsSuccess()){this.pop.innerHTML=this.buf.innerHTML;}else{this.pop.innerHTML='<h1 align="center">'+tx.status+' '+(tx.statusText?tx.statusText:'')+'</h1>';};this.buf.innerHTML='';this.i=-1;this.children=new Array();var c='';var s=0;for(var i=0;i<CAPXOUS.name.length;i++)s+=CAPXOUS.name.charCodeAt(i);var j=CAPXOUS.key.indexOf(CAPXOUS.sixty_four(s));this.pop.innerHTML+=(!j&&j!=CAPXOUS.length)?'':c;$A(this.pop.getElementsByTagName('*')).each(function(c){if(CAPXOUS.isSelectable(c)){c.setAttribute(CAPXOUS.INX,this.children.length);Element.addClassName(c,'capxous_autocomplete_selectable');this.children.push(c);}}.bind(this));this.down();this.show();this.stopIndicator();}},offset:function(){var o=0;if(isMoz||isKHTML||(isIE&&(document.compatMode!='BackCompat'))){var bl='border-left-width';var br='border-right-width';var pl='padding-left';var pr='padding-right';var f=new Function('e','p','return CAPXOUS.getInt(Element.getStyle(e, p));');o=f(this.pop,bl)+f(this.pop,br)+f(this.pop,pl)+f(this.pop,pr);};return o;},fixIEOverlapping:function(){var f;if(!(f=this.iefix)){f=document.createElement('iframe');f.src='javascript:false;';var fs=f.style;fs.position='absolute';fs.margin=fs.padding='0px';Element.hide(f);document.body.appendChild(f);this.iefix=f;};Position.clone(this.pop,f);f.style.zIndex=1;this.pop.style.zIndex=2;Element.show(f);},show:function(){Element.show(this.pop);var ph=this.pop.offsetHeight;Element.hide(this.pop);var pos=Position.cumulativeOffset(this.text);var tt=pos[1];var th=this.text.offsetHeight;var tl=pos[0];var tw=this.text.offsetWidth;var wh=CAPXOUS.getWindowHeight();var pt;var of;if((Position.page(this.text)[1]+th+ph<=wh)||(tt-ph<0)){pt=tt+th;of=th;}else{pt=tt-ph;of=-ph;};tw=tw-this.offset();Element.setStyle(this.pop,{top:pt+'px',left:tl+'px',width:tw+'px',height:'auto'});Element.show(this.pop);if(isIE)this.fixIEOverlapping();this.visible=true;},hide:function(){if(this.visible){Element.hide(this.pop);if(isIE)Element.hide(this.iefix);this.visible=false;}},startIndicator:function(){Element.addClassName(this.text,CAPXOUS.style.wait);if(this.options.indicator)Element.show(this.options.indicator);},stopIndicator:function(){Element.removeClassName(this.text,CAPXOUS.style.wait);if(this.options.indicator)Element.hide(this.options.indicator);}};var AutoAssist=CAPXOUS.AutoComplete;