(function(){"use strict";u8.Base.Widgets.RadioButton=function(t,e){this.Widget(t,$.extend({},u8.Base.Widgets.RadioButton.defaults,e));this._create();var i=this.checked;if(typeof i!="undefined"){delete this.checked;this.withoutEvents(this.setChecked.bind(this,i))}};u8.Base.Widgets.RadioButton.makeClass({name:"RadioButton",deriveFrom:u8.Base.Widgets.Widget,events:["GroupValueChange","CheckedChange"]});u8.Base.Widgets.RadioButton.defaults={label:"",groupName:null};$.extend(u8.Base.Widgets.RadioButton.prototype,{getChecked:function(){return this._radio&&this._radio.prop("checked")||this.checked||false},setChecked:function(t){if(typeof t=="string"){t=u8.services.typeConverter.toBoolean(t)}t=!!t;if(this._radio.is(":checked")==t){return this}this._radio.prop("checked",t);this._updateCheckedStates();return this},getGroupValue:function(){var t=this._getGroup();return t&&t.value},setGroupValue:function(t){if(!this._isCreated){this.checked=this.value==t}else{var e=this.$("input[name='"+this.groupName+"'][value='"+t+"']");e.prop("checked",true)}this._updateCheckedStates();return this},getDisabled:function(){if(this._radio){return!!this._radio.attr("disabled")}return this.disabled},setDisabled:function(t){if(!this._radio){this.disabled=t}else if(t){this._radio.attr("disabled","disabled")}else{this._radio.removeAttr("disabled")}return this},update:function(t){if(t.reason=="attr"){if(t.properties.label&&this._radio){this.$("~ label",this._radio).remove();this._radio.attr("title",this.label).after("<label for='"+this.id+"_radio'>"+this.label+"</label>");if(this.showOnlyLabel){this.$("~ label",this._radio).css("width",this.labelWidth||"auto")}}else if(t.properties.groupName&&this._radio){this._create()}}return this},dispose:function(){this._removeFromGroup();this.Widget_dispose()},_create:function(){this.$(this.element).empty();if(this.horizontal){this.element.style["float"]="left"}this._removeFromGroup();this._getGroup();this._widgetContainer=this.$('<div style="display: table;" />').attr("class",this.showOnlyLabel?"u8-radiobutton-hidden":"u8-radiobutton").appendTo(this.element);var t=u8.services.texts.isTextId(this.label);var e=this.id+"_radio";this._radio=this.$('<input type="radio"/>').attr({id:e,name:this.groupName,tabIndex:0}).appendTo(this._widgetContainer);if(this.value){this._radio.attr("value",this.value)}if(!this.designer){this._radio.focus(this._onFocus.bind(this)).change(this._onChange.bind(this)).on("click",this._onClick.bind(this)).on("keydown",this._onKeyDown.bind(this))}else{this._radio.attr("readonly","readonly")}if(this.label){var i=this.$("<label/>").attr("for",e).text(t?"":this.label).appendTo(this._widgetContainer);if(this.showOnlyLabel){i.css("width",this.labelWidth||"auto")}}if(typeof this.disabled!="undefined"){this.setDisabled(this.disabled)}this._getGroup();this._isCreated=true;if(t&&!this.designer){var s=this,a=this.label;u8.using({texts:[a],usingOptions:{synchronousIfPossible:true}},function(){s.attr({label:u8.services.texts.get(a)})})}},_onChange:function(){if(this.getChecked()){this._updateCheckedStates()}},_onClick:function(t){this.$(t.target).focus()},_onFocus:function(){if(this.getChecked()){this._updateCheckedStates()}},_onKeyDown:function(t){if(t.keyCode!=9){return}var e;if(t.shiftKey){e=this.$(t.target).parents("[data-widget='RadioButton']").prev("[data-widget='RadioButton']")}else{e=this.$(t.target).parents("[data-widget='RadioButton']").next("[data-widget='RadioButton']")}if(e.length){t.preventDefault();e.find("input").focus()}},_getGroup:function(t){if(!this.groupName){return undefined}var e=this.$(this.element).closest("[data-group='"+this.groupName+"']");if(!e.length){e=this.$(this.element.ownerDocument)}var i="radiogroup-"+this.groupName;var s=e.data(i);if(!s&&t!==false){s={items:{}};e.data(i,s)}if(s&&typeof s.items[this.id]=="undefined"){s.items[this.id]=this.getChecked()}return s},_removeFromGroup:function(){var t=this._getGroup(false);if(!t){return}delete t.items[this.id];if(!u8.tools.hasOwnProperties(t.items)){this._removeGroup()}},_removeGroup:function(){var t=this.$(this.element).closest("[data-group='"+this.groupName+"']");if(!t.length){t=this.$(this.element.ownerDocument)}t.removeData("radiogroup-"+this.groupName)},_updateCheckedStates:function(){var t=this._getGroup();if(!t){return this}var e=$w(this.element);for(var i in t.items){var s=e.$widget(i);if(!s){continue}var a=t.items[i];var o=s.getChecked();if(o!=a){s.raise("CheckedChange");t.items[i]=o}if(o&&t.value!=s.value){t.value=s.value;s.raise("GroupValueChange")}}return this}})})();