/*
Script Name: 	Autocomplete (http://jastegg.it/eggs/autocomplete/ ) 
version: 		1.0 beta
version date:	2007-10-19
Plugin for:		JAST ( http://jastegg.it )
--------------------------------
*/
JASTEggIt.extend('autocomplete',{info:{title:'Autocompleter',version:'1.0 beta',eggUrl:'http://jastegg.it/eggs/autocomplete',author:'Diego La Monica',url:'http://diegolamonica.info'},keyboards:{up:38,down:40,enter:13},options:{source:'page',page:'autocomplete-response.php',outputId:'div-autocomplete',pageParam:'q',method:'GET',parser:'parseJSON',formatOutput:null,caseSensitive:false,minChar:3,separator:',',refresh:100,jsonItems:null,cacheValue:'',selectedItem:null},_items:[],setup:function(id,options){var txt=JASTEggIt._id(id);if(txt==null)return false;if(options==null)options=this.options;for(o in this.options){if(options[o]==null)options[o]=this.options[o];}options['currentValue']=txt.value;options['interval']=setInterval('JASTEggIt.autocomplete._check(\''+id+'\')',100);JASTEggIt.event(id,'keydown','JASTEggIt.autocomplete._keyDown(event)');txt.setAttribute('autocomplete','off');txt.onkeypress=JASTEggIt.autocomplete._keyConfirm;JASTEggIt._id(options.outputId).style.display='none';this._items[id]=options;},_keyConfirm:function(e){var kp=JASTEggIt.kbd.getKeyPressed(e);if(kp==JASTEggIt.autocomplete.keyboards.enter){var id='';if(e==null)e=window.event;if(e.srcElement!=null)id=e.srcElement.id;if(e.target!=null)id=e.target.id;i=JASTEggIt.autocomplete._items[id].selectedItem;JASTEggIt.autocomplete._append(id,i);return false;}},_keyDown:function(e){var kp=JASTEggIt.kbd.getKeyPressed(e);var id='';var refresh=false;if(e.srcElement!=null)id=e.srcElement.id;if(e.target!=null)id=e.target.id;if(kp==this.keyboards.up){refresh=true;this._items[id].selectedItem-=1;};if(kp==this.keyboards.down){refresh=true;this._items[id].selectedItem+=1;};if(refresh){if(this._items[id].selectedItem<0)this._items[id].selectedItem=0;this._processJSON(id,this._items[id].cacheValue);return false;}},_check:function(id){var txt=JASTEggIt._id(id);if(txt==null)return false;var currentValue=txt.value;var itm=this._items[id];if(currentValue!=itm.currentValue){var detail='';if(itm.separator!=''){var details=eval('currentValue.split(/'+itm.separator+'/)');detail=details[details.length-1];}else{detail=currentValue;}detail=JASTEggIt.strings.trim(detail);if(detail.length>=itm.minChar){this._items[id].currentValue=currentValue;switch(itm.source){case'page':var params=new Object;params[itm.pageParam]=detail;JASTEggIt.xhttp.sendRequest(itm.method,itm.page,params,'JASTEggIt.autocomplete.'+itm.parser+'(\''+id+'\',%%BUFFER%%);');break;case'json':this._processJSON(id,itm.jsonItems);break;}}else{JASTEggIt._id(itm.outputId).style.display='none';}}},_append:function(id,rowIndex){var itm=this._items[id];value=itm.cacheValue[rowIndex][0];if(itm.separator!=''){var details=eval('itm.currentValue.split(/'+itm.separator+'/)');details[details.length-1]=(details.length!=1?' ':'')+value;detail=details.join(itm.separator);}else{detail=value;};JASTEggIt._id(id).value=detail;this._items[id].currentValue=detail;JASTEggIt._id(itm.outputId).style.display='none';},_processJSON:function(id,json){var itm=this._items[id];var selected=itm.selectedItem;var buffer='';var detail='';if(itm.separator!=''){var details=eval('itm.currentValue.split(/'+itm.separator+'/)');detail=details[details.length-1];}else{detail=itm.currentValue;};detail=JASTEggIt.strings.trim(detail);var viewCount=0;json=this._purgeJson(json,detail,itm.caseSensitive);itm.cacheValue=json;for(var i=0;i<json.length;i++){var row=json[i];if(selected==null)selected=viewCount;var bufferText='';if(itm.formatOutput!=null){bufferText=itm.formatOutput(row);}else{bufferText+=row[0];};var k=bufferText.indexOf(detail);if(k!=-1){bufferText=bufferText.substring(0,k-1)+'<strong>'+detail+'</strong>'+bufferText.substring(k+detail.length,bufferText.length);};if(i==json.length-1&&selected>viewCount)selected=viewCount;var _class=(selected!=viewCount)?'':'class="selected" ';if(_class!=''){this._items[id].selectedItem=viewCount;};buffer+='<li '+_class+'onclick="JASTEggIt.autocomplete._append(\''+id+'\','+i+')">'+bufferText+'<'+'/li>';viewCount+=1;};buffer='<ul class="JAST-autocompleter">'+buffer+'</ul>';var txt=JASTEggIt._id(id);var pos=JASTEggIt.DOM.position(txt);var outElem=JASTEggIt._id(itm.outputId);outElem.style.display='';outElem.style.position='absolute';outElem.style.left=pos.x+'px';outElem.style.top=pos.y+txt.offsetHeight+'px';outElem.innerHTML=buffer;},_purgeJson:function(json,detail,caseSensitive){var retJson=Array();if(caseSensitive)detail=detail;for(var i=0;i<json.length;i++){var row=json[i];var k=-1;if(caseSensitive)var k=row[0].indexOf(detail);else var k=row[0].toUpperCase().indexOf(detail.toUpperCase());if(k!=-1)retJson[retJson.length]=json[i];}return retJson;},parseJSON:function(id,buffer){var json=eval(buffer);this._processJSON(id,json);}});
