(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){a.extend(b.opts,{source:b.opts.autocomplete,select:function(e,f){a.log("TEXTBOXLIST: selected value="+f.item.value+" label="+f.item.label);b.select(f.item.value);return false}});b.input.attr("autocomplete","off").autocomplete(b.opts)}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){a.log("TEXTBOXLIST: closing suggestion list");b.input.autocomplete("close");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(l,d){a.log("TEXTBOXLIST: called select("+l+") "+typeof(l));var n=this,f,e,c,k,o,b,h,m,g;if(typeof(l)==="object"){l=l.join(",")}if(typeof(l)!=="undefined"&&typeof(l)!=="null"){l=l.split(/\s*,\s*/).sort()}else{l=""}for(f=0;f<l.length;f++){c=l[f];o=false;if(!c){continue}k=c;if(c.match(/^(.*)=(.*)$/)){l[f]=c=RegExp.$1;n.titleOfValue["_"+c]=RegExp.$2}}if(n.currentValues.length>0){for(f=0;f<l.length;f++){c=l[f];o=false;if(!c){continue}for(e=0;e<n.currentValues.length;e++){b=n.currentValues[e];if(b==c){o=true;break}}if(!o){n.currentValues.push(c)}}}else{n.currentValues=[];for(f=0;f<l.length;f++){c=l[f];if(c){n.currentValues.push(c)}}}if(n.opts.doSort){n.currentValues=n.currentValues.sort()}a.log("TEXTBOXLIST: self.currentValues="+n.currentValues+" length="+n.currentValues.length);n.container.find("."+n.opts.listValueClass).remove();for(f=n.currentValues.length-1;f>=0;f--){c=n.currentValues[f];if(!c){continue}k=n.titleOfValue["_"+c]||c;a.log("TEXTBOXLIST: val="+c+" title="+k);g="tag_"+k.replace(/["' ]/,"_");h="<input type='hidden' name='"+n.opts.inputName+"' value='"+c+"' title='"+k+"' />";m=a("<a href='#' title='remove "+k+"'></a>").addClass(n.opts.closeClass).click(function(i){i.preventDefault();n.input.trigger("DeleteValue",a(this).parent().find("input").val());return false});a("<span></span>").addClass(n.opts.listValueClass+" "+g).append(h).append(m).append(k).prependTo(n.container)}n.input.val("");if(!d&&typeof(n.opts.onSelect)=="function"){a.log("TEXTBOXLIST: calling onSelect handler");n.opts.onSelect(n)}n.input.trigger("SelectedValue",l)};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);