var Tags=function(n){this.selector={tagGroup:"[data-js-tag-group]",tagField:"[data-js-tag-field]",tagAddButton:"[data-js-tag-button]",tagList:"[data-js-tag-list]",tagError:"[data-js-tag-error]"};this.element={tagGroup:undefined,tagField:undefined,tagAddButton:undefined,tagList:undefined,tagError:undefined};this.property={tagSearchDataSource:undefined};this.init=function(){this.setup();this.bloodhound.init(this);this.typeahead.init(this);this.events.init(this)};this.setup=function(){this.element.tagField=$(this.selector.tagField);this.element.tagAddButton=$(this.selector.tagAddButton);this.element.tagGroup=$(this.selector.tagGroup);this.element.tagList=$(this.selector.tagList);this.element.clear=$(this.selector.clear)};this.bloodhound={property:{_this:undefined},init:function(n){this.property._this=n;this.setup()},setup:function(){var n=this,t=n.property._this;t.property.tagSearchDataSource=new Bloodhound({initialize:!1,datumTokenizer:Bloodhound.tokenizers.obj.whitespace("Name"),queryTokenizer:Bloodhound.tokenizers.obj.whitespace,identify:function(n){return n.SearchMoniker},remote:{cache:!1,url:"/AIHWWebServices/TagService.asmx/SearchTopicsAndTags",prepare:function(n,t){var i=n;return t.type="POST",t.beforeSend=function(n,t){t.data=JSON.stringify({search:i})},t.error=function(n,t,i){console.error("Error. ",n,t,i)},t.contentType="application/json; charset=utf-8",t},transform:function(t){return n.getUnusedKeywords(t.d.Keywords)}}});t.property.tagSearchDataSource.initialize()},getUnusedKeywords:function(t){var i=n.Filter;return t.filter(function(n){var t=n.Name,r=typeof i.property.parametersForAjax.Keywords!="undefined"?i.property.parametersForAjax.Keywords:[],u=r.filter(function(n){return t===n.Name});return!u.length})}};this.typeahead={init:function(t){var r=n.Filter,i=t.element.tagField;i.typeahead({minLength:3},{name:"tag-search",source:t.property.tagSearchDataSource,limit:8,display:"Name"}).on("typeahead:selected",function(n,u){r.Tags.add(u.Name,u.Value,u.Type);i.val("");i.typeahead("val","");t.hideMessage()});this.afterInit(t)},afterInit:function(n){var t=$(n.selector.tagField).filter(function(){return $(this).hasClass("tt-hint")});t.each(function(){var t=$(this),i=t.next(n.selector.tagField),r;i.length&&(r=[i[0].id,"_hint"].join(""),t.attr("id",r))})}};this.events={init:function(n){this.click(n);this.keydown(n)},click:function(n){$(document).on("click",function(t){var i=$(t.target),r=i.closest("[data-js-tag-remove]"),u=i.closest("[data-js-tag-button]"),f=i.closest("[data-js-clear-filters]");r.length&&(n.remove(r),t.preventDefault());u.length&&(n.hideMessage(),n.element.tagField.val()!==""?$(".tt-menu .tt-selectable",n.element.tagGroup).filter(function(){return this.innerHTML===n.element.tagField.val()}).length==1?$(".tt-suggestion:first-child",n.element.tagGroup).trigger("click"):n.showMessage("Sorry, no tag exists to match your selection."):n.showMessage("Please enter a tag name."));f.length&&($("[data-js-tag-remove]").each(function(){n.remove($(this))}),n.hideMessage(),t.preventDefault())})},keydown:function(n){n.element.tagField.on("keydown",function(t){var i=t.which;i===13?(t.preventDefault(),n.element.tagAddButton.trigger("click")):n.hideMessage()})}};this.remove=function(t){var i=n.Filter,r=t.closest("[data-js-tag-section]");t.closest("[data-js-tag-item]").remove();i.property.filtersInitialised&&i.DoSearch();typeof i.property.parametersForAjax.Keywords!="undefined"&&i.property.parametersForAjax.Keywords.length?i.element.clear.show():(i.element.clear.hide(),r.length>0&&r.hide())};this.add=function(t,i,r){var u=n.Filter,f=typeof t!="undefined"&&typeof i!="undefined"&&typeof r!="undefined";f&&this.element.tagList.append(["",'<div class="e-tag-filter__item" data-js-tag-item>','<a href="#" class="e-tag-filter__item__close" data-js-tag-remove>','<span class="e-tag-filter__item__text" data-tag-value="'+i+'" data-tag-type="'+r+'" data-js-tag-text>'+t+"<\/span>",'<span class="e-tag-filter__item__icon">',Site.SVGMarkup("close"),"<\/span>","<\/a>","<\/div>",""].join(""));u.property.filtersInitialised&&u.DoSearch();u.property.parametersForAjax.Keywords.length?u.element.clear.show():u.element.clear.hide()};this.showMessage=function(n){$(".e-tag-filter__error").removeClass("hidden").html(n)};this.hideMessage=function(){$(".e-tag-filter__error").addClass("hidden").html("")}}