// E2cs alpha  0.0.11
// Extjs-Event-Calendar Solution 
// dayview.js
// Author: Carlos Mendez
// Contact: cmendez21@gmail.com   (gmail and gtalk) 
//  Dayview LOG ------------------------------------------------------------------------------------------
//  Works fine on firefox 2.0, 3.0, ie6, ie7, chrome, chrome may have some css issues as safari  too 
//  ------------------------------------------------------------------------------------------------------
//  10-Dic-2008  0.0.11
//  td .hour-marker when clicekd launches a new addevent based upen the hour when clicked , (bdetermined by taskAdd_dblclick property) 
//	Changed some issues on Menus for BODY element, added ShowMenuItems:[1,1,1,1,1,1,1,1] so you can allow which menu item appear 
//  Changed some issues on Menus for Each task/Event element, added task_ShowMenuItems:[1,1,1,1,1] so you can allow which menu item appear 
//	BUG - Fixed for Check if other views are still present or not=null and adjust the menu Items for changing view
//	Dynamic field modification on Code  very nice this one :) 
//  Added integration to Scheduler view  :)
//	27-Sep-2008
//  	Changed style on Day (main) Header 
//		Changed Style on Hour / minute display 
//		Changed hourFormat default value changed G for H 
//		New property taskAdd_dblclick: true
//			- When set to true if you click on the body of the dayview the event  'taskAdd' will be launched 
//		WIPs for next version: 
//			1.- Working for google style for day view just like google calendar (similar to week view on google style) 
//			2.- Handlers for click on the hour so you can add a new event at with the date and starting time which was clicked
//				2.1- will work with the upcoming default forms for capture/edit  events 
//  
// 		Note : the changes on styles were made in the calendar.css file 
//	08-sep-2008 
//  ------------------------------------------------------------------
//	repaired some display and render issues 
//  fix some ID conflicts when two or more calendars are  on the same page  (see //0.0.6 comment on sources) 
//  ------------------------------------------------------------------
//  17-june-2008
// 	1.- 'beforeDayChange' and 'afterDayChange'	events so you can query on the store for the records
//  2.- store  property removed, it uses the calendar store so no necesity to use another
//	3.- refactored the display and layout of the day cause issues of resize, more tasks on day 
//	(width greater than main layout)
//  4.- on fit layouts or containers goes weel everything  (firefox2) 
//	5.- addedd fix for scroll (overflow) 
//  --------------------------- CHANGE LOG ---------------------------
//  10-June-2008
//  ------------------------------------------------------------------
//  localized in lang file see locale dir for files 
//  no more need to set these properties 
//  :) 
//	headerTooltips: { prev: 'Previous day..', next: 'Next day' }, 
//	task_MoreDaysFromTask: '<br>(+)',   //used on task.js file
//	task_LessDaysFromTask: '(-)<br>',	//used on task.js file
//	contextMenuLabels: { 
//		taskAdd: "New Task", 
//		taskDelete: "Delete Task", 
//		taskEdit: "Edit Task:",
//		NextDay: "Go to Next Day", 
//		PreviousDay: "Go to Previous Day"
//	},	
//  ------------------------------------------------------------------
Ext.ECalendar.dayview = function(config) {
	Ext.apply(this, config);
	this.addEvents('beforeDayChange', // fix for request to the store specific data from only a day 
	'afterDayChange' // After the day was changed
	);
	Ext.ECalendar.dayview.superclass.constructor.call(this);
};
Ext.extend(Ext.ECalendar.dayview, Ext.util.Observable, {
	referid: 'dayview',
	//0.0.9
	header: true,
	headerFormat: 'l - F d Y',
	// according to Ext.Date object 
	headerButtons: true,
	moreMenuItems: [],
	// NOTE: this one is for the context menu on each task/event, the body will not take this one for use 
	// day specific 
	hourFormat: 'H',
	// according to Ext.Date object    //0.0.7 - beta 0.0.2  changed G for H 
	startTime: '01:00:00 am',
	// format has to be 'H:s:i a'
	endTime: '11:00:00 pm',
	// format has to be 'H:s:i a'
	// task settings 
	taskBaseColor: '#ffffff',
	useMultiColorTasks: false,
	// not implemented yet
	multiColorTasks: [],
	// not implemented yet
	task_increment: 5,
	// ** added on alpha 0.0.2
	task_width: 150,
	// ** added on alpha 0.0.2
	tasksOffset: 'auto',
	// for overlapping task  
	task_clsOver: '',
	// ** added on alpha 0.0.2  not implemented
	task_showqtip: true,
	// ** added on alpha 0.0.2
	task_format: 'n/j/y g:i a',
	// added on 0.0.4 
	taskAdd_dblclick: true,
	// added on 0.0.7 - beta 0.0.2
	taskAdd_timer_dblclick: true,
	// 0.0.11  When the user dblclick on the Thout display in the body will launch an add event with the date and time selected :) 
	task_DDeditable: true,
	// 0.0.11   
	task_eventLaunch: 'click',
	// 0.0.11  'click, dblclick, if set to '' then no action is taken 
	ShowMenuItems: [1, 1, 1, 1, 1, 1],
	// 0.0.11  ADD, next day, prev day , chg Month , CHG Week, chg Sched, (for daybody menu) 
	task_ShowMenuItems: [1, 1, 1, 1, 1],
	// 0.0.11  ADD, delete, edit, Next day , Prev Day  (for Taks/events) 
	tasks: [],
	allDayEvents: [],
	sortTasks: function(aTask, bTask) {
		aStart = new Date(aTask.task_starts);
		aEnd = new Date(aTask.task_ends);
		bStart = new Date(bTask.task_starts);
		bEnd = new Date(bTask.task_ends);
		if (aStart > bStart || (aStart >= bStart && aEnd > bEnd)) {
			return 1;
		} else if (aStart < bStart || (aStart <= bStart && aEnd < bEnd)) {
			return -1;
		} else {
			return 0;
		}
	},
	setSpans: function(columnArray) {
		for (i = 0; i < this.tasks.length; i++) {
			aTask = this.tasks[i];
			if (aTask.task_allday == '0') {
				aStart = new Date(aTask.task_starts);
				aEnd = new Date(aTask.task_ends);
				rowSpan = 0;
				intersection = false;
				nextColNum = aTask.column + 1;
				for (j = aTask.column; j < columnArray.length; j++) {
					if (j == columnArray.length - 1 && !intersection) {
						intersection = true;
						rowSpan++;
					} else {
						nextCol = columnArray[nextColNum];
						for (k = 0; k < nextCol.length; k++) {
							bTask = nextCol[k];
							bStart = new Date(bTask.task_starts);
							bEnd = new Date(bTask.task_ends);
							if ((aStart <= bStart && bStart < aEnd) || (bStart <= aStart && aStart < bEnd) || (aStart.format('m/d/Y H:i:s a') == bStart.format('m/d/Y H:i:s a'))) {
								intersection = true;
								k = nextCol.length + 1; //Exit the loop
							}
						}
						rowSpan++;
						nextColNum++;
					}
					if (intersection) {
						j = columnArray.length + 1; //Exit the loop
						aTask.rowSpan = rowSpan;
					}
				}
			}
		}
	},
	renderTasks: function() {
		allDayEvents = Ext.get('calAllDayEvents');
		if (this.allDayEvents.length != 0) {
			newChild = allDayEvents.createChild({
				tag: 'div'
			});
			newChild.setStyle({
				float: 'left'
			});
			newChild.setStyle({
				width: (allDayEvents.getWidth() - 53) + 'px'
			});
			newChild.setStyle({
				height: '20px'
			});
			for (i = 0; i < this.allDayEvents.length; i++) {
				newTask = newChild.createChild({
					tag: 'div',
					cls: 'allDayTask',
					html: this.allDayEvents[i].task_subject
				});
				if (Ext.isIE) {
					newTask.setStyle({
						height: '20px'
					});
				}
				newTask.setStyle({
					width: ((newChild.getWidth() / this.allDayEvents.length) - 6) + 'px'
				});
			}
		}
		dayBody = Ext.get(this.calx.id + '-daybody');
		for (i = 0; i < this.tasks.length; i++) {
			nextTask = this.tasks[i];
			if (nextTask.task_allday == '0') {
				startTime = new Date(nextTask.task_starts);
				endTime = new Date(nextTask.task_ends);
				tasktop = (startTime.getHours() * 60 + startTime.getMinutes());
				taskbottom = (endTime.getHours() * 60 + endTime.getMinutes());
				taskheight = taskbottom - tasktop;
				if (taskheight >= 4) {
					taskheight -= 4;
				}
				this.taskobject = dayBody.createChild({
					tag: 'div',
					cls: 'task',
					html: nextTask.task_subject
				});
				this.taskobject.dom.setAttribute('id', nextTask.calx.id + '-ecaltask-' + nextTask.task_index + '');
				this.taskobject.dom.setAttribute('ec_id', '' + nextTask.task_id + '');
				this.taskobject.dom.setAttribute('ec_starts', '' + nextTask.task_starts + '');
				this.taskobject.dom.setAttribute('ec_ends', '' + nextTask.task_ends + '');
				this.taskobject.dom.setAttribute('ec_subject', '' + nextTask.task_subject + '');
				this.taskobject.dom.setAttribute('ec_cnt', '' + nextTask.task_description + '');
				this.taskobject.dom.setAttribute('ec_allday', '' + nextTask.task_allday + '');
				this.taskobject.setStyle({
					left: nextTask.column * this.spanWidth + 'px'
				});
				this.taskobject.setStyle({
					border: '2px solid black'
				});
				this.taskobject.setStyle({
					top: '' + tasktop + 'px'
				});
				this.taskobject.setStyle({
					height: '' + taskheight + 'px'
				});
				this.taskobject.setStyle({
					margin: '0px 1px 0px 1px'
				});
				this.taskobject.setStyle({
					position: 'absolute'
				});
				this.taskobject.setStyle({
					width: '' + nextTask.task_width - 6
				});
			}
		}
	},
	setColumns: function() {
		columnArray = [];
		this.allDayEvents = [];
		for (i = 0; i < this.tasks.length; i++) {
			notAdded = true;
			aTask = this.tasks[i];
			if (aTask.task_allday == '0') {
				aStart = new Date(aTask.task_starts);
				aEnd = new Date(aTask.task_ends);
				for (j = 0; notAdded; j++) {
					if (columnArray[j] == undefined) {
						columnArray.push([]);
					}
					alreadyFilled = false;
					for (h = 0; h < columnArray[j].length; h++) {
						bTask = columnArray[j][h];
						bStart = new Date(bTask.task_starts);
						bEnd = new Date(bTask.task_ends);
						if ((bStart >= aStart && bStart < aEnd) || (bEnd > aStart && bStart <= aEnd) || (aStart.format('m/d/Y H:i:s a') == bStart.format('m/d/Y H:i:s a'))) {
							alreadyFilled = true;
							h = columnArray[j].length;
						}
					}
					if (!alreadyFilled) {
						columnArray[j].push(aTask);
						aTask.column = j;
						notAdded = false;
					}
				}
			} else {
				this.allDayEvents.push(aTask);
			}
		}
		return columnArray;
	},
	init: function(calendar, dateval) {
		this.calx = calendar;
		this.datetohandle = dateval;
	},
	refreshView: function() {
		this.render();
	},
	// Private 	
	render: function() {
		this.tasks = [];
		var updateview = Ext.get(this.calx.body);
		updateview.update('');
		var updateview = Ext.get(this.calx.body);
		var daycntbase = '<div id="' + this.calx.id + '-main-calendar-header"></div>';
		daycntbase += '<div id="' + this.calx.id + '-main-calendar-day-body"></div>';
		updateview.update(daycntbase);
		this.datetohandle = (this.calx.viewmonth.selectedDate != 'undefined') ? new Date(this.calx.viewmonth.selectedDate) : this.calx.currentdate;
		if (this.header) {
			var dt = this.datetohandle;
			var tmpheader = this.genHeader(this.datetohandle);
			var tmpheader = Ext.get(this.calx.id + '-main-calendar-header');
			var prueba2 = tmpheader;
			var myheaderwrap = prueba2.wrap({
				tag: 'div',
				cls: 'x-calendar-dayv-header',
				html: ''
			});
			if (this.headerButtons) { // 0.0.4 bug 
				var buttonDiv = myheaderwrap.createChild({
					tag: 'div',
					cls: 'dayvButtonContainer'
				});
				var prevdclick = buttonDiv.createChild({
					id: this.calx.id + '-btn-pd',
					tag: 'div',
					cls: 'x-calendar-day-previous',
					html: ''
				}); //0.0.6 added ID  // 0.0.10 bug fix thanks to PTG 	
				var nextdclick = buttonDiv.createChild({
					id: this.calx.id + '-btn-nd',
					tag: 'div',
					cls: 'x-calendar-day-next',
					html: ''
				}); //0.0.6 added ID  // 0.0.10 bug fix thanks to PTG
				prevdclick.dom['qtip'] = e2cs.cal_locale.headerTooltipsDay.prev;
				prevdclick.addListener('click', this.onclickprev_day, this);
				prevdclick.addClassOnOver('x-calendar-day-previous-over'); // 0.0.10 bug fix thanks to PTG
				nextdclick.dom['qtip'] = e2cs.cal_locale.headerTooltipsDay.next;
				nextdclick.addListener('click', this.onclicknext_day, this);
				nextdclick.addClassOnOver('x-calendar-day-next-over'); // 0.0.10 bug fix thanks to PTG
			}
			var headerdx = myheaderwrap.createChild({
				tag: 'div',
				id: 'calendarheader',
				html: '' + dt.format(this.headerFormat) + ''
			});
		}
		// creates thebody of the month --------------------------------------------
		var htmlday = this.genBody(this.datetohandle);
		var tmpdays = Ext.get(this.calx.id + '-main-calendar-day-body');
		var myheaderdayswrap = tmpdays.wrap({
			tag: 'div',
			cls: 'x-calendar-dayv-day',
			html: ''
		});
		newWrap = myheaderdayswrap.wrap({
			tag: 'div'
		});
		newChild = newWrap.createChild({
			tag: 'div',
			id: 'calAllDayEvents',
			cls: 'allDayEvents'
		}, myheaderdayswrap);
		newChild.setStyle({
			width: Ext.get('content-panel').getWidth() + 'px'
		});
		newChildTime = newChild.createChild({
			tag: 'div',
			cls: 'allDayEventsTime',
			html: 'all-day'
		});
		if (Ext.isIE) {
			newChildTime.setStyle({
				width: '51px'
			});
		}
		//newChild = myheaderdayswrap.createChild({tag: 'div', cls: 'allDayEvents'},tmpdays);
		//newChild.setStyle({width: Ext.get('content-panel').getWidth()-20 + 'px' });
		if (this.calx.ownerCt != undefined) { //0.0.6 fix for ext.component containers such as tab and others and properly draw correctly
			if (this.calx.ownerCt.ctype && this.calx.ownerCt.ctype == "Ext.Component") {
				//this.calx.height =  this.calx.ownerCt.height;  
				this.calx.height = this.calx.ownerCt.getInnerHeight(); //0.0.7  - beta 0.0.2 
			}
		}
		if (!this.calx.height || this.calx.height == 'undefined') {
			var tmpheight = this.calx.getEl().dom.offsetParent.clientHeight; //+ this.calx.getFrameHeight() ; //0.0.6
		} else {
			var tmpheight = this.calx.height;
		}
		if (this.header) {
			tmpheight += -24;
		}
		if (this.calx.showCal_tbar) {
			tmpheight += -26;
		}
		if (this.calx.header) {
			tmpheight += -26;
		}
		var morehoffst = 0;
/*
		    newHeight = Ext.get('calendar-panel').getHeight();
    newHeight -= 80;  	
		myheaderdayswrap.setStyle({height:'' +  newHeight + 'px' } );
		*/
		//if (this.calx.showCal_tbar){ var morehoffst=76;  } else { var morehoffst=24; }
		mainRegion = Ext.get('content-panel');
		newWidth = mainRegion.getWidth();
		myheaderdayswrap.setStyle({
			width: '' + newWidth + 'px'
		});
		morehoffst2 = 20;
		if (Ext.isIE) {
			morehoffst2 = 35;
		}
		myheaderdayswrap.setStyle({
			height: '' + tmpheight - morehoffst - morehoffst2 + 'px'
		});
		// ------------------------------------------------------------------		
		//0.0.3 bug posted on forum 
		//Correct the bug in IE7 when you scroll in DayView
		// ------------------------------------------------------------------
		tmpid = this.calx.id;
		myheaderdayswrap.addListener('scroll', function() {
			if (Ext.get(tmpid + '-daybody')) {
				Ext.get(tmpid + '-daybody').setStyle('filter', 'alpha(opacity=100)');
				Ext.get(tmpid + '-daybody').setStyle('filter', '');
			}
		});
		// ------------------------------------------------------------------
		var mydays = myheaderdayswrap.createChild({
			tag: 'div',
			id: this.calx.id + '-calendar-view-day',
			html: htmlday
		});
		if (Ext.isIE || Ext.isIE6) {
			Ext.get('daylayoutbody').setWidth(Ext.get('daylayoutbody').getWidth() - 20, false);
		}
		// fix for the day grid 
		var mydaybody = Ext.get(this.calx.id + '-daybody');
		// 0.0.7 - beta 0.0.2  new feature , when the user dblclicks on the boy od the day TaskAdd event launches similar to outlook 
		var dblclickdate = this.calx.currentdate;
		var tmpcalendarobjinstance = this.calx;
		var testdblclick = this.taskAdd_dblclick;
		mydaybody.addListener('dblclick', function(evx, elx, obx) {
			if (elx.id.indexOf('-daybody') < 0) {
				return false;
			}
			if (testdblclick) {
				tmpcalendarobjinstance.fireEvent("taskAdd", dblclickdate);
			}
		});
		//------------------------------------------------------------------
		//0.0.11 if this.taskAdd_dblclick then also we will handle addevents to Labels for time so when the atasks add then will be on the specific hour set :) 
		if (this.taskAdd_timer_dblclick) {
			var mytimeTD_elements = Ext.select('td.hour-marker', true);
			mytimeTD_elements.each(function(el, thisobj, index) {
				if (el.id.indexOf(tmpcalendarobjinstance.id + '-tdbody-dayv-') + 1 >= 1) {
					el.addListener('dblclick', function(evx, elx, obx) {
						if (elx.id.indexOf(tmpcalendarobjinstance.id + '-tdbody-dayv-') < 0) {
							var mydt_element_test = Ext.get(elx).parent().id.replace(tmpcalendarobjinstance.id + '-tdbody-dayv-', '');
						} else {
							var mydt_element_test = elx.id.replace(tmpcalendarobjinstance.id + '-tdbody-dayv-', '');
						}
						var mytmpdate = new Date(mydt_element_test);
						tmpcalendarobjinstance.fireEvent("taskAdd", mytmpdate);
					});
				}
			}, this);
		}
		//		tmpdatetohandleontd = dt.format('m/d/Y');
		//		tmpdatetohandleontd+= ' ' + inittime.add(Date.HOUR, (i) ).format(this.hourFormat); 
		//		tmpdatetohandleontd+= ':' + inittime.add(Date.HOUR,(i)).format('i a'); 
		//		dviewbody +='<tr><td id="' + this.calx.id + '-tdbody-dayv-' + tmpdatetohandleontd + '" class="hour-marker"><span>'+ inittime.add(Date.HOUR, (i) ).format(this.hourFormat) + '</span>'; 
		//		dviewbody +='<span class="minute">' + inittime.add(Date.HOUR,(i)).format('i a') + '</span></td></tr>'; 		
		mydaybody.setHeight(Ext.get('tdbaseday').getHeight(true));
		if (Ext.isOpera) {
			mydaybody.addListener('mousedown', this.operadaybuttons, this);
		} else { // feature posted by IOMANIP for context menu (right click) on the body of each day view 
			mydaybody.addListener('contextmenu', this.oncontextmenuBody, this, {
				stopPropagation: false,
				normalized: true,
				preventDefault: true
			});
		}
		// now process the task from the store to display in the grid of the day
		this.tasks = [];
		//check how many will be rendered before 
		var dateinionthisday = new Date(this.datetohandle.format('m/d/Y') + ' ' + this.startTime);
		var dateendonthisday = new Date(this.datetohandle.format('m/d/Y') + ' ' + this.endTime);
		var counttasks = this.calx.store.getCount();
		var countdone = 0;
		for (var i = 0; i < counttasks; i++) {
			var testrec = this.calx.store.getAt(i).data;
			testdateinit = this.calx.store.getAt(i).data[this.calx.fieldsRefer.startdate]; //0.0.11dynamic fields
			testdateend = this.calx.store.getAt(i).data[this.calx.fieldsRefer.enddate]; //0.0.11dynamic fields		
			checkdates = this.datetohandle.between(new Date(testdateinit), new Date(testdateend));
			chkformat = this.datetohandle.format('m/d/Y');
			test = new Date(testdateinit);
			if (test.format('m/d/Y') == chkformat) {
				checkdates = true;
			}
			test = new Date(testdateend);
			if (test.format('m/d/Y') == chkformat) {
				checkdates = true;
			}
			if (checkdates) {
				countdone += 1;
			}
		}
		var counttasks = countdone;
		if (counttasks > 0) {
			currentindex = 0;
			created = 0;
			previndex = 0;
			if (this.tasksOffset == 'auto') {
				var newwfix = counttasks * this.task_width;
			} else {
				var newwfix = (counttasks * this.task_width) - ((counttasks - 1) * this.tasksOffset);
			}
			//if (Ext.get('tdbaseday').getWidth(true)<newwfix){ 
			mainRegion = Ext.get('content-panel');
			newwfix = mainRegion.getWidth() - 75;
			Ext.get(this.calx.id + '-daybody').setWidth(newwfix, false);
			Ext.get('daylayoutbody').setWidth(Ext.get(this.calx.id + '-tableallday').getWidth(true) + 1, false); //0.0.6 added calendar ID
			//} 
			var test = 11;
			//}
			var counttasks = this.calx.store.getCount();
			for (var itask = 0; itask < counttasks; itask++) {
				//				dateinit 	= this.calx.store.getAt(itask).data.startdate;  
				//				dateend 	= this.calx.store.getAt(itask).data.enddate; 
				dateinit = this.calx.store.getAt(itask).data[this.calx.fieldsRefer.startdate]; //0.0.11dynamic fields	
				dateend = this.calx.store.getAt(itask).data[this.calx.fieldsRefer.enddate]; //0.0.11dynamic fields		
				checkdates = this.datetohandle.between(new Date(dateinit), new Date(dateend));
				chkformat = this.datetohandle.format('m/d/Y');
				test = new Date(dateinit);
				if (test.format('m/d/Y') == chkformat) {
					checkdates = true;
				}
				test = new Date(dateend);
				if (test.format('m/d/Y') == chkformat) {
					checkdates = true;
				}
				if (this.calx.store.getAt(itask).data[this.calx.fieldsRefer.color]) { //0.0.11dynamic fields	
					colortask = this.calx.store.getAt(itask).data[this.calx.fieldsRefer.color]; //0.0.11dynamic fields	
				} else {
					colortask = this.taskBaseColor;
				}
				if (checkdates) {
					this.tasks[currentindex] = new Ext.ECalendar.daytask({
						tasksOffset: this.tasksOffset,
						evenLaunch: this.task_eventLaunch,
						//0.0.11  define event on which the task/event will be activated 
						editable: this.task_DDeditable,
						//0.0.11  OldValue = true, 
						parentview: this,
						//dayview object
						baseBody: mydaybody,
						//ext element already created 
						datehandle: this.datetohandle,
						//date to handle 
						showQtip: this.task_showqtip,
						//if show or not the qtip  deafults true 
						contextMenuLabels: e2cs.cal_locale.contextMenuLabelsDay,
						tplqTip: this.calx.tplTaskTip,
						task_index: itask,
						task_id: this.calx.store.getAt(itask).data[this.calx.fieldsRefer.id],
						//0.0.11 dynamic fields				
						task_subject: this.calx.store.getAt(itask).data[this.calx.fieldsRefer.subject],
						//0.0.11 dynamic fields
						task_starts: this.calx.store.getAt(itask).data[this.calx.fieldsRefer.startdate],
						//0.0.11 dynamic fields
						task_ends: this.calx.store.getAt(itask).data[this.calx.fieldsRefer.enddate],
						//0.0.11 dynamic fields
						task_description: this.calx.store.getAt(itask).data[this.calx.fieldsRefer.description],
						//0.0.11 dynamic fields
						task_allday: this.calx.store.getAt(itask).data[this.calx.fieldsRefer.alldayevent],
						task_clsOver: this.task_clsOver,
						//0.0.11 dynamic fields
						task_increment: this.task_increment,
						task_width: this.task_width,
						task_format: this.task_format,
						bgcolor: colortask,
						//0.0.11 dynamic fields
						moreMenuItems: this.moreMenuItems,
						ShowMenuItems: this.task_ShowMenuItems //0.0.11 setting permissions on the menu 
					});
					this.tasks[currentindex].init(this.calx, this);
					//this.tasks[currentindex].render(); 
					currentindex += 1;
				}
			}
/*			this.tasks = [];
			

			event4 = new Object;
			event4.task_starts = "03/25/2010 08:30 AM"
			event4.task_ends = "03/25/2010 01:00 PM" 
			this.tasks.push(event4);
      			
			event2 = new Object;
			event2.task_starts = "03/25/2010 05:30 AM"
			event2.task_ends = "03/25/2010 09:30 AM" 
			this.tasks.push(event2);
      
			event3 = new Object;
			event3.task_starts = "03/25/2010 06:00 AM"
			event3.task_ends = "03/25/2010 08:00 AM" 
			this.tasks.push(event3);
      

			
			event6 = new Object;
			event6.task_starts = "03/25/2010 01:00 PM"
			event6.task_ends = "03/25/2010 04:30 PM" 
			this.tasks.push(event6); 
             
			event5 = new Object;
			event5.task_starts = "03/25/2010 09:30 AM"
			event5.task_ends = "03/25/2010 01:00 PM" 
			this.tasks.push(event5);

			event5Dup = new Object;
			event5Dup.task_starts = "03/25/2010 010:00 AM"
			event5Dup.task_ends = "03/25/2010 03:00 PM" 
			this.tasks.push(event5Dup);			         
      
                      			
			
			event1 = new Object;
			event1.task_starts = "03/25/2010 05:00 AM"
			event1.task_ends = "03/25/2010 10:00 AM" 
			this.tasks.push(event1);
                                                 */
			this.tasks.sort(this.sortTasks);
			colArray = this.setColumns();
			this.setSpans(colArray);
			maxCols = colArray.length;
			'test_calx-daybody'
			grid = Ext.get('test_calx-daybody');
			maxWidth = grid.getWidth();
			this.spanWidth = maxWidth / maxCols;
			var firstPartTask;
			firstNotFound = true;
			for (i = 0; i < this.tasks.length; i++) {
				this.tasks[i].task_width = this.tasks[i].rowSpan * this.spanWidth;
				if (this.tasks[i].task_allday == '0' && firstNotFound) {
					firstNotFound = false
					firstPartTask = this.tasks[i];
				}
			}
			this.renderTasks();
			timeScroll = this.datetohandle;
			today = new Date();
			if (this.calx.store.dayLoaded) {
				if (firstPartTask != undefined && firstPartTask.task_starts != undefined) {
					timeScroll = new Date(firstPartTask.task_starts);
				}
				if (this.datetohandle.format('m/d/Y') == today.format('m/d/Y')) {
					timeScroll = today;
				}
				myheaderdayswrap.scroll('b', timeScroll.getHours() * 60 + timeScroll.getMinutes());
			}
			if (Ext.get('daylayoutbody').getWidth(true) < Ext.get(this.calx.id + '-tableallday').getWidth(true)) { //0.0.6 added calendar ID
				Ext.get('daylayoutbody').setWidth(Ext.get(this.calx.id + '-tableallday').getWidth(true) + 1, false); //0.0.6 added calendar ID				
			}
			if (this.tasks.length <= 0) {
				mydaybody.update('&nbsp;');
			}
		} else {
			mydaybody.update('&nbsp;');
		}
	},
	operadaybuttons: function(evx, elx, obx) {
		if (Ext.isOpera) {
			if (evx.button == 2) {
				this.oncontextmenuBody(evx, elx, obx);
			}
		} else {
			return false;
		}
	},
	//---------------------------------------------------------------------
	// oncontextmenuBody: feature posted by IOMANIP (ext forums)
	// for context menu (right click) on the body of each day view 
	oncontextmenuBody: function(evx, elx, obx) {
		if (Ext.isOpera) {
			if (evx.button != 2) {
				return false;
			}
		}
		//0.0.11 Avoid to show menu cause no permission is set and also no custom Menus also 
		if (this.ShowMenuItems[0] != true && this.ShowMenuItems[1] != true && this.ShowMenuItems[2] != true && this.ShowMenuItems[3] != true && this.ShowMenuItems[4] != true && this.ShowMenuItems[5] != true) {
			return false;
		}
		evx.stopEvent();
		if (elx.id.indexOf(this.calx.id + "-daybody") < 0) {
			return false;
		}
		var tmpdata = Ext.get(elx.id);
		if (this.menu) {
			this.menu.removeAll();
		}
		this.menu = new Ext.menu.Menu({
			// 0.0.10 bug fix thanks to PTG
			id: this.calx.id + '-contextmenu-day',
			//0.0.6 modified ID  and menu items also on ID 
			shadow: true,
			items: [{
				id: this.calx.id + '-day_ctxbtn_task-add',
				iconCls: 'x-calendar-day-btnmv_add',
				text: e2cs.cal_locale.contextMenuLabelsDay.taskAdd,
				scope: this
			}, '-',
			{
				id: this.calx.id + '-day_ctxbtn_task-go-nd',
				iconCls: 'x-calendar-day-btnmv_nextday',
				text: e2cs.cal_locale.contextMenuLabelsDay.NextDay,
				scope: this
			},
			{
				id: this.calx.id + '-day_ctxbtn_task-go-pd',
				iconCls: 'x-calendar-day-btnmv_prevday',
				text: e2cs.cal_locale.contextMenuLabelsDay.PreviousDay,
				scope: this
			}, '-',
			{
				id: this.calx.id + '-day_ctxbtn_viewmonth',
				iconCls: 'x-calendar-month-btnmv_viewmonth',
				text: e2cs.cal_locale.contextMenuLabelsDay.chgmview,
				scope: this
			},
			{
				id: this.calx.id + '-day_ctxbtn_viewweek',
				iconCls: 'x-calendar-month-btnmv_viewweek',
				text: e2cs.cal_locale.contextMenuLabelsDay.chgwview,
				scope: this
			},
			{
				id: this.calx.id + '-day_ctxbtn_viewsched',
				iconCls: 'x-calendar-month-btnmv_viewsched',
				text: e2cs.cal_locale.contextMenuLabelsDay.chgsview,
				scope: this
			} //0.0.11 				   				   
			]
		});
		this.menu.items.items[0].addListener('click', function() {
			this.calx.fireEvent("taskAdd", this.calx.currentdate);
		}, this);
		this.menu.items.items[2].addListener('click', function() {
			this.onclicknext_day();
		}, this); //next day		
		this.menu.items.items[3].addListener('click', function() {
			this.onclickprev_day();
		}, this); //prev day	
		this.menu.items.items[5].addListener('click', function() {
			this.changeCalview(Ext.get(elx), this, 1);
		}, this);
		this.menu.items.items[6].addListener('click', function() {
			this.changeCalview(Ext.get(elx), this, 2);
		}, this);
		this.menu.items.items[7].addListener('click', function() {
			this.changeCalview(Ext.get(elx), this, 3);
		}, this);
		//0.0.11 - check visibility on the menu-items according to the new property this.ShowMenuItems
		if (this.ShowMenuItems[0] != true) {
			this.menu.items.items[0].hidden = true;
			this.menu.items.items[1].hidden = true;
		}
		if (this.ShowMenuItems[1] != true) {
			this.menu.items.items[2].hidden = true;
		}
		if (this.ShowMenuItems[2] != true) {
			this.menu.items.items[3].hidden = true;
		}
		if (this.ShowMenuItems[1] != true && this.ShowMenuItems[2] != true) {
			this.menu.items.items[4].hidden = true;
		}
		if (this.ShowMenuItems[3] != true) {
			this.menu.items.items[5].hidden = true;
		}
		if (this.ShowMenuItems[4] != true) {
			this.menu.items.items[6].hidden = true;
		}
		if (this.ShowMenuItems[5] != true) {
			this.menu.items.items[7].hidden = true;
		}
		// check the existence of view objects  0.0.11 
		if (!this.calx.mview) {
			this.menu.items.items[6].hidden = true;
		}
		if (!this.calx.wview) {
			this.menu.items.items[5].hidden = true;
		}
		if (!this.calx.sview) {
			this.menu.items.items[7].hidden = true;
		}
		//this.menu.showAt(evx.xy);	
		this.menu.showAt([evx.getPageX(), evx.getPageY()]); //0.0.11 
	},
	//---------------------------------------------------------------------
	changeCalview: function(objx, mviewx, typeview) {
		if (objx.dom.className == 'noday' || objx.dom.className == 'today' || objx.dom.className == 'monthday') {
			var refdate = new Date(objx.id);
		} else if (objx.dom.className == 'tasks') {
			var refdate = new Date(objx.dom.parentNode.firstChild.id);
		} else {
			var refdate = new Date(objx.dom.firstChild.id);
		}
		if (typeview == 1) {
			varview = 'month';
		} else if (typeview == 2) {
			varview = 'week';
		} else {
			varview = 'schedule';
		}
		this.calx.changeView(varview);
	},
	onclickprev_day: function(evx, elx, obx) {
		var changedaydate = this.datetohandle.add(Date.DAY, -1);
		var check = this.fireEvent("beforeDayChange", this.datetohandle, changedaydate);
		if (!check) {
			this.calx.currentdate = changedaydate;
			this.datetohandle = changedaydate;
			this.render();
			this.fireEvent("afterDayChange", changedaydate);
		}
	},
	onclicknext_day: function(evx, elx, obx) {
		var changedaydate = this.datetohandle.add(Date.DAY, 1);
		var check = this.fireEvent("beforeDayChange", this.datetohandle, changedaydate);
		if (!check) {
			this.calx.currentdate = changedaydate;
			this.datetohandle = changedaydate;
			this.render();
			this.fireEvent("afterDayChange", changedaydate);
		}
	},
	genHeader: function(dateval) {
		var dt = new Date(dateval);
		Date.monthNames = e2cs.cal_locale.monthtitles;
		Date.dayNames = e2cs.cal_locale.daytitles;
		var myheader = '<div class="x-calendar-dayv-header" style="width:' + (this.calx.width - 10) + 'px;">';
		myheader += '<div id="calendarheader">' + dt.format(this.headerFormat) + '</div>';
		if (this.headerButtons) {
			myheader += '<div class="x-calendar-day-previous"></div>';
			myheader += '<div class="x-calendar-day-next"></div>';
		}
		myheader += '</div>';
		return myheader;
	},
	genBody: function(dateval) {
		var dt = new Date(dateval);
		var inittime = new Date(dt.format('m/d/Y') + ' ' + this.startTime);
		var endtime = new Date(dt.format('m/d/Y') + ' ' + this.endTime);
		this.diffhrs = this.calx.dateDiff(inittime, endtime, e2cs.dateParts.HOUR);
		//fix for IE6 and IE7
		var wtdisp = '';
		if (Ext.isIE || Ext.isIE6) {
			wtdisp = '97.8';
		} else {
			wtdisp = '200';
		}
		wtdisp = '200';
		//---------------------------------
		var dviewbody = '<div id="daylayoutbody" class="x-calendar-dayv-body">'; //0.0.6 added calendar ID
		dviewbody += '<table id="' + this.calx.id + '-tableallday" width="' + wtdisp + '%" border="0" cellspacing="1" cellpadding="0"><tr><td valign="top" width="50">';
		dviewbody += '<table width="50" border="0" align="center" cellpadding="0" cellspacing="0">';
		for (var i = 0; i < this.diffhrs; i++) {
			//dviewbody +='<tr><td class="hour-marker">'+ inittime.add(Date.HOUR, (i) ).format( this.hourFormat + ':i a') + '</td></tr>'; 
			tmpdatetohandleontd = dt.format('m/d/Y');
			tmpdatetohandleontd += ' ' + inittime.add(Date.HOUR, (i)).format('h');
			tmpdatetohandleontd += ':' + inittime.add(Date.HOUR, (i)).format('a');
			dviewbody += '<tr><td id="' + this.calx.id + '-tdbody-dayv-' + tmpdatetohandleontd + '" class="hour-marker"><div class="dayvTimeWrapper"><div class="dayvTimeWrapper2"><div>' + inittime.add(Date.HOUR, (i)).format('g') + '</div>';
			dviewbody += '<div class="minute">' + inittime.add(Date.HOUR, (i)).format('A') + '</div></div></div></td></tr>';
		}
		dviewbody += '</table></td><td valign="top" id="tdbaseday">';
		dviewbody += '<div id="' + this.calx.id + '-daybody" class="basegridday">';
		dviewbody += '&nbsp;';
		dviewbody += '</div>';
		dviewbody += '</td></tr></table>';
		dviewbody += '</div>';
		return dviewbody;
	}
});

