zul.tab.Tabbox=zk.$extends(zul.Widget,{_orient:"top",_tabscroll:!0,_maximalHeight:!1,_animating:!1,$define:{tabscroll:_zkf=function(){this.rerender()},orient:function(a){"horizontal"==a?this._orient="top":"vertical"==a&&(this._orient="left");this.rerender()},maximalHeight:_zkf,panelSpacing:_zkf},getTabs:function(){return this.tabs},getTabpanels:function(){return this.tabpanels},getToolbar:function(){return this.toolbar},domClass_:function(a){var b=this.$supers("domClass_",arguments);if(!a||!a.zclass){var c=
this.inAccordionMold()?this.$s("accordion"):this.$s(this.getOrient());b+=" "+c}return b},isHorizontal:function(){var a=this.getOrient();return"horizontal"==a||"top"==a||"bottom"==a},isTop:function(){var a=this.getOrient();return"horizontal"==a||"top"==a},isBottom:function(){return"bottom"==this.getOrient()},isVertical:function(){var a=this.getOrient();return"vertical"==a||"left"==a||"right"==a},isRight:function(){return"right"==this.getOrient()},isLeft:function(){var a=this.getOrient();return"vertical"==
a||"left"==a},inAccordionMold:function(){return-1!=this.getMold().indexOf("accordion")},getSelectedIndex:function(){return this._selTab?this._selTab.getIndex():-1},setSelectedIndex:function(a){this.tabs&&this.setSelectedTab(this.tabs.getChildAt(a))},getSelectedPanel:function(){return this._selTab?this._selTab.getLinkedPanel():null},setSelectedPanel:function(a){a&&a.getTabbox()!=this||(a=a.getLinkedTab())&&this.setSelectedTab(a)},getSelectedTab:function(){return this._selTab},setSelectedTab:function(a,
b){this._selTab!=a&&(this._setSel(a,!b),this._selTab=a)},bind_:function(a,b,c){this.$supers(zul.tab.Tabbox,"bind_",arguments);this._scrolling=!1;var d=this.getToolbar();this.inAccordionMold()?zWatch.listen({onResponse:this}):d&&this.getTabs()&&(zWatch.listen({onResponse:this}),this._toolbarWidth=jq(d.$n()).width());var f;d=["right","left","down","up"];for(var e=d.length;e--;)(f=this.$n(d[e]))&&this.domListen_(f,"onClick","_doClick",d[e]);this._fixMaxHeight();zk.afterMount(this.proxy(function(){var g=
this.tabs,h=this._selTab;h&&g&&(this.isVertical()?g._scrollcheck("vsel",h):this.inAccordionMold()||g._scrollcheck("sel",h))}))},unbind_:function(){zWatch.unlisten({onResponse:this});for(var a,b=["right","left","down","up"],c=b.length;c--;)(a=this.$n(b[c]))&&this.domUnlisten_(a,"onClick","_doClick",b[c]);this._toolbarWidth=null;this.$supers(zul.tab.Tabbox,"unbind_",arguments)},_doClick:function(a,b){if(this.tabs&&this.tabs.nChildren){a=this.tabs.$n("cave");var c=jq(a).children(),d=this.tabs.$n(),f=
this.isVertical();a=f?this.tabs._tabsScrollTop:this.tabs._tabsScrollLeft;d=f?d.offsetHeight:d.offsetWidth;f=a+d;switch(b){case "right":b=0;for(var e=c.length;b<e;b++)if(c[b].offsetLeft+c[b].offsetWidth>f){a=c[b].offsetLeft+c[b].offsetWidth-a-d;if(!a||isNaN(a))return;this.tabs._doScroll("right",a);return}break;case "left":b=0;for(e=c.length;b<e;b++)if(c[b].offsetLeft>=a){b=jq(c[b]).prev("li")[0];if(!b)return;a-=b.offsetLeft;if(isNaN(a))return;this.tabs._doScroll("left",a);return}a-=c[c.length-1].offsetLeft;
if(isNaN(a))break;this.tabs._doScroll("left",a);break;case "up":b=0;for(e=c.length;b<e;b++)if(c[b].offsetTop>=a){b=jq(c[b]).prev("li")[0];if(!b)return;a-=b.offsetTop;this.tabs._doScroll("up",a);return}b=c[c.length-1];if(!b)break;a-=b.offsetTop;this.tabs._doScroll("up",a);break;case "down":for(b=0,e=c.length;b<e;b++)if(c[b].offsetTop+c[b].offsetHeight>f){a=c[b].offsetTop+c[b].offsetHeight-a-d;if(!a||isNaN(a))break;this.tabs._doScroll("down",a);break}}}},syncSize:function(){this._shallSize=!1;this.desktop&&
zUtl.fireSized(this,-1)},onResponse:function(){if(this.inAccordionMold())this._shallSize&&this.syncSize();else if(this._toolbarWidth){var a=jq(this.getToolbar().$n()).width();a!=this._toolbarWidth&&(this._toolbarWidth=a,this.getTabs().onSize())}},_syncSize:function(){this.desktop&&(this._shallSize=!0)},onChildAdded_:function(a){this.$supers("onChildAdded_",arguments);a.$instanceof(zul.wgt.Toolbar)?this.toolbar=a:a.$instanceof(zul.tab.Tabs)?this.tabs=a:a.$instanceof(zul.tab.Tabpanels)&&(this.tabpanels=
a);this.rerender()},onChildRemoved_:function(a){this.$supers("onChildRemoved_",arguments);a==this.toolbar?this.toolbar=null:a==this.tabs?this.tabs=null:a==this.tabpanels&&(this.tabpanels=null);this.childReplacing_||this.rerender()},setWidth:function(a){this.$supers("setWidth",arguments);this.desktop&&zUtl.fireSized(this,-1)},setHeight:function(a){this.$supers("setHeight",arguments);this.desktop&&zUtl.fireSized(this,-1)},_fixMaxHeight:function(){if(this._maximalHeight){for(var a=0,b=this.getTabpanels().firstChild,
c=b;c;c=c.nextSibling){var d=c?c.getCaveNode():null;if(d)d=jq(d).outerHeight(),d>a&&(a=d);else return}for(c=b;c;c=c.nextSibling)if(d=c.getCaveNode())d.style.height=jq.px0(a)}},_setSel:function(a,b){if(a){var c=this._selTab;if(c!=a){if(c&&this.inAccordionMold()){var d=a.getLinkedPanel();d&&d._changeSel(c.getLinkedPanel())}c&&c!=a&&c._sel(!1,!1);a._sel(!0,b)}}}});
