var WPNavMenuHandler=function(d){var h={},f=30,c=11,j=function(l,p,m,o){if(l&&l[0]){var n=d.parseJSON(l[0]);if(n.post_title){if(n.ID&&n.post_type){h[n.post_title]={ID:n.ID,object_type:n.post_type}}return n.post_title}}},k=function(l,p,m,o){if(l&&l[0]){var n=d.parseJSON(l[0]);if(n.post_title){return n.post_title}}},b=function(r,q){if(!r){return false}q=q||document;var m=["menu-item-db-id","menu-item-object-id","menu-item-object","menu-item-parent-id","menu-item-position","menu-item-type","menu-item-append","menu-item-title","menu-item-url","menu-item-description","menu-item-attr-title","menu-item-target","menu-item-classes","menu-item-xfn"],l={},n=q.getElementsByTagName("input"),p=n.length,o,s=document.getElementById("nav-menu-meta-object-id").value;while(p--){o=m.length;while(o--){if(n[p]&&n[p].name&&"menu-item["+r+"]["+m[o]+"]"==n[p].name){l[m[o]]=n[p].value}}}return l},a=function(){g.find(".menu-item-data-position").val(function(l){return l+1})},e=function(l){return l*f},i=function(l){return Math.floor(l/f)},g;d.fn.extend({menuItemDepth:function(){return i(this.eq(0).css("margin-left").slice(0,-2))},updateDepthClass:function(m,l){return this.each(function(){var n=d(this);l=l||n.menuItemDepth();d(this).removeClass("menu-item-depth-"+l).addClass("menu-item-depth-"+m)})},shiftDepthClass:function(l){return this.each(function(){var m=d(this),n=m.menuItemDepth();d(this).removeClass("menu-item-depth-"+n).addClass("menu-item-depth-"+(n+l))})},childMenuItems:function(){var l=d();this.each(function(){var m=d(this),o=m.menuItemDepth(),n=m.next();while(n.length&&n.menuItemDepth()>o){l=l.add(n);n=n.next()}});return l},updateParentMenuItemDBId:function(){return this.each(function(){var n=d(this),l=n.find(".menu-item-data-parent-id"),o=n.menuItemDepth(),m=n.prev();if(o==0){l.val(0)}else{while(m.menuItemDepth()!=o-1){m=m.prev()}l.val(m.find(".menu-item-data-object-id").val())}})},hideAdvancedMenuItemFields:function(){return this.each(function(){var l=d(this);d(".hide-column-tog").not(":checked").each(function(){l.find(".field-"+d(this).val()).addClass("hidden-field")})})},});return{init:function(){g=d("#menu-to-edit");this.attachMenuEditListeners();this.attachMenuMetaListeners(document.getElementById("nav-menu-meta"));this.attachTabsPanelListeners();if(g.length){this.initSortables()}this.initToggles()},initToggles:function(){postboxes.add_postbox_toggles("nav-menus");columns.useCheckboxesForHidden();columns.checked=function(l){d(".field-"+l).removeClass("hidden-field")};columns.unchecked=function(l){d(".field-"+l).addClass("hidden-field")};g.hideAdvancedMenuItemFields()},initSortables:function(){var o=0,m,p,r,n=g.offset().left;g.sortable({handle:" > dl",placeholder:"sortable-placeholder",start:function(w,x){var v,z,s,y,t,A,u=x.item.children(".menu-item-transport");m=x.item.menuItemDepth();l(x,m);y=(x.item.next()[0]==x.placeholder[0])?x.item.next():x.item;t=y.childMenuItems();u.append(t);q(x);z=u.outerHeight();z+=(z>0)?(x.placeholder.css("margin-top").slice(0,-2)*1):0;z+=x.item.outerHeight();z-=2;x.placeholder.height(z);A=m;t.each(function(){var B=d(this).menuItemDepth();A=(B>A)?B:A});s=x.item.find("dl dt").outerWidth();s+=e(A-m);s-=2;x.placeholder.width(s)},stop:function(v,u){var t,s=o-m;t=u.item.children(".menu-item-transport").children().insertAfter(u.item);if(s!=0){u.item.updateDepthClass(o);t.shiftDepthClass(s)}u.item.updateParentMenuItemDBId();a()},change:function(t,s){if(!s.placeholder.parent().hasClass("menu")){s.placeholder.appendTo(g)}q(s)},sort:function(t,s){var u=i(s.item.offset().left-n);if(ur){u=r}}if(u!=o){l(s,u)}}});function q(u){var t=u.placeholder.prev(),s=u.placeholder.next(),v;if(t[0]==u.item[0]){t=t.prev()}if(s[0]==u.item[0]){s=s.next()}p=(s.length)?s.menuItemDepth():0;if(t.length){r=((v=t.menuItemDepth()+1)>c)?c:v}else{r=0}}function l(s,t){s.placeholder.updateDepthClass(t,o);o=t}},attachMenuEditListeners:function(){var l=this;d("#update-nav-menu").bind("click",function(m){if(m.target&&m.target.className){if(-1!=m.target.className.indexOf("item-edit")){return l.eventOnClickEditLink(m.target)}else{if(-1!=m.target.className.indexOf("menu-delete")){return l.eventOnClickMenuDelete(m.target)}else{if(-1!=m.target.className.indexOf("item-delete")){return l.eventOnClickMenuItemDelete(m.target)}else{if(-1!=m.target.className.indexOf("item-close")){return l.eventOnClickCloseLink(m.target)}}}}}})},setupInputWithDefaultTitle:function(){var l="input-with-default-title";d("."+l).each(function(){var o=d(this),n=o.attr("title"),m=o.val();o.data(l,n);if(""==m){o.val(n)}else{if(n==m){return}else{o.removeClass(l)}}}).focus(function(){var m=d(this);if(m.val()==m.data(l)){m.val("").removeClass(l)}}).blur(function(){var m=d(this);if(""==m.val()){m.val(m.data(l)).addClass(l)}})},attachMenuMetaListeners:function(l){if(!l){return}var m=this;this.setupInputWithDefaultTitle();d("input.quick-search").each(function(n,o){m.setupQuickSearchEventListeners(o)});d(l).bind("submit",function(n){return m.eventSubmitMetaForm.call(m,this,n)})},attachTabsPanelListeners:function(){d("#menu-settings-column").bind("click",function(q){if(q.target&&q.target.className&&-1!=q.target.className.indexOf("nav-tab-link")){var r,m=/#(.*)$/.exec(q.target.href),p,s=d(q.target).parents(".inside").first()[0],l=s?s.getElementsByTagName("input"):[],n=l.length;while(n--){l[n].checked=false}d(".tabs-panel",s).each(function(){if(this.className){this.className=this.className.replace("tabs-panel-active","tabs-panel-inactive")}});d(".tabs",s).each(function(){this.className=this.className.replace("tabs","")});q.target.parentNode.className+=" tabs";if(m&&m[1]){r=document.getElementById(m[1]);if(r){r.className=r.className.replace("tabs-panel-inactive","tabs-panel-active")}}return false}else{if(q.target&&q.target.className&&-1!=q.target.className.indexOf("select-all")){var o=/#(.*)$/.exec(q.target.href);if(o&&o[1]){d("#"+o[1]+" .tabs-panel-active input[type=checkbox]").attr("checked","checked");return false}}}})},setupQuickSearchEventListeners:function(l){var m=this;d(l).autocomplete(ajaxurl+"?action=menu-quick-search&type="+l.name,{delay:500,formatItem:j,formatResult:k,minchars:2,multiple:false}).bind("blur",function(p){var n=h[this.value],o=this;if(n){d.post(ajaxurl+"?action=menu-quick-search&type=get-post-item&response-format=markup",n,function(q){m.processQuickSearchQueryResponse.call(m,q,n);h[o.value]=false})}})},eventOnClickEditLink:function(l){var n,m=/#(.*)$/.exec(l.href);if(m&&m[1]){n=d("#"+m[1]);if(0!=n.length){if(n.hasClass("menu-item-edit-inactive")){n.slideDown("fast").siblings("dl").andSelf().removeClass("menu-item-edit-inactive").addClass("menu-item-edit-active")}else{n.slideUp("fast").siblings("dl").andSelf().removeClass("menu-item-edit-active").addClass("menu-item-edit-inactive")}return false}}},eventOnClickCloseLink:function(l){d(l).closest(".menu-item-settings").siblings("dl").find(".item-edit").click();return false},eventOnClickMenuDelete:function(l){if(confirm(navMenuL10n.warnDeleteMenu)){return true}else{return false}},eventOnClickMenuItemDelete:function(l){var o,n,m=this;if(confirm(navMenuL10n.warnDeleteMenuItem)){n=/_wpnonce=([a-zA-Z0-9]*)$/.exec(l.href);if(n&&n[1]){o=parseInt(l.id.replace("delete-",""),10);d.post(ajaxurl,{action:"delete-menu-item","menu-item":o,_wpnonce:n[1]},function(p){if("1"==p){m.removeMenuItem(document.getElementById("menu-item-"+o))}});return false}return true}else{return false}},eventSubmitMetaForm:function(m,s){var q=m.getElementsByTagName("input"),p=q.length,o,u,l,r,n={},t=function(){},v=new RegExp("menu-item\\[([^\\]]*)");m.className=m.className+" processing",that=this;n.action="";while(p--){if(q[p].name&&-1!=q[p].name.indexOf("menu-item-object-id")&&q[p].checked||("undefined"!=typeof q[p].id&&"custom-menu-item-url"==q[p].id&&""!=q[p].value&&"http://"!=q[p].value)){n.action="add-menu-item";t=that.processAddMenuItemResponse;r=v.exec(q[p].name);l="undefined"==typeof r[1]?0:parseInt(r[1],10);u=b(l);for(o in u){n["menu-item["+l+"]["+o+"]"]=u[o]}q[p].checked=false}else{if(""==n.action&&""!=q[p].value&&q[p].className&&-1!=q[p].className.search(/quick-search\b[^-]/)){n.action="menu-quick-search";n.q=q[p].value;n["response-format"]="markup";n.type=q[p].name;t=that.processQuickSearchQueryResponse}}}n.menu=m.elements.menu.value;n["menu-settings-column-nonce"]=m.elements["menu-settings-column-nonce"].value;d.post(ajaxurl,n,function(w){t.call(that,w,n);m.className=m.className.replace(/processing/g,"")});return false},processAddMenuItemResponse:function(l,m){d(l).hideAdvancedMenuItemFields().appendTo(g);d("#custom-menu-item-name").val("").blur();d("#custom-menu-item-url").val("http://")},processQuickSearchQueryResponse:function(p,u){if(!u){u={}}var m=document.createElement("ul"),l=document.getElementById("nav-menu-meta"),q,t,n,v,r,s=new RegExp("menu-item\\[([^\\]]*)"),o;n=s.exec(p);if(n&&n[1]){r=n[1];while(l.elements["menu-item["+r+"][menu-item-type]"]){r--}if(r!=n[1]){p=p.replace(new RegExp("menu-item\\["+n[1]+"\\]","g"),"menu-item["+r+"]")}}m.innerHTML=p;t=m.getElementsByTagName("li");if(t[0]&&u.object_type){o=document.getElementById(u.object_type+"-search-checklist");if(o){o.appendChild(t[0])}}else{if(u.type){n=/quick-search-(posttype|taxonomy)-([a-zA-Z_-]*)/.exec(u.type);if(n&&n[2]){o=document.getElementById(n[2]+"-search-checklist");if(o){q=t.length;if(!q){v=document.createElement("li");v.appendChild(document.createTextNode(navMenuL10n.noResultsFound));o.appendChild(v)}while(q--){o.appendChild(t[q])}}}}}},removeMenuItem:function(m){m=d(m);var l=m.childMenuItems();m.addClass("deleting").fadeOut(350,function(){m.remove();l.shiftDepthClass(-1).updateParentMenuItemDBId();a()})}}};var wpNavMenu=new WPNavMenuHandler(jQuery);jQuery(function(){wpNavMenu.init()});