// Version 0.0.14
// nach Vorlage _sample_db.htm 

Ext.onReady(function() {    
	Ext.QuickTips.init(); 
	test_calendar();
});

function updateStoreincalendar(){
var viewx = prueba.currentView; 
if (viewx=='month'){ 
	var mynewmonth= parseInt(prueba.currentdate.format('m'), 10); 
	calendarstoretest.baseParams={action:'month', displaymonth:mynewmonth, calendar: CALENDAR_NAME};
	calendarstoretest.reload(); 
}else if (viewx=='day'){ 
	var mynewdate= prueba.currentdate.format('m/d/Y'); 
	calendarstoretest.baseParams={action:'day', day:mynewdate, calendar: CALENDAR_NAME};
	calendarstoretest.reload(); 
} else if (viewx=='week'){ 
	var mynewdate= parseInt(prueba.currentdate.format('W'), 10);
	var myrange = prueba.getDateRangeOfWeek(mynewdate); 
	calendarstoretest.baseParams={
				action:'week',
				weeknumber:mynewdate,
				startweek:myrange[0].format('m/d/Y'),
				endweek:myrange[1].format('m/d/Y'),
				calendar: CALENDAR_NAME
				};
	calendarstoretest.reload(); 
} else if (viewx=='schedule'){
	// e2cs.schedviews.subView ={ Day:  0,  Week: 1,  Month: 2, TwoMonths: 3,  Quarter: 4};
	if (prueba.viewscheduler.listbody.periodType==0){  // day 
		var mynewdate= prueba.currentdate.format('m/d/Y'); 
		calendarstoretest.baseParams={action:'day', day:mynewdate, calendar: CALENDAR_NAME};
		calendarstoretest.reload();
	} else if (prueba.viewscheduler.listbody.periodType==1){ //week 
		var mynewdate= parseInt(prueba.currentdate.format('W'), 10);
		var myrange = prueba.getDateRangeOfWeek(mynewdate); 
		calendarstoretest.baseParams={
					action:'week',
					weeknumber:mynewdate,
					startweek:myrange[0].format('m/d/Y'),
					endweek:myrange[1].format('m/d/Y'),
					calendar: CALENDAR_NAME
					};
		calendarstoretest.reload();
	} else if (prueba.viewscheduler.listbody.periodType==2){ //month
		var mynewmonth= parseInt(prueba.currentdate.format('m'), 10); 
		calendarstoretest.baseParams={action:'month', displaymonth:mynewmonth, calendar: CALENDAR_NAME};
		calendarstoretest.reload(); 
	} else if (prueba.viewscheduler.listbody.periodType==3){ // two months 
		var myrange = prueba.viewscheduler.getDatesforBimonth(prueba.currentdate); 
		calendarstoretest.baseParams={
			action:'period',
			start:myrange[0].format('m/d/Y'),
			ends:myrange[1].format('m/d/Y'),
			calendar: CALENDAR_NAME
			};
		calendarstoretest.reload(); 
	} else if (prueba.viewscheduler.listbody.periodType==4){ // Quarter 
		var myrange = prueba.viewscheduler.getDatesforBimonth(prueba.currentdate); 
		calendarstoretest.baseParams={
			action:'period',
			start:myrange[0].format('m/d/Y'),
			ends:myrange[1].format('m/d/Y'),
			calendar: CALENDAR_NAME
			};
		calendarstoretest.reload(); 		
	} 		
}	
} 

