!function(e){"use strict";var t={instance:[],menuId:0,depth:0,init:function(){this.menuButton(),e(document).on("click.WCFMegaMenuAdmin",".wcf-mega-menu-settings-save",this.saveMenuOpt).on("click.WCFMegaMenuAdmin",".wcf-mega-menu-trigger",this.openPopup).on("click.WCFMegaMenuAdmin",".wcf-mega-menu-popup-close",this.closePopup).on("click.WCFMegaMenuAdmin",".wcf-mega-menu-popup-close-btn",this.closePopup).on("click.WCFMegaMenuAdmin",".wcf-mega-menu-submit-btn",this.saveMenuData)},saveMenuOpt:function(){e(this).parent().find(".spinner").addClass("loading"),t.save_menu_options(e(this))},save_menu_options:function(t){var n={enable_menu:!0===t.parents("#wcf_mega_menu_meta_box").find("#wcf-megamenu-menu-metabox-input-is-enabled").is(":checked")?"on":"off"};e.ajax({type:"POST",url:ajaxurl,data:{action:"wcf_mega_menu_ajax_requests",sub_action:"save_menu_options",settings:n,menu_id:e("#wcf-megamenu-metabox-input-menu-id").val(),nonce:WCFMEGAMENU.nonce},cache:!1,success:function(e){t.parent().find(".spinner").removeClass("loading")}})},menuButton:function(){var n=wp.template("wcf-mega-menu-button"),a=e("#wcf-megamenu-menu-metabox-input-is-enabled");e("#menu-to-edit .menu-item").each((function(){var a=e(this),i=t.getItemDepth(a),u=t.getItemId(a);a.find(".item-title").append(n({id:u,depth:i,label:"WCF Mega Menu"}))})),!0!==a.is(":checked")?e(".wcf-mega-menu-trigger").hide():e(".wcf-mega-menu-trigger").show(),a.on("change",(function(){!0!==a.is(":checked")?e(".wcf-mega-menu-trigger").hide():e(".wcf-mega-menu-trigger").show()}))},getItemId:function(e){return e.attr("id").replace("menu-item-","")},getItemDepth:function(e){var t=e.attr("class").match(/menu-item-depth-\d/);return t[0]?t[0].replace("menu-item-depth-",""):0},openPopup:function(){var n=e(this),a=n.data("item-id"),i=n.data("item-depth"),u="#wcf-mega-popup-"+a,s=null,c=wp.template("wcf-mega-menu-popup");e.ajax({url:ajaxurl,type:"GET",dataType:"JSON",data:{action:"wcf_mega_menu_ajax_requests",sub_action:"get_menu_options",menu_item_id:a,nonce:WCFMEGAMENU.nonce},cache:!1,beforeSend:function(){},success:function(n){s=c({id:a,depth:i,content:n.data.content,templatelist:WCFMEGAMENU.templates}),e("body").append(s);var o=e(u).find(".wcf-mega-menu-submit-btn");t.init_tab(".mega-menu-popup-tab-menu"),e(u+" form.wcf-mega-menu-data").on("keyup",".mega-menu-popup-input",(function(){o.removeClass("disabled").attr("disabled",!1).text(WCFMEGAMENU.button.text)})),e(u+" form.wcf-mega-menu-data").on("change",".mega-menu-popup-input",(function(){o.removeClass("disabled").attr("disabled",!1).text(WCFMEGAMENU.button.text)})),e('[data-checked="true"]').prop("checked",!0);var m=e(u+" .width_lists input"),p=e(u+" .custom-width-row");m.each((function(){var t=e(this).prop("checked"),n=e(this).val();t&&"custom_width"===n?p.show():p.hide()})),m.on("change",(function(){"custom_width"!==e(this).val()?p.hide():p.show()}))},complete:function(t){e(".wcf-mega-menu-popup").addClass("open-popup")}})},closePopup:function(t){t.preventDefault(),e(this).closest(".wcf-mega-menu-popup").removeClass("open-popup"),e(this).closest(".wcf-mega-menu-popup").remove()},saveMenuData:function(){var t=e(this).data("id"),n=e("#wcf-mega-menu-form-"+t),a=n.find(".wcf-mega-menu-submit-btn");n.on("submit",(function(i){i.preventDefault(),e.ajax({type:"POST",url:ajaxurl,data:{action:"wcf_mega_menu_ajax_requests",sub_action:"save_menu_settings",settings:n.serialize(),menu_item_id:t,nonce:WCFMEGAMENU.nonce},cache:!1,beforeSend:function(){a.text(WCFMEGAMENU.button.lodingtext).addClass("updating-message")},success:function(e){a.removeClass("updating-message").addClass("disabled").attr("disabled",!0).text(WCFMEGAMENU.button.successtext)},complete:function(e){a.removeClass("updating-message").addClass("disabled").attr("disabled",!0).text(WCFMEGAMENU.button.successtext)}})}))},init_tab:function(t){e(t).on("click","a",(function(n){n.preventDefault();var a=e(this),i=a.data("target"),u=a.closest(t).siblings(".mega-menu-popup-tab-content").find(".mega-menu-popup-tab-pane[data-id="+i+"]");a.addClass("active").closest("li").siblings().find("a").removeClass("active"),u.addClass("active").siblings().removeClass("active")}))}};t.init()}(jQuery);