(function(){zul.menu.Menubar=zk.$extends(zul.Widget,{_orient:"horizontal",_bodyScrollLeft:0,$define:{orient:function(){this.rerender()},scrollable:function(a){this.checkScrollable()&&this.rerender()},autodrop:null},setWidth:function(){this.$supers("setWidth",arguments);this._checkScrolling()},domClass_:function(a){var b=this.$supers("domClass_",arguments);a&&a.zclass||(b+=" "+this.$s(this.isVertical()?"vertical":"horizontal"));return b},unbind_:function(){if(this.checkScrollable()){var a=this.$n("left"),
b=this.$n("right");a&&b&&this.domUnlisten_(a,"onClick","_doScroll").domUnlisten_(b,"onClick","_doScroll");zWatch.unlisten({onSize:this})}a=this.$n();a.removeEventListener("mouseleave",this.proxy(this._doMouseLeave));a.removeEventListener("mouseenter",this.proxy(this._doMouseEnter));this._lastTarget=null;this.$supers(zul.menu.Menubar,"unbind_",arguments)},bind_:function(){this.$supers(zul.menu.Menubar,"bind_",arguments);var a=this.$n();a.addEventListener("mouseenter",this.proxy(this._doMouseEnter));
a.addEventListener("mouseleave",this.proxy(this._doMouseLeave));if(this.checkScrollable()){a=this.$n("left");var b=this.$n("right");a&&b&&this.domListen_(a,"onClick","_doScroll").domListen_(b,"onClick","_doScroll");zWatch.listen({onSize:this})}},checkScrollable:function(){return this._scrollable&&!this.isVertical()},onSize:function(){this._checkScrolling()},onChildAdded_:function(a){this.$supers("onChildAdded_",arguments);this._checkScrolling()},onChildRemoved_:function(a){this.$supers("onChildRemoved_",
arguments);this.childReplacing_||this._checkScrolling()},_checkScrolling:function(){if(this.checkScrollable()){var a=this.$n();if(a){jq(a).addClass(this.$s("scroll"));for(var b=zk(a).contentWidth(),c=this.$n("body"),d=jq(this.$n("cave")).children().filter(":visible"),e=d.length,g=0,f=e;f--;)g+=jq(d[f]).outerWidth(!0);zk.ie&&(g+=e);g>=b?this._scrolling=!0:(this._scrolling=!1,this._fixBodyScrollLeft(0),c.style.width="");this._fixButtonPos(a);a=b-zk(this.$n("left")).offsetWidth()-zk(this.$n("right")).offsetWidth();
this._scrolling&&(c.style.width=jq.px0(a),this._fixScrollPos(d.last()[0]))}}},_fixScrollPos:function(a){a&&(a=a.offsetLeft,a<this._bodyScrollLeft&&this._fixBodyScrollLeft(a))},_fixButtonPos:function(a){var b=this.$n("body"),c=this.$n("left"),d=this.$n("right"),e=this._scrolling?"addClass":"removeClass";c.style.display=d.style.display=this._scrolling?"block":"none";jq(a)[e](this.$s("scroll"));b.style.marginLeft=this._scrolling?jq.px(c.offsetWidth):"0";b.style.marginRight=this._scrolling?jq.px(d.offsetWidth):
"0"},_forceStyle:function(a,b){0>zk.parseInt(b)||(a.style.width=b)},_doMouseEnter:function(a){this._bOver=!0;this._noFloatUp=!1},_doMouseLeave:function(a){this._bOver=!1;this._closeOnOut()},_doScroll:function(a){this._scroll(a.domTarget==this.$n("left")||a.domTarget.parentNode==this.$n("left")?"left":"right")},_fixBodyScrollLeft:function(a){this.$n("body").scrollLeft=this._bodyScrollLeft=a},_scroll:function(a){if(this.checkScrollable()&&!this._runId){var b=this,c=this.$n("body"),d=this._bodyScrollLeft,
e=jq(this.$n("cave")).children().filter(":visible"),g=e.length,f=0;if(g)switch(a){case "left":for(a=0;a<g;a++)if(e[a].offsetLeft>=d||e[a].offsetLeft+(e[a].offsetWidth-c.offsetWidth)>=d){d=e[a].previousSibling;if(!d)return;f=d.offsetLeft;if(isNaN(f))return;b._runId=setInterval(function(){b._moveTo(c,f)||b._afterMove()},10);return}break;case "right":var h=d+c.offsetWidth;for(a=0;a<g;a++){var k=e[a].offsetLeft+e[a].offsetWidth;if(k>h){f=d+(k-h);if(isNaN(f))break;b._runId=setInterval(function(){b._moveTo(c,
f)||b._afterMove()},10);break}}}}},_moveTo:function(a,b){var c=this._bodyScrollLeft;if(c==b)return!1;a=c>b?-5:5;c+=a;if(c<b&&0>a||c>b&&0<a)c=b;this._fixBodyScrollLeft(c);return!0},_afterMove:function(){clearInterval(this._runId);this._runId=null},insertChildHTML_:function(a,b,c){var d=this.isVertical();b?jq(b.$n("chdextr")||b.$n()).before(this.encloseChildHTML_({child:a,vertical:d})):jq(this.$n("cave")).append(this.encloseChildHTML_({child:a,vertical:d}));a.bind(c)},removeChildHTML_:function(a){this.$supers("removeChildHTML_",
arguments);jq(a.$n("chdextr")).remove()},encloseChildHTML_:function(a){var b=a.out||new zk.Buffer;a.child.redraw(b);if(!a.out)return b.join("")},_closeOnOut:function(){var a=this;a._autodrop&&!zul.Widget.getOpenTooltip()&&setTimeout(function(){a._noFloatUp||a._bOver||!zul.menu._nOpen||zWatch.fire("onFloatUp",a)},200)},isVertical:function(){return"vertical"==this.getOrient()},doKeyDown_:function(a){var b=0,c=this.isVertical(),d=a.target;switch(a.key){case "ArrowLeft":c||(b=-1);break;case "ArrowUp":c&&
(b=-1);break;case "ArrowRight":c||(b=1);break;case "ArrowDown":c&&(b=1)}b&&d&&((b=0>b?this._getPrevVisibleMenuTarget(d):this._getNextVisibleMenuTarget(d))&&b.focus(),a.stop());this.$supers("doKeyDown_",arguments)},_getPrevVisibleMenuTarget:function(a){a=a.previousSibling;a||(a=this.lastChild);return a?this._prevVisibleMenu(a):null},_getNextVisibleMenuTarget:function(a){a=a.nextSibling;a||(a=this.firstChild);return a?this._nextVisibleMenu(a):null},_nextVisibleMenu:function(a){for(var b=a;b;b=b.nextSibling)if(this.$class._isActiveItem(b))return b;
return this.firstChild==a?a:this._nextVisibleMenu(this.firstChild)},_prevVisibleMenu:function(a){for(var b=a;b;b=b.previousSibling)if(this.$class._isActiveItem(b))return b;return this.lastChild==a?a:this._prevVisibleMenu(this.lastChild)}},{_isActiveItem:function(a){return a.isVisible()&&(a.$instanceof(zul.menu.Menu)||a.$instanceof(zul.menu.Menuitem))&&!a.isDisabled()}})})();
