var _months = ["January","February","March","April","May","June","July","August","September","October","November","December"]
var _weekdays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]

function Calendar() {
	this.parent = null;
	this.el = null;
	this.month;
	this.year;
	this.dates = new Array();
	this.events = new Array();
	this.eventItems = new Array();
}

Calendar.prototype.render = function(element) {
	this.el = document.createElement('dl');
	this.month = this.events[0].getMonth();
	this.year = this.events[0].getFullYear();
	var totalDays = new Array(31, ((this.year % 4 == 0 && this.year % 100 != 0) || this.year % 400 == 0 ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	this.firstDay = (new Date(this.year, this.month, 1)).getDay();
	var dt = document.createElement('dt');
	var imgM = document.createElement('img');
	imgM.setAttribute('alt', _months[this.month]);
	imgM.setAttribute('src', '/imgs/calendar/m_' + this.month + '.gif');
	var imgY = document.createElement('img');
	imgY.setAttribute('alt', this.year);
	imgY.setAttribute('src', '/imgs/calendar/y_' + this.year + '.gif');
	dt.appendChild(imgM);
	dt.appendChild(imgY);
	this.el.appendChild(dt);
	for (var i=0; i<_weekdays.length; i++) {
			var dd = document.createElement('dd');
			var img = document.createElement('img');
			img.setAttribute('alt', _weekdays[i]);
			img.setAttribute('src', '/imgs/calendar/w_' + i + '.gif');
			dd.appendChild(img);
			this.el.appendChild(dd);
	}
	if (this.firstDay > 0) {
		for (i=0; i<this.firstDay; i++) {
			var dd = document.createElement('dd');
			var txt = document.createTextNode(' ');
			dd.appendChild(txt);
			this.el.appendChild(dd);
		}
	}
	for(i=0; i<totalDays[this.month]; i++)
	{
		var dd = document.createElement('dd');
		var img = document.createElement('img');
		var num = i+1;
		img.setAttribute('alt', num);
		img.setAttribute('src', '/imgs/calendar/d_' + num + '.gif');
		dd.appendChild(img);
		this.el.appendChild(dd);
		this.dates.push(dd);
	}
	element.appendChild(this.el);
	this.applyStyles();
}

Calendar.prototype.applyStyles = function() {
	var els = this.el.getElementsByTagName('dd');
	for (i=0; i<els.length; i++) {
		num = i+1;
		if (i>0 & num%7 == 0) {
			els[i].className = 'last';	
		}
	}
}

Calendar.prototype.bind = function() {
	for (var i=0; i<this.events.length; i++) {
		var date = this.events[i].getDate();
		var el = this.dates[date-1];
		el.firstChild.setAttribute('src', '/imgs/calendar/a_' + date + '.gif');
		this.parent.eventTriggers.push(el);
	}
}