function test_calendar(obj){
buttonx1= new Ext.menu.Item({ id: 'buttonx1_task', iconCls:'x-calendar-month-btnmv_task',	text: "Custom menu test 1" });
buttonx2= new Ext.menu.Item({ id: 'buttonx2_task',iconCls:'x-calendar-month-btnmv_task',	text: "Custom menu test 2" });

buttonz1= new Ext.menu.Item({ id: 'buttonz1_task', iconCls:'x-calendar-month-btnmv_task',	text: "Custom action 1" });
buttonz2= new Ext.menu.Item({ id: 'buttonz2_task',iconCls:'x-calendar-month-btnmv_task',	text: "Custom action 2" });

boton_daytimertask  = new Ext.menu.Item({ id: 'btnTimerTask', iconCls:'task_time', text: "Set Task Alarm...."  });
boton_daytimertaskb = new Ext.menu.Item({ id: 'btnTimerOff' , iconCls:'task_time_off', text: "Delete Task's Alarm...."  });	

button_sched_1= new Ext.menu.Item({ id: 'buttonx1_task',iconCls:'x-calendar-month-btnmv_task',text: "Custom menu  on sched test 1" });
button_sched_2= new Ext.menu.Item({ id: 'buttonx2_task',iconCls:'x-calendar-month-btnmv_task',text: "Custom menu  on sched test 2" });

calendarstoretest=new Ext.data.Store({
    url:'/ttevents/events.php',
	baseParams:{ action:'all', calendar: CALENDAR_NAME },
    reader: new Ext.data.JsonReader({
               root:'records',
	           id: 'id',
			   totalProperty: 'totalCount'
       	   },
		   [{name:'recid', 		 mapping:'id', 			type: 'string'},
		    {name:'subject', 	 mapping:'subject', 	type: 'string'},
			{name:'description', mapping:'description', type: 'string'},
			{name:'startdate', 	 mapping:'startdate', 	type: 'string'},
			{name:'enddate', 	 mapping:'enddate', 	type: 'string'},				
			{name:'bgcolor', 	 mapping:'bgcolor',		type: 'string'},
			{name:'color', 		 mapping:'color', 		type: 'string'},
			{name:'parent',		 mapping:'parent',		type: 'int'}, 
		    {name:'priority',	 mapping:'priority',	type: 'int'} 
			]
		   )
});		
prueba = new Ext.ECalendar({
	id: 'test_calx', 
	name: 'test_calx',
	title: CALENDAR_NAME,
	height:500, 
	fieldsRefer:{ //0.0.11 
		id:'recid',
		subject:'subject',
		description:'description', 
		bgcolor: 'bgcolor',
		color:'color',
		startdate:'startdate',
		enddate:'enddate',
		priority:'priority',
		parent:'parent'
	},
	storeOrderby:'priority', 	//0.0.11 
	storeOrder:'DESC',		//0.0.11 
	showCal_tbar: true, 
	showRefreshbtn:true,
	refreshAction:'data', //0.0.11 
	currentView: 'month',
	currentdate: new Date(),
	dateSelector: true,
	dateSelectorIcon: '/ttevents/js/e2cs/images/date.png',
	dateSelectorIconCls: 'x-cmscalendar-icon-date-selector',
	dateformat :'d-m-Y',
	header: true,
	title: CALENDAR_NAME,
	iconCls: 'x-cmscalendar-icon-main',
	dateSelector:true,
	store:	calendarstoretest, 
	monitorBrowserResize:true, 
	widgetsBind: {bindMonth:null,bindDay:null,binWeek:null},
	tplTaskZoom: new Ext.XTemplate( 
	'<tpl for=".">',
		'<div class="ecal-show-basetasktpl-div"><b>Event:</b>{subject}<br>',
		'<b>Beginnt:</b> {startdate_de}<br><b>Endet:</b> {enddate_de}',
		'<br><b>Details:</b><div><hr><div> {description}<div><hr>',
	'</tpl>'
	),
	iconToday:'/ttevents/js/e2cs/images/cms_calendar.png',
	iconMonthView:'/ttevents/js/e2cs/images/calendar_view_month.png',
	iconWeekView:'/ttevents/js/e2cs/images/calendar_view_week.png',
	iconDayView:'/ttevents/js/e2cs/images/calendar_view_day.png',
	iconSchedView:'/ttevents/js/e2cs/images/calendar_view_schedule.png', //0.0.10  // NEW :) 
	loadMask:true, //0.0.12 
	customMaskText:'Bitte warten...', //0.0.12 
	//-------- NEW on 0.0.10 -------------------
	sview:{
			header: true, headerFormat:'M. Y', 
			headerButtons: true,
			headerAction:'event',  //gotoview
			periodselector:false,
			blankzonebg:'#6C90B4',
			//sched_addevent_id
			blankHTML:'<div id="{calx}-test-img" class="custom_image_addNewEvent_scheduler" style=" width:100%; background-color:#6C90B4"><div align="center" id="{sched_addevent_id}"><img src="/ttevents/js/e2cs/images/no_events_default.jpg" width="174" height="143"></div><div class="custom_text_addNewEvent_scheduler">Klicken Sie auf ein Bild, um einen neuen Termin hinzuzufügen.</div></div>',
			listItems: { 
				headerTitle:"Events for Test by CMS", 		
				periodFormats:{ 
					Day:		'l d.F.Y', 	
					DayScheduler_format: 'd', 	
					hourFormat: 'h:i', 				 
					startTime:  '7:00:00 am',		
					endTime:    '10:00:00 pm',
					WeekTPL:  	'<tpl for=".">Week No.{numweek} Starting on {datestart} Ending on {dateend}</tpl>',  
					WeekFormat:	'W',	
					DatesFormat:'d.m.Y', 
					Month:'M-Y', 
					TwoMonthsTPL:'<tpl for=".">Period No.{numperiod} Starting on {datestart} Ending on {dateend}</tpl>',
					QuarterTPL:  '<tpl for=".">Period No.{numperiod} Starting on {datestart} Ending on {dateend}</tpl>'
				},	
				useStoreColor:false, 	
				descriptionWidth:246,
				parentLists:false, //to expand collapse Parent Items if false all tasks shown as parent
				launchEventOn:'click',
				editableEvents:false, // If true a context menu will appear 
				ShowMenuItems:[1,1,1,1,1,1,1,1], // ADD, EDIT, DELETE, GO NEXT PERIOD , GO PREV PERIOD, Chg Month, Chg Week, CHG Day
				taskdd_ShowMenuItems:[0,0,0],    // ADD, EDIT, DELETE
				moreMenuItems:[button_sched_1,button_sched_2],
				taskdd_BaseColor:'#6C90B4',
				taskdd_clsOver:'test_taskovercss_sched',
				taskdd_showqtip:true,
				taskdd_shownames:true
			},
			listbody:{
				//e2cs.schedviews.subView ={ Day:  0,  Week: 1,  Month: 2, TwoMonths: 3,  Quarter: 4};
				//e2cs.schedviews.Units   ={ Hours:0,  Days: 1,  Weeks: 2};
				periodType:e2cs.schedviews.subView.Month,
				headerUnit:e2cs.schedviews.Units.Days,
				headerUnitWidth:25
			}
	}, 
	//-------------------------------------------
	mview:{
		header: true,
		headerFormat:'F Y',
		headerButtons: true,
		dayAction:'viewday',    //dayAction: //viewday , event, window
		moreMenuItems:[],
		showTaskcount: false,
		taskStyle:'margin-top:2px;', //Css style for text in day(if it has tasks and showtaskcount:true)
		showTaskList: true,
		showNumTasks:10,
		startDay:1, //Monday!   0.0.14 
		TaskList_launchEventOn:'click', //0.0.11 
		TaskList_tplqTip: new Ext.XTemplate( '<tpl for=".">{starxl}{startval}<br>{endxl}{endval}<hr color=\'#003366\' noshade>{details}</tpl>' ), //0.0.11 
		ShowMenuItems:[0,1,1,1,1,0],  //0.0.11  - ADD, nextmonth, prevmonth, chg Week , CHG Day, chg Sched,	
		TaskList_moreMenuItems:[], 	  //0.0.11
		TaskList_ShowMenuItems:[0,0,0]//0.0.11 	- Add, DELETE, EDIT 	
	},
	wview:{
		headerlabel:'Semana #',
		headerButtons: true,
		dayformatLabel:'D j', 
		moreMenuItems:[],
		style: 'google',
		alldayTaksMore:'window', 
		alldayTasksMaxView:9,
		store: null, 
		task_width:25, 
		tasksOffset:40,
		headerDayClick:'viewday',
		ShowMenuItems:[0,1,1,1,1,0],	//0.0.11  add, go next w , go prev w , chg month , chg day, chg sched 
		task_ShowMenuItems:[0,0,0,1,1], //0.0.11  add, delete, edit, go next w , go prev w
		task_eventLaunch:'click',		//0.0.11
		task_clsOver:'test_taskovercss',
		startDay:1, //Monday!   0.0.14 
		forceTaskFit:true // 0.0.14 
	},
	dview:{
		header:true,
		headerFormat:'l d.F.Y',
		headerButtons: true,
		headerTooltips: { prev: 'Previous month..',	next: 'Next Month'  },
		contextMenuLabels: { 
				task: "Create Task", 
				chgwview: "Change to month view.", 
				chgdview: "Change to week view."
		},
		moreMenuItems:[],
		// day specific 
		hourFormat: 'h',
		startTime: '07:00:00 am',
		endTime:   '10:00:00 pm',
		// task settings 
		store: null,
		taskBaseColor: '#ffffff', 
		task_width:30,
		taskAdd_dblclick: true,				//added on 0.0.7
		taskAdd_timer_dblclick:true,		//0.0.11
		useMultiColorTasks: false, 
		multiColorTasks:[], 
		forceTaskFit: true,					//0.0.14	
		tasks:[],
		moreMenuItems:[	boton_daytimertask,	boton_daytimertaskb	],
		task_clsOver:'test_taskovercss',	
		ShowMenuItems:[0,1,1,1,1,0],		//0.0.11 ADD, next day, prev day , chg Month , CHG Week, chg Sched, (for daybody menu) 
		task_DDeditable:true, 			    //0.0.11   
		task_eventLaunch:'dblclick',  		    //0.0.11 'click, dblclick, if set to '' then no action is taken
		task_ShowMenuItems:[0,0,0,1,1]		//0.0.11 ADD, delete, edit, Next day , Prev Day  (for Taks/events) 
	}
});

//scheduler only event on this object  
prueba.viewscheduler.on({
	'headerClick':{
			fn: function(refunit,datex, mviewx, calx) { 
				var msgstring = "Header Value clicked -(" + refunit +')<br>';
				if (refunit=='day' || refunit=='hour'){ 
					msgstring += 'on date -(' + datex.format('d/m/Y h:i:s') +')<br>';	
				} else { 
					msgstring += 'on Week# ' + datex[0].format('W');
					msgstring += '-(From ' + datex[0].format('d/m/Y h:i:s') +'  to '; 
					msgstring +=datex[1].format('d/m/Y h:i:s') + ')';	
				} 	
				//Ext.Msg.alert('Information', msgstring);	
			},
			scope:this
	},
	'listItemSendData':{
			fn:function(recordId,dataObj){
					//Ext.Msg.alert('Information', "You clicked on task/Event ListItem #" + recordId + ':' + dataObj.recid);													
			} 
	},
	'beforePeriodChange':{
			fn:function(refperiod,datexold,datexnew){
				if (refperiod==1){ //week 
					//do your stuff here 
				} else { 
					//do your stuff here 
				}
				return false; 
			},
			scope:this
	}, 
	'afterPeriodChange':{
			fn:function(refperiod,datexnew){
				if (refperiod==1){ 
					//alert ("Changed date from " + datexnew[0] + " to " + datexnew[1]);					
				} else { 
					//alert ("Changed date to " + datexnew);					
				} 
				updateStoreincalendar();
			}
	}
});
//dayClick only event on this object  
prueba.viewmonth.on({		
	'dayClick':{
			fn: function(datex, mviewx, calx) { 
				//alert ("dayclick event for " + datex);
			},
			scope:this
	},
	'beforeMonthChange':{
			fn: function(currentdate,newdate) { 
				//alert ("gonna change month to " + newdate.format('m/Y') + ' Actual date=' + currentdate.format('m/Y') );
				return false; 
			},
			scope:this
	},
	'afterMonthChange':{
			fn: function(newdate) { 
				//alert ("Month changed to " + newdate.format('m/Y') ) ;
				updateStoreincalendar();  // refresh the data for that selected month 
			},
			scope:this
	}
}); 
prueba.viewweek.on({
	'dblClickTaskAllDay':{
			fn: function(task,dxview,calendar) { 
				var datatest= '';
				//'Task all day <br>record No:' + task[0] + '<br>';
				datatest+= task[2] + '<br>';
				datatest+='Beginnt: '    + task[3] + '<br>';
					datatest+='Endet: '   + task[4] + '<br>';
				if (task[5] != undefined)	datatest+='Details: '  + task[5] + '<br>';	
				//datatest+='index'     + task[6] + '<br>';	
				//Ext.Msg.alert('Informationen', datatest);	
			},
			scope:this
	},
	'beforeWeekChange':{
		fn: function (currentDate, newDate){
				return false;	
		}
	}, 
	'afterWeekChange':{
		fn: function(newdate){
			updateStoreincalendar();
			return false; 
		}
	}
});  
//'beforeDayChange' and  'afterDayChange' unique events on day view 	
prueba.viewday.on({
	'beforeDayChange':{
			fn: function(currentdate, newdate) { 
				//alert ("gonna change to " + newdate.format('m/d/Y') + ' Actual date=' + currentdate.format('m/d/Y') );
				return false; 
			},
			scope:this
	},		
	'afterDayChange':{
			fn: function(newdate) { 
				//alert ("changed to " + newdate.format('m/d/Y'));
				updateStoreincalendar();
			},
			scope:this
	}	
}); 
prueba.on({
	'beforeChangeDate': {
		fn: function( newdate , calobj){
			return true; 
		} 		
	},
	'afterChangeDate':{
		fn: function( newdate , calobj){
			//alert ("Date changed to:" + newdate.format('d-m-Y'));
			updateStoreincalendar();  // refresh the data for that selected date and view 
		}
	},
	'onChangeView':{
		fn: function(newView, oldView, calobj){ 	
			updateStoreincalendar(); // refresh the data for that selected date and view 
		},scope: this
	},
	'beforeChangeView':{
			fn: function (newView,OldView,calendarOBJ){
				return true; 
			},scope:this
	},
	'taskAdd':{
			fn: function( datex ) { 
				var test=tstfunctiontoadd(datex); 
				if  (test){
					//alert ("Adding Task for " + datex);
				} 
				// do your stuff and then send the data to the php file and 
				// updateStoreincalendar();					
			}
	},
	'taskDblClick':{
			fn: function (task,dxview,calendar,refviewname){
				var datatest= '';
				datatest+= task[2] + '<br><br>';
				datatest+='Beginnt: '    + task[3] + '<br>';
					datatest+='Endet: '   + task[4] + '<br><br>';
				if (task[5] != undefined) datatest+='Details: '  + task[5] + '<br>';	
				//datatest+='index'     + task[6] + '<br>';	
				Ext.Msg.alert('Details', datatest);		
			},
			scope:this 
	},
	'beforeTaskDelete': {
			fn: function (datatask,dxview) { 
				return false; 
				// do your stuff to check if the event/task could be deleted 
			}, scope:this
	},
	'onTaskDelete':{
		fn:function(datatask){
			var r=confirm("Delete event " + datatask[1] + " " + datatask[2] + "...? YES/NO" );
			return r; 
			// do your stuf for deletion and return the value 
		},scope:this
	},
   'afterTaskDelete':{
   		fn: function(datatask,action){
			action ? alert("Event: " + datatask[1] + " " + datatask[2] + " Deleted"): alert("Event Delete was canceled..!"); 
			// perform any action after deleting the event/task
			// do your stuff and then send the data to the php file and 
			// updateStoreincalendar();						
		},scope:this
	    },
	'beforeTaskEdit': {
			fn: function (datatask,dxview) { 
				return false; 							
			}, scope:this
	},	
   '_onTaskEdit':{
		fn:function(datatask){
			//var r=confirm("Edit event " + datatask[1] + " " + datatask[2] + "...? YES/NO" );
			return true; 
			// do your stuff for editing and return the value				
		},scope:this
    },
    'afterTaskEdit':{
   		fn: function(datatask,action){// perform any action after deleting the event/task
			if (action){ 
				//alert("Event: " + datatask[1] + " " + datatask[2] + " Edited");
				// do your stuff and then send the data to the php file and 
				// updateStoreincalendar();						
			} else { 
				//alert("Event Edit was canceled..!"); 
			} 
			return false; 		
		},scope:this
    },
	'_beforeTaskMove':{
			fn: function (datatask,Taskobj,dxview,TaskEl) { // return "true" to cancel or "false" to go on 
				return false; 	
			}, scope:this
	},
	'_TaskMoved':{
			fn: function (newDataTask,Taskobj,dxview,TaskEl) {   // do some stuff 
				var test=21;  // use breakpoint in firefox here 
				task = newDataTask; 
				datatest ='Task id:'  + task[0] + ' ' + task[2] + '<br>';
				datatest+='recid:'    + task[1] + '<br>';
				datatest+='starts:'    + task[3] + '<br>';
				datatest+='ends:'   + task[4] + '<br>';
				datatest+='contents:' + task[5] + '<br>';	
				datatest+='index:'    + task[6] + '<br>';	
				Ext.Msg.alert('Information Modified task', datatest);			
				// do your stuff and then send the data to the php file and 
				// updateStoreincalendar();	
			}, scope:this
	},
	'customMenuAction':{
			fn: function (MenuId, Currentview,datatask,objEl,dxview){
				var datatest = ''; 
				if (Currentview=='month'){ 
					task = datatask; 
					datatest ='Element ID :'  + task[0] + '<br>';
					datatest+='Task ID :'  + task[1] + '<br>';
					datatest+='Menu ID :'  + MenuId + '<br>';
					Ext.Msg.alert('(Month) Information- ' + Currentview, datatest);	
				} else if (Currentview=='day'){ 
					task = datatask; 
					datatest ='Task id:'  + task[0] + ' ' + task[1] + '<br>';
					datatest+='starts:'    + task[2] + '<br>';
						datatest+='Ends:'   + task[3] + '<br>';
						datatest+='contents:' + task[4] + '<br>';	
					datatest+='index:'    + task[5] + '<br>';	
					datatest+='Test Menu:'  + MenuId + '<br>';	
					Ext.Msg.alert('(Day) Task information' + Currentview, datatest);			
				} else if (Currentview=='week'){
					//misssing sample 
				} else if (Currentview=='scheduler'){
					task = datatask; 
					datatest ='Task id:'  + task[0] + ':' + task[2] + '<br>';
					datatest+='starts:'    + task[3] + '<br>';
						datatest+='Ends:'   + task[4] + '<br>';
						datatest+='contents:' + task[5] + '<br>';	
					datatest+='index:'    + task[6] + '<br>';	
					datatest+='Test Menu:'  + MenuId + '<br>';	
					Ext.Msg.alert('(Scheduler) Task information' + Currentview, datatest);			
				} 
			},scope:this		
	}

});
prueba.render('calendar');
calendarstoretest.load();	
myMasktest = new Ext.LoadMask( prueba.id, {msg:e2cs.cal_locale.loadmaskText});
calendarstoretest.on({
		'beforeload':{fn:function(){
				prueba.calendarMask.show();
			}
		},	
		'load':{
			fn:function(success,dataxx,purebax){
				if(success==false){
					alert ("Error in loading Data");
				} else { 
				 	prueba.refreshCalendarView(true); 
				} 
				prueba.calendarMask.hide();
			}
		} 
});		

Ext.EventManager.onWindowResize( function(){ 
	prueba.refreshCalendarView(); 
});
}

