/**
 * FYCal
 * Inline javascript-generated calendar
 * @author Mike Matz <mike@pixor.net>
 */

/**
 * Constructor
 * @param array events
 */
function FYCal(events) {
	for (var x = 0; x < events.length; x++) {
		this.addEvent(events[x]);
	}
	this._events = events;
}

FYCal.prototype._eventsHash;
FYCal.prototype._year;
FYCal.prototype._month;
FYCal.prototype._target;

FYCal.prototype.addEvent = function(theEvent) {
	if (this._eventsHash == undefined) {
		this._eventsHash = new Object();
	}
	var k = 'd' + theEvent.date.getFullYear().toString() + '.' + theEvent.date.getMonth().toString() + '.' + theEvent.date.getDate().toString();
	this._eventsHash[k] = theEvent;
}

FYCal.prototype.next = function() {
	var theDate = new Date(this._year, this._month, 1);
	theDate.setMonth(theDate.getMonth() + 1);
	this._month = theDate.getMonth();
	this._year = theDate.getFullYear();
	this.write();
}

FYCal.prototype.prev = function() {
	var theDate = new Date(this._year, this._month, 1);
	theDate.setMonth(theDate.getMonth() - 1);
	this._month = theDate.getMonth();
	this._year = theDate.getFullYear();
	this.write();
}

FYCal.prototype.write = function(target) {
	if (target != '' && target != undefined) {
		this._target = target;
	}
	var currDate = new Date();
	var theDate = new Date();
	if (this._year == undefined) {
		this._year = theDate.getFullYear();
		this._month = theDate.getMonth();
	}
	currDate.setDate(1);
	currDate.setMonth(this._month);
	currDate.setFullYear(this._year);
	var html = "";
	html += '<table id="calendar" cellpadding="2" cellspacing="0" border="0">';
	html += '<tr><td colspan="7" class="month">';
	html += '<table width="100%"><tr><td><a href="javascript:fyCalPrev()">&laquo;</a></td><td width="100%">' + this.monthName(currDate.getMonth()) + ' ' + currDate.getFullYear().toString() + '</td><td><a href="javascript:fyCalNext()">&raquo;</a></td></tr></table>';
	html += '</tr><tr>';
	html += '<td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td>';
	html += '</tr>';
	for (var col = 0; currDate.getMonth() == this._month; col++) {
		html += '<tr>';
		for (var day = 0; day < 7; day++) {
			html += '<td';
			if (currDate.getDate() == theDate.getDate() && currDate.getMonth() == theDate.getMonth() && currDate.getYear() == theDate.getYear()) {
				html += ' class="today"';
			}
			html += '>';
			if ((!col && day < currDate.getDay()) || (currDate.getMonth() > this._month)) {
				html += '</td>';
			} else {
				var k = 'd' + currDate.getFullYear().toString() + '.' + currDate.getMonth().toString() + '.' + currDate.getDate().toString();
				if (this._eventsHash[k] != undefined) {
					html += '<a href="' + this._eventsHash[k].url + '" title="' + this._eventsHash[k].title + '">' + currDate.getDate().toString() + '</a></td>';
				} else {
					html += currDate.getDate().toString() + '</td>';
				}
				currDate.setDate(currDate.getDate() + 1);
			}
			//alert('currDate.getDate()='+currDate.getDate());
		}
		html += '</tr>';
	}
	html += '</table>';
	document.getElementById(this._target).innerHTML = html;
}
FYCal.prototype.monthName = function(month) {
	var months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	return months[month];
}
FYCal.prototype.dayName = function(day) {
	var days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
	return days[day];
}
