// fcp.Calendar - simple Calendar control for Javascript.
// Copyright (C) 2006, 2007 Felix Pleşoianu
// This file is distributed under the MIT license.
// Last changes (2007-11-04):
// * option to put Sunday first, US style;
// * removed useless clone_date() method.

if (!fcp)
	var fcp = new Object();
if (!fcp.msg)
	fcp.msg = new Object();

fcp.week_days = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"];
fcp.us_week_days = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
fcp.months = ["January", "February", "March", "April", "May", "June",
	"July", "August", "September", "October", "November", "December"];
fcp.msg.prev_year = "Previous year";
fcp.msg.prev_month = "Previous month";
fcp.msg.next_month = "Next month";
fcp.msg.next_year = "Next year";
fcp.initial = new Array(2010, 5, 18, 20100618); //month is actual month minus 1
fcp.date_info = {
	20100618: {"title": "<a href='events/event20100618.shtml'><strong>All New Token Shop Spenders - Get A Free Gem!</strong></a>", "details": "To show our appreciation toward our players, we are giving away a free level 3 Ruby (Stamina +37) to anyone who redeems tokens for the ...<a href='events/event20100618.shtml' class='more'>Read More</a>"},
	20100603: {"title": "<a href='http://do.us.changyou.com/events/event20100603.shtml'><strong>Leveling Contest on the Nirvana PVE Server!</strong></a>", "details": "Want to win a top-of-the-line iPad 3G? Be the highest level on the Nirvana server at 4 pm June 10th to win!...<a href='events/event20100603.shtml' class='more'>Read More</a>"},
	20100405: {"title": "<a href='http://do.us.changyou.com/events/event20100405.shtml'><strong>Storytelling Contest on Ventrilo! </strong></a>", "details": "Here's what you've all been waiting for - the first of those new events Dragon Oath’s new community manager Lucy Song plans to throw!!...<a href='events/event20100405.shtml' class='more'>Read More</a>"},
	20100324: {"title": "<a href='http://do.us.changyou.com/events/event20100324.shtml'><strong>Ventrilo Chat Event</strong></a>", "details": "Looking for the updates on Dragon Oath? Well the Dragon Oath Ventrilo chat is where you need to be! Talk to the Dragon Oath Product team...<a href='events/event20100324.shtml' class='more'>Read More</a>"}
	};

fcp.Calendar = function(element, us_week) {
	if (!element.childNodes)
		throw "HTML element expected";
	this.element = element;
	//this.selection = new Date();
	this.selection = new Date(fcp.initial[0], fcp.initial[1], fcp.initial[2]);
	this.us_week = us_week;
	this.selected_cell = undefined;
	this.generate_month();
	this.render_calendar();
}

fcp.Calendar.prototype.set_date_time = function (date_time) {
	if (date_time.constructor == Date) {
		this.selection = date_time;
		this.generate_month();
		this.render_calendar();
	} else {
		throw "Date object expected (in fcp.Calendar.set_date_time)";
	}
}

fcp.Calendar.prototype.next_month = function () {
	var month = this.selection.getMonth();
	if (month == 11) {
		this.selection.setMonth(0);
		this.selection.setYear(this.selection.getFullYear() + 1);
	} else {
		this.selection.setMonth(month + 1);
	}
	this.generate_month();
	this.render_calendar();
}

fcp.Calendar.prototype.prev_month = function () {
	var month = this.selection.getMonth();
	if (month == 0) {
		this.selection.setMonth(11);
		this.selection.setYear(this.selection.getFullYear() - 1);
	} else {
		this.selection.setMonth(month - 1);
	}
	this.generate_month();
	this.render_calendar();
}

fcp.Calendar.prototype.next_year = function () {
	var is_feb29 = (this.selection.getMonth() == 1)
		&& (this.selection.getDate() == 29);
	if (is_feb29) {
		this.selection.setDate(1);
		this.selection.setMonth(2); // March
	}
	this.selection.setFullYear(this.selection.getFullYear() + 1);
	this.generate_month();
	this.render_calendar();
}

fcp.Calendar.prototype.prev_year = function () {
	var is_feb29 = (this.selection.getMonth() == 1)
		&& (this.selection.getDate() == 29);
	if (is_feb29) {
		this.selection.setDate(1);
		this.selection.setMonth(2); // March
	}
	this.selection.setFullYear(this.selection.getFullYear() - 1);
	this.generate_month();
	this.render_calendar();
}

fcp.Calendar.prototype.generate_month = function () {
	this.raw_data = new Array();
	var week = 0;
	this.raw_data[week] = new Array(7);
	
	var first_of_month = new Date(this.selection.getTime());
	first_of_month.setDate(1);
	var first_weekday = first_of_month.getDay();
	// Move Sunday last were it belongs, he he.
	if (!this.us_week)
		first_weekday = (first_weekday == 0) ? 6 : first_weekday - 1;
	// Fill in the last days from the previous month.
	for (var i = 0; i < first_weekday; i++) {
		this.raw_data[week][i] = 0;
	}
	
	var last_of_month = fcp.Calendar.days_in_month(
		this.selection.getYear(),
		this.selection.getMonth());
	var weekday = first_weekday;
	for (var i = 1; i <= last_of_month; i++) {
		this.raw_data[week][weekday] = i;
		weekday++;
		if (weekday > 6) {
			weekday = 0;
			week++;
			this.raw_data[week] = new Array(7);
		}
	}

	// Fill in the first days from the next month.
	for (var i = weekday; i < 7; i++) {
		this.raw_data[week][i] = 0;
	}
}

