!function(e){"use strict";wp.customize.controlConstructor["bloghash-widget"]=wp.customize.Control.extend({ready:function(){var t=this;t.widget_count=t.container.find(".widget").length,t.setupSortable(),t.container.on("click",".bloghash-widget-container .widget-top",(function(){e(this).closest(".widget").toggleClass("bloghash-expanded").find(".widget-inside").slideToggle(200)})),t.container.on("click",".bloghash-widget-container .widget-control-close",(function(){e(this).closest(".widget").toggleClass("bloghash-expanded").find(".widget-inside").slideToggle(200)})),t.container.on("click",".bloghash-add-widget",(function(e){e.preventDefault(),t.updateList()})),t.container.on("change paste keyup","input, textarea, select",(function(e){t.update()})),t.container.on("click",".widget-control-remove",(function(){e(this).closest(".widget").remove(),t.update(),t.updateList()})),t.container.on("click",".bloghash-widget-edit-nav",(function(){wp.customize.control("nav_menu_locations["+e(this).closest(".bloghash-widget-nav-container").data("menu-location")+"]").focus(),t.close()})),wp.customize.previewer.bind("url",this.close),e(t.container).find(".bloghash-widget-nav-container").each((function(){var i=e(this);t.bindMenuLocation(i)})),t.container.on("click",".widget-media-upload",(function(i){i.preventDefault();const a=e(this).closest(".widget");t.mediaUploader=wp.media({title:"Choose Image",button:{text:"Select"},multiple:!1}),t.mediaUploader.on("select",(function(){var e=t.mediaUploader.state().get("selection").first().toJSON();a.find(".banner-preview").html('Preview'),a.find(".widget-media-upload").prev().val(JSON.stringify(e.id)).trigger("change"),a.find(".widget-media-upload").addClass("hide"),a.find(".remove-image").removeClass("hide")})),t.mediaUploader.open()})),t.container.on("click",".remove-image",(function(t){t.preventDefault();const i=e(this).closest(".widget");i.find(".banner-preview").html(""),i.find(".widget-media-upload").prev().val("").trigger("change"),i.find(".widget-media-upload").removeClass("hide"),i.find(".remove-image").addClass("hide")})),t.container.on("change",'[name^="widget-ad"] [data-option-name="display_area"]',(function(){e(this).trigger("change")}))},bindMenuLocation:function(e){var t=e.data("menu-location");wp.customize("nav_menu_locations["+t+"]",(function(i){i.bind((function(i){if(i){var a=wp.customize.control("nav_menu_locations["+t+"]").container.find("option:selected").html();e.addClass("bloghash-widget-nav-has-menu").find(".bloghash-widget-nav-name").html(a)}else e.removeClass("bloghash-widget-nav-has-menu")}))}))},updateList:function(){var t,i=this,a=i.params.widgets;a?(e("#bloghash-available-widgets-list .bloghash-widget").hide().removeClass("disabled"),e.each(a,(function(a,n){(t=e("#bloghash-available-widgets-list #bloghash-widget-tpl-bloghash_customizer_widget_"+a)).show(),n.hasOwnProperty("max_uses")&&n.max_uses>0&&n.max_uses<=e(i.container).find('.bloghash-widget-container [data-widget-type="'+a+'"]').length&&t.addClass("disabled")}))):e("#bloghash-available-widgets-list .bloghash-widget").show()},addWidget:function(t){var i,a;a=this.setting.id+"-"+this.widget_count,i=(i=e.trim(e(this.container).find(".bloghash-widget-tpl-"+t).html())).replace(/<[^<>]+>/g,(function(e){return e.replace(/__i__|%i%/g,a)}));var n=e(i).appendTo(this.container.find(".bloghash-widget-container"));this.widget_count++,n.find(".widget-top").trigger("click"),this.update(),n.find(".bloghash-widget-nav-container").length&&this.bindMenuLocation(n.find(".bloghash-widget-nav-container"))},close:function(){e("body").removeClass("bloghash-adding-widget")},update:function(){var t,i,a,n,o,d=this.container.find(".bloghash-widget-container .widget"),s=[];d.length?(_.each(d,(function(d){o=e(d),(i={}).classname=o.data("widget-base"),i.type=o.data("widget-type"),i.values={},t=o.find("input, textarea, select"),_.each(t,(function(t){"multiple"!=e(t).data("input-type")&&void 0!==(a=e(t).attr("data-option-name"))&&!1!==a&&(i.values[a]=e(t).val())})),_.each(o.find(".buttonset"),(function(t){void 0!==(n=e(t).find('input[type="radio"]:checked'))&&!1!==n&&(i.values[n.data("option-name")]=n.val())})),_.each(o.find('input[type="checkbox"]'),(function(t){var a=e(t),n=a.data("option-name");if(void 0!==t&&!1!==t)if("multiple"==a.data("input-type"))if(a.is(":checked"))i.values[n]||(i.values[n]=[]),i.values[n].push(a.val());else{const e=i.values[n]?.indexOf(a.val());e>-1&&i.values[n]?.splice(e,1)}else a.is(":checked")&&(i.values[n]=a.val())})),s.push(i)})),this.setting.set(s)):this.setting.set(!1)},setupSortable:function(){var t=this;e(this.container).find(".bloghash-widget-container").sortable({items:"> .widget",handle:".widget-top",intersect:"pointer",axis:"y",update:function(){t.update()}})}}),e(document).ready((function(){var t;e(".wp-full-overlay").on("click",".bloghash-add-widget, .bloghash-close-widgets-panel",(function(i){i.preventDefault(),e("body").toggleClass("bloghash-adding-widget"),e(this).data("location-title")&&(t=wp.customize.control(e(this).data("control")),e("#bloghash-available-widgets").attr("data-control",t.params.id).find(".bloghash-widget-caption").find("h3").html(e(this).data("location-title")))})),e(".wp-full-overlay").on("click",".customize-section-back",(function(t){e("body").removeClass("bloghash-adding-widget"),e("#bloghash-available-widgets").removeAttr("data-control")})),e("#bloghash-available-widgets").on("click",".bloghash-widget",(function(i){t=wp.customize.control(e("#bloghash-available-widgets").attr("data-control"));var a=e(this).data("widget-id");t.addWidget(a);t.close()}))}))}(jQuery);