jQuery.fn.tagInput=function(options){if(!options.tags&&!options.jsonUrl){options.tags=[{tag:"tag1",freq:1},{tag:"tag2",freq:2},{tag:"tag3",freq:3},{tag:"tag4",freq:4}]}if(typeof(options.tagSeparator)=="undefined")options.tagSeparator=",";if(typeof(options.autoFilter)=="undefined")options.autoFilter=true;if(typeof(options.autoStart)=="undefined")options.autoStart=false;if(typeof(options.boldify)=="undefined")options.boldify=true;if(typeof(options.sortBy)=="undefined")options.sortBy="tag";this.each(function(){var theInput=$(this);var theDiv;theInput.addClass("tagInput");theInput.tagOptions=options;var suggestedTagsPlaceHolder=options.suggestedTagsPlaceHolder;if(options.suggestedTags){if(!suggestedTagsPlaceHolder){var stl=$("<div class='tagInputSuggestedTags'><span class='label'>suggested tags: </span><span class='tagInputSuggestedTagList'></span></div>");suggestedTagsPlaceHolder=stl.find(".tagInputSuggestedTagList");theInput.after(stl)}for(var tag in options.suggestedTags){suggestedTagsPlaceHolder.append($("<span class='tag'>"+options.suggestedTags[tag]+"</span>"))}suggestedTagsPlaceHolder.find(".tag").click(function(){var element=$(this);var val=theInput.val();var tag=element.text();var re=new RegExp(tag+"\\b","g");if(containsTag(val,tag)){val=val.replace(re,"");element.removeClass("tagUsed")}else{val=val+options.tagSeparator+tag;element.addClass("tagUsed")}theInput.val(refurbishTags(val))})}var tagInputFocus=function(){theDiv=$("#__tagInputDiv");if(theDiv.size()<=0){theDiv=$("<div id='__tagInputDiv' class='tagInputDiv' style='width:"+theInput.get(0).clientWidth+";display:none; '></div>");theInput.after(theDiv);theDiv.css({left:theInput.position().left})}if(options.autoStart)tagInputRefreshDiv(theInput,theDiv)};var tagInputBlur=function(){theDiv=$("#__tagInputDiv");theInput.val(refurbishTags(theInput.val()));theDiv.fadeOut(200,function(){theDiv.remove()})};var tagInputKey=function(e){var rows=theDiv.find("div.tagInputLine");var rowNum=rows.index(theDiv.find("div.tagInputSel"));var ret=true;switch(e.which){case 38:rowNum=(rowNum<1?0:rowNum-1);tagInputHLSCR(rows.eq(rowNum),true);break;case 40:rowNum=(rowNum<rows.size()-1?rowNum+1:rows.size()-1);tagInputHLSCR(rows.eq(rowNum),false);break;case 9:case 13:if(theDiv.is(":visible")){var theRow=rows.eq(rowNum);tagInputClickRow(theRow);ret=false}break;case 27:theDiv.fadeOut(200);break;default:$(document).stopTime("tagInputRefresh");$(document).oneTime(400,"tagInputRefresh",function(){tagInputRefreshDiv()});break}return ret};var tagInputHLSCR=function(theRowJQ,isUp){if(theRowJQ.size()>0){var div=theDiv.get(0);var theRow=theRowJQ.get(0);if(isUp){if(theDiv.scrollTop()>theRow.offsetTop){theDiv.scrollTop(theRow.offsetTop)}}else{if((theRow.offsetTop+theRow.offsetHeight)>(div.scrollTop+div.offsetHeight)){div.scrollTop=theRow.offsetTop+theRow.offsetHeight-div.offsetHeight}}theDiv.find("div.tagInputSel").removeClass("tagInputSel");theRowJQ.addClass("tagInputSel")}};var tagInputClickRow=function(theRow){var lastComma=theInput.val().lastIndexOf(options.tagSeparator);var sep=lastComma<=0?(""):(options.tagSeparator+(options.tagSeparator==" "?"":" "));var newVal=theRow.find(".tagInputLineTag").text();theInput.val(newVal);theDiv.hide().remove();$().oneTime(200,function(){theInput.focus()})};var tagInputRefreshDiv=function(){var lastComma=theInput.val().lastIndexOf(options.tagSeparator);var search=theInput.val();var fillingCallbak=function(tags){if(options.sortBy=="frequency"){tags=tags.sort(function(a,b){if(a.freq<b.freq)return 1;if(a.freq>b.freq)return-1;return 0})}else if(options.sortBy=="tag"){tags=tags.sort(function(a,b){if(a.tag<b.tag)return-1;if(a.tag>b.tag)return 1;return 0})}for(var i in tags){var el=tags[i];var matches=el.tag.toLocaleLowerCase().indexOf(search.toLocaleLowerCase())==0;if(!options.autoFilter||matches){var line=$("<div class='tagInputLine'></div>");var tag=el.tag;if(options.boldify&&matches){tag="<b>"+tag.substring(0,search.length)+"</b>"+tag.substring(search.length)}line.append("<div class='tagInputLineTag'>"+tag+"</div>");theDiv.append(line)}}if(theDiv.html()!=""){theDiv.fadeIn(700)}theDiv.find("div:first").addClass("tagInputSel");theDiv.find("div.tagInputLine").bind("click",function(){tagInputClickRow($(this))})};if(search!=""||options.autoStart){theDiv.html("");if(options.tags)fillingCallbak(options.tags);else{var data={search:search};if(search.length>1)$.getJSON(options.jsonUrl,data,fillingCallbak)}}else{theDiv.fadeOut(700)}};var refurbishTags=function(tagString){var splitted=tagString.split(options.tagSeparator);var res="";var first=true;for(var i=0;i<splitted.length;i++){if(splitted[i].trim()!=""){if(first){first=false;res=res+splitted[i].trim()}else{res=res+options.tagSeparator+(options.tagSeparator==" "?"":" ")+splitted[i].trim()}}}return(res)};var containsTag=function(tagString,tag){var splitted=tagString.split(options.tagSeparator);var res="";var found=false;tag=tag.trim();for(i=0;i<splitted.length;i++){var testTag=splitted[i].trim();if(testTag==tag){found=true;break}}return found};var delayedSelectTagFromInput=function(){var element=$(this);$().stopTime("suggTagRefresh");$().oneTime(400,"suggTagRefresh",function(){selectSuggTagFromInput()})};var selectSuggTagFromInput=function(){var val=theInput.val();suggestedTagsPlaceHolder.find(".tag").each(function(){var el=$(this);var tag=el.text();if(containsTag(val,tag)){el.addClass("tagUsed")}else{el.removeClass("tagUsed")}})};$(this).bind("focus",tagInputFocus).bind("blur",tagInputBlur).bind("keydown",tagInputKey);if(options.suggestedTags)$(this).bind("keyup",delayedSelectTagFromInput)});return this};