fcp.Calendar.prototype.render_calendar = function () {
	this.element.selected_cell = undefined;
	this.element.innerHTML = "";
	this.element.appendChild(this.render_month());
}

fcp.Calendar.prototype.render_heading = function () {
	var heading = document.createElement("caption");
	
	var prev_year = document.createElement("a");
	prev_year.href = "#";
	prev_year.calendar = this;
	prev_year.onclick = function() {
		this.calendar.prev_year();
		return false;
	};
	prev_year.innerHTML = "&lt;&lt;";
	prev_year.title = fcp.msg.prev_year;
	
	var prev_month = document.createElement("a");
	prev_month.href = "#";
	prev_month.calendar = this;
	prev_month.onclick = function() {
		this.calendar.prev_month();
		return false;
	};
	prev_month.innerHTML = "&lt;";
	prev_month.title = fcp.msg.prev_month;
	
	var month_year = document.createTextNode(
		"\u00a0\u00a0" + fcp.months[this.selection.getMonth()]
		+ " " + this.selection.getFullYear() + "\u00a0\u00a0");
		
	var next_month = document.createElement("a");
	next_month.href = "#";
	next_month.calendar = this;
	next_month.onclick = function() {
		this.calendar.next_month();
		return false;
	};
	next_month.innerHTML = "&gt;";
	next_month.title = fcp.msg.next_month;
	
	var next_year = document.createElement("a");
	next_year.href = "#";
	next_year.calendar = this;
	next_year.onclick = function() {
		this.calendar.next_year();
		return false;
	};
	next_year.innerHTML = "&gt;&gt;";
	next_year.title = fcp.msg.next_year;

	heading.appendChild(prev_year);
	heading.appendChild(document.createTextNode("\u00a0\u00a0"));
	heading.appendChild(prev_month);
	heading.appendChild(month_year);
	heading.appendChild(next_month);
	heading.appendChild(document.createTextNode("\u00a0\u00a0"));
	heading.appendChild(next_year);
	return heading;
}

fcp.Calendar.prototype.render_month = function() {
	var html_month = document.createElement("table");
	html_month.className = "calendar";
	html_month.appendChild(this.render_heading());
	
	var thead = document.createElement("thead");
	var tr = document.createElement("tr");
	var week_days = this.us_week ? fcp.us_week_days : fcp.week_days;
	for (var i = 0; i < week_days.length; i++) {
		var th = document.createElement("th");
		th.innerHTML =  week_days[i];
		tr.appendChild(th);
	}
	thead.appendChild(tr);
	html_month.appendChild(thead);
	
	var tbody = document.createElement("tbody");
	for (var i = 0; i < this.raw_data.length; i++) {
		tbody.appendChild(this.render_week(this.raw_data[i]));
	}
	html_month.appendChild(tbody);
	return html_month;
}

fcp.Calendar.prototype.render_week = function (day_numbers) {
	var html_week = document.createElement("tr");
	html_week.align = "right";
	for (var i = 0; i < 7; i++) {
		html_week.appendChild(this.render_day(day_numbers[i]));
	}
	return html_week;
}

fcp.Calendar.prototype.render_day = function (day_number) {
	var td = document.createElement("td");
	var flag = false;
	if (day_number >= 1 && day_number <= 31) {
		var anchor = document.createElement("a");
		anchor.id = this.selection.getFullYear() * 10000 + (this.selection.getMonth() + 1) * 100 + day_number;
		anchor.innerHTML = day_number;
		anchor.calendar = this;
		anchor.date = day_number;
		if(anchor.id in fcp.date_info) 
		{
			anchor.onclick = fcp.Calendar.handle_select;
			anchor.href = "#";
			td.className = "in_month hasevent";
			flag = true;
		}
		td.appendChild(anchor);
		
		/*if (day_number == this.selection.getDate()) {
			this.selected_cell = td;
			td.className = "in_month selected";
		} else if(!flag) {
			td.className = "in_month";
		}*/
		if(!flag)
		{
			td.className = "in_month";
		}
		
		//For initial rendering		
		if(this.selection.getFullYear() == fcp.initial[0] && this.selection.getMonth() == fcp.initial[1] && this.selection.getDate() == day_number)
		{
			this.selected_cell = td;
			td.className = "in_month selected";
		}
	}
	return td;
}

// Do nothing by default.
fcp.Calendar.prototype.onselect = function () {}

fcp.Calendar.days_in_month = function (year, month) {
	if (month < 0 || month > 11)
		throw "Month must be between 0 and 11";
	var day_count = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	if (month != 1) {
		return day_count[month];
	} else if ((year % 4) != 0) {
		return 28;
	} else if ((year % 400) == 0) {
		return 29;
	} else if ((year % 100) == 0) {
		return 28;
	} else {
		return 29;
	}
}

fcp.Calendar.handle_select = function () {
	if (this.calendar.selected_cell)
		this.calendar.selected_cell.className = "in_month";
	this.calendar.selected_cell = this.parentNode;
	this.parentNode.className = "in_month selected"; 
	
	this.calendar.selection.setDate(this.date);
	this.calendar.onselect(this.calendar.selection);
	return false;
}

