var wpWidgets;!function(a){var b=a(document);wpWidgets={hoveredSidebar:null,init:function(){var c,d,e=this,f=a(".widgets-chooser"),g=f.find(".widgets-chooser-sidebars"),h=a("div.widgets-sortables"),i=!("undefined"==typeof isRtl||!isRtl);a("#widgets-right .sidebar-name").click(function(){var c=a(this),d=c.closest(".widgets-holder-wrap");d.hasClass("closed")?(d.removeClass("closed"),c.parent().sortable("refresh")):d.addClass("closed"),b.triggerHandler("wp-pin-menu")}),a("#widgets-left .sidebar-name").click(function(){a(this).closest(".widgets-holder-wrap").toggleClass("closed"),b.triggerHandler("wp-pin-menu")}),a(document.body).bind("click.widgets-toggle",function(b){var c,d,e,f,g,h=a(b.target),j={"z-index":100},k=h.closest(".widget").find(".widget-top button.widget-action");h.parents(".widget-top").length&&!h.parents("#available-widgets").length?(c=h.closest("div.widget"),d=c.children(".widget-inside"),e=parseInt(c.find("input.widget-width").val(),10),f=c.parent().width(),d.is(":hidden")?(e>250&&e+30>f&&c.closest("div.widgets-sortables").length&&(g=c.closest("div.widget-liquid-right").length?i?"margin-right":"margin-left":i?"margin-left":"margin-right",j[g]=f-(e+30)+"px",c.css(j)),k.attr("aria-expanded","true"),d.slideDown("fast",function(){c.addClass("open")})):(k.attr("aria-expanded","false"),d.slideUp("fast",function(){c.attr("style",""),c.removeClass("open")})),b.preventDefault()):h.hasClass("widget-control-save")?(wpWidgets.save(h.closest("div.widget"),0,1,0),b.preventDefault()):h.hasClass("widget-control-remove")?(wpWidgets.save(h.closest("div.widget"),1,1,0),b.preventDefault()):h.hasClass("widget-control-close")?(c=h.closest("div.widget"),c.removeClass("open"),k.attr("aria-expanded","false"),wpWidgets.close(c),b.preventDefault()):"inactive-widgets-control-remove"===h.attr("id")&&(wpWidgets.removeInactiveWidgets(),b.preventDefault())}),h.children(".widget").each(function(){var b=a(this);wpWidgets.appendTitle(this),b.find("p.widget-error").length&&b.find(".widget-action").trigger("click").attr("aria-expanded","true")}),a("#widget-list").children(".widget").draggable({connectToSortable:"div.widgets-sortables",handle:"> .widget-top > .widget-title",distance:2,helper:"clone",zIndex:100,containment:"#wpwrap",refreshPositions:!0,start:function(b,c){var f=a(this).find(".widgets-chooser");c.helper.find("div.widget-description").hide(),d=this.id,f.length&&(a("#wpbody-content").append(f.hide()),c.helper.find(".widgets-chooser").remove(),e.clearWidgetSelection())},stop:function(){c&&a(c).hide(),c=""}}),h.droppable({tolerance:"intersect",over:function(b){var c=a(b.target).parent();wpWidgets.hoveredSidebar&&!c.is(wpWidgets.hoveredSidebar)&&wpWidgets.closeSidebar(b),c.hasClass("closed")&&(wpWidgets.hoveredSidebar=c,c.removeClass("closed")),a(this).sortable("refresh")},out:function(a){wpWidgets.hoveredSidebar&&wpWidgets.closeSidebar(a)}}),h.sortable({placeholder:"widget-placeholder",items:"> .widget",handle:"> .widget-top > .widget-title",cursor:"move",distance:2,containment:"#wpwrap",tolerance:"pointer",refreshPositions:!0,start:function(b,c){var d,e=a(this),f=e.parent(),g=c.item.children(".widget-inside");"block"===g.css("display")&&(c.item.removeClass("open"),c.item.find(".widget-top button.widget-action").attr("aria-expanded","false"),g.hide(),a(this).sortable("refreshPositions")),f.hasClass("closed")||(d=c.item.hasClass("ui-draggable")?e.height():1+e.height(),e.css("min-height",d+"px"))},stop:function(e,f){var g,h,i,j,k,l,m=f.item,n=d;return wpWidgets.hoveredSidebar=null,m.hasClass("deleting")?(wpWidgets.save(m,1,0,1),void m.remove()):(g=m.find("input.add_new").val(),h=m.find("input.multi_number").val(),m.attr("style","").removeClass("ui-draggable"),d="",g&&("multi"===g?(m.html(m.html().replace(/<[^<>]+>/g,function(a){return a.replace(/__i__|%i%/g,h)})),m.attr("id",n.replace("__i__",h)),h++,a("div#"+n).find("input.multi_number").val(h)):"single"===g&&(m.attr("id","new-"+n),c="div#"+n),wpWidgets.save(m,0,0,1),m.find("input.add_new").val(""),b.trigger("widget-added",[m])),i=m.parent(),i.parent().hasClass("closed")&&(i.parent().removeClass("closed"),j=i.children(".widget"),j.length>1&&(k=j.get(0),l=m.get(0),k.id&&l.id&&k.id!==l.id&&a(k).before(m))),void(g?m.find(".widget-action").trigger("click"):wpWidgets.saveOrder(i.attr("id"))))},activate:function(){a(this).parent().addClass("widget-hover")},deactivate:function(){a(this).css("min-height","").parent().removeClass("widget-hover")},receive:function(b,c){var d=a(c.sender);return this.id.indexOf("orphaned_widgets")>-1?void d.sortable("cancel"):void(d.attr("id").indexOf("orphaned_widgets")>-1&&!d.children(".widget").length&&d.parents(".orphan-sidebar").slideUp(400,function(){a(this).remove()}))}}).sortable("option","connectWith","div.widgets-sortables"),a("#available-widgets").droppable({tolerance:"pointer",accept:function(b){return"widget-list"!==a(b).parent().attr("id")},drop:function(b,c){c.draggable.addClass("deleting"),a("#removing-widget").hide().children("span").empty()},over:function(b,c){c.draggable.addClass("deleting"),a("div.widget-placeholder").hide(),c.draggable.hasClass("ui-sortable-helper")&&a("#removing-widget").show().children("span").html(c.draggable.find("div.widget-title").children("h3").html())},out:function(b,c){c.draggable.removeClass("deleting"),a("div.widget-placeholder").show(),a("#removing-widget").hide().children("span").empty()}}),a("#widgets-right .widgets-holder-wrap").each(function(b,c){var d=a(c),e=d.find(".sidebar-name h2").text(),f=d.find(".widgets-sortables").attr("id"),h=a('<li tabindex="0">').text(a.trim(e));0===b&&h.addClass("widgets-chooser-selected"),g.append(h),h.data("sidebarId",f)}),a("#available-widgets .widget .widget-title").on("click.widgets-chooser",function(){var b=a(this).closest(".widget");b.hasClass("widget-in-question")||a("#widgets-left").hasClass("chooser")?e.closeChooser():(e.clearWidgetSelection(),a("#widgets-left").addClass("chooser"),b.addClass("widget-in-question").children(".widget-description").after(f),f.slideDown(300,function(){g.find(".widgets-chooser-selected").focus()}),g.find("li").on("focusin.widgets-chooser",function(){g.find(".widgets-chooser-selected").removeClass("widgets-chooser-selected"),a(this).addClass("widgets-chooser-selected")}))}),f.on("click.widgets-chooser",function(b){var c=a(b.target);c.hasClass("button-primary")?(e.addWidget(f),e.closeChooser()):c.hasClass("widgets-chooser-cancel")&&e.closeChooser()}).on("keyup.widgets-chooser",function(b){b.which===a.ui.keyCode.ENTER?a(b.target).hasClass("widgets-chooser-cancel")?e.closeChooser():(e.addWidget(f),e.closeChooser()):b.which===a.ui.keyCode.ESCAPE&&e.closeChooser()})},saveOrder:function(b){var c={action:"widgets-order",savewidgets:a("#_wpnonce_widgets").val(),sidebars:[]};b&&a("#"+b).find(".spinner:first").addClass("is-active"),a("div.widgets-sortables").each(function(){a(this).sortable&&(c["sidebars["+a(this).attr("id")+"]"]=a(this).sortable("toArray").join(","))}),a.post(ajaxurl,c,function(){a("#inactive-widgets-control-remove").prop("disabled",!a("#wp_inactive_widgets .widget").length),a(".spinner").removeClass("is-active")})},save:function(c,d,e,f){var g,h=c.closest("div.widgets-sortables").attr("id"),i=c.find("form").serialize();c=a(c),a(".spinner",c).addClass("is-active"),g={action:"save-widget",savewidgets:a("#_wpnonce_widgets").val(),sidebar:h},d&&(g.delete_widget=1),i+="&"+a.param(g),a.post(ajaxurl,i,function(g){var i;d?(a("input.widget_number",c).val()||(i=a("input.widget-id",c).val(),a("#available-widgets").find("input.widget-id").each(function(){a(this).val()===i&&a(this).closest("div.widget").show()})),e?(f=0,c.slideUp("fast",function(){a(this).remove(),wpWidgets.saveOrder()})):(c.remove(),"wp_inactive_widgets"===h&&a("#inactive-widgets-control-remove").prop("disabled",!a("#wp_inactive_widgets .widget").length))):(a(".spinner").removeClass("is-active"),g&&g.length>2&&(a("div.widget-content",c).html(g),wpWidgets.appendTitle(c),b.trigger("widget-updated",[c]),"wp_inactive_widgets"===h&&a("#inactive-widgets-control-remove").prop("disabled",!a("#wp_inactive_widgets .widget").length))),f&&wpWidgets.saveOrder()})},removeInactiveWidgets:function(){var b,c,d=a(".remove-inactive-widgets");a(".spinner",d).addClass("is-active"),b={action:"delete-inactive-widgets",removeinactivewidgets:a("#_wpnonce_remove_inactive_widgets").val()},c=a.param(b),a.post(ajaxurl,c,function(){a("#wp_inactive_widgets .widget").remove(),a("#inactive-widgets-control-remove").prop("disabled",!0),a(".spinner",d).removeClass("is-active")})},appendTitle:function(b){var c=a('input[id*="-title"]',b).val()||"";c&&(c=": "+c.replace(/<[^<>]+>/g,"").replace(/</g,"&lt;").replace(/>/g,"&gt;")),a(b).children(".widget-top").children(".widget-title").children().children(".in-widget-title").html(c)},close:function(a){a.children(".widget-inside").slideUp("fast",function(){a.attr("style","").find(".widget-top button.widget-action").attr("aria-expanded","false").focus()})},addWidget:function(c){var d,e,f,g,h,i,j,k=c.find(".widgets-chooser-selected").data("sidebarId"),l=a("#"+k);d=a("#available-widgets").find(".widget-in-question").clone(),e=d.attr("id"),f=d.find("input.add_new").val(),g=d.find("input.multi_number").val(),d.find(".widgets-chooser").remove(),"multi"===f?(d.html(d.html().replace(/<[^<>]+>/g,function(a){return a.replace(/__i__|%i%/g,g)})),d.attr("id",e.replace("__i__",g)),g++,a("#"+e).find("input.multi_number").val(g)):"single"===f&&(d.attr("id","new-"+e),a("#"+e).hide()),l.closest(".widgets-holder-wrap").removeClass("closed"),l.append(d),l.sortable("refresh"),wpWidgets.save(d,0,0,1),d.find("input.add_new").val(""),b.trigger("widget-added",[d]),h=a(window).scrollTop(),i=h+a(window).height(),j=l.offset(),j.bottom=j.top+l.outerHeight(),(h>j.bottom||i<j.top)&&a("html, body").animate({scrollTop:j.top-130},200),window.setTimeout(function(){d.find(".widget-title").trigger("click")},250)},closeChooser:function(){var b=this;a(".widgets-chooser").slideUp(200,function(){a("#wpbody-content").append(this),b.clearWidgetSelection()})},clearWidgetSelection:function(){a("#widgets-left").removeClass("chooser"),a(".widget-in-question").removeClass("widget-in-question")},closeSidebar:function(b){this.hoveredSidebar.addClass("closed"),a(b.target).css("min-height",""),this.hoveredSidebar=null}},b.ready(function(){wpWidgets.init()})}(jQuery);