// object to hold multiple events
var buildEvent = function(today){
	return new Object({
		entries: new Array(),
		dates: new Array(),
		today: today,
		last: false,
		newEntry: function(){
			newEntry = buildEntry(this);
			this.entries.push(newEntry);
			return newEntry;
		},
		begin: function(){
			if(!this.begin_date){
				this.entries.sort(this.begin_sort);
				this.begin_date = this.entries[0].begin;
			}
			return this.begin_date;
		},
		end: function(){
			if(!this.end_date){
				this.entries.sort(this.end_sort);
				this.end_date = this.entries[this.entries.length-1].end;
			}
			return this.end_date;
		},
		begin_sort: function(a, b){
			return a.begin.compareTo(b.begin);
		},
		end_sort: function(a, b){
			if(a.end == "no_end" && b.end == "no_end"){
				return 0;
			}else if(a.end == "no_end"){
				return 1;
			}else if(b.end == "no_end"){
				return -1;
			}else{
				return a.end.compareTo(b.end);
			}
		},
		push_date: function(date){
			// tests to see if the date is already in the dates array and adds
			// it if not
			if(this.dates.toString().indexOf(date.toString())==-1){
				this.dates.push(date);
			}
		},
		getDatesForSpan: function(startDate, endDate){
			startDate = Date.parse(startDate);
			endDate = Date.parse(endDate);
	    }
	});
};

// object to hold the information for a single event. 
var buildEntry = function(obj){
	return new Object({
		freq: "norepeat",
		end: "no_end",
		freq: 1,
		parent_event: obj,
		nondates: new Array(),
    	setBegin: function(x){this.begin = Date.parse(x)},
	    setInt: function(x){
			this.interval = x
			if(this.interval == "no repeat"){
				this.end = this.begin.clone();
			}
			if(this.interval == "monthByDay"){
				this.weekCount = Math.floor(this.begin.getDate()/7);
			}
		}, // named shorter to avoid javascript conflicts
		setEnd: function(x){
			this.end = Date.parse(x);
			if(this.end == null){
				this.end = "no_end";
			}
		},
		setFreq: function(arg){
			if(arg!=""){
				this.freq = arg;
			}
		},
		isOnDay: function(compareDate){
			switch(this.interval){
		        case "day":
					if(this.counterCompare(compareDate)){
						return true;
					}else{
						return false;
					}
					break;
				case "week":
					// need to set cases for if the event is biweekly. shuckz.
					if(compareDate.getDayName() == this.begin.getDayName() && this.counterCompare(compareDate)){
						return true;
					}else{
						return false;
					}
					break;
				case "month":
					if(compareDate.getUTCDate() == this.begin.getUTCDate() && this.counterCompare(compareDate)){
						return true;
					}else{
						return false;
					}         
					break;
				case "year":
					if(compareDate.getUTCDate()+"_"+compareDate.getUTCMonth() == this.begin.getUTCDate()+"_"+this.begin.getUTCMonth() && this.counterCompare(compareDate)){
						return true;
					}else{
						return false;
					}
					break;
				case "monthByDay":
					if(compareDate.equals(this.begin)){
						return true;
					}
				case "no repeat":
					if(compareDate.equals(this.begin)){
						return true;
					}else{
						return false;
					}
					break;				
        	}
		},
	    setID: function(x){this.eventID = x},
		counterCompare: function(counterDate){
			return (counterDate.compareTo(this.begin) >= 0 && (this.end == "no_end" || counterDate.compareTo(this.end) <= 0));
		},
		setNonDates: function(x){
			for(i=0;i<x.length;i++){
				nondates.push(x[i]);
			}
		}
	});
};