/* Copyright 2009 Palm, Inc.  All rights reserved. */

var RecurrenceRules = Class.create( {
  	initialize: function() {
   	},
	
  	dayMap: ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'],		// Do NOT Localize
	
	constructRRuleFromModel:function(targetEvent){
		//this function constructs an rrule string from rruleModel 
		//this model resides in targetEvent- "targetEvent.rruleModel" 
		if(targetEvent.rruleModel==undefined)
		 	return;
		
		if (targetEvent.rruleModel.frequency != '') {
			targetEvent.rrule = "RRULE:";
			targetEvent.rrule += 'FREQ=' + targetEvent.rruleModel.frequency;		
		}
		if (targetEvent.rrule != '') {
			if (targetEvent.rruleModel.interval && targetEvent.rruleModel.interval != 0 /*&& targetEvent.rruleModel.interval != 1*/) 
				targetEvent.rrule += ';INTERVAL=' + targetEvent.rruleModel.interval;
			
			
			if (targetEvent.rruleModel.byday && targetEvent.rruleModel.byday.length != 0) {
				targetEvent.rrule += ';BYDAY=';
				targetEvent.rruleModel.byday.each(function(day, index){
					if (index != 0) 
						targetEvent.rrule += ',';
					targetEvent.rrule += day;
				});
			}
			if (targetEvent.rruleModel.bymonth && targetEvent.rruleModel.bymonth.length != 0) {
				targetEvent.rrule += ';BYMONTH=';
				targetEvent.rruleModel.bymonth.each(function(month, index){
					if (index != 0) 
						targetEvent.rrule += ',';
					targetEvent.rrule += month;
				});
			}
			if (targetEvent.rruleModel.bymonthday && targetEvent.rruleModel.bymonthday.length != 0) {
				targetEvent.rrule += ';BYMONTHDAY=';
				targetEvent.rruleModel.bymonthday.each(function(monthday, index){
					if (index != 0) 
						targetEvent.rrule += ',';
					targetEvent.rrule += monthday;
				});
			}
			//we can have either UNTIL or count, they are mutually exclusive
			if (targetEvent.rruleModel.until && targetEvent.rruleModel.until != '') 
				targetEvent.rrule += ';UNTIL=' + targetEvent.rruleModel.until;
			else 
				if (targetEvent.rruleModel.count && targetEvent.rruleModel.count != 1 && targetEvent.rruleModel.count != -1) 
					targetEvent.rrule += ';COUNT=' + targetEvent.rruleModel.count;
			if (targetEvent.rruleModel.count && targetEvent.rruleModel.count == 1) 
				targetEvent.endValidity = getCurrentDateTime().getTime();
			else 
				if (targetEvent.rruleModel.count && targetEvent.rruleModel.count == -1) 
					targetEvent.endValidity = AppAssistant.long_max_value;
			
			
			
			//if (targetEvent.rrule && targetEvent.rrule != '') 
			//	targetEvent.rrule += ';';
			
			//lets add the exdates while constructing the rrule itself.
			//this is more accurate for checking if the event is dirty or not
			// and if a set event is necessary
			
			if (targetEvent.exdates) 
				this.addExdateToRRule(targetEvent);
			
			
		}
		//if(targetEvent.endValidity==0)
		// targetEvent.endValidity = AppAssistant.long_max_value;
			
	
	},
	extractExdateFromRRule:function(targetEvent){
		if (targetEvent.rrule && targetEvent.rrule.include('EXDATE'))
		{
			//remove the EXDATE part and store it in a different place in targetEvent
			var oldExdatepos=targetEvent.rrule.indexOf("EXDATE");
		    targetEvent.exdates= targetEvent.rrule.substr(oldExdatepos);			
		}
	},
	addExdateToRRule:function(targetEvent){
		if (targetEvent.exdates && targetEvent.exdates!='')			
				targetEvent.rrule += '\r\n'+targetEvent.exdates ;
				
	},
	calculateRecurTime:function(localtime){
		
		var time = new Date(localtime);		
	//console.info("^^^^^^^^^^^^^^^^^^^^^^^^ entered calculate recurrence time");
		var year = time.getUTCFullYear();
		//console.info(year);
	
		var month =  time.getUTCMonth()+1;
		if(String(month).length <2)
			month ="0"+month;
				
		var date = time.getUTCDate();
		if(String(date).length <2)
			date ="0"+date;
			
		var hour = time.getUTCHours();
		if(String(hour).length <2)
			hour ="0"+hour;
			
		var minute = time.getUTCMinutes();
		if(String(minute).length <2)
			minute ="0"+minute;
			
		var second =	time.getUTCSeconds();
		if(String(second).length <2)
			second ="0"+second;
			
		
		returnTime=""+year+month+date+'T'+hour+minute+second+'Z';
		
		return returnTime;
		
	},
	
	// Performs simple array compare for rrule arrays, assumes arrays are ordered and only compares one level deep
	arraysEqual: function(array1, array2) {
	    if (array1.length != array2.length) return false;
	    for (var i = 0; i < array2.length; i++) {
	        if (array1[i] !== array2[i]) return false;
	    }
	    return true;
	},
	
  });
 
