!function(a,b,c){"use strict";function d(a){return"nav_menu_item["+a+"]"}wpNavMenu.originalInit=wpNavMenu.init,wpNavMenu.options.menuItemDepthPerLevel=20,wpNavMenu.options.sortableItems=".customize-control-nav_menu_item",wpNavMenu.init=function(){this.jQueryExtensions()},a.Menus=a.Menus||{},a.Menus.data={nonce:"",itemTypes:{taxonomies:{},postTypes:{}},l10n:{},menuItemTransport:"postMessage",phpIntMax:0,defaultSettingValues:{nav_menu:{},nav_menu_item:{}}},"undefined"!=typeof _wpCustomizeNavMenusSettings&&c.extend(a.Menus.data,_wpCustomizeNavMenusSettings),a.Menus.generatePlaceholderAutoIncrementId=function(){return-Math.ceil(a.Menus.data.phpIntMax*Math.random())},a.Menus.AvailableItemModel=Backbone.Model.extend(c.extend({id:null},a.Menus.data.defaultSettingValues.nav_menu_item)),a.Menus.AvailableItemCollection=Backbone.Collection.extend({model:a.Menus.AvailableItemModel,sort_key:"order",comparator:function(a){return-a.get(this.sort_key)},sortByField:function(a){this.sort_key=a,this.sort()}}),a.Menus.availableMenuItems=new a.Menus.AvailableItemCollection(a.Menus.data.availableMenuItems),a.Menus.AvailableMenuItemsPanelView=b.Backbone.View.extend({el:"#available-menu-items",events:{"input #menu-items-search":"debounceSearch","keyup #menu-items-search":"debounceSearch","click #menu-items-search":"debounceSearch","focus .menu-item-tpl":"focus","click .menu-item-tpl":"_submit","click #custom-menu-item-submit":"_submitLink","keypress #custom-menu-item-name":"_submitLink",keydown:"keyboardAccessible"},selected:null,currentMenuControl:null,debounceSearch:null,$search:null,searchTerm:"",rendered:!1,pages:{},sectionContent:"",loading:!1,initialize:function(){var b=this;this.$search=c("#menu-items-search"),this.sectionContent=this.$el.find(".accordion-section-content"),this.debounceSearch=_.debounce(b.search,500),_.bindAll(this,"close"),c("#customize-controls, .customize-section-back").on("click keydown",function(a){var d=c(a.target).is(".item-delete, .item-delete *"),e=c(a.target).is(".add-new-menu-item, .add-new-menu-item *");!c("body").hasClass("adding-menu-items")||d||e||b.close()}),this.$el.on("input","#custom-menu-item-name.invalid, #custom-menu-item-url.invalid",function(){c(this).removeClass("invalid")}),a.panel("nav_menus").container.bind("expanded",function(){b.rendered||(b.initList(),b.rendered=!0)}),this.sectionContent.scroll(function(){var a=b.$el.find(".accordion-section.open .accordion-section-content").prop("scrollHeight"),d=b.$el.find(".accordion-section.open").height();if(!b.loading&&c(this).scrollTop()>.75*a-d){var e=c(this).data("type"),f=c(this).data("obj_type");"search"===e?b.searchTerm&&b.doSearch(b.pages.search):b.loadItems(e,f)}}),a.previewer.bind("url",this.close)},search:function(a){a&&(this.searchTerm&&!c("#available-menu-items-search").hasClass("open")&&(c("#available-menu-items .accordion-section-content").slideUp("fast"),c("#available-menu-items-search .accordion-section-content").slideDown("fast"),c("#available-menu-items .accordion-section.open").removeClass("open"),c("#available-menu-items-search").addClass("open")),""===a.target.value&&c("#available-menu-items-search").removeClass("open"),this.searchTerm!==a.target.value&&(this.searchTerm=a.target.value,this.pages.search=1,this.doSearch(1)))},doSearch:function(d){var e,f=this,g=c("#available-menu-items-search"),h=g.find(".accordion-section-content"),i=b.template("available-menu-item");if(f.currentRequest&&f.currentRequest.abort(),!(0>d)){if(d>1)g.addClass("loading-more"),h.attr("aria-busy","true"),b.a11y.speak(a.Menus.data.l10n.itemsLoadingMore);else if(""===f.searchTerm)return h.html(""),void b.a11y.speak("");g.addClass("loading"),f.loading=!0,e={"customize-menus-nonce":a.Menus.data.nonce,wp_customize:"on",search:f.searchTerm,page:d},f.currentRequest=b.ajax.post("search-available-menu-items-customizer",e),f.currentRequest.done(function(c){var e;1===d&&h.empty(),g.removeClass("loading loading-more"),h.attr("aria-busy","false"),g.addClass("open"),f.loading=!1,e=new a.Menus.AvailableItemCollection(c.items),f.collection.add(e.models),e.each(function(a){h.append(i(a.attributes))}),f.pages.search=20>e.length?-1:f.pages.search+1,e&&d>1?b.a11y.speak(a.Menus.data.l10n.itemsFoundMore.replace("%d",e.length)):e&&1===d&&b.a11y.speak(a.Menus.data.l10n.itemsFound.replace("%d",e.length))}),f.currentRequest.fail(function(a){a.message&&(h.empty().append(c('
').text(a.message)),b.a11y.speak(a.message)),f.pages.search=-1}),f.currentRequest.always(function(){g.removeClass("loading loading-more"),h.attr("aria-busy","false"),f.loading=!1,f.currentRequest=null})}},initList:function(){var b=this;_.each(a.Menus.data.itemTypes,function(a,c){_.each(a,function(a,d){"postTypes"===c?c="post_type":"taxonomies"===c&&(c="taxonomy"),b.pages[d]=0,b.loadItems(d,c)})})},loadItems:function(d,e){var f,g,h,i=this;h=b.template("available-menu-item"),0>i.pages[d]||(c("#available-menu-items-"+d+" .accordion-section-title").addClass("loading"),i.loading=!0,f={"customize-menus-nonce":a.Menus.data.nonce,wp_customize:"on",type:d,obj_type:e,page:i.pages[d]},g=b.ajax.post("load-available-menu-items-customizer",f),g.done(function(b){var e,f;return e=b.items,0===e.length?void(i.pages[d]=-1):(e=new a.Menus.AvailableItemCollection(e),i.collection.add(e.models),f=c("#available-menu-items-"+d+" .accordion-section-content"),e.each(function(a){f.append(h(a.attributes))}),void(i.pages[d]=i.pages[d]+1))}),g.fail(function(a){"undefined"!=typeof console&&console.error&&console.error(a)}),g.always(function(){c("#available-menu-items-"+d+" .accordion-section-title").removeClass("loading"),i.loading=!1}))},itemSectionHeight:function(){var a,b,c,d;b=window.innerHeight,a=this.$el.find(".accordion-section-content"),c=46*(1+a.length)-16,d=b-c,d>120&&290>d?a.css("max-height",d):120>=d&&this.$el.addClass("allow-scroll")},select:function(a){this.selected=c(a),this.selected.siblings(".menu-item-tpl").removeClass("selected"),this.selected.addClass("selected")},focus:function(a){this.select(c(a.currentTarget))},_submit:function(a){("keypress"!==a.type||13===a.which||32===a.which)&&this.submit(c(a.currentTarget))},submit:function(a){var b,d;a||(a=this.selected),a&&this.currentMenuControl&&(this.select(a),b=c(this.selected).data("menu-item-id"),d=this.collection.findWhere({id:b}),d&&(this.currentMenuControl.addItemToMenu(d.attributes),c(a).find(".menu-item-handle").addClass("item-added")))},_submitLink:function(a){("keypress"!==a.type||13===a.which)&&this.submitLink()},submitLink:function(){var b,d=c("#custom-menu-item-name"),e=c("#custom-menu-item-url");if(this.currentMenuControl){if(""===d.val())return void d.addClass("invalid");if(""===e.val()||"http://"===e.val())return void e.addClass("invalid");b={title:d.val(),url:e.val(),type:"custom",type_label:a.Menus.data.l10n.custom_label,object:""},this.currentMenuControl.addItemToMenu(b),e.val("http://"),d.val("")}},open:function(a){this.currentMenuControl=a,this.itemSectionHeight(),c("body").addClass("adding-menu-items"),_(this.currentMenuControl.getMenuItemControls()).each(function(a){a.collapseForm()}),this.$el.find(".selected").removeClass("selected"),this.$search.focus()},close:function(a){a=a||{},a.returnFocus&&this.currentMenuControl&&this.currentMenuControl.container.find(".add-new-menu-item").focus(),this.currentMenuControl=null,this.selected=null,c("body").removeClass("adding-menu-items"),c("#available-menu-items .menu-item-handle.item-added").removeClass("item-added"),this.$search.val("")},keyboardAccessible:function(a){var b=13===a.which,d=27===a.which,e=40===a.which,f=38===a.which,g=9===a.which&&a.shiftKey,h=null,i=this.$el.find("> .menu-item-tpl:visible:first"),j=this.$el.find("> .menu-item-tpl:visible:last"),k=c(a.target).is(this.$search);return e||f?(e?k?h=i:this.selected&&0!==this.selected.nextAll(".menu-item-tpl:visible").length&&(h=this.selected.nextAll(".menu-item-tpl:visible:first")):f&&(k?h=j:this.selected&&0!==this.selected.prevAll(".menu-item-tpl:visible").length&&(h=this.selected.prevAll(".menu-item-tpl:visible:first"))),this.select(h),void(h?h.focus():this.$search.focus())):void((!b||this.$search.val())&&(k&&g?(this.currentMenuControl.container.find(".add-new-menu-item").focus(),a.preventDefault()):d&&this.close({returnFocus:!0})))}}),a.Menus.MenusPanel=a.Panel.extend({attachEvents:function(){a.Panel.prototype.attachEvents.call(this);var b=this,d=b.container.find(".panel-meta"),e=d.find(".customize-help-toggle"),f=d.find(".customize-panel-description"),g=c("#screen-options-wrap"),h=d.find(".customize-screen-options-toggle");h.on("click",function(){return f.not(":hidden")&&(f.slideUp("fast"),e.attr("aria-expanded","false")),"true"===h.attr("aria-expanded")?(h.attr("aria-expanded","false"),d.removeClass("open"),d.removeClass("active-menu-screen-options"),g.slideUp("fast")):(h.attr("aria-expanded","true"),d.addClass("open"),d.addClass("active-menu-screen-options"),g.slideDown("fast")),!1}),e.on("click",function(){"true"===h.attr("aria-expanded")&&(h.attr("aria-expanded","false"),e.attr("aria-expanded","true"),d.addClass("open"),d.removeClass("active-menu-screen-options"),g.slideUp("fast"),f.slideDown("fast"))})},ready:function(){var a=this;this.container.find(".hide-column-tog").click(function(){var b=c(this),d=b.val();b.prop("checked")?a.checked(d):a.unchecked(d),a.saveManageColumnsState()}),this.container.find(".hide-column-tog").each(function(){var b=c(this),d=b.val();b.prop("checked")?a.checked(d):a.unchecked(d)})},saveManageColumnsState:function(){var a=this.hidden();c.post(b.ajax.settings.url,{action:"hidden-columns",hidden:a,screenoptionnonce:c("#screenoptionnonce").val(),page:"nav-menus"})},checked:function(a){this.container.addClass("field-"+a+"-active")},unchecked:function(a){this.container.removeClass("field-"+a+"-active")},hidden:function(){this.hidden=function(){return c(".hide-column-tog").not(":checked").map(function(){var a=this.id;return a.substring(a,a.length-5)}).get().join(",")}}}),a.Menus.MenuSection=a.Section.extend({initialize:function(b,d){var e=this;a.Section.prototype.initialize.call(e,b,d),e.deferred.initSortables=c.Deferred()},ready:function(){var b=this;if("undefined"==typeof b.params.menu_id)throw new Error("params.menu_id was not defined");b.active.validate=function(){return a.has(b.id)?!!a(b.id).get():!1},b.populateControls(),b.navMenuLocationSettings={},b.assignedLocations=new a.Value([]),a.each(function(a,c){var d=c.match(/^nav_menu_locations\[(.+?)]/);d&&(b.navMenuLocationSettings[d[1]]=a,a.bind(function(){b.refreshAssignedLocations()}))}),b.assignedLocations.bind(function(a){b.updateAssignedLocationsInSectionTitle(a)}),b.refreshAssignedLocations()},populateControls:function(){var b,c,d,e=this;b=e.id+"[name]",d=a.control(b),d||(d=new a.controlConstructor.nav_menu_name(b,{params:{type:"nav_menu_name",content:'',label:"",active:!0,section:e.id,priority:0,settings:{"default":e.id}}}),a.control.add(d.id,d),d.active.set(!0)),c=a.control(e.id),c||(c=new a.controlConstructor.nav_menu(e.id,{params:{type:"nav_menu",content:'',section:e.id,priority:999,active:!0,settings:{"default":e.id},menu_id:e.params.menu_id}}),a.control.add(c.id,c),c.active.set(!0))},refreshAssignedLocations:function(){var a=this,b=a.params.menu_id,c=[];_.each(a.navMenuLocationSettings,function(a,d){a()===b&&c.push(d)}),a.assignedLocations.set(c)},updateAssignedLocationsInSectionTitle:function(b){var d,e=this;d=e.container.find(".accordion-section-title:first"),d.find(".menu-in-location").remove(),_.each(b,function(b){var e=c('');e.text(a.Menus.data.l10n.menuLocation.replace("%s",b)),d.append(e)}),e.container.toggleClass("assigned-to-menu-location",0!==b.length)},onChangeExpanded:function(b,d){var e=this;b&&(wpNavMenu.menuList=e.container.find(".accordion-section-content:first"),wpNavMenu.targetList=wpNavMenu.menuList,c("#menu-to-edit").removeAttr("id"),wpNavMenu.menuList.attr("id","menu-to-edit").addClass("menu"),_.each(a.section(e.id).controls(),function(a){"nav_menu_item"===a.params.type&&a.actuallyEmbed()}),"resolved"!==e.deferred.initSortables.state()&&(wpNavMenu.initSortables(),e.deferred.initSortables.resolve(wpNavMenu.menuList),a.control("nav_menu["+String(e.params.menu_id)+"]").reflowMenuItems())),a.Section.prototype.onChangeExpanded.call(e,b,d)}}),a.Menus.NewMenuSection=a.Section.extend({attachEvents:function(){var a=this;this.container.on("click",".add-menu-toggle",function(){a.expanded()?a.collapse():a.expand()})},onChangeExpanded:function(a){var b=this,c=b.container.find(".add-menu-toggle"),d=b.container.find(".new-menu-section-content"),e=b.container.closest(".wp-full-overlay-sidebar-content");a?(c.addClass("open"),d.slideDown("fast",function(){e.scrollTop(e.height())})):(c.removeClass("open"),d.slideUp("fast"))}}),a.Menus.MenuLocationControl=a.Control.extend({initialize:function(b,c){var d=this,e=b.match(/^nav_menu_locations\[(.+?)]/);d.themeLocation=e[1],a.Control.prototype.initialize.call(d,b,c)},ready:function(){var b=this,c=/^nav_menu\[(-?\d+)]/;b.setting.validate=function(a){return parseInt(a,10)},a.bind("add",function(a){var d,e,f=a.id.match(c);f&&!1!==a()&&(e=f[1],d=new Option(a().name,e),b.container.find("select").append(d))}),a.bind("remove",function(a){var d,e=a.id.match(c);e&&(d=parseInt(e[1],10),b.setting()===d&&b.setting.set(""),b.container.find("option[value="+d+"]").remove())}),a.bind("change",function(a){var d,e=a.id.match(c);e&&(d=parseInt(e[1],10),!1===a()?(b.setting()===d&&b.setting.set(""),b.container.find("option[value="+d+"]").remove()):b.container.find("option[value="+d+"]").text(a().name))})}}),a.Menus.MenuItemControl=a.Control.extend({initialize:function(b,c){var d=this;a.Control.prototype.initialize.call(d,b,c),d.active.validate=function(){return a.section(d.section()).active()}},embed:function(){var b,c=this,d=c.section();d&&(b=a.section(d),b&&b.expanded()&&c.actuallyEmbed())},actuallyEmbed:function(){var a=this;"resolved"!==a.deferred.embedded.state()&&(a.renderContent(),a.deferred.embedded.resolve())},ready:function(){if("undefined"==typeof this.params.menu_item_id)throw new Error("params.menu_item_id was not defined");this._setupControlToggle(),this._setupReorderUI(),this._setupUpdateUI(),this._setupRemoveUI(),this._setupLinksUI(),this._setupTitleUI()},_setupControlToggle:function(){var a=this;this.container.find(".menu-item-handle").on("click",function(b){b.preventDefault(),b.stopPropagation();var c=a.getMenuControl();c.isReordering||c.isSorting||a.toggleForm()})},_setupReorderUI:function(){var a,d,e=this;a=b.template("menu-item-reorder-nav"),e.container.find(".item-controls").after(a),d=e.container.find(".menu-item-reorder-nav"),d.find(".menus-move-up, .menus-move-down, .menus-move-left, .menus-move-right").on("click",function(){var a=c(this);a.focus();var b=a.is(".menus-move-up"),d=a.is(".menus-move-down"),f=a.is(".menus-move-left"),g=a.is(".menus-move-right");b?e.moveUp():d?e.moveDown():f?e.moveLeft():g&&e.moveRight(),a.focus()})},_setupUpdateUI:function(){var b=this,c=b.setting();b.elements={},b.elements.url=new a.Element(b.container.find(".edit-menu-item-url")),b.elements.title=new a.Element(b.container.find(".edit-menu-item-title")),b.elements.attr_title=new a.Element(b.container.find(".edit-menu-item-attr-title")),b.elements.target=new a.Element(b.container.find(".edit-menu-item-target")),b.elements.classes=new a.Element(b.container.find(".edit-menu-item-classes")),b.elements.xfn=new a.Element(b.container.find(".edit-menu-item-xfn")),b.elements.description=new a.Element(b.container.find(".edit-menu-item-description")),_.each(b.elements,function(a,d){a.bind(function(c){a.element.is("input[type=checkbox]")&&(c=c?a.element.val():"");var e=b.setting();e&&e[d]!==c&&(e=_.clone(e),e[d]=c,b.setting.set(e))}),c&&a.set(c[d])}),b.setting.bind(function(c,d){var e,f=b.params.menu_item_id,g=[],h=[];!1===c?(e=a.control("nav_menu["+String(d.nav_menu_term_id)+"]"),b.container.remove(),_.each(e.getMenuItemControls(),function(a){d.menu_item_parent===a.setting().menu_item_parent&&a.setting().position>d.position?g.push(a):a.setting().menu_item_parent===f&&h.push(a)}),_.each(g,function(a){var b=_.clone(a.setting());b.position+=h.length,a.setting.set(b)}),_.each(h,function(a,b){var c=_.clone(a.setting());c.position=d.position+b,c.menu_item_parent=d.menu_item_parent,a.setting.set(c)}),e.debouncedReflowMenuItems()):(_.each(c,function(a,d){b.elements[d]&&b.elements[d].set(c[d])}),b.container.find(".menu-item-data-parent-id").val(c.menu_item_parent),(c.position!==d.position||c.menu_item_parent!==d.menu_item_parent)&&b.getMenuControl().debouncedReflowMenuItems())})},_setupRemoveUI:function(){var d,e=this;d=e.container.find(".item-delete"),d.on("click",function(){var d;d=e.container.next().is(".customize-control-nav_menu_item")?e.container.next().find(c("body").hasClass("adding-menu-items")?".item-delete:first":".item-edit:first"):e.container.prev().is(".customize-control-nav_menu_item")?e.container.prev().find(c("body").hasClass("adding-menu-items")?".item-delete:first":".item-edit:first"):e.container.next(".customize-control-nav_menu").find(".add-new-menu-item"),e.container.slideUp(function(){e.setting.set(!1),b.a11y.speak(a.Menus.data.l10n.itemDeleted),d.focus()})})},_setupLinksUI:function(){var b;b=this.container.find("a.original-link"),b.on("click",function(b){b.preventDefault(),a.previewer.previewUrl(b.target.toString())})},_setupTitleUI:function(){var b=this;b.setting.bind(function(c){if(c){var d=b.container.find(".menu-item-title");c.title?d.text(c.title).removeClass("no-title"):d.text(a.Menus.data.l10n.untitled).addClass("no-title")}})},getDepth:function(){var b=this,c=b.setting(),d=0;if(!c)return 0;for(;c&&c.menu_item_parent&&(d+=1,b=a.control("nav_menu_item["+c.menu_item_parent+"]"));)c=b.setting();return d},renderContent:function(){var b,c=this,d=c.setting();c.params.title=d.title||"",c.params.depth=c.getDepth(),c.container.data("item-depth",c.params.depth),b=["menu-item","menu-item-depth-"+String(c.params.depth),"menu-item-"+d.object,"menu-item-edit-inactive"],d.invalid?(b.push("invalid"),c.params.title=a.Menus.data.invalidTitleTpl.replace("%s",c.params.title)):"draft"===d.status&&(b.push("pending"),c.params.title=a.Menus.data.pendingTitleTpl.replace("%s",c.params.title)),c.params.el_classes=b.join(" "),c.params.item_type_label=a.Menus.getTypeLabel(d.type,d.object),c.params.item_type=d.type,c.params.url=d.url,c.params.target=d.target,c.params.attr_title=d.attr_title,c.params.classes=_.isArray(d.classes)?d.classes.join(" "):d.classes,c.params.attr_title=d.attr_title,c.params.xfn=d.xfn,c.params.description=d.description,c.params.parent=d.menu_item_parent,c.params.original_title=d.original_title||"",c.container.addClass(c.params.el_classes),a.Control.prototype.renderContent.call(c)},getMenuControl:function(){var b=this,c=b.setting();return c&&c.nav_menu_term_id?a.control("nav_menu["+c.nav_menu_term_id+"]"):null},expandControlSection:function(){var a=this.container.closest(".accordion-section");a.hasClass("open")||a.find(".accordion-section-title:first").trigger("click")},expandForm:function(){this.toggleForm(!0)},collapseForm:function(){this.toggleForm(!1)},toggleForm:function(b){var c,d,e,f=this;c=this.container,d=c.find(".menu-item-settings:first"),"undefined"==typeof b&&(b=!d.is(":visible")),d.is(":visible")!==b&&(b?(a.control.each(function(a){f.params.type===a.params.type&&f!==a&&a.collapseForm()}),e=function(){c.removeClass("menu-item-edit-inactive").addClass("menu-item-edit-active"),f.container.trigger("expanded")},d.slideDown("fast",e),f.container.trigger("expand")):(e=function(){c.addClass("menu-item-edit-inactive").removeClass("menu-item-edit-active"),f.container.trigger("collapsed")},f.container.trigger("collapse"),d.slideUp("fast",e)))},focus:function(){this.expandControlSection(),this.expandForm(),this.container.find(".menu-item-settings :focusable:first").focus()},moveUp:function(){this._changePosition(-1),b.a11y.speak(a.Menus.data.l10n.movedUp)},moveDown:function(){this._changePosition(1),b.a11y.speak(a.Menus.data.l10n.movedDown)},moveLeft:function(){this._changeDepth(-1),b.a11y.speak(a.Menus.data.l10n.movedLeft)},moveRight:function(){this._changeDepth(1),b.a11y.speak(a.Menus.data.l10n.movedRight)},_changePosition:function(a){var b,d,e=this,f=_.clone(e.setting()),g=[];if(1!==a&&-1!==a)throw new Error("Offset changes by 1 are only supported.");if(e.setting()){if(_(e.getMenuControl().getMenuItemControls()).each(function(a){a.setting().menu_item_parent===f.menu_item_parent&&g.push(a.setting)}),g.sort(function(a,b){return a().position-b().position}),d=_.indexOf(g,e.setting),-1===d)throw new Error("Expected setting to be among siblings.");0===d&&0>a||d===g.length-1&&a>0||(b=g[d+a],b&&b.set(c.extend(_.clone(b()),{position:f.position})),f.position+=a,e.setting.set(f))}},_changeDepth:function(b){if(1!==b&&-1!==b)throw new Error("Offset changes by 1 are only supported.");var d,e,f,g=this,h=_.clone(g.setting()),i=[];if(_(g.getMenuControl().getMenuItemControls()).each(function(a){a.setting().menu_item_parent===h.menu_item_parent&&i.push(a)}),i.sort(function(a,b){return a.setting().position-b.setting().position}),d=_.indexOf(i,g),-1===d)throw new Error("Expected control to be among siblings.");if(-1===b){if(!h.menu_item_parent)return;f=a.control("nav_menu_item["+h.menu_item_parent+"]"),_(i).chain().slice(d).each(function(a,b){a.setting.set(c.extend({},a.setting(),{menu_item_parent:g.params.menu_item_id,position:b}))}),_(g.getMenuControl().getMenuItemControls()).each(function(a){var b,d;d=a.setting().menu_item_parent===f.setting().menu_item_parent&&a.setting().position>f.setting().position,d&&(b=_.clone(a.setting()),a.setting.set(c.extend(b,{position:b.position+1})))}),h.position=f.setting().position+1,h.menu_item_parent=f.setting().menu_item_parent,g.setting.set(h)}else if(1===b){if(0===d)return;e=i[d-1],h.menu_item_parent=e.params.menu_item_id,h.position=0,_(g.getMenuControl().getMenuItemControls()).each(function(a){a.setting().menu_item_parent===h.menu_item_parent&&(h.position=Math.max(h.position,a.setting().position))}),h.position+=1,g.setting.set(h)}}}),a.Menus.MenuNameControl=a.Control.extend({ready:function(){var b=this,c=b.setting();b.active.validate=function(){return a.section(b.section()).active()},b.nameElement=new a.Element(b.container.find(".menu-name-field")),b.nameElement.bind(function(a){var c=b.setting();c&&c.name!==a&&(c=_.clone(c),c.name=a,b.setting.set(c))}),c&&b.nameElement.set(c.name),b.setting.bind(function(a){a&&b.nameElement.set(a.name)})}}),a.Menus.MenuControl=a.Control.extend({ready:function(){var b=this,d=b.params.menu_id;if("undefined"==typeof this.params.menu_id)throw new Error("params.menu_id was not defined");b.active.validate=function(){return a.section(b.section()).active()},b.$controlSection=b.container.closest(".control-section"),b.$sectionContent=b.container.closest(".accordion-section-content"),this._setupModel(),a.section(b.section(),function(a){a.deferred.initSortables.done(function(a){b._setupSortable(a)})}),this._setupAddition(),this._setupLocations(),this._setupTitle(),b.setting()&&(a.control.each(function(c){if(c.extended(a.controlConstructor.widget_form)&&"nav_menu"===c.params.widget_id_base){var e=c.container.find("select");0===e.find("option[value="+String(d)+"]").length&&e.append(new Option(b.setting().name,d))}}),c("#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first").append(new Option(b.setting().name,d)))},_setupModel:function(){var b=this,d=b.params.menu_id;b.elements={},b.elements.auto_add=new a.Element(b.container.find("input[type=checkbox].auto_add")),b.elements.auto_add.bind(function(a){var c=b.setting();c&&c.auto_add!==a&&(c=_.clone(c),c.auto_add=a,b.setting.set(c))}),b.elements.auto_add.set(b.setting().auto_add),b.setting.bind(function(a){a&&b.elements.auto_add.set(a.auto_add)}),b.setting.bind(function(e){!1===e?b._handleDeletion():(a.control.each(function(b){if(b.extended(a.controlConstructor.widget_form)&&"nav_menu"===b.params.widget_id_base){var c=b.container.find("select");c.find("option[value="+String(d)+"]").text(e.name)}}),c("#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first option[value="+String(d)+"]").text(e.name))}),b.container.find(".menu-delete").on("click",function(a){a.stopPropagation(),a.preventDefault(),b.setting.set(!1)})},_setupSortable:function(b){var c=this;if(!b.is(c.$sectionContent))throw new Error("Unexpected menuList.");b.on("sortstart",function(){c.isSorting=!0}),b.on("sortstop",function(){setTimeout(function(){var b=c.$sectionContent.sortable("toArray"),d=[],e=0,f=10;c.isSorting=!1,_.each(b,function(b){var c,e,f;f=b.match(/^customize-control-nav_menu_item-(-?\d+)$/,""),f&&(c=parseInt(f[1],10),e=a.control("nav_menu_item["+String(c)+"]"),e&&d.push(e))}),_.each(d,function(a){if(!1!==a.setting()){var b=_.clone(a.setting());e+=1,f+=1,b.position=e,a.priority(f),b.menu_item_parent=parseInt(a.container.find(".menu-item-data-parent-id").val(),10),b.menu_item_parent||(b.menu_item_parent=0),a.setting.set(b)}})})}),c.isReordering=!1,this.container.find(".reorder-toggle").on("click",function(){c.toggleReordering(!c.isReordering)})},_setupAddition:function(){var b=this;this.container.find(".add-new-menu-item").on("click",function(d){b.$sectionContent.hasClass("reordering")||(c("body").hasClass("adding-menu-items")?(c(this).attr("aria-expanded","false"),a.Menus.availableMenuItemsPanel.close(),d.stopPropagation()):(c(this).attr("aria-expanded","true"),a.Menus.availableMenuItemsPanel.open(b)))})},_handleDeletion:function(){var d,e,f=this,g=f.params.menu_id;d=a.section(f.section()),e=function(){d.container.remove(),a.section.remove(d.id)},d&&d.expanded()?d.collapse({completeCallback:function(){e(),b.a11y.speak(a.Menus.data.l10n.menuDeleted),a.panel("nav_menus").focus()}}):e(),a.control.each(function(b){if(b.extended(a.controlConstructor.widget_form)&&"nav_menu"===b.params.widget_id_base){var c=b.container.find("select");c.val()===String(g)&&c.prop("selectedIndex",0).trigger("change"),c.find("option[value="+String(g)+"]").remove()}}),c("#available-widgets-list .widget-inside:has(input.id_base[value=nav_menu]) select:first option[value="+String(g)+"]").remove()},_setupLocations:function(){var b=this;b.container.find(".assigned-menu-location").each(function(){var d,e,f=c(this),g=f.find("input[type=checkbox]"),h=a("nav_menu_locations["+g.data("location-id")+"]");e=function(b){var c=a("nav_menu["+String(b)+"]");b&&c&&c()?f.find(".theme-location-set").show().find("span").text(c().name):f.find(".theme-location-set").hide()},d=new a.Element(g),d.set(h.get()===b.params.menu_id),g.on("change",function(){h.set(this.checked?b.params.menu_id:0)}),h.bind(function(a){d.set(a===b.params.menu_id),e(a)}),e(h.get())})},_setupTitle:function(){var b=this;b.setting.bind(function(d){if(d&&d.name){var e=b.container.closest(".accordion-section"),f=b.params.menu_id,g=e.find(".accordion-section-title"),h=e.find(".customize-section-title h3"),i=e.find(".menu-in-location"),j=h.find(".customize-action");g.text(d.name),i.length&&i.appendTo(g),h.text(d.name),j.length&&j.prependTo(h),a.control.each(function(a){/^nav_menu_locations\[/.test(a.id)&&a.container.find("option[value="+f+"]").text(d.name)}),e.find(".customize-control-checkbox input").each(function(){c(this).prop("checked")&&c(".current-menu-location-name-"+c(this).data("location-id")).text(d.name)})}})},toggleReordering:function(a){a=Boolean(a),a!==this.$sectionContent.hasClass("reordering")&&(this.isReordering=a,this.$sectionContent.toggleClass("reordering",a),this.$sectionContent.sortable(this.isReordering?"disable":"enable"),a&&_(this.getMenuItemControls()).each(function(a){a.collapseForm()}))},getMenuItemControls:function(){var b=this,c=[],d=b.params.menu_id;return a.control.each(function(a){"nav_menu_item"===a.params.type&&a.setting()&&d===a.setting().nav_menu_term_id&&c.push(a)}),c},reflowMenuItems:function(){var b,c=this,d=a.section("nav_menu["+String(c.params.menu_id)+"]"),e=c.getMenuItemControls();b=function(a){var c=[],d=a.currentParent;_.each(a.menuItemControls,function(a){d===a.setting().menu_item_parent&&c.push(a)}),c.sort(function(a,b){return a.setting().position-b.setting().position}),_.each(c,function(c){a.currentAbsolutePosition+=1,c.priority.set(a.currentAbsolutePosition),c.container.hasClass("menu-item-depth-"+String(a.currentDepth))||(_.each(c.container.prop("className").match(/menu-item-depth-\d+/g),function(a){c.container.removeClass(a)}),c.container.addClass("menu-item-depth-"+String(a.currentDepth))),c.container.data("item-depth",a.currentDepth),a.currentDepth+=1,a.currentParent=c.params.menu_item_id,b(a),a.currentDepth-=1,a.currentParent=d}),c.length&&(_(c).each(function(a){a.container.removeClass("move-up-disabled move-down-disabled move-left-disabled move-right-disabled")}),c[0].container.addClass("move-up-disabled").addClass("move-right-disabled").toggleClass("move-down-disabled",1===c.length),c[c.length-1].container.addClass("move-down-disabled").toggleClass("move-up-disabled",1===c.length))},b({menuItemControls:e,currentParent:0,currentDepth:0,currentAbsolutePosition:0}),d.container.find(".menu-item .menu-item-reorder-nav button").prop("tabIndex",0),d.container.find(".menu-item.move-up-disabled .menus-move-up").prop("tabIndex",-1),d.container.find(".menu-item.move-down-disabled .menus-move-down").prop("tabIndex",-1),d.container.find(".menu-item.move-left-disabled .menus-move-left").prop("tabIndex",-1),d.container.find(".menu-item.move-right-disabled .menus-move-right").prop("tabIndex",-1),c.container.find(".reorder-toggle").toggle(e.length>1)},debouncedReflowMenuItems:_.debounce(function(){this.reflowMenuItems.apply(this,arguments)},0),addItemToMenu:function(d){var e,f,g,h,i,j=this,k=0,l=10;return _.each(j.getMenuItemControls(),function(a){!1!==a.setting()&&(l=Math.max(l,a.priority()),0===a.setting().menu_item_parent&&(k=Math.max(k,a.setting().position)))}),k+=1,l+=1,d=c.extend({},a.Menus.data.defaultSettingValues.nav_menu_item,d,{nav_menu_term_id:j.params.menu_id,original_title:d.title,position:k}),delete d.id,i=a.Menus.generatePlaceholderAutoIncrementId(),e="nav_menu_item["+String(i)+"]",f={type:"nav_menu_item",transport:"postMessage",previewer:a.previewer},g=a.create(e,e,{},f),g.set(d),h=new a.controlConstructor.nav_menu_item(e,{params:{type:"nav_menu_item",content:'',section:j.id,priority:l,active:!0,settings:{"default":e},menu_item_id:i},previewer:a.previewer}),a.control.add(e,h),g.preview(),j.debouncedReflowMenuItems(),b.a11y.speak(a.Menus.data.l10n.itemAdded),h}}),a.Menus.NewMenuControl=a.Control.extend({ready:function(){this._bindHandlers()},_bindHandlers:function(){var a=this,b=c("#customize-control-new_menu_name input"),d=c("#create-new-menu-submit");b.on("keydown",function(b){13===b.which&&a.submit()}),d.on("click",function(b){a.submit(),b.stopPropagation(),b.preventDefault()})},submit:function(){var d,e,f=this,g=f.container.closest(".accordion-section-new-menu"),h=g.find(".menu-name-field").first(),i=h.val(),j=a.Menus.generatePlaceholderAutoIncrementId();e="nav_menu["+String(j)+"]",a.create(e,e,{},{type:"nav_menu",transport:"postMessage",previewer:a.previewer}),a(e).set(c.extend({},a.Menus.data.defaultSettingValues.nav_menu,{name:i})),d=new a.Menus.MenuSection(e,{params:{id:e,panel:"nav_menus",title:i,customizeAction:a.Menus.data.l10n.customizingMenus,type:"nav_menu",priority:10,menu_id:j}}),a.section.add(e,d),h.val(""),b.a11y.speak(a.Menus.data.l10n.menuAdded),a.section(e).focus()}}),c.extend(a.controlConstructor,{nav_menu_location:a.Menus.MenuLocationControl,nav_menu_item:a.Menus.MenuItemControl,nav_menu:a.Menus.MenuControl,nav_menu_name:a.Menus.MenuNameControl,new_menu:a.Menus.NewMenuControl}),c.extend(a.panelConstructor,{nav_menus:a.Menus.MenusPanel}),c.extend(a.sectionConstructor,{nav_menu:a.Menus.MenuSection,new_menu:a.Menus.NewMenuSection}),a.bind("ready",function(){a.Menus.availableMenuItemsPanel=new a.Menus.AvailableMenuItemsPanelView({collection:a.Menus.availableMenuItems}),a.bind("saved",function(b){(b.nav_menu_updates||b.nav_menu_item_updates)&&a.Menus.applySavedData(b) }),a.previewer.bind("refresh",function(){a.previewer.refresh()})}),a.Menus.applySavedData=function(b){var c={};_(b.nav_menu_updates).each(function(b){var d,e,f,g,h,i,j;if("inserted"===b.status){if(!b.previous_term_id)throw new Error("Expected previous_term_id");if(!b.term_id)throw new Error("Expected term_id");if(d="nav_menu["+String(b.previous_term_id)+"]",!a.has(d))throw new Error("Expected setting to exist: "+d);if(f=a(d),!a.section.has(d))throw new Error("Expected control to exist: "+d);if(i=a.section(d),h=f.get(),!h)throw new Error("Did not expect setting to be empty (deleted).");h=_.clone(h),c[b.previous_term_id]=b.term_id,e="nav_menu["+String(b.term_id)+"]",g=a.create(e,e,h,{type:"nav_menu",transport:"postMessage",previewer:a.previewer}),i.expanded()&&i.collapse(),j=new a.Menus.MenuSection(e,{params:{id:e,panel:"nav_menus",title:h.name,customizeAction:a.Menus.data.l10n.customizingMenus,type:"nav_menu",priority:i.priority.get(),active:!0,menu_id:b.term_id}}),i.container.remove(),a.section.remove(d),a.section.add(e,j),f.callbacks.disable(),f.set(!1),f.preview(),g.preview(),a.each(function(c){var d=a.state("saved").get();/^nav_menu_locations\[/.test(c.id)&&c.get()===b.previous_term_id&&(c.set(b.term_id),c._dirty=!1,a.state("saved").set(d),c.preview())}),i.expanded.get()&&j.expand()}}),_(b.nav_menu_item_updates).each(function(b){var d,e,f,g,h,i,j;if("inserted"===b.status){if(!b.previous_post_id)throw new Error("Expected previous_post_id");if(!b.post_id)throw new Error("Expected post_id");if(d="nav_menu_item["+String(b.previous_post_id)+"]",!a.has(d))throw new Error("Expected setting to exist: "+d);if(f=a(d),!a.control.has(d))throw new Error("Expected control to exist: "+d);if(i=a.control(d),h=f.get(),!h)throw new Error("Did not expect setting to be empty (deleted).");h=_.clone(h),c[h.nav_menu_term_id]&&(h.nav_menu_term_id=c[h.nav_menu_term_id]),e="nav_menu_item["+String(b.post_id)+"]",g=a.create(e,e,h,{type:"nav_menu_item",transport:"postMessage",previewer:a.previewer}),j=new a.controlConstructor.nav_menu_item(e,{params:{type:"nav_menu_item",content:'',menu_id:b.post_id,section:"nav_menu["+String(h.nav_menu_term_id)+"]",priority:i.priority.get(),active:!0,settings:{"default":e},menu_item_id:b.post_id},previewer:a.previewer}),i.container.remove(),a.control.remove(d),a.control.add(e,j),f.callbacks.disable(),f.set(!1),f.preview(),g.preview(),j.container.toggleClass("menu-item-edit-inactive",i.container.hasClass("menu-item-edit-inactive"))}})},a.Menus.focusMenuItemControl=function(b){var c=a.Menus.getMenuItemControl(b);c&&c.focus()},a.Menus.getMenuControl=function(b){return a.control("nav_menu["+b+"]")},a.Menus.getTypeLabel=function(b,c){var d,e=a.Menus.data;return d="post_type"===b?e.itemTypes.postTypes[c]?e.itemTypes.postTypes[c].label:e.l10n.postTypeLabel:"taxonomy"===b?e.itemTypes.taxonomies[c]?e.itemTypes.taxonomies[c].label:e.l10n.taxonomyTermLabel:e.l10n.custom_label},a.Menus.getMenuItemControl=function(b){return a.control(d(b))}}(wp.customize,wp,jQuery);