(function(a){a.fn.extend({textboxlist:function(b){var c=a.extend({},a.TextboxLister.defaults,b);return this.each(function(){var d=new a.TextboxLister(this,c)})}});a.TextboxLister=function(d,c){var b=this;b.input=a(d);d.textboxList=b;b.opts=a.extend({},b.input.metadata(),c);if(!b.opts.inputName){b.opts.inputName=b.input.attr("name")}b.container=b.input.wrap("<div class="+b.opts.containerClass+"></div>").parent().append("<span class='foswikiClear'></span>");if(b.opts.clearControl){a(b.opts.clearControl).click(function(){b.clear();this.blur();return false})}if(b.opts.resetControl){a(b.opts.resetControl).click(function(){b.reset();this.blur();return false})}if(b.opts.autocomplete){b.input.attr("autocomplete","off").autocomplete(b.opts.autocomplete,b.opts).result(function(e,g,f){b.select(f)})}else{a.log("TEXTBOXLIST: no autocomplete")}b.input.bind((a.browser.opera?"keypress":"keydown")+".textboxlist",function(e){if(e.keyCode==13){var f=b.input.val();if(f){b.select(f);e.preventDefault();return false}}});b.input.bind("AddValue",function(f,g){a.log("TEXTBOXLIST: got add event, val="+g);if(g){b.select(g)}});b.input.bind("DeleteValue",function(f,g){if(g){b.deselect([g])}});b.input.bind("Reset",function(f){b.reset()});b.input.bind("Clear",function(f){b.clear()});b.currentValues=[];b.titleOfValue=[];if(b.input.val()){b.select(b.input.val().split(/\s*,\s*/).sort(),true)}b.initialValues=b.currentValues.slice();b.input.removeClass("foswikiHidden").show();return this};a.TextboxLister.prototype.clear=function(){a.log("TEXTBOXLIST: called clear");var b=this;b.container.find("."+b.opts.listValueClass).remove();b.currentValues=[];b.titleOfValue=[];if(typeof(b.opts.onClear)=="function"){a.log("TEXTBOXLIST: calling onClear handler");b.opts.onClear(b)}};a.TextboxLister.prototype.reset=function(){a.log("TEXTBOXLIST: called reset");var b=this;b.clear();b.select(b.initialValues);if(typeof(b.opts.onReset)=="function"){a.log("TEXTBOXLIST: calling onReseet handler");b.opts.onReset(b)}};a.TextboxLister.prototype.select=function(k,d){a.log("TEXTBOXLIST: called select("+k+") "+typeof(k));var m=this,f,e,c,h,n,b,g,l;if(typeof(k)==="object"){k=k.join(",")}if(typeof(k)!=="undefined"&&typeof(k)!=="null"){k=k.split(/\s*,\s*/).sort()}else{k=""}for(f=0;f<k.length;f++){c=k[f];n=false;if(!c){continue}h=c;if(c.match(/^(.*)=(.*)$/)){k[f]=c=RegExp.$1;m.titleOfValue["_"+c]=RegExp.$2}}if(m.currentValues.length>0){for(f=0;f<k.length;f++){c=k[f];n=false;if(!c){continue}for(e=0;e<m.currentValues.length;e++){b=m.currentValues[e];if(b==c){n=true;break}}if(!n){m.currentValues.push(c)}}}else{m.currentValues=[];for(f=0;f<k.length;f++){c=k[f];if(c){m.currentValues.push(c)}}}if(m.opts.doSort){m.currentValues=m.currentValues.sort()}a.log("TEXTBOXLIST: self.currentValues="+m.currentValues+" length="+m.currentValues.length);m.container.find("."+m.opts.listValueClass).remove();for(f=m.currentValues.length-1;f>=0;f--){c=m.currentValues[f];if(!c){continue}h=m.titleOfValue["_"+c]||c;a.log("TEXTBOXLIST: val="+c+" title="+h);g="<input type='hidden' name='"+m.opts.inputName+"' value='"+c+"' title='"+h+"' />";l=a("<a href='#' title='remove "+h+"'></a>").addClass(m.opts.closeClass).click(function(i){i.preventDefault();m.input.trigger("DeleteValue",a(this).parent().find("input").val());return false});a("<span></span>").addClass(m.opts.listValueClass).append(g).append(l).append(h).prependTo(m.container)}m.input.val("");if(!d&&typeof(m.opts.onSelect)=="function"){a.log("TEXTBOXLIST: calling onSelect handler");m.opts.onSelect(m)}m.input.trigger("SelectedValue",k)};a.TextboxLister.prototype.deselect=function(c){a.log("TEXTBOXLIST: called deselect("+c+")");var b=this,f=[],g,e,d,h,k;if(typeof(c)=="object"){c=c.join(",")}c=c.split(/\s*,\s*/);if(!c.length){return}for(g=0;g<b.currentValues.length;g++){d=b.currentValues[g];if(!d){continue}h=false;for(e=0;e<c.length;e++){k=c[e];if(k&&d==k){h=true;break}}if(!h&&d){f.push(d)}}b.currentValues=f;if(typeof(b.opts.onDeselect)=="function"){a.log("TEXTBOXLIST: calling onDeselect handler");b.opts.onDeselect(b)}b.select(f,true)};a.TextboxLister.defaults={containerClass:"jqTextboxListContainer",listValueClass:"jqTextboxListValue",closeClass:"jqTextboxListClose",doSort:false,inputName:undefined,resetControl:undefined,clearControl:undefined,autocomplete:undefined,onClear:undefined,onReset:undefined,onSelect:undefined,onDeselect:undefined,selectFirst:false,autoFill:false,matchCase:false,matchContains:false,matchSubset:true}})(jQuery);