(function(){u8.Crm.Widgets.CalendarTimelineView=function(e,t){this.Widget(e,$.extend({},u8.Crm.Widgets.CalendarTimelineView.defaults,t));this._window=$w(e);this._itemsCreated=false;this._selectedItem=null;this._create()};u8.Crm.Widgets.CalendarTimelineView.makeClass({name:"CalendarTimelineView",deriveFrom:u8.Base.Widgets.Widget,events:["Create","Action","SelectedItemChange"]});u8.Crm.Widgets.CalendarTimelineView.defaults={monthRange:12};$.extend(u8.Crm.Widgets.CalendarTimelineView.prototype,{dispose:function(){this.model.unbind("Update",this.id);this.$(this.element).unbind()},_create:function(){if(!this.model){$error("Cannot create the view without a model")}this.model.bind("Update",this._onModelUpdate.bind(this),this.id);this.model.loadItems()},_createTimeline:function(){this._createViewModel();var e=this.$(this.element);e.unbind();e.hide();e.utml({url:"~/crm/calendartimeline.utml.htm",itemOptions:{parent:this},data:this.viewModel},function(){e.on("click",this._onClick.bind(this));this._layoutHtml()}.bind(this))},_createViewModel:function(){this.viewModel={id:this.id,dateUnits:[],items:[]};var e=[];var t;for(t=this.model.range.start.toDate();t<=this.model.range.end;t=t.clone().addMonths(1,1)){this.viewModel.dateUnits.push({caption:u8.services.typeConverter.dateToString(t)});e.push(t)}e.push(t);var i=this.model.items.length;var n=this.model.range.end.diffDays(this.model.range.start);for(var s=0;s<i;s++){if(!this.model.items[s].visible){continue}var a=this.model.items[s];this.viewModel.items.push({id:a.id,caption:a.caption,cssClass:u8.Crm.CalendarItem.getCssClass(2,a)});var d=this.viewModel.items.peek();var o=0;var l=100;if(a.start<this.model.range.start){d.caption="<< "+d.caption}else{var r=a.start.diffDays(this.model.range.start);o=Math.round(r/n*100);if(o>=100){o=99}}if(a.end>this.model.range.end){d.caption+=" >>";l-=o}else{var m=this.model.range.end.diffDays(a.end);l=l-Math.round(m/n*100)-o;if(l==0){l=1}}d.left=o+"%";d.width=l+"%"}},_layoutHtml:function(){this.$(this.element).show();u8.Base.Dom.layout(this.element,{name:"dock",fixed:true});u8.Base.Dom.layout(this._window.$el(this.id+"_content"),{name:"dock"});u8.Base.Dom.layout(this._window.$el(this.id+"_contentHeader"),{name:"dock"});u8.Base.Dom.layout(this._window.$el(this.id+"_contentHeaderCaption"),{name:"grid"});u8.Base.Dom.layout(this._window.$el(this.id+"_contentContainer"),{name:"dock"})},_onModelUpdate:function(e,t){var i={loadItems:this._createTimeline.bind(this),range:function(){this.model.loadItems()}.bind(this)};if(i[t.reason]){i[t.reason](t)}},_onClick:function(e){var t=this.$(e.target);var i=t.data();if(i&&i.action){var n=this._window.$el(this.id+"_menu");if(!n){return}if(this._menu){this._menu.dispose();this._menu=null}var s=this.model.getItem(i.id);var a=s.menuName;if(!a&&s.uid.infoAreaId){a="Default:"+s.uid.infoAreaId}else if(!a){return}var d=new u8.Base.Widgets.MenuModel({menuName:a});this._menu=new u8.Base.Widgets.ContextMenu(n,{parent:this,model:d,alignTo:e.target,executeOptions:{arguments:{uid:s.uid}}});this._menu.show();e.preventDefault();e.stopPropagation()}}})})();