Formatter = Class.create({
	
	getDateFormatted:function(startLong,endLong,isAllDay,timeFormat){
		Mojo.Log.info("*****************dateFormatted entered");
		
		var start= new Date(startLong);
		var end = new Date(endLong);
		var result;
		var info={};
		var isSameDay =(start.getDate()==end.getDate() && start.getMonth()==end.getMonth() && start.getYear()==end.getYear())?true:false;
		if(isSameDay){
			if(isAllDay){
				
				result =Mojo.Format.formatDate(start,$L('EEE, MMM d yyyy'));
				info.type ="all-day-single";
			}
			else{
					var template = new Template($L("#{date} <br> #{time1} to #{time2}"));
					var templateModel = {};
		

					templateModel.date=Mojo.Format.formatDate(start,$L('EEE, MMM d yyyy'));
					templateModel.time1 =Mojo.Format.formatDate(start,{time:'short'});
					templateModel.time2=Mojo.Format.formatDate(end,{time:'short'});
					result = template.evaluate(templateModel);
					info.type ="hourly-single";
			}
		}
		else{
			if(isAllDay){
				var template = new Template($L("#{date1} to <br> #{date2}"));
				var templateModel = {};	
				templateModel.date1=Mojo.Format.formatDate(start,$L('EEE, MMM d yyyy'));
				templateModel.date2=Mojo.Format.formatDate(end,$L('EEE, MMM d yyyy'));
				result= template.evaluate(templateModel);
				
			}else{
				var template = new Template($L("#{dateTime1} to <br> #{dateTime2}"));
				var templateModel = {};	
				templateModel.dateTime1=Mojo.Format.formatDate(start,{date: 'medium',time:"short"});
				templateModel.dateTime2=Mojo.Format.formatDate(end,{date: 'medium',time:"short"});
				result= template.evaluate(templateModel);
				info.type="hourly-multi";
			}
			
		}
		Mojo.Log.info("getDateFormatted result:*************"+result);
		
		info.value=result;
		return info;
	},

	getEventDurationText:function(startLong, endLong,isAllDay){
		console.info("*********in getEvent duration");		
			
		if(startLong == endLong && !isAllDay)
		{
			result= $L("This event is 0 minutes long");
		}
		else if(endLong < startLong)
		{
			result = $L("This event is invalid");
		}
		else
		{
			var period =getTimePeriodParts(new Date(startLong),new Date(endLong),isAllDay);
			var template = new Template($L("This event is #{period} long"));
			var templateModel = {};
		
			templateModel.period=this.periodFormatter(period);
			//console.info("***********result:"+templateModel.period);
			result= template.evaluate(templateModel);
			
		}
		Mojo.Log.info("getEventDurationText result:*************"+result);
		return result;

	},
	
	getRepeatStrings:function(repeatList){
		var formattedRepeat=[];
		for(i=0;i<repeatList.length;i++){
			
			switch(repeatList[i])
			{
				case "never":
				formattedRepeat.push($L("No Repeat"));
				break;
				
				case "daily":
				formattedRepeat.push($L("Daily"));
				break;
				
				case "weekly":
				formattedRepeat.push($L("Weekly"));
				break;
								
				case "weekday":
				formattedRepeat.push($L("Weekdays"));
				break;
				
				case "custom":
				formattedRepeat.push($L("Custom"));
				break; 
			}
		}
		Mojo.Log.info("getRepeatStrings result:*************"+Object.toJSON(formattedRepeat));
		return formattedRepeat;
		
	},
	
	getAlarmStrings:function(alarms){
		Mojo.Log.info("**********in getAlarmStrings:"+Object.toJSON(alarms));
		//we are hardcoding static options to avoid 
		// having to parse the period unnecessarily every time.
		
		var alarmList=[];
		for(i=0;i<alarms.length;i++)
		{
			var val;
			switch (alarms[i]) {
				case "none":
					val = $L('No Reminder');
					break;
					
				case '-PT0M':
				case 'PT0M':
				case '+PT0M':
					val = $L('At Start Time');
					break;
					
				case '-PT5M':
					val = $L('5 minutes before');
					break;
					
				case '-PT10M':
					val = $L('10 minutes before');
					break;
					
				case '-PT15M':
					val = $L('15 minutes before');
					break;
					
				case '-PT30M':
					val = $L('30 minutes before');
					break;
					
				case '-PT1H':
					val = $L('1 hour before');
					break;
					
				case '-PT5M':
					val = $L('5 minutes before');
					break;
					
				case '-P1D':
					val = $L('1 day before');
					break;
					
				case '-P1D':
					val = $L('1 day before');
					break;
					
				case '-P2D':
					val = $L('2 days before');
					break;
					
				case '-P3D':
					val = $L('3 days before');
					break;
					
				case '-P1W':
					val = $L('1 week before');
					break;
					
				default:
					{
						var period = getAlarmPartsFromString(alarms[i]);
						var val = this.periodFormatter(period);
						var template;
						var templateModel = {};
						if (period.negative) {
							template = new Template($L("#{val} before"));
						}
						else {
							template = new Template($L("#{val} after"));
						}
						
						templateModel.val = val;
						val = template.evaluate(templateModel);
					}
					break;
					
			}
			
			alarmList.push(val);
		}
		
		Mojo.Log.info("********** in alarmList:"+Object.toJSON(alarmList));
		return alarmList;
	},
	
	periodFormatter:function(period){
		Mojo.Log.info("***********period:"+Object.toJSON(period));
		var params =[];	
		var result;
		tempModel={};
		
		if(period.years >0){
			var temp = $L("1# #{year} year|# #{year} years");	
			tempModel.year=period.years;	
			params.push(Mojo.Format.formatChoice(period.years, temp, tempModel));
			
		}
		if(period.months>0){
			var temp = $L("1# #{month} month|# #{month} months");	
			tempModel.month=period.months;	
			params.push(Mojo.Format.formatChoice(period.months, temp, tempModel));

		}
		if(period.weeks>0){
			
			var temp = $L("1# #{week} week|# #{week} weeks");	
			tempModel.week=period.weeks;	
			params.push(Mojo.Format.formatChoice(period.weeks, temp, tempModel));
		}
		if(period.days>0){
			
			var temp = $L("1# #{day} day|# #{day} days");	
			tempModel.day=period.days;	
			params.push(Mojo.Format.formatChoice(period.days, temp, tempModel));
		}
		
		if(period.hours>0){			
			var temp = $L("1# #{hour} hour|# #{hour} hours");	
			tempModel.hour=period.hours;	
			params.push(Mojo.Format.formatChoice(period.hours, temp, tempModel));

		}
		if(period.minutes>0){
			var temp = $L("1# #{minute} minute|# #{minute} minutes");	
			tempModel.minute=period.minutes;	
			params.push(Mojo.Format.formatChoice(period.minutes, temp, tempModel));

		}
		Mojo.Log.info("***********params content:"+Object.toJSON(params));
			
		if(params.length ==1){
			result= params[0]; 
		}
		else if(params.length == 2){
			result= params[0]+$L(" and ")+params[1];
		}
		else{
			var resultStr="";
			for (i=0;i<params.length-1;i++)
			{
				resultStr = resultStr + params[i]+$L(", ");
			}
			result=resultStr+$L(" and ")+params[params.length-1];
			
		}
		Mojo.Log.info("************result string is:"+result)
		return result;;
	}
})

