/*
 * XenForo title_prefix.min.js
 * Copyright 2010-2013 XenForo Ltd.
 * Released under the XenForo License Agreement: http://xenforo.com/license-agreement
 */
(function(b,r,s){XenForo.TitlePrefix=function(e){function p(a){var d,c=e.val();e.find("option, optgroup").not("[value=0]").empty().remove();b.each(a,function(a,c){d=a!=0?b("<optgroup />").attr("label",c.title).appendTo(e):e;b.each(c.prefixes,function(c,a){b("<option />").attr("value",c).data("css",a.css).text(a.title).appendTo(d)})});q(i);var f=j;setTimeout(function(){var a=e.find("option[value="+f+"]");a.length<1&&(a=e.find("option[value=0]"));k(a.data("link"),!0)},0);e.val(c).trigger("change")}
function l(){if(d.length){var a=g.innerWidth()-10;d.siblings().not(d).each(function(){a-=b(this).outerWidth(!0)});a<130?(d.css("width","100%"),g.addClass("blockInput")):(d.css("width",a),g.removeClass("blockInput"))}}function k(a,b){if(d.length){var c=a.data("option"),f;a.closest("ul.PrefixMenu").find("li.PrefixOption, li.PrefixGroup").removeClass("selected");c instanceof jQuery&&(c.val()!=0&&a.closest("li.PrefixOption").addClass("selected"),f=a.closest("li.PrefixGroup"),f.length&&f.find("li.PrefixOption").not(".selected").length==
0&&f.addClass("selected"),h.data("css")&&h.removeClass(h.data("css")),h.addClass(c.data("css")).data("css",c.data("css")).find("span.prefixText").text(c.text()),j=c.val(),console.info("set prefix %s",j),e.val(j).trigger("change"));l();e.trigger({type:"XFSetPrefix",link:a});b||d.get(0).select()}}function m(a,e){var c=b(a),f=b('<a href="javascript:" />').data("option",c).text(c.text()).addClass(c.data("css")).click(function(){k(f)});e.append(b("<li />").addClass("PrefixOption").append(f));c.data("link",
f);a.selected&&setTimeout(function(){k(f,!0)},0)}function t(){if(d.length)return i=b('<ul class="Menu PrefixMenu secondaryContent" />'),q(i),b('<div class="Popup PrefixPopup"></div>').append(h).append(i)}function q(a){d.length&&(a.empty(),e.children("optgroup").each(function(e,c){var f=b(c),d,g;d=b("<li />").addClass("PrefixGroup").appendTo(a);b("<h3 />").text(f.attr("label")).appendTo(d);g=b("<ul />").appendTo(d);f.children("option").each(function(a,c){m(c,g)})}),e.children("option:not([value=0])").each(function(b,
c){m(c,a)}),e.children("option[value=0]").each(function(b,c){m(c,a)}))}var g,u=b(e.data("container")),d=b(e.data("textbox")),h=b('<span rel="Menu"><span class="prefixText"></span></span>').addClass("prefix noPrefix").data("css","prefix noPrefix"),i=null,n=b(e.data("nodecontrol")),j=0,o={};n.length&&e.data("prefixurl")&&n.change(function(){var a=n.val(),b=e.data("prefixurl"),c=null;o[a]?p(o[a]):b&&(c&&c.abort(),c=XenForo.ajax(b,{node_id:a},function(b){c=null;if(XenForo.hasResponseError(b))return!1;
if(b.prefixGroups)o[a]=b.prefixGroups,p(b.prefixGroups)}))});d.length&&(u.hide(),g=b("<div />").addClass("textCtrlWrap").addClass(d.attr("class")).insertBefore(d).append(d),g.prepend(t()),d.bind({focus:function(){g.addClass("Focus")},blur:function(){g.removeClass("Focus")}}),b(s).bind("XenForoActivationComplete OverlayOpened TitlePrefixRecalc",l),b(r).on("resize",l))};XenForo.register("select.TitlePrefix","XenForo.TitlePrefix")})(jQuery,this,document);
