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,d=0,c,e=this.$n();this.clearSelection();for(c=this.firstChild;c&&b<a;c=c.nextSibling,b++)c.$instanceof(zul.sel.Option)?c.isVisible()||d++:b--;a-=d;e&&(e.selectedIndex=
a);-1<a&&c&&c.$instanceof(zul.sel.Option)&&(c.setSelected(!0),this._selItems.push(c))},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={},d=0;;){var c=a.indexOf(",",d);(d=(0<=c?a.substring(d,c):a.substring(d)).trim())&&(b[d]=!0);if(0>c)break;d=c+1}for(a=this.firstChild;a;a=a.nextSibling)this._changeSelect(a,1==b[a.uuid])},_changeSelect:function(a,b){var d=
!!a.isSelected()!=b;d&&a.setSelected(b);return d},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())if(this._multiple){var b=a.getOptionIndex_?a.getOptionIndex_():a.getChildIndex();if(b<this._selectedIndex||
0>this._selectedIndex)this._selectedIndex=b;a._setSelectedDirectly(!0);this._selItems.push(a)}else this.selectItem(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){var b=[],d=this.$n();if(this._multiple){d=d.options;for(var c,e=0,k=d.length;e<k;++e){var g=d[e],h=zk.Widget.$(g.id);
a=g.selected;h&&h._selected!=a&&(h.setSelected(a),c=!0);if(a&&(b.push(g.id),!f))var f=g.id}if(!c)return}else{a=d.selectedIndex;zk.opera&&(d.selectedIndex=a);f=0>a?a:-1;c=this.firstChild;for(e=a;c&&-1<e;c=c.nextSibling)c.$instanceof(zul.sel.Option)&&(c.isVisible()&&e--,f++);if(this._selectedIndex==f)return;this.setSelectedIndex(f);b.push(f=d.options[a].id)}this.fire("onSelect",{items:b,reference:f})},doBlur_:zk.ie8_?function(a){this._doChange(a);return this.$supers("doBlur_",arguments)}:zk.$void,beforeCtrlKeys_:function(a){this._doChange(a)},
onChildAdded_:function(a){a.$instanceof(zul.sel.Optgroup)&&this._groupsInfo.push(a);this.requestRerender_(!0)},onChildRemoved_:function(a){a.$instanceof(zul.sel.Optgroup)&&this._groupsInfo.$remove(a);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()}});
