(function(){function k(a){a=a.parent;return a.$instanceof(zul.menu.Menu)?a:null}function l(a){for(;a&&(a=k(a));a=a.parent)if(a.isTopmost())return a;return null}function f(a){return a.isVisible()&&(a.$instanceof(zul.menu.Menu)||a.$instanceof(zul.menu.Menuitem))&&!a.isDisabled()}function n(a){var c=a._curIndex;if(0<=c){a=a.firstChild;for(var b=0;a;a=a.nextSibling)if(f(a)&&b++==c)return a}}function r(a){var c=a.menupopup;c&&(c._shallClose=!1,c.isOpen()||c.open());a.$class._addActive(a);zWatch.fire("onFloatUp",
a)}zul.menu.Menupopup=zk.$extends(zul.wgt.Popup,{_curIndex:-1,_keepOpen:!1,zsync:function(){this.$supers("zsync",arguments);this._shadow||(this._shadow=new zk.eff.Shadow(this.$n()));this._shadow.sync()},_hideShadow:function(){this._shadow&&this._shadow.hide()},_syncPos:function(){var a;if(a=k(this)){var c=this.$n(),b=a.$n(),d=jq(c),e=jq(b),g=d.offset().left,f=e.offset().left;d=d.outerWidth();e=e.outerWidth();var m=a.parent,p=a.getMenubar();p=p?p.getOrient():"";var l=!1;var h=-1;a.isTopmost()&&"horizontal"==
p&&c&&(c.style.top=jq.px0(zk.parseInt(c.style.top)));for(;m&&!m.$instanceof(zul.menu.Menupopup);)m=m.parent;if(m)if(m._reverseDirection){a=f-d;var n=0>a;a=n?f+e:a;var q=a+d;h=jq.innerX()+jq.innerWidth();h=q>h?h-d:a;this._reverseDirection=!n}else l=!0;!m&&zk(c).isOverlapped(b,1)&&(5<f+e-g&&"vertical"!=p||g<f+e/2&&"vertical"==p)&&(l=!0);l&&(a=f+e,q=a+d,h=jq.innerX()+jq.innerWidth(),b=q>h,h=Math.max(b?f-d:a,0),this._reverseDirection=b);if(0<=h&&(c.style.left=jq.px0(h),zk.ie))this.onShow()}},close:function(){this.isOpen()&&
zul.menu._nOpen--;this.$supers("close",arguments);jq(this.$n()).hide();this._hideShadow();var a;(a=k(this))&&a.isTopmost()&&jq(a.$n()).removeClass(a.$s("selected"));(a=n(this))&&a.$class._rmActive(a);this._curIndex=-1;this.$class._rmActive(this)},open:function(a,c,b,d){this.isOpen()||zul.menu._nOpen++;var e;(e=k(this))&&!c&&(a=e.getAnchor_(),b||(b=e.isTopmost()?"vertical"==e.parent.getOrient()?"end_before":"after_start":"end_before"));this.$super("open",a,c,b,d||{sendOnOpen:!0,disableMask:!0});this._syncPos()},
shallStackup_:function(){return!1},setTopmost:function(){this.$supers("setTopmost",arguments);this.zsync()},onFloatUp:function(a,c){if(this.isVisible()){var b=this._openInfo;this._shallToggle&&b&&c&&(void 0===c.triggerByClick||b[3].which==c.triggerByClick&&zUtl.isAncestor(this._openInfo[0],a.origin))||this._doFloatUp(a)}},_doFloatUp:function(a){if(this.isVisible()){a=a.origin;if(this.parent.menupopup!=this||this.parent.isTopmost()||this.parent.$class._isActive(this.parent)){for(var c,b=a;b;b=b.parent){if(b==
this||b.menupopup==this&&!this._shallClose){c||this.setTopmost();return}c=c||b.isFloating_()}if(a&&a.ignoreDescendantFloatUp_(this))for(b=this;b=b.parent;){if(b==a){if(this._shallClose)break;c||this.setTopmost();return}c=c||b.isFloating_()}}this.close({sendOnOpen:!0})}},onShow:function(){this.zsync();var a=this.$n("a");a&&zk(a).isRealVisible()&&(a.focus(),zk.currentFocus=this);zk(this).redoCSS(-1,{fixFontIcon:!0})},onHide:function(){this.isOpen()&&this.close();this._hideShadow()},bind_:function(){this.$supers(zul.menu.Menupopup,
"bind_",arguments);zWatch.listen({onHide:this,onResponse:this});var a=this.$n();this.domListen_(a,"onMouseEnter").domListen_(a,"onMouseLeave");if(!zk.css3)jq.onzsync(this)},unbind_:function(){this.isOpen()&&this.close();this._shadow&&this._shadow.destroy();zk.css3||jq.unzsync(this);this._shadow=null;zWatch.unlisten({onHide:this,onResponse:this});var a=this.$n();this.domUnlisten_(a,"onMouseEnter").domUnlisten_(a,"onMouseLeave");this.$supers(zul.menu.Menupopup,"unbind_",arguments)},onResponse:function(){this.isOpen()&&
(this.zsync(),this.$supers("onResponse",arguments),this._syncPos())},doKeyDown_:function(a){var c;var b=n(this);var d,e=a.keyCode;switch(e){case 38:case 40:b&&b.$class._rmActive(b);if(38==e)a:{if(b)for(;b=b.previousSibling;)if(f(b)){this._curIndex--;break a}this._curIndex=-1;for(c=this.firstChild;c;c=c.nextSibling)f(c)&&(b=c,this._curIndex++)}else a:{if(b)for(;b=b.nextSibling;)if(f(b)){this._curIndex++;break a}for(b=this.firstChild;b;b=b.nextSibling)if(f(b)){this._curIndex=0;break a}b=void 0}b&&b.$class._addActive(b);
break;case 37:if(b&&b.$instanceof(zul.menu.Menu)&&b._contentHandler&&b._contentHandler.isOpen())b._contentHandler.onHide();else if((d=k(this))&&!d.isTopmost()){if(this.close(),d.$class._addActive(d),c=d.parent){(b=c.$n("a"))&&b.focus();b=c;a:{var g=-1;for(c=c.firstChild;c;c=c.nextSibling)if(f(c)&&g++,c==d){c=g;break a}c=g}b._curIndex=c}}else(b=l(this))&&(b=b._getPrevVisibleMenu())?r(b):this.close();break;case 39:b&&b.$instanceof(zul.menu.Menu)&&!b.isDisabled()?b._togglePopup():(b=l(this))&&(b=b._getNextVisibleMenu())&&
r(b);break;case 13:if(b&&b.$instanceof(zul.menu.Menuitem))b.doClick_(new zk.Event(b,"onClick",{})),zWatch.fire("onFloatUp",b),this.close({sendOnOpen:!0});else if(b&&b.$instanceof(zul.menu.Menu))b._togglePopup();else if(d=k(this))this.close(),d.isTopmost()?d.focus():(d.$class._addActive(d),(c=d.parent)&&(b=c.$n("a"))&&b.focus());break;case 27:if(d=k(this))if(d.isTopmost())this.close(),d.focus();else if(d._contentHandler&&d._contentHandler.isOpen())content.onHide();else this.close(),d.$class._addActive(d),
(c=d.parent)&&(b=c.$n("a"))&&b.focus();break;case 9:(b=l(this))&&b.focus_(void 0,11>zk.ie),this.close()}9!=e&&27!=e&&a.stop();this.$supers("doKeyDown_",arguments)},getMenubar:function(){for(var a=this.parent;a;){if(a.$instanceof(zul.menu.Menubar))return a;if(a.$instanceof(zul.menu.Menu))return a.getMenubar();break}return null},_doMouseEnter:function(a){if(a=this.getMenubar())a._bOver=!0;this._shallClose=!1},_doMouseLeave:function(a){if(a=this.getMenubar())a._bOver=!1,a._autodrop&&a._closeOnOut()},
setActive:function(a){if(0<=a&&a<this.nChildren){for(var c=-1,b=this.firstChild,d=0,e=0;b;b=b.nextSibling,d++){var g=f(b);if(a===d&&g){c=e;break}g&&e++}0<=c&&((b=n(this))&&b.$class._rmActive(b),this._curIndex=c,(a=this.getChildAt(a))&&a.$class._addActive(a))}}},{_rmActive:function(a){a.parent.$instanceof(zul.menu.Menu)&&a.parent.$class._rmActive(a.parent)}});zul.menu._nOpen=0})();
