// E2cs alpha  0.0.6 - Beta 0.0.1 
// Extjs-Event-Calendar Solution 
// calendar.js
// Author: Carlos Mendez
// E2CS - is licensed under the terms of  the Open Source LGPL 3.0 license.  
// Commercial use is permitted to the extent
// that the code/component(s) do NOT become part of another Open Source or Commercially
// licensed development library or toolkit without explicit permission.
// 
// License details: http://www.gnu.org/licenses/lgpl.html
//  ------------------------------------------------------------------
//	NOTE: this widget its not stable for the moment to use on a production application (on your risk), 
//  only test and learning for the moments 
//  ------------------------------------------------------------------
//	Credits 
//  e2cs.dateParts and dateDiff taken from DOJO toolkit  
//	(im not stealing code so i give the proper credits)
//  ------------------------------------------------------------------
//  Thanks  to: 
//  ------------------------------------------------------------------
//	Jack and all the Extjs staff: for making a great framework with some unexplored power yet...!  
//  Saki: cause of his tutorials and samples and replies on forum i could make this new widget 
//	and learn a lot of new things , thanks Saki..!
//
//  LOG
//	27-Sep-2008
//  -----------------------------------------------------------------
//  Polish locale file added thanks to Remy 
//  Changed style on Week (main) Header 
//	Changed Style on Hour / minute display 
//	bug fixes on task.js file (see file for more details)
//	week view fixed and plain style working now (see file for more details)
//	month view fix minor bugs (see file for more details)
//	day view 	some minor bugs and some changes (see file for more details)
//	General calendar WIP: 
//		1.- working on Slate theme for the calendar 
//		2.- Working on default forms for capture/edit events 
//		3.- google style for dayview 
//  -----------------------------------------------------------------
//  04-08-2008 
//  -----------------------------------------------------------------
//	user manual revision 0.0.1 first release on english only
//  new property added (tplTaskTip) for making a template for qtip on events tasks 
//  monthth view, day view and week view fixes (see on each file for changes with tag or text 0.0.4 ) 
//	tasks.js modified due to new property 
//  elocale files modified  (need change on localization files  chinese, portugesem german and french)
//  E2CS blog Web site almost ready for launch need some tutorials or samples(in the making) 
//  ------------------------------------------------------------------
//  To do 
//  ------------------------------------------------------------------
//  Developer manual / Documentation for All the classes (PDF) 
//  General 
//	- Clean up the code it's a big mess
// 	- toolbar fixes for beheavior and display 
// 	Week View
//	In development for the moment  
//  Utils 
//  - Exporters (server side files) for gmail and outlook (file generation) 


Ext.namespace('e2cs.dateParts');
e2cs.dateParts={ YEAR: 0,  MONTH: 1,  DAY: 2, HOUR: 3,  MINUTE: 4, SECOND: 5,    MILLISECOND: 6,	 QUARTER: 7, 	WEEK: 8, 	WEEKDAY: 9};

