/* (c) 2009 Michael Odden - http://michaelodden.com/ */
jQuery.fn.highlighter=function(params){this.version=0.3;params=$.extend(jQuery.fn.highlighter.defaults,params);var focusEl=$("<input />");var inputreader=function(){var _return={active:false,onInput:null,input_lastupdate:-1,debug:false,debug_element:null,cur_input:"",toString:function(){return"[MIO.inputreader v"+jQuery.fn.highlighter.version+"]";},init:function(listenat){this.active=true;listenat=listenat?listenat:document;var that=this;$(listenat).keyup(function(e){that.listener(e)});},setDebugElement:function(el){this.debug=true;this.debug_element=el;},register:function(code){var date=new Date();if((date.getTime()-this.input_lastupdate)>params.inputTimeout){this.clear();}
this.input_lastupdate=date.getTime();var char=String.fromCharCode(code);this.cur_input+=char;this.afterRegister();},afterRegister:function(){if(typeof this.onInput=="function"){this.onInput(this.cur_input);}
if(this.debug==true&&this.debug_element){this.debug_element.val(this.cur_input);}},clear:function(){this.cur_input="";this.afterRegister();},listener:function(e){if(!this.active)return;switch(e.keyCode){case 38:break;case 40:break;case 27:this.clear();break;default:this.register(e.keyCode);break;}}};return _return;};var ir=new inputreader();if(params.debugElement!=null){ir.setDebugElement($(params.debugElement));}
ir.init(document);return this.each(function(){if(this.nodeName!="TABLE")return;var rows=$("tbody tr",this);var cols=$("tbody tr td:nth-child("+params.column+")",this);ir.onInput=function(str){rows.removeClass(params.highlightClass);if(str=="")return;var checkexp=new RegExp("^"+str,"i");var foundMatch=false;cols.each(function(){if(!foundMatch&&checkexp.test($(this).html())){$(focusEl).appendTo(this).focus().remove();$(this).parent().addClass(params.highlightClass);foundMatch=true;}});};});};jQuery.fn.highlighter.defaults={highlightClass:"highlight",inputTimeout:1000,debugElement:null,column:1};
