var wpWidgets;!function(a){wpWidgets={init:function(){var b,c,d=this,e=a("#widgets-chooser"),f=e.find(".widgets-chooser-sidebars"),g=a("div.widgets-sortables"),h=!("undefined"==typeof isRtl||!isRtl),i=h?"marginRight":"marginLeft";a("#widgets-right").children(".widgets-holder-wrap").children(".sidebar-name").click(function(){var b=a(this),c=b.parent();c.hasClass("closed")?(c.removeClass("closed"),b.siblings(".widgets-sortables").sortable("refresh")):c.addClass("closed")}),a("#widgets-left").children(".widgets-holder-wrap").children(".sidebar-name").click(function(){a(this).parent().toggleClass("closed")}),g.each(function(){if(a(this).parent().hasClass("inactive"))return!0;var b=50,c=a(this).children(".widget").length;b+=parseInt(48*c,10),a(this).css("minHeight",b+"px")}),a(document.body).bind("click.widgets-toggle",function(b){var c,d,e,f=a(b.target),g={};f.parents(".widget-top").length&&!f.parents("#available-widgets").length?(c=f.closest("div.widget"),d=c.children(".widget-inside"),e=parseInt(c.find("input.widget-width").val(),10),d.is(":hidden")?(e>250&&d.closest("div.widgets-sortables").length&&(g.width=e+30+"px",d.closest("div.widget-liquid-right").length&&(g[i]=235-e+"px"),c.css(g)),wpWidgets.fixLabels(c),d.slideDown("fast")):d.slideUp("fast",function(){c.css({width:"",margin:""})}),b.preventDefault()):f.hasClass("widget-control-save")?(wpWidgets.save(f.closest("div.widget"),0,1,0),b.preventDefault()):f.hasClass("widget-control-remove")?(wpWidgets.save(f.closest("div.widget"),1,1,0),b.preventDefault()):f.hasClass("widget-control-close")&&(wpWidgets.close(f.closest("div.widget")),b.preventDefault())}),g.children(".widget").each(function(){wpWidgets.appendTitle(this),a("p.widget-error",this).length&&a("a.widget-action",this).click()}),a("#widget-list").children(".widget").draggable({connectToSortable:"div.widgets-sortables",handle:"> .widget-top > .widget-title",distance:2,helper:"clone",zIndex:100,containment:"document",start:function(a,b){b.helper.find("div.widget-description").hide(),c=this.id},stop:function(){b&&a(b).hide(),b=""}}),g.sortable({placeholder:"widget-placeholder",items:"> .widget",handle:"> .widget-top > .widget-title",cursor:"move",distance:2,containment:"document",start:function(a,b){b.item.children(".widget-inside").hide(),b.item.css({margin:"",width:""})},stop:function(d,e){if(e.item.hasClass("ui-draggable")&&e.item.data("draggable")&&e.item.draggable("destroy"),e.item.hasClass("deleting"))return wpWidgets.save(e.item,1,0,1),e.item.remove(),void 0;var f=e.item.find("input.add_new").val(),g=e.item.find("input.multi_number").val(),h=c,i=a(this).attr("id");return e.item.css({margin:"",width:""}),c="",f?("multi"===f?(e.item.html(e.item.html().replace(/<[^<>]+>/g,function(a){return a.replace(/__i__|%i%/g,g)})),e.item.attr("id",h.replace("__i__",g)),g++,a("div#"+h).find("input.multi_number").val(g)):"single"===f&&(e.item.attr("id","new-"+h),b="div#"+h),wpWidgets.save(e.item,0,0,1),e.item.find("input.add_new").val(""),e.item.find("a.widget-action").click(),void 0):(wpWidgets.saveOrder(i),void 0)},receive:function(b,c){var d=a(c.sender);(!a(this).is(":visible")||this.id.indexOf("orphaned_widgets")>-1)&&d.sortable("cancel"),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").html("")},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("h4").html())},out:function(b,c){c.draggable.removeClass("deleting"),a("div.widget-placeholder").show(),a("#removing-widget").hide().children("span").html("")}}),a("#widgets-right .widgets-holder-wrap").each(function(b,c){var d=a(c),e=d.find(".sidebar-name h3").text(),g=d.find(".widgets-sortables").attr("id"),h=a('
  • ').text(a.trim(e));0===b&&h.addClass("widgets-chooser-selected"),f.append(h),h.data("sidebarId",g)}),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")?d.closeChooser():(d.clearWidgetSelection(),a("#widgets-left").addClass("chooser"),b.addClass("widget-in-question"),b.after(e),e.slideDown(200,function(){f.find(".widgets-chooser-selected").focus()}))}),e.on("click.widgets-chooser",function(b){var c=a(b.target);c.hasClass("button-primary")?(d.addWidget(e),d.closeChooser()):c.hasClass("button-secondary")?d.closeChooser():c.is(".widgets-chooser-sidebars li")&&(e.find(".widgets-chooser-selected").removeClass("widgets-chooser-selected"),c.addClass("widgets-chooser-selected"))})},saveOrder:function(b){b&&a("#"+b).closest("div.widgets-holder-wrap").find(".spinner").css("display","inline-block");var c={action:"widgets-order",savewidgets:a("#_wpnonce_widgets").val(),sidebars:[]};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(".spinner").hide()}),this.resize()},save:function(b,c,d,e){var f,g=b.closest("div.widgets-sortables").attr("id"),h=b.find("form").serialize();b=a(b),a(".spinner",b).show(),f={action:"save-widget",savewidgets:a("#_wpnonce_widgets").val(),sidebar:g},c&&(f.delete_widget=1),h+="&"+a.param(f),a.post(ajaxurl,h,function(f){var g;c?(a("input.widget_number",b).val()||(g=a("input.widget-id",b).val(),a("#available-widgets").find("input.widget-id").each(function(){a(this).val()===g&&a(this).closest("div.widget").show()})),d?(e=0,b.slideUp("fast",function(){a(this).remove(),wpWidgets.saveOrder()})):(b.remove(),wpWidgets.resize())):(a(".spinner").hide(),f&&f.length>2&&(a("div.widget-content",b).html(f),wpWidgets.appendTitle(b),wpWidgets.fixLabels(b))),e&&wpWidgets.saveOrder()})},appendTitle:function(b){var c=a('input[id*="-title"]',b).val()||"";c&&(c=": "+c.replace(/<[^<>]+>/g,"").replace(//g,">")),a(b).children(".widget-top").children(".widget-title").children().children(".in-widget-title").html(c)},resize:function(){a("div.widgets-sortables").each(function(){if(a(this).parent().hasClass("inactive"))return!0;var b=50,c=a(this).children(".widget").length;b+=parseInt(48*c,10),a(this).css("minHeight",b+"px")})},fixLabels:function(b){b.children(".widget-inside").find("label").each(function(){var b=a(this).attr("for");b&&b===a("input",this).attr("id")&&a(this).removeAttr("for")})},close:function(a){a.children(".widget-inside").slideUp("fast",function(){a.css({width:"",margin:""})})},addWidget:function(b){var c=a("#available-widgets").find(".widget-in-question").clone(),d=c.attr("id"),e=c.find("input.add_new").val(),f=c.find("input.multi_number").val(),g=b.find(".widgets-chooser-selected").data("sidebarId"),h=a("#"+g);"multi"===e?(c.html(c.html().replace(/<[^<>]+>/g,function(a){return a.replace(/__i__|%i%/g,f)})),c.attr("id",d.replace("__i__",f)),f++,a("#"+d).find("input.multi_number").val(f)):"single"===e&&(c.attr("id","new-"+d),a("#"+d).hide()),h.closest(".widgets-holder-wrap").removeClass("closed"),h.sortable("refresh"),h.find(".sidebar-description").after(c),wpWidgets.save(c,0,0,1),c.find("input.add_new").val(""),a("html, body").animate({scrollTop:h.offset().top-130},200),window.setTimeout(function(){c.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("#available-widgets").find(".widget-in-question").removeClass("widget-in-question")}},a(document).ready(function(){wpWidgets.init()})}(jQuery);