Ext.ECalendar = Ext.extend(Ext.Panel,{		   
	id:'e-calendar',
	title:'Calendar',
	tools: [
        {
          id:'help',
          handler: function(btn){
            helppopup.loadHelpFile("Calendar", 500, 455);
          }  
        }
    ], 
	mytitle: '',
	html:null, 
	showCal_tbar:true,
	showRefreshbtn:false,  // ** added on alpha 0.0.2
	refreshAction:'view',  // 0.0.11    'view', 'data'
	// currentView  :  'month', 'week' or 'day'
	currentView: 'month',
	currentdate: new Date(),
	currentcategory: '',
	dateSelector: false,
	dateSelectorIcon: '',
	// dateSelectorIconCls: 'selector', removed on alpha 0.0.4 
	dateformat :'d-m-Y',
	categorySelector: false,
	categoryFieldsRefer:{
		id:'',
		category:''
	},
	categoryStore: null,
	//binding controls 
	//same control type to refresh and keep  in the same frequence
	fieldsRefer:{	//0.0.11
		id:'',
		subject:'',
		description:'', 
		color:'',
		startdate:'',
		enddate:'',
		priority:'',
		parent:''
	},
	storeOrderby:'', 		//0.0.11 
	storeOrder:'ASC',		//0.0.11 
	widgetsBind: { 
		bindMonth:null,	
		bindDay:null,
		binWeek:null
	},
	//added on 0.0.4 
	tplTaskTip : new Ext.XTemplate( 
		'<tpl for=".">{starxl}{startval}<br>{endxl}{endval}<hr>{details}</tpl>'
    ),
	tplTaskZoom: new Ext.XTemplate( 
		'<tpl for=".">',
		'<div class="ecal-show-basetasktpl-div">Tarea:{subject}<br>',
		'Inicia:{startdate}<br>Termina:{enddate}<br>Descripcion:<br>{description}<div><hr>',
		'</tpl>'
	),		
	monitorBrowserresize:false,  //0.0.7 beta 0.0.2 
	//Today button config
	iconToday:'',
	// month view config 
	mview: null,
	iconMonthView:'',
	// week view config 
	wview: null,
	iconWeekView:'',
	// day view config 
	dview: null, 
	iconDayView:'',
	
	sview:null,
	iconSchedView:'',

	//store
	store: null, 
	// private use 
	viewmonth:null,
	viewday	 :null, 
	viewweek :null,
	viewscheduler:null, //0.0.10 
	viewready:false,  //0.0.7
	//functions 
	initComponent:function() {
		this.addEvents(
		   'beforeRefresh',
		   'onRefresh',

		   'beforeChangeView',
		   'onChangeView',
		   
		   'beforeChangeDate',
		   'afterChangeDate',
		   
		   'beforeChangeCategory',
		   'afterChangeCategory',
		   
		   'customMenuAction',
		   'taskAdd',
		   'taskDblClick',

		   'beforeTaskMove',
		   'TaskMoved',

		   'beforeTaskDelete',
		   'onTaskDelete',
		   'afterTaskDelete',

		   'beforeTaskEdit',
		   'onTaskEdit',
		   'afterTaskEdit'
		);	
		//'taskDeleted', 0.0.4  removed  not use at all with the other events
		//'taskChanged'  0.0.4  removed not yet an implementation 
		
		if (this.html!=null) { this.html =null; } 

		toolspanel=[]; 
		this.btnrefresh	= {	id:'refresh',tooltip:'Actualizar contenido'	};
		if (this.showRefreshbtn){toolspanel.push(this.btnrefresh); 	} 
		this.btn_today ={
			id: this.id + '-btn_settoday', 
			cls: 'x-btn-text-icon', 
			text: e2cs.cal_locale.todayLabel,
			icon: this.iconToday, 
			tooltip: e2cs.cal_locale.todayToolTip
		};
		
		this.btn_monthviewchange = { 
			id: this.id + '-btn_monthview', 
			iconCls: 'x-btn-icon',	
			text: 'Month view', 
			icon: this.iconMonthView, 
			tooltip: e2cs.cal_locale.tooltipMonthView 
		};
		
		this.btn_weekviewchange =  { 
			id: this.id + '-btn_weekview',	 
			iconCls: 'x-btn-icon',	
			text: 'Week view', 
			icon: this.iconWeekView,	
			tooltip: e2cs.cal_locale.tooltipWeekView  
		};
		
		this.btn_dayviewchange =   { 
			id: this.id + '-btn_dayview',	 
			iconCls: 'x-btn-icon',	
			text: 'Day view', 
			icon: this.iconDayView, 	
			tooltip: e2cs.cal_locale.tooltipDayView   
		};		
		this.btn_schedviewchange = {
			id: this.id + '-btn_sched_view',	 
			iconCls: 'x-btn-icon',	
			text: '',
			icon: this.iconSchedView, 	
			tooltip: e2cs.cal_locale.tooltipSchedView   
		};
		if (this.showCal_tbar){ 
			this.tbar_calendar= new Ext.Toolbar({	
				id:  this.id +'-cmscalendartoolbar', 
				autoWidth: true, 
				autoHeight:false,	
				items:[ this.btn_today ,'-', this.btn_dayviewchange, this.btn_weekviewchange, this.btn_monthviewchange, this.btn_schedviewchange,'-']  
			});	
		} else { 
			this.tbar_calendar=null; 
		} 
		this.selector_dateMenu  = new Ext.menu.DateMenu({ defaultAlign:'tr-br',subMenuAlign :''});
		if ( this.width){  var_autoWidth = false; } else { 	var_autoWidth = true;	} 
		if ( this.height){ var_autoHeight = false;} else {  var_autoHeight = true;	} 
		Ext.apply(this,{
			header: this.header,
			headerAsText: true, 
			title: this.title + this.mytitle,
			border:true,
			width:   this.width, 		//common width for all views 
			height:  this.height,  		//used on week view and day view
			monitorResize:true, 
			autoShow   :true, 
			autoWidth  :var_autoWidth,		    // default :( 
			autoHeight :var_autoHeight,			// used on month view
			autoScroll :false,  		// used on dayview
			html:this.html,    			// avoid other data displayed 
			tools: toolspanel,
			tbar:this.tbar_calendar
        });
		Ext.ECalendar.superclass.initComponent.call(this); 
		if (this.mview){ 
			this.viewmonth = this.getViewMonth();
      newDate = new Date();
	    this.viewmonth.selectedDate = newDate.format('m/d/Y'); 
			this.viewmonth.init(this,this.currentdate);		
		}
		if (this.dview ){ 
			this.viewday = this.getViewDay();
			this.viewday.init(this,this.currentdate);		
		} 
		if (this.wview){
			this.viewweek = this.getViewWeek();
			this.viewweek.init(this,this.currentdate);		
		} 
		if (this.sview){
			this.viewscheduler = this.getViewShedule();
			this.viewscheduler.init(this,this.currentdate);		
		} 
		

	}, // end of function initComponent
	// Override other inherited methods 
	onResize: function(){
		Ext.ECalendar.superclass.onResize.apply(this, arguments);
		this.doLayout();
		if (this.viewready){  //0.0.7
			if (this.currentView=='month'){  this.viewmonth.render(); 	} 
			if (this.currentView=='week') {  this.viewweek.render();  	} 
			if (this.currentView=='day')  {	 this.viewday.render(); 	} 
		} 
	}, 
    onRender: function(){
        Ext.ECalendar.superclass.onRender.apply(this,arguments);	 
    },
	afterRender: function(){
		Ext.ECalendar.superclass.afterRender.call(this);
		//0.0.11 Sort the order of the store if this.storeOrderby is specified 
		if (this.storeOrderby==''){ } else { this.store.sort(this.storeOrderby, this.storeOrder); 	} 		
		
		if (this.showCal_tbar){
			var btntoday = this.topToolbar.items.items[0]; 
			btntoday.setHandler( this.setCurrentDate, this );
			var btnmonth =  this.topToolbar.items.items[4]; 
			var btnweek  =  this.topToolbar.items.items[3];
			var btnday   =  this.topToolbar.items.items[2];
			var btnsched =  this.topToolbar.items.items[5]; // 0.0.10 
			
			if (!this.mview){ btnmonth.setVisible(false);  } else { btnmonth.setVisible(true);	}
			if (!this.dview){ btnday.setVisible(false);    } else { btnday.setVisible(true);	}
			if (!this.wview){ btnweek.setVisible(false);   } else { btnweek.setVisible(true);	}
			if (!this.sview){ btnsched.setVisible(false);  } else { btnsched.setVisible(true);	} //0.0.10
			
			btnmonth.addListener('click',  function(){   this.changeView('month'); 		}  , this); 
			btnday.addListener('click',    function(){   this.changeView('day');   		}  , this); 
			btnweek.addListener('click',   function(){   this.changeView('week');  		}  , this); 
			btnsched.addListener('click',  function(){   this.changeView('schedule');  	}  , this); 
			
		}
		if (this.header && this.tools.refresh){ // 0.0.10 Bug fix Thanks to Remy 
			if 	(this.refreshAction=='view'){   // 0.0.11    'view', 'data'
					this.tools.refresh.addListener('click', this.refreshCalendarView, this );
			} else { 
					this.tools.refresh.addListener('click', function(){
					    this.store.reload(); 
					}, this );
			} 
		} 
		if (this.dateSelector && this.showCal_tbar){
			this.selector_dateMenu.picker.todayText = e2cs.cal_locale.todayLabel;
			this.selector_dateMenu.picker.todayTip  = e2cs.cal_locale.todayToolTip;
			this.selector_dateMenu.picker.monthNames= e2cs.cal_locale.monthtitles;
			this.selector_dateMenu.picker.dayNames  = e2cs.cal_locale.daytitles;
			this.selector_dateMenu.addListener('select', this.selectdatefromSelector , this); 
			//this.tbar_calendar.addFill() ;
			this.btn_selector  = {
					id: this.id + '-btn_dateselector',		
					cls: 'x-btn-text-icon',	
					text: 'Select date',
					icon: this.dateSelectorIcon,
					tooltip: e2cs.cal_locale.dateSelectorTooltip,
					menu: this.selector_dateMenu
			};	
			this.tbar_calendar.addButton(this.btn_selector);
		}
		if (this.categorySelector && this.showCal_tbar){

	/*		this.cmb_categoryviewchange = new Ext.form.ComboBox({
			    id: this.id + '-cmb_category_view',
			    store: this.categoryStore,
			    displayField: 'category',
			    valueField: 'id',			    
			    typeAhead: true,			    			    
			    triggerAction: 'all',
			    emptyText: e2cs.cal_locale.categorySelectorText,
			    tooltip: e2cs.cal_locale.categorySelectorTooltip,
			    selectOnFocus:true,
			    width: 100,
			    listWidth: 120			    
		        });		        
		        this.cmb_categoryviewchange.addListener('select', this.selectcategoryfromSelector, this);
		        this.cmb_categoryviewchange.addListener('render', this.rendercategory, this);
		        this.tbar_calendar.add('-'); 
			this.tbar_calendar.addField(this.cmb_categoryviewchange);    */
		}
		if (this.ownerCt==undefined){
			if (this.currentView=='month'){  	this.viewmonth.render(); 	} 
			if (this.currentView=='week') {  	this.viewweek.render(); 	} 
			if (this.currentView=='day')  {  	this.viewday.render(); 		} 
			if (this.currentView=='schedule'){  this.viewscheduler.render();}	 //0.0.10
		} else { 
			if (this.currentView=='month'){  	this.viewmonth.render(); 	} 
			if (this.currentView=='week') {  	this.viewweek.render(); 	} 
			if (this.currentView=='day')  {  	this.viewday.render(); 		} 
			if (this.currentView=='schedule'){  this.viewscheduler.render();}	 //0.0.10
		} 
		this.doLayout();
		this.viewready = true; //0.0.7 
		tmpobj = this;
		if (this.monitorBrowserResize){  //0.0.7 
			Ext.EventManager.onWindowResize( function(){ //console.log('change...inside calendar');
				tmpobj.refreshCalendarView(); 
			});
		} 
	},
	refreshCalendarView: function(btn){
		if (this.rendered){ 
			//0.0.11 Sort the order of the store if this.storeOrderby is specified 
			if (this.storeOrderby==''){ } else { this.store.sort(this.storeOrderby, this.storeOrder); 	} 	
			if (this.currentView=='month'){  	this.viewmonth.render(); 		} 
			if (this.currentView=='week') {  	this.viewweek.render(); 		} 
			if (this.currentView=='day')  {	 	this.viewday.render(); 			} 
			if (this.currentView=='schedule'){  this.viewscheduler.render(); 	}	 //0.0.10   
			this.doLayout();
			this.fireEvent("onRefresh",this);
		}	
	}, 
	setNewDate:function(newdate){ 
		if ( this.fireEvent("beforeChangeDate", newdate , this)==false ) { return false; } 
		//0.0.11 Sort the order of the store if this.storeOrderby is specified 
		if (this.storeOrderby==''){ } else { this.store.sort(this.storeOrderby, this.storeOrder); 	} 
		newDate = new Date(newdate)
    this.viewmonth.selectedDate = newDate.format('m/d/Y');
    this.currentdate = newDate; 	
		this.selector_dateMenu.picker.setValue(this.currentdate);
		if (this.currentView=='month'){ 	this.viewmonth.render(); 	} 
		if (this.currentView=='week') { 	this.viewweek.render(); 	} 
		if (this.currentView=='day')  {	 	this.viewday.render(); 	}  
		if (this.currentView =='schedule'){ this.viewscheduler.render(); 	} //0.0.10   
		this.doLayout();
		this.fireEvent("afterChangeDate", this.currentdate , this);
	} ,	 
	setCurrentDate: function(){
    this.viewmonth.selectedDate = (new Date()).format('m/d/Y');
    
		this.setNewDate(Date());
	},
	setNewCategory:function(newcategory){
		if ( this.fireEvent("beforeChangeCategory", newcategory , this)==false ) { return false; }  		
		//0.0.11 Sort the order of the store if this.storeOrderby is specified 
		if (this.storeOrderby==''){ } else { this.store.sort(this.storeOrderby, this.storeOrder); 	} 
		this.currentcategory = newcategory; 			
		if (this.currentView=='month'){ 	this.viewmonth.render(); 	} 
		if (this.currentView=='week') { 	this.viewweek.render(); 	} 
		if (this.currentView=='day')  {	 	this.viewday.render(); 	}  
		if (this.currentView =='schedule'){ this.viewscheduler.render(); 	} //0.0.10   
		this.doLayout();
		this.fireEvent("afterChangeCategory", this.currentcategory , this);		
	} ,
	changeView: function(datastr, opdate){
    newView  = datastr;
		oldView  = this.currentView; 
		if ( this.fireEvent("beforeChangeView", newView , this.currentView, this)==false ) { return false; } 
		//if (oldView=='schedule' && newView !='schedule'){ }//0.0.10  not implemented yet //remove period selector if any 
		if(datastr=='month'){		
			this.currentView='month';  	  
			this.viewmonth.render();
		} else if (datastr=='week'){
			this.currentView='week';  	  
			this.viewweek.render(); 	
		} else if (datastr=='day') {
			 this.currentView='day'; 	  
			 this.viewday.render(); 	
		} else if (datastr =='schedule'){ 
			 this.currentView='schedule'; 
			 this.viewscheduler.render(); 	
		} else {
			return false; 
		}
		this.fireEvent("onChangeView", newView, oldView, this); 
	}, 
	//--------------------------------------------------------------
	//private ------------------------------------------------------
	//--------------------------------------------------------------	
	selectdatefromSelector: function(dp, dateval){ 
		this.setNewDate(dateval);
	},
	selectcategoryfromSelector: function( combo, record, index ) {
		this.setNewCategory(record.data.id);
	},
	rendercategory: function() {
	    var tt = new Ext.ToolTip({
		target: this.cmb_categoryviewchange.id,		
		width: 104,
		html: e2cs.cal_locale.categorySelectorTooltip		
	    });
	},
	getCurrentDate: function(){ //0.0.11 
		return this.currentdate; 
	},	
	getViewMonth:function(){  
		if(!this.viewmonth){ this.viewmonth = new Ext.ECalendar.monthview(this.mview); }
        return this.viewmonth;
	}, 
	getViewDay: function(){
		if(!this.viewday){   this.viewday = new Ext.ECalendar.dayview(this.dview); }
        return this.viewday;
	},
	getViewWeek: function(){
		if(!this.viewweek){  this.viewweek = new Ext.ECalendar.weekview(this.wview);}
        return this.viewweek;
	},	
	getViewShedule: function(){ //0.0.10 new View (O.O) 
		if(!this.viewscheduler){  this.viewscheduler = new Ext.ECalendar.scheduleview(this.sview);}
        return this.viewscheduler;
	}, 
	//------------------------------------------------------------------
	//------------------------------------------------------------------
	//------------------------------------------------------------------
	// Util functions  
	// -----------------------------------------------------------------
	//goNextMonth: function(){   alert("Mes siguiente"); 	}, removed for alpha 0.0.4
	//goPrevMonth: function(){   alert("Mes Anterior"); 	}, removed for alpha 0.0.4
	getCalendarMonth:	function(){			return ( this.currentdate.getMonth() + 1); 		}, 
	getCalendarYear: 	function(){ 		return ( this.currentdate.getFullYear() ) ;  	}, 
	getCalendarDay:		function(){  		return ( this.currentdate.getUTCDate() ) ; 		},
	getCalendayWeekDay: function(dataformat){ 
		if (dataformat=='str') { 
			return  Date.dayNames[this.currentdate.getDay()]; 
		} else { 
			return ( this.currentdate.getDay() ) ; 
		} 
	},
	//-------------------------------------------------------------------------------
	//getDateRangeOfWeek - Utility Date tool  new //0.0.10   also this give us the hope of week view to set the view with ChangebyWeek(weekno)  
	//-------------------------------------------------------------------------------
	getDateRangeOfWeek: function(weekNo){
		var d1 = new Date();
		numOfdaysPastSinceLastMonday = eval(d1.getDay()- 1);
		d1.setDate(d1.getDate() - numOfdaysPastSinceLastMonday);
		var weekNoToday = d1.getWeekOfYear();
		var weeksInTheFuture = eval( weekNo - weekNoToday );
		d1.setDate(d1.getDate() + eval( 7 * weeksInTheFuture ));
		var rangeIsFrom = new Date(    (d1.getMonth()+1) +"/" + d1.getDate() + "/" + d1.getFullYear() );
		d1.setDate(d1.getDate() + 6);
		var rangeIsTo = new Date (     (d1.getMonth()+1) +"/" + d1.getDate() + "/" + d1.getFullYear() );
		return [rangeIsFrom , rangeIsTo] ;
	},
	//-------------------------------------------------------------------------------
	// Dojo Date function -  altered  only the part of (fleegix.date.util.dateParts)
	//-------------------------------------------------------------------------------
	dateDiff: function (date1, date2, interv) {
	// date1  	// Date object or Number equivalent
	// date2	// Date object or Number equivalent
	// interval	// A constant representing the interval, e.g. YEAR, MONTH, DAY. See fleegix.date.util.dateParts.
	// Accept timestamp input
	  if (typeof date1 == 'number') { date1 = new Date(date1); }
	  if (typeof date2 == 'number') { date2 = new Date(date2); }
	  if  (date1.format('m/d/Y H:i:s') == date2.format('m/d/Y H:i:s') ) { return 0;  } 
	  var yeaDiff = date2.getFullYear() - date1.getFullYear();
	  var monDiff = (date2.getMonth() - date1.getMonth()) + (yeaDiff * 12);
	  var msDiff = date2.getTime() - date1.getTime(); // Millisecs
	  var secDiff = msDiff/1000;
	  var minDiff = secDiff/60;
	  var houDiff = minDiff/60;
	  var dayDiff = houDiff/24;
	  var weeDiff = dayDiff/7;
	  var delta = 0; // Integer return value
	  with (e2cs.dateParts) {
		switch (interv) {
		  case YEAR:
			delta = yeaDiff;
			break;
		  case QUARTER:
			var m1 = date1.getMonth();
			var m2 = date2.getMonth();
			// Figure out which quarter the months are in
			var q1 = Math.floor(m1/3) + 1;
			var q2 = Math.floor(m2/3) + 1;
			// Add quarters for any year difference between the dates
			q2 += (yeaDiff * 4);
			delta = q2 - q1;
			break;
		  case MONTH:
			delta = monDiff;
			break;
		  case WEEK:
			// Truncate instead of rounding
			// Don't use Math.floor -- value may be negative
			delta = parseInt(weeDiff);
			break;
		  case DAY:
			delta = dayDiff;
			break;
		  case WEEKDAY:
			var days = Math.round(dayDiff);
			var weeks = parseInt(days/7);
			var mod = days % 7;
	 
			// Even number of weeks
			if(mod == 0){
			  days = weeks*5;
			}else{
			  // Weeks plus spare change (< 7 days)
			  var adj = 0;
			  var aDay = date1.getDay();
			  var bDay = date2.getDay();
			  weeks = parseInt(days/7);
			  mod = days % 7;
			  // Mark the date advanced by the number of
			  // round weeks (may be zero)
			  var dtMark = new Date(date1);
			  dtMark.setDate(dtMark.getDate()+(weeks*7));
			  var dayMark = dtMark.getDay();
			  // Spare change days -- 6 or less
			  if(dayDiff > 0){
				switch(true){
				  // Range starts on Sat
				  case aDay == 6:
					adj = -1;
					break;
				  // Range starts on Sun
				  case aDay == 0:
					adj = 0;
					break;
				  // Range ends on Sat
				  case bDay == 6:
					adj = -1;
					break;
				  // Range ends on Sun
				  case bDay == 0:
					adj = -2;
					break;
				  // Range contains weekend
				  case (dayMark + mod) > 5:
					adj = -2;
					break;
				  default:
					// Do nothing
					break;
				}
			  }else if(dayDiff < 0){
				switch (true) {
				  // Range starts on Sat
				  case aDay == 6:
					adj = 0;
					break;
				  // Range starts on Sun
				  case aDay == 0:
					adj = 1;
					break;
				  // Range ends on Sat
				  case bDay == 6:
					adj = 2;
					break;
				  // Range ends on Sun
				  case bDay == 0:
					adj = 1;
					break;
				  // Range contains weekend
				  case (dayMark + mod) < 0:
					adj = 2;
					break;
				  default:// Do nothing
					break;
				}
			  }
			  days += adj;
			  days -= (weeks*2);
			}
			delta = days;
	 
			break;
		  case HOUR:
			delta = houDiff;
			break;
		  case MINUTE:
			delta = minDiff;
			break;
		  case SECOND:
			delta = secDiff;
	
			break;
		  case MILLISECOND:
			delta = msDiff;
			break;
		  default:// Do nothing
			break;
		}
	  }
	  return Math.round(delta); // Number (integer) // Round for fractional values and DST leaps
	}
});
// ** added on alpha 0.0.2
Ext.reg('e2cs_calendar',Ext.ECalendar); 
