(function(){"use strict";u8.Crm.Widgets.WeekdaysSelector=function(e,s){this.Widget(e,$.extend({},u8.Crm.Widgets.WeekdaysSelector.defaults,s));this.editMode=s.mode==="Edit";this._create();var a=this.value;if(typeof a!="undefined"){delete this.value;this.withoutEvents(this.setValue,a)}};u8.Crm.Widgets.WeekdaysSelector.defaults={daysValues:{monday:1,tuesday:2,wednesday:4,thursday:8,friday:16,saturday:32,sunday:64}};$.extend(u8.Crm.Widgets.WeekdaysSelector.prototype,{dispose:function(){if(this._isDisposed){return}this._checkboxes=null;this.Widget_dispose()},_create:function(){this.children.clear();delete this._widgets;this.$(this.element).addClass("u8-weekdays-selector");var e=this.element;var s=e.ownerDocument;var a=s.createElement("div");a.style.marginLeft=5;e.style.whiteSpace="nowrap";e.appendChild(a);this._widgets=[];if(this.editMode){for(var t=0;t<7;t++){var i=s.createElement("div");a.appendChild(i);this._widgets[t]=u8.widget("CheckBox",i,$.extend(this.comboBox,{parent:this,label:Date.getDayName(t===6?0:t+1),onValueChange:this._onCheckBoxValueChange.bind(this)}))}}else{var d=s.createElement("div");a.appendChild(d);this._widgets[0]=u8.widget("Label",d,$.extend(this.label,{parent:this,text:""}))}this._isCreated=true},_onCheckBoxValueChange:function(){this.setValue(this._calculateValueFromCheckboxes())},_calculateValueFromCheckboxes:function(){var e=0;for(var s=0;s<7;s++){if(this._widgets[s].getValue()===true){switch(s){case 0:e|=this.daysValues.monday;break;case 1:e|=this.daysValues.tuesday;break;case 2:e|=this.daysValues.wednesday;break;case 3:e|=this.daysValues.thursday;break;case 4:e|=this.daysValues.friday;break;case 5:e|=this.daysValues.saturday;break;case 6:e|=this.daysValues.sunday;break}}}return e},getValue:function(){return this.editMode?this._calculateValueFromCheckboxes():this._value},setValue:function(e){if(!e){e=0}if(!this._isCreated){this.value=e;return}if(this._value===e){return}this._value=e;this._updateView();this.raise("ValueChange",{value:e})},_setCheckboxesStates:function(){var e=this._value;var s=false;for(var a=0;a<7;a++){var t=this._widgets[a];switch(a){case 0:s=(e&this.daysValues.monday)===this.daysValues.monday;break;case 1:s=(e&this.daysValues.tuesday)===this.daysValues.tuesday;break;case 2:s=(e&this.daysValues.wednesday)===this.daysValues.wednesday;break;case 3:s=(e&this.daysValues.thursday)===this.daysValues.thursday;break;case 4:s=(e&this.daysValues.friday)===this.daysValues.friday;break;case 5:s=(e&this.daysValues.saturday)===this.daysValues.saturday;break;case 6:s=(e&this.daysValues.sunday)===this.daysValues.sunday;break}t.withoutEvents(t.setValue,s)}},_setLabelText:function(){var e="";var s=this._value;if(s!==0){var a=[];if(s&this.daysValues.monday){a.push(Date.getDayName(1))}if(s&this.daysValues.tuesday){a.push(Date.getDayName(2))}if(s&this.daysValues.wednesday){a.push(Date.getDayName(3))}if(s&this.daysValues.thursday){a.push(Date.getDayName(4))}if(s&this.daysValues.friday){a.push(Date.getDayName(5))}if(s&this.daysValues.saturday){a.push(Date.getDayName(6))}if(s&this.daysValues.sunday){a.push(Date.getDayName(0))}e=a.join(", ")}this._widgets[0].setText(e)},_updateView:function(){if(this.editMode){this._setCheckboxesStates()}else{this._setLabelText()}}});u8.Crm.Widgets.WeekdaysSelector.makeClass({name:"WeekdaysSelector",deriveFrom:u8.Base.Widgets.Widget,events:["ValueChange"]})})();