function Calendar(name){
	document.write('<div id="calendar"></div>');
	this.name = name;
	this.now = new Date();
	this.year = this.now.getFullYear();
	this.month = this.now.getMonth();
	this.day = this.now.getDate();
	this.monthName = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	this.weekName = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
	this.daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31,30, 31, 30, 31);
	this.element = document.getElementById('calendar');
	this.init();
}
Calendar.prototype.init = function() {
	var strHtml = '';
	strHtml += this.createHead();
	strHtml += this.createWeek();
	strHtml += this.createDays();
	strHtml += this.createFoot();
	this.element.innerHTML = strHtml;
}
Calendar.prototype.getDays = function() {
	if (1 == this.month) return ((0 == this.year % 4) && (0 != (this.year % 100))) ||(0 == this.year % 400) ? 29 : 28;
	else return this.daysInMonth[this.month];
}
Calendar.prototype.createHead = function() {
	var strResult = '';
	strResult += '<span class="head">' + this.monthName[this.month] + ' ' + this.year + '</span>';
	strResult += '<span class="back"><a href="javascript:' + this.name + '.backToday();" title="Back today" class="backtoday">Today</a></span>';
	return strResult;
}
Calendar.prototype.createWeek = function() {
	var strResult = '';
	for (var i = 0; i < 7; i++) {
		if (i == 0 || i == 6) strResult += '<span class="we">';
		else strResult += '<span class="ww">';
		strResult += this.weekName[i].substr(0,2) + '</span>';
	}
	return strResult;
}
Calendar.prototype.createDays = function() {
	var strResult = '';
	var i = 0;
	var d = this.getDays();
	var n = Math.ceil(d / 7) * 7;
	var w = new Date(this.year, this.month, 1).getDay();
	for (var j = 0; j < w; j++) { //输出前空格
		i += 1;
		strResult += '<span>&nbsp;</span>';
	}
	for (var j = 1; j <= d; j++) { //输出日期格
		i += 1;
		if (j == this.day) {
			strResult += '<span class="today">' + j + '</span>';
		} else {
			var k = new Date(this.year, this.month, j).getDay();
			if (k == 0 || k == 6) {
				strResult += '<span class="weekend">' + j + '</span>';
			} else {
				strResult += '<span>' + j + '</span>';
			}
		}
	}
	for (var j = 0; j < (Math.ceil(i / 7) * 7 - i); j++) { //输出后空格
		strResult += '<span>&nbsp;</span>';
	}
	return strResult;
}
Calendar.prototype.createFoot = function() {
	return '<span class="foot"><a href="javascript:' + this.name + '.changeMonth(\'p\');" title="Prev Month" class="arrow">3</a> MONTH <a href="javascript:' + this.name + '.changeMonth(\'n\');" title="Next Month" class="arrow">4</a>　　<a href="javascript:' + this.name + '.changeYear(\'p\');" title="Prev Year" class="arrow">3</a> YEAR <a href="javascript:' + this.name + '.changeYear(\'n\');" title="Next Year" class="arrow">4</a></span>';
}
Calendar.prototype.changeYear = function(arg) {
	if (arg == 'p') this.year -= 1;
	if (arg == 'n') this.year += 1;
	this.init();
}
Calendar.prototype.changeMonth = function(arg) {
	var m;
	if (arg == 'p') m = this.month - 1;
	if (arg == 'n') m = this.month + 1;
	if (arg == 'p' || arg == 'n') {
		if ( m > -1 && m < 12) {
			this.month = m;
		} else if (m < 0) {
			this.year -= 1;
			this.month = 11;
		} else if (m > 11) {
			this.year += 1;
			this.month = 0;
		}
	}
	this.init();
}
Calendar.prototype.backToday = function() {
	this.year = this.now.getFullYear();
	this.month = this.now.getMonth();
	this.init();
}
