$(function(){
	var date = new Date();
	var month = date.getMonth()+1;
	var year = date.getFullYear();
	get_month(month, year);
	//console.log(date.getMonth());

	$("#prev_month").click(function(){
		month--;
		if (month == 0){
			month = 12;
			year--;
		}

		get_month(month, year);
	});

	$("#next_month").click(function(){
		month++;
		if (month == 13){
			month = 1;
			year++;
		}

		get_month(month, year);
	});

	function get_month(month, year){
		$.post('../events/index/true', {
				m : month,
				y : year
			}, function(data) {
				$("#calendar").html(data);

				// hide any tooltips loaded
				$(".event_tooltip").hide();
				$(".delete_event").hide();

				$("#month").text($("#month_hidden").text());

				// show tooltip if it exists
				$(".cal_day").hover(function(){
					$(this).children(".event_tooltip").fadeIn("fast");
				}, function(){
					$(this).children(".event_tooltip").fadeOut("fast");
				});

				$(".event_text").hover(function(){
					$(this).children(".delete_event").fadeIn("fast");
				}, function(){
					$(this).children(".delete_event").fadeOut("fast");
				});
			}, "html"
		);
	}
});
