/**
 * @author jmagner
 */
function EventsCalendar(_id,_components){
	this._id = _id;
	this.components = _components;
	this.events = null;
	this.loadEvents();
}
/*
 * loadEvents();
 * This will populate `this.events` with a multidimensional associative array of events
 */
EventsCalendar.prototype.loadEvents = function(){
	var self = this;
	// read from the file
	$.ajax({
	    type: "GET",
		url: "scripts/events.xml",
		dataType: "xml",
		success: function(xml) {
			self.events = Array();
			$(xml).find('event').each(function(){
				self.events.push({
					title: $(this).find('title').text(),
					start: $(this).find('start').text(),
					end: $(this).find('end').text(),
					description: $(this).find('description').text(),
					img: $(this).find('img').text(),
					href: $(this).find('href').text()
				});
			});
			// we're good so move on.
			self.bindDatepicker();
		},
		error: function(e){
			// didn't get our xml, failed, so never draw our calendar
		}
	});
}
/*
 * bindDatepicker();
 * We loaded our events, it's time to bind the datePicker
 */
EventsCalendar.prototype.bindDatepicker = function(){
	var self = this;
	$(this._id).datepicker({
		onSelect: function(d,obj){ if(!self.onSelect(d,obj)){ self.noEventsScheduled(d); } },
		beforeShowDay: function(d){ return self.beforeShowDay(d); } 
	});
	if(!this.onSelect(new Date().getMonth()+1+"/"+new Date().getDate()+"/"+new Date().getFullYear())){
		this.noEventsScheduled(new Date().getMonth()+1+"/"+new Date().getDate()+"/"+new Date().getFullYear());
	}
	$(this.components.bottom).find('a').live('click',function(e){
		e.preventDefault();
		$(self.components.date).html(self.evts[$(this).html()-1].start);
		$(self.components.title).html(self.evts[$(this).html()-1].title);
		$(self.components.image).attr('src', self.evts[$(this).html()-1].img);
		$(self.components.description).html(self.evts[$(this).html()-1].description).append("<br/><a href='"+self.evts[$(this).html()-1].href+"'>Read more</a>");
	});
	
}
/*
 * noEventsScheduled();
 * No events scheduled for today, so show it
 */
EventsCalendar.prototype.noEventsScheduled = function(d){
	$(this.components.date).html(d);
	$(this.components.title).html("No events scheduled.");
	$(this.components.description).html("");
	$(this.components.href).hide();
}
/*
 * onSelect(d,obj);
 * We want to display a list of events for this day, sorted by order of appearance in XML
 */
EventsCalendar.prototype.onSelect = function(d, obj){
	var self = this;
	this.evts = this.hasEvent(new Date(d));
	if (this.evts.length > 0) {
		$(this.components.bottom).html("<div style='clear:both;'></div>");
		for (var x = this.evts.length; x >= 1; x--) {
			if (x == 1) {
				$(this.components.date).html(self.evts[x-1].start);
				$(this.components.title).html(self.evts[x-1].title);
				$(this.components.image).attr('src', self.evts[x-1].img);
				$(this.components.description).html(self.evts[x-1].description).append("<br/><a href='"+self.evts[x-1].href+"'>Read more</a>");
			}
			if (x >= 1){
				$(this.components.bottom).show();
				$(this.components.bottom).prepend("<a href='#'>"+ x +"</a>");
			} else { $(this.components.bottom).hide(); }
		}
	}else{
		// we don't have an event
		$(this.components.bottom).hide();
		return false;
	}
	return true;
}
/*
 * beforeShowDay(d);
 * beforeShowDay is called before each day is written to the screen, use this to denote events on the calendar
 */
EventsCalendar.prototype.beforeShowDay = function(d){
	for (var x = 0; x < this.events.length; x++) {
		if (d >= new Date(this.events[x].start) && d <= new Date(this.events[x].end)) {return [true,"has-event"];}
	}
	return [true,""];
}
/*
 * hasEvent(d);
 * this function returns an array of events that occur during a particular day
 */
EventsCalendar.prototype.hasEvent = function(d){
	var evts = Array();
	for (var x = 0; x < this.events.length; x++) {
		if (d >= new Date(this.events[x].start) && d <= new Date(this.events[x].end)) {evts.push(this.events[x]);}
	}
	return evts;
}
