/* Require jQuery for Ajax and DOM Ready event*/

var now = new Date();
var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());

var calendar_c_month = now.getMonth() + 1;
var calendar_c_year = now.getFullYear();
$(document).ready(function() {
	calendar_draw_calendar(calendar_c_month, calendar_c_year);
});

// Output Buffering
var calendar_buffer = ''; 
function calendar_buffer_clean() {
	calendar_buffer = '';
}
function calendar_buffer_flush() {
	calendar_container.innerHTML = calendar_buffer;
	calendar_buffer_clean();
}
function calendar_echo(t) {
	calendar_buffer += t;
}

var calendar_monthnames = [
'Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin',
'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'
]; // You can translate it for your language.

var calendar_daynames = [
'Dim', 'Lun', 'Mar', 'Me', 'Jeu', 'Ven', 'Sam'
]; // You can translate it for your language.

// Calendar template
function calendar_template_main_above(t) {
	return '<table cellpadding="3" cellspacing="1" class="calendar_tbl" align="center">'
	     + '<tr>'
		 + '<td class="calendar_head" style="cursor: pointer" onclick="calendar_py();">&lt;&lt;</td>'
		 + '<td class="calendar_head" style="cursor: pointer" onclick="calendar_pm();">&lt;</td>'
		 + '<td class="calendar_head" id="calendar_month_display" style="cursor: default" colspan="3">' + t + '</td>'
		 + '<td class="calendar_head" style="cursor: pointer" onclick="calendar_nm();">&gt;</td>'
		 + '<td class="calendar_head" style="cursor: pointer" onclick="calendar_ny();">&gt;&gt;</td>'
		 + '</tr>'
		 + '<tr>';
}

function calendar_template_day_row(t) {
	return '<td class="calendar_subhead">' + t + '</td>';
	// Define width in CSS, XHTML 1.0 Strict doesn't have width property for it.
}

function calendar_template_new_week() {
	return '</tr><tr>';
}

function calendar_template_blank_cell(colspan) {
	return '<td colspan="' + (colspan+6)%7 + '"></td>'
}

function calendar_template_day(d, m, y) {
	var cell_date = new Date(y, m-1, d);
	var cell_css_class = 'calendar_cell';
	var html_cell_attributes = '';
	var cell_event=false;
	if (month_events && (cell_event=month_events[((''+d).length<2?'0':'')+d])) {
		cell_css_class+= '_event';
		html_cell_attributes+= ' title="'+cell_event.introduction+'"';
		html_cell_attributes+= ' onclick="document.location=\''+cell_event.url_alias+'\';"';		
	}
	if (today.valueOf()>cell_date.valueOf()) {
		cell_css_class+= '_past';
	} else if (today.valueOf()==cell_date.valueOf()) {
		cell_css_class+= '_today';
	} else {
		cell_css_class+= '_future';		
	}
	html_cell_attributes+= ' class="'+cell_css_class+'"';
	return '<td'+html_cell_attributes+'>' + d + '</td>';
	// Define width the day row.
}

function calendar_template_main_below() {
	return '</tr>'
	     + '</table>';
}

// This one draws calendar...
var month_events;
function calendar_draw_calendar(m, y) {
	// Load month events through AJAX/JSON synchronal request
	$('#calendar_month_display').html('Charge<br/>...');
	month_events={};
	$.ajax({
		async: false,
		dataType: 'json',
		url: agenda_url+'/(year)/'+y+'/(month)/'+m,
		error: function(XMLHttpRequest, textStatus) {
			//alert(textStatus);
			//TODO:
		},
		success: function(data) {
			month_events=data;
		}
	});
	// First clean the output buffer.
	calendar_buffer_clean();
	// Here we go, do the header
	calendar_echo (calendar_template_main_above(calendar_monthnames[m - 1] + '<br />' + y));
	for (i = 1; i < 8; i ++) {
		calendar_echo (calendar_template_day_row(calendar_daynames[i%7]));
	}
	// Make a date object.
	var calendar_dc_date = new Date();
	calendar_dc_date.setMonth(m - 1);
	calendar_dc_date.setFullYear(y);
	calendar_dc_date.setDate(1);
	if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
		days = 31;
	} else if (m == 4 || m == 6 || m == 9 || m == 11) {
		days = 30;
	} else {
		days = (y % 4 == 0) ? 29 : 28;
	}
	var first_day = calendar_dc_date.getDay();
	var first_loop = 1;
	// Start the first week
	calendar_echo (calendar_template_new_week());
	// If monday is not the first day of the month, make a blank cell...
	if (first_day != 1) {
		calendar_echo (calendar_template_blank_cell(first_day));
	}
	var j = first_day;
	for (i = 0; i < days; i++) {
		// Today is monday, make a new week.
		// If this monday is the first day of the month,
		// we've made a new row for you already.
		if (j == 1 && !first_loop) {
			// New week!!
			calendar_echo (calendar_template_new_week());
		}
		// Make a row of that day!
		calendar_echo (calendar_template_day(i + 1, m, y));
		// This is not first loop anymore...
		first_loop = 0;
		// What is the next day?
		j ++;
		j %= 7;
	}
	// Do the footer
	calendar_echo (calendar_template_main_below());
	// And let's display..
	calendar_buffer_flush();
}

// Moves to the next month...
function calendar_nm() {
	$('#calendar_month_display').html('Charge<br/>...');
	// Increase the current month.
	calendar_c_month++;
	// We have passed December, let's go to the next year.
	// Increase the current year, and set the current month to January.
	if (calendar_c_month > 12) {
		calendar_c_month = 1; 
		calendar_c_year++;
	}
	// Redraw the calendar.
	calendar_draw_calendar(calendar_c_month, calendar_c_year);
}

// Moves to the previous month...
function calendar_pm() {
	// Decrease the current month.
	calendar_c_month--; 
	// We have passed January, let's go back to the previous year.
	// Decrease the current year, and set the current month to December.
	if (calendar_c_month < 1) {
		calendar_c_month = 12; 
		calendar_c_year--; 
	}
	// Redraw the calendar.
	calendar_draw_calendar(calendar_c_month, calendar_c_year);
}

// Moves to the next year...
function calendar_ny() {
	// Increase the current year.
	calendar_c_year++;
	// Redraw the calendar.
	calendar_draw_calendar(calendar_c_month, calendar_c_year);
}

// Moves to the previous year...
function calendar_py() {
	// Decrease the current year.
	calendar_c_year = calendar_c_year--
	// Redraw the calendar.
	calendar_draw_calendar(calendar_c_month, calendar_c_year);
}

