(function(){"use strict";u8.Crm.Widgets.CalendarMonthView=function(e,t){this.Widget(e,$.extend({},u8.Crm.Widgets.CalendarMonthView.defaults,t));this._isCreated=false;this._create()};u8.Crm.Widgets.CalendarMonthView.makeClass({name:"CalendarMonthView",deriveFrom:u8.Base.Widgets.Widget,events:["Create"]});u8.Crm.Widgets.CalendarMonthView.defaults={item:{colorType:"type"}};$.extend(u8.Crm.Widgets.CalendarMonthView.prototype,{dispose:function(){if(this._isDisposed){return}u8.Base.Widgets.Tools.unregisterFromResizeOnWindowResize(this);this.model.unbind("Update",this.id);this.page.unbind("IsActiveChange",this.id);this.$(this.element).unbind();this.Widget_dispose()},update:function(e){if(e=="layout"){this.updateLayout()}else{this._draw()}},updateLayout:function(){var e=this.$(this.element).children(".u8-calendar-month");var t=this.$("#"+this.id+"_content");var i=e.children(".top");u8.Base.Dom.layout(e,{name:"dock"});u8.Base.Dom.layout(t,{name:"grid"});var n=t.children(".row:eq(0)").outerWidth();var a=e.width();i.css("right",Math.max(0,a-n))},_create:function(){if(!this.model){$error("model must not be null")}this.model.bind("Update",this._onModelUpdate.bind(this),this.id);Function.chain(Function.chain.onDisposedAbort,this._using,this._draw).call(this,this._onCreated)},_using:function(e){u8.using({texts:["net_weekdays","net_calendar"],templates:[{name:"u8-calendar-month-view",url:"~/crm/CalendarMonthView.utml.htm"}]},e)},_onCreated:function(){this.page.bind("IsActiveChange",function(e,t){this._draw()}.bind(this),this.id);u8.Base.Widgets.Tools.registerForResizeOnWindowResize(this,this._draw.bind(this,null).despam(500));this._createPrintStyle();u8.tools.created(this)},_createPrintStyle:function(){var e=["@page { size: landscape }"].join("");this.model.setPrintStyle(e)},_draw:function(e){Function.chain(Function.chain.onNextOrDisposedError("_drawed"),this._drawCleanUp.async(),this._createViewModel.async(),this._createMonthView).call(this,e)},_drawCleanUp:function(){this.children.clear()},_createViewModel:function(){this._viewModel={id:this.id,captionDays:[{caption:$t("TXT_net_calendar_15")}],weeks:[]};var e,t;for(t=0;t<7;t++){var i=(t+this.model.weekStart)%7;this._viewModel.captionDays.push({caption:Date.getDayName(i)})}var n=this.model.range&&this.model.range.start||this.model.days&&this.model.days[0];var a=n.getFirstDayOfMonth().getFirstDayOfWeek(this.model.weekStart);for(e=0;e<6;e++){this._viewModel.weeks.push({days:[],model:this.model,colorType:this.item.colorType,weekNumber:a.getWeekOfYear(),externalDropDataTypes:this.externalDropDataTypes});for(t=0;t<7;t++){var s="";if(!a.monthEquals(n)){s+="u8-calendar-month-other-month "}this._viewModel.weeks[e].days.push({caption:a.getDate(),dayClass:s,date:a.clone()});a.addDays(1)}}},_createMonthView:function(e){this.empty();this.$(this.element).unbind().utml({name:"u8-calendar-month-view",itemOptions:{parent:this},data:this._viewModel,inserter:function(e,t){e.innerHTML=t;this.updateLayout()}.bind(this)},e)},_onModelUpdate:function(e,t){if(this._isDisposed){return}var i=this["_onModel"+t.reason.toPascalCase()];if(i){i.apply(this,arguments)}if(t&&t.reason==="loadItems"){this._draw(null)}},_onModelRange:function(){this._draw()}})})();