zul.sel.Select=zk.$extends(zul.Widget,{_selectedIndex:-1,_rows:0,$init:function(){this.$supers("$init",arguments);this._selItems=[];this._groupsInfo=[]},$define:{multiple:function(a){var b=this.$n();b&&(b.multiple=a?"multiple":"")},disabled:function(a){var b=this.$n();b&&(b.disabled=a?"disabled":"")},selectedIndex:function(a){var b=0,c=0,d,e=this.$n();this.clearSelection();for(d=this.firstChild;d&&b<a;d=d.nextSibling,b++)d.$instanceof(zul.sel.Option)?d.isVisible()||c++:d.$instanceof(zul.sel.Optgroup)?
c++:b--;a-=c;e&&(e.selectedIndex=a);-1<a&&d&&d.$instanceof(zul.sel.Option)&&(d.setSelected(!0),this._selItems.push(d))},name:function(a){var b=this.$n();b&&(b.name=a)},rows:function(a){var b=this.$n();b&&(b.size=a)},maxlength:function(a,b){this.desktop&&this.requestRerender_(b)}},setChgSel:function(a){for(var b={},c=0;;){var d=a.indexOf(",",c);(c=(0<=d?a.substring(c,d):a.substring(c)).trim())&&(b[c]=!0);if(0>d)break;c=d+1}for(a=this.firstChild;a;a=a.nextSibling)this._changeSelect(a,1==b[a.uuid])},
_changeSelect:function(a,b){var c=!!a.isSelected()!=b;c&&a.setSelected(b);return c},toggleItemSelection:function(a){a.isSelected()?this._removeItemFromSelection(a):this._addItemToSelection(a)},selectItem:function(a){if(!a)this.setSelectedIndex(-1);else if(this._multiple||!a.isSelected())a.getOptionIndex_?this.setSelectedIndex(a.getOptionIndex_()):this.setSelectedIndex(a.getChildIndex())},_addItemToSelection:function(a){if(!a.isSelected()){var b=this._multiple;b||this.clearSelection();var c=a.getOptionIndex_?
a.getOptionIndex_():a.getChildIndex();if(!b||c<this._selectedIndex||0>this._selectedIndex)this._selectedIndex=c;a._setSelectedDirectly(!0);this._selItems.push(a)}},_removeItemFromSelection:function(a){a.isSelected()&&(this._multiple?(a._setSelectedDirectly(!1),this._selItems.$remove(a)):this.clearSelection())},clearSelection:function(){if(this._selItems.length){for(var a;a=this._selItems.pop();)a._setSelectedDirectly(!1);this._selectedIndex=-1}},domAttrs_:function(){var a;return this.$supers("domAttrs_",
arguments)+(this.isDisabled()?' disabled="disabled"':"")+(this.isMultiple()?' multiple="multiple"':"")+(-1<(a=this.getSelectedIndex())?' selectedIndex="'+a+'"':"")+(0<(a=this.getRows())?' size="'+a+'"':"")+((a=this.getName())?' name="'+a+'"':"")},bind_:function(){this.$supers(zul.sel.Select,"bind_",arguments);var a=this.$n();this.domListen_(a,"onChange").domListen_(a,"onFocus","doFocus_").domListen_(a,"onBlur","doBlur_");zk.gecko||(a=[this,this._fixSelIndex],zWatch.listen({onRestore:a,onVParent:a}));
zWatch.listen({onCommandReady:this});this._fixSelIndex()},unbind_:function(){zWatch.unlisten({onCommandReady:this});var a=this.$n();this.domUnlisten_(a,"onChange").domUnlisten_(a,"onFocus","doFocus_").domUnlisten_(a,"onBlur","doBlur_").$supers(zul.sel.Select,"unbind_",arguments);a=[this,this._fixSelIndex];zWatch.unlisten({onRestore:a,onVParent:a})},_fixSelIndex:function(){0>this._selectedIndex&&(this.$n().selectedIndex=-1)},_doChange:function(a){a=this.$n().options;for(var b=this._multiple,c=[],d=
!1,e,k=0,l=a.length;k<l;++k){var f=a[k],g=zk.Widget.$(f.id),h=f.selected;if(b)g&&g._selected!=h&&(this.toggleItemSelection(g),d=!0),h&&(c.push(f.id),e||(e=f.id));else if(g&&g._selected!=h&&h){this._addItemToSelection(g);d=!0;c.push(f.id);e=f.id;break}}d&&this.fire("onSelect",{items:c,reference:e})},doBlur_:zk.$void,beforeCtrlKeys_:function(a){this._doChange(a)},onChildAdded_:function(a){a.$instanceof(zul.sel.Optgroup)&&this._groupsInfo.push(a);this.desktop&&this.requestRerender_(!0)},onChildRemoved_:function(a){a.$instanceof(zul.sel.Optgroup)&&
this._groupsInfo.$remove(a);this.desktop&&!this.childReplacing_&&this.requestRerender_(!0)},requestRerender_:function(a){a?this._shouldRerenderFlag=!0:this.rerender()},onCommandReady:function(){this._shouldRerenderFlag&&(this._shouldRerenderFlag=!1,this.rerender())},hasGroup:function(){return this._groupsInfo.length},getGroupCount:function(){return this._groupsInfo.length},getGroups:function(){return this._groupsInfo.$clone()},setItemsInvalid_:function(a){var b=this;zAu.createWidgets(a,function(c){b.replaceCavedChildren_("",
c)},function(c){return c})}});
