var calendar_handler = null;

/**
 * @brief 构造器
 **/
function Calendar()
{
	this.calendar_date = new Date();
	this.week_days = ['S','M','S','W','T','F','S'];
	this.month_days = [31,28,31,30,31,30,31,31,30,31,30,31];
	this.month_names = ['January','February','March','April','May','June','July','August','September','October','November','December'];
	this.calenndar_receiver = 'Calendar';
	this.had_show = false;
	this.set_background = false;
	this.background_path = '';
	this.DEFAULT_SETTING = {
		prevMonth : 'PreviousMonth',
		nextMonth : 'NextMonth',
		yearAndMonthName : 'YearAndMonthName',
		currentDay : 'CurrentDay',
		header: 'header'
	};
}

/**
 * @brief 显示日历
 **/
Calendar.prototype.show = function(carticles_list)
{
	start_date = this.calendar_date;
	start_date.setDate(1);
	month_day = (10 > this.calendar_date.getMonth() + 1) ? '0' + (this.calendar_date.getMonth() + 1) : this.calendar_date.getMonth() + 1;
	str = '<ul><li class="' + this.DEFAULT_SETTING.prevMonth + '" id="' + this.DEFAULT_SETTING.prevMonth + '">&lt;</li><li class="' + this.DEFAULT_SETTING.yearAndMonthName + '" onClick="location.href=\'' + TEMP_CONF['url'] + '/../archive/' + this.calendar_date.getFullYear() + month_day +'.html\';" style="cursor:pointer;">' + this.month_names[this.calendar_date.getMonth()] + ' ' + this.calendar_date.getFullYear() + '</li><li class="' + this.DEFAULT_SETTING.nextMonth + '" id="' + this.DEFAULT_SETTING.nextMonth + '">&gt;</li></ul><ul>';
	for ( var i = 0; i < this.week_days.length; i++) str += '<li class="' + this.DEFAULT_SETTING.header + '">' + this.week_days[i] + '</li>';
	str += '</ul>';
	str += '<ul>';
	start_padding = start_date.getDay();
	for ( var i = 0; i < start_padding; i++) str += '<li>&nbsp;</li>';
	start_date = null;
	lines_num = (start_padding > 0) ? 1 : 0;
	current_date = new Date();
	if (this.calendar_date.getFullYear() == current_date.getFullYear() && this.calendar_date.getMonth() == current_date.getMonth()) match_day = true;
	else match_day = false;
	for ( var i = 1; i <= this.getDays(this.calendar_date.getMonth(), this.calendar_date.getFullYear()); i++)
	{
		if (1 === ((i + start_padding) % 7))
		{
			str += '</ul><ul>';
			lines_num++;
		}
		str += '<li' + ((match_day && current_date.getDate() == i) ? ' class="' + this.DEFAULT_SETTING.currentDay + '"' : '');
		if (carticles_list['d' + i])
		{
			append_text = 'There were <b>' + carticles_list['d' + i].length + '</b> articles in this day';
			for (var j = 0; j < carticles_list['d' + i].length; j++) append_text += "<br />&nbsp; " + (j + 1) + '. ' + carticles_list['d' + i][j].replace(/\'|"/g, "\\\'");
			str += ' style="cursor:pointer;color:#0000FF;" onMouseOver="showCalendarHint(this, \'' + append_text + '\');" onMouseOut="hiddenCalendarHint();" onClick="location.href=\'' + TEMP_CONF['url'] + '/../archive/' + this.calendar_date.getFullYear() + month_day +'.html?day=' + i + '\';"';
		}
		str += '>' + i + '</li>' + "\n";
	}
	str += '</ul>';
	current_date = null;
	lines_num++;
	if (xGetElementById(this.calenndar_receiver) && !this.had_show)
	{
		xInnerHtml(this.calenndar_receiver, str);
		this.setShowStatus(true);
		xHeight(this.calenndar_receiver, (lines_num - 1) * 20 + 30);
		xAddEventListener(this.DEFAULT_SETTING.prevMonth, 'click', this.previousMonth, true);
		xAddEventListener(this.DEFAULT_SETTING.nextMonth, 'click', this.nextMonth, true);
		if (this.set_background && '' != this.background_path) xGetElementById(this.calenndar_receiver).style.backgroundImage = "url('" + this.background_path + this.calendar_date.getMonth() + '.png' + "')";
	}
}

/**
 * @brief 跳至上一个月
 **/
Calendar.prototype.previousMonth = function(event)
{
	if (calendar_handler.calendar_date.getMonth() > 0) calendar_handler.calendar_date.setMonth(calendar_handler.calendar_date.getMonth() - 1);
	else
	{
		calendar_handler.calendar_date.setYear(calendar_handler.calendar_date.getFullYear() - 1);
		calendar_handler.calendar_date.setMonth(11);
	}
	calendar_handler.setShowStatus(false);
	start_time = Date.parse(new Date(calendar_handler.calendar_date.getFullYear(), calendar_handler.calendar_date.getMonth(), 1)) / 1000;
	end_time = start_time + calendar_handler.getDays(calendar_handler.calendar_date.getMonth(), calendar_handler.calendar_date.getFullYear()) * 24 * 3600;
	url = TEMP_CONF['url'] + '/../action.php?action=datelist&start=' + start_time + '&end=' + end_time;
	getUrl(url, 'articles_list', 'calendar_handler.show(articles_list);');
	calendar_handler.show();
}

/**
 * @brief 跳至下一个月
 **/
Calendar.prototype.nextMonth = function(event)
{
	if (calendar_handler.calendar_date.getMonth() < 11) calendar_handler.calendar_date.setMonth(calendar_handler.calendar_date.getMonth() + 1);
	else
	{
		calendar_handler.calendar_date.setYear(calendar_handler.calendar_date.getFullYear() + 1);
		calendar_handler.calendar_date.setMonth(0);
	}
	calendar_handler.setShowStatus(false);
	start_time = Date.parse(new Date(calendar_handler.calendar_date.getFullYear(), calendar_handler.calendar_date.getMonth(), 1)) / 1000;
	end_time = start_time + calendar_handler.getDays(calendar_handler.calendar_date.getMonth(), calendar_handler.calendar_date.getFullYear()) * 24 * 3600;
	url = TEMP_CONF['url'] + '/../action.php?action=datelist&start=' + start_time + '&end=' + end_time;
	getUrl(url, 'articles_list', 'calendar_handler.show(articles_list);');
	calendar_handler.show();
}

/**
 * @brief 设置显示状态
 **/
Calendar.prototype.setShowStatus = function(status)
{
	this.had_show = status;
}

/**
 * @brief 判断是否为闰年
 **/
Calendar.prototype.getDays = function (month, year)
{
	if (1 == month) return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;
	else return this.month_days[month];
}