/*=======================================================================*\
|| ###################################################################### ||
|| # vBulletin 5.4.2
|| # ------------------------------------------------------------------ # ||
|| # Copyright 2000-2018 vBulletin Solutions Inc. All Rights Reserved.  # ||
|| # This file may not be redistributed in whole or significant part.   # ||
|| # ----------------- VBULLETIN IS NOT FREE SOFTWARE ----------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html   # ||
|| ###################################################################### ||
\*========================================================================*/
window.vBulletin=window.vBulletin||{};window.vBulletin.phrase=window.vBulletin.phrase||{};window.vBulletin.phrase.precache=window.vBulletin.phrase.precache||[];window.vBulletin.phrase.precache=$.merge(window.vBulletin.phrase.precache,["add_tags","error","error_adding_search_tips_code_x","error_adding_tags_code_x","error_fetching_popular_tags","invalid_server_response_please_try_again","invalid_x_tag_length","loading","popular_tags_cloud","tag_cloud","you_may_add_one_more_tag","you_may_add_x_more_tags","you_may_not_add_any_more_tags"]);window.vBulletin.options=window.vBulletin.options||{};window.vBulletin.options.precache=window.vBulletin.options.precache||[];window.vBulletin.options.precache=$.merge(window.vBulletin.options.precache,["tagforcelower","tagmaxlen","tagminlen"]);(function(B){var A={};vBulletin.tagEditor=vBulletin.tagEditor||{};vBulletin.tagEditor.instance=function(M,T,e){var V=this,Q,X=false,b=false,a=window.vBulletin.options.get("tagmaxlen")||25,H=window.vBulletin.options.get("tagminlen")||3,D=0,L=0,O=false,h=B(M),I=h.closest(".tag-editor-container"),G=h.closest(".tag-editor-wrapper"),N=I.data("node-id")||0,d=false,K=h.attr("placeholder")?h.attr("placeholder"):"",J=K?K.length:10,g=function(){if(h.length==0){return false}h.attr("maxlength",a);d=new vBulletin_Autocomplete(h,{apiClass:"Tags",containerClass:(e||"")+" entry-field h-clearfix",placeholderText:K,editorContext:{type:"tags",context:V},minLength:H,beforeAdd:function(j,k){X=b?true:false;if(i()==0){return false}return true},afterAdd:function(j,k){var l=c();if(l==0){h.hide()}h.attr("placeholder","");f(I.find(".tagcloudlink"),I)},afterDelete:function(k,l){X=b?true:false;var m=c();if(T||m>0){h.show();setTimeout(function(){h[0].focus()},10)}h.attr("placeholder",function(){var n=C();return(!n||n.length==0)?K:""});if(h.attr("placeholder")){h.placeholder()}var j=I.find(".tag-cloud .tagcloudlink");if(j.length==0){j=B(".ui-tooltip-popular-tags{0} .tag-cloud .tagcloudlink".format(G.data("tag-editor-class")?"."+G.data("tag-editor-class"):""))}j.each(function(){if(B(this).data("tag")==k){B(this).removeClass("added")}})}});h=d.getInputField();if(h.attr("placeholder")){h.placeholder()}G.data("autocomplete-instance",d)},W=function(j){if(window.vBulletin.options.get("tagforcelower")==1){j=j.toLowerCase()}if(j.length>a){j=j.substr(0,a)}return j},Y=function(m,k){if(!d){return false}if(typeof k=="undefined"||k<1){k=pageData.userid}var j=d.getMinLength();if(m.length<j){vBulletin.warning("error",["invalid_x_tag_length",j]);return false}m=W(m);var l=d.addElement(m,k);h.val("");c();return l},P=function(l){var j=l.length,k;for(k=0;k<j;++k){Y(l[k])}},Z=function(j){if(!d){return false}d.clearElements();P(j)},S=function(){h.attr("size",J);var j=JShtmlEncode(B.trim(h.val()));if(j==""||(j==K&&K)){return false}var k="Adding tag: "+j;if(i()>0){setTimeout(function(){Y(j)},10);k+=" (Tag added)"}console.log(k)},i=function(){if(T||(D==0&&L==0)){return true}var n=d.countElements(),j=B(d.getElements()).filter(function(o,p){return(pageData.userid==0||p.value==pageData.userid)}).size(),m=D>0?D-n:L-n,l=L==0?m:L-j,k=Math.min(l,m);console.log(["tagCount: "+n,"userTagCount: "+j,"maxTags: "+D,"maxUserTags: "+L,"totalCanAdd: "+m,"userCanAdd: "+l,"canAdd: "+k]);return k},c=function(){var k=I.find(".you-may-add-x-tags"),l=i(),j;if(l===true){return true}else{if(l>1){j=["you_may_add_x_more_tags",l]}else{if(l==1){j="you_may_add_one_more_tag"}else{j="you_may_not_add_any_more_tags";l=0}}}k.toggleClass("warning",(l==0)).html(vBulletin.phrase.get(j));return l},U=function(){d.clearElements()},C=function(){if(!d){return false}return d.getLabels()},F=function(j){if(T){h[0].focus();return }else{if(!A[j]){A[j]={}}else{D=A[j].maxTags;L=A[j].maxUserTags;O=A[j].canManageTags;R();return }}vBulletin.AJAX({call:"/ajax/api/tags/getNodeTags",data:{nodeid:j},success:function(k){if(B.isArray(k.tags)){D=parseInt(k.maxtags,10);L=parseInt(k.maxusertags,10);O=k.canmanagetags;A[j]={maxTags:D,maxUserTags:L,canManageTags:O};R()}},api_error:B.noop,error:B.noop,})};this.addTag=function(k,j){Y(k,j)};this.addTags=function(j){P(j)};this.setTags=function(j){Z(j)};this.removeAllTags=function(){U()};this.getTags=function(){return C()};this.getTagEditor=function(){return h};if(G.data("initialized")){console.log("Tag Editor is already initialized.");return }else{G.attr("data-initialized",true)}g();h.off("keydown").on("keydown",function(k){var j=B(k.target).val().length;B(k.target).attr("size",(j>J?j:J));if(k.which==13){X=b?true:false;S();k.preventDefault();return false}});h.off("blur").on("blur",function(j){b=window.setTimeout(function(){if(!X){S()}X=false;b=false},0)});h.off("click").on("click",function(j){if(!B(j.target).hasClass("tag-input")){B(".tag-input",B(this)).focus();X=b?true:false}return false});h.each(function(){var j=B(this);j.closest("form").submit(function(){S()})});var R=function(){var k=I.find(".you-may-add-text");if(!D&&!L){k.addClass("h-hide")}else{var j;if(D&&!L){j=D}else{if(!D&&L){j=L}else{j=Math.min(D,L)}}k.find("span").text(j)}if(!i()){h.hide()}else{if(h.is(":visible")){h[0].focus()}}};B(".tag-cloud-text.inline-tag-cloud .popular-tags-link",I).off("click").on("click",function(){var j=I.find(".tag-cloud");vBulletin.tagEditor.fetchPopularTags({type:"nodes",noformat:1,success:function(k){j.html(k);f(j.find(".tagcloudlink"),I)},error:function(k){j.html('<span class="error">{0}</span>'.format(k))}})});B(".tag-cloud-text:not(.inline-tag-cloud) .popular-tags-link",I).qtip({content:{text:vBulletin.phrase.get("loading")+"...",ajax:{url:vBulletin.getAjaxBaseurl()+"/search/fetchTagCloud",data:({type:"nodes",noformat:1}),type:"POST",dataType:"json",success:function(j,m,l){console.log("/search/fetchTagCloud successful, response: "+JSON.stringify(j));if(j){if(j.errors){this.set("content.text",'<span class="error">{0} {1}</span>'.format(vBulletin.phrase.get("error_fetching_popular_tags"),vBulletin.phrase.get(j.errors[0][0])))}else{this.set("content.text",'<div class="tag-cloud">{0}</div>'.format(j));B(".ui-tooltip-popular-tags").delegate(".tag-cloud .tagcloudlink label","click",E);var k=B(".ui-tooltip-popular-tags{0} .tag-cloud .tagcloudlink".format(G.data("tag-editor-id")?"."+G.data("tag-editor-id"):""));f(k,G)}}else{this.set("content.text",'<span class="error">{0}</span>'.format(vBulletin.phrase.get("error_fetching_popular_tags")))}},error:function(l,k,j){console.log("/search/fetchTagCloud failed, error: "+j);this.set("content.text",'<span class="error">{0}</span>'.format(vBulletin.phrase.get("error_fetching_popular_tags")))}},title:{text:vBulletin.phrase.get("tag_cloud"),button:false}},position:{at:"bottom center",my:"top center",viewport:B(window),effect:false},show:{event:"click",solo:true},hide:"unfocus",style:{classes:"ui-tooltip-shadow ui-tooltip-light ui-tooltip-rounded ui-tooltip-popular-tags "+(G.data("tag-editor-class")||""),width:280},events:{show:function(l,k){var j=B(".ui-tooltip-popular-tags{0} .tag-cloud .tagcloudlink".format(G.data("tag-editor-class")?"."+G.data("tag-editor-class"):""));f(j,G)}}});var E=function(k){var j=B(this);if(!j.parent().hasClass("added")&&Y(j.text())){j.parent().addClass("added");if(h.is(":visible")){h[0].focus()}}return false};var f=function(j,k){if(j.length>0){var l=(k.find(".tag-input").val()||"").split(",");B.each(l,function(m,n){var o=B.trim(n);j.each(function(){if(B(this).data("tag")==o){B(this).addClass("added")}})})}};I.delegate(".tag-cloud .tagcloudlink label","click",E);G.delegate(".add-tag-link","click",function(k){k.stopPropagation();var j=B(this);var l=G.find(".tag-editor-container");if(l.length==1){l.dialog({title:vBulletin.phrase.get("add_tags"),autoOpen:false,modal:true,resizable:false,closeOnEscape:false,showCloseButton:false,width:500,dialogClass:"dialog-container tag-editor-dialog-container dialog-box",open:function(){U();var m=G.find(".tag-input").val();if(m){var p=m.split(",");for(var o in p){Y(B.trim(p[o]))}}else{if(K&&!h.attr("placeholder")){h.attr("placeholder",K)}}var q=c();if(q>0){h.show()}F(j.data("node-id"));var n=B(".tag-cloud",l);vBulletin.tagEditor.fetchPopularTags({type:G.data("type")||"nodes",noformat:1,success:function(r){n.html(r);f(n.find(".tagcloudlink"),G)},error:function(r){n.html('<span class="error">{0}</span>'.format(r))}})},close:function(){var m=G.data("autocomplete-instance");m.close()}}).dialog("open");j.data("tag-dialog",l);l.data("parent",j.closest("form, .search-controls-tags, .form_row"))}else{l=j.data("tag-dialog")||B();l.dialog("open")}return false})};vBulletin.tagEditor.fetchPopularTags=function(E){E=E||{};E.type=E.type!="search"?"nodes":E.type;var D=(typeof E.error=="function")?E.error:B.noop;var C=function(){D(vBulletin.phrase.get("error_fetching_popular_tags"))};vBulletin.AJAX({call:"/search/fetchTagCloud",data:({type:E.type,noformat:E.noformat}),success:function(F,H,G){if(typeof E.success=="function"){E.success(F,H,G)}},api_error:function(F){D("{0} {1}".format(vBulletin.phrase.get("error_fetching_popular_tags"),vBulletin.phrase.get(F[0])))},emptyResponse:C,error:C,})};B(document).ready(function(){B(document).off("click",".tag-editor-container .cancel-tag-btn").on("click",".tag-editor-container .cancel-tag-btn",function(){B(".tag-editor-container").dialog("close")});B(document).off("click",".tag-editor-container .save-tag-btn").on("click",".tag-editor-container .save-tag-btn",function(){var F=B(this).closest(".tag-editor-container"),E=F.find(".tag-input").val(),C=E.replace(/\,/g,", "),D=F.data("parent");D.find(".tag-list span").html(C);D.find(".tag-input").val(E);D.find(".tag-list").toggleClass("h-hide",!E);F.dialog("close")});B(document).off("mousedown",".tag-editor-container .autocomplete-container").on("mousedown",".tag-editor-container .autocomplete-container",function(){var C=this;setTimeout(function(){var D=B(".autocompleteHelper:visible",C);if(D.length>0){D[0].focus()}},10);return false});if(B(".js-content-entry-tag-editor").length>0&&vBulletin.tagEditor){new vBulletin.tagEditor.instance(".js-content-entry-tag-editor")}})})(jQuery);