//Base height : 241
//Base width  : 211

function openCalendar(e)
{
	if(e != undefined) e.preventDefault();
	$(this).find('a').addClass('active');
	$("#calendrier").height(0);
	$("#calendrier").animate({height:241}, 500, function()
	{
		linkCalendar();
		//$("#calendrier").css('overflow','visible');
	});
	backpop.unbind('click');
	backpop.css({"opacity": "0"});
	backpop.show();
	backpop.animate({"opacity": "0.50"}, 500);
	centerPopup();
	backpop.click(closeCalendar);
}

function linkCalendar()
{
	$("table.calendar td.active a").unbind('click');
	$("table.calendar td.active a").click(function(e)
	{
		e.preventDefault();
		$("table.calendar td.active a").removeClass('active');
		$(this).addClass('active');
		var $a = $(this);
		
		if($("#calendrier .date").length > 0)
		{
			$("#calendrier .date").fadeOut(500, function()
			{
				$(this).remove();
			});
			$("#calendrier .mainspec").fadeOut(500, function()
			{
				$(this).remove();
				calendar_showSpec($a);
			});
		}
		else
		{
			$("#calendrier").animate({width:687}, 500, function()
			{
				calendar_showSpec($a);
			});
		}
	});
}

function calendar_showSpec($a)
{
	$.get("dyn/calendar_spec.php?ts="+$a.attr('rel')+"&nometa=true", function(data)
	{
		$("#calendrier").append(data);
		var thesize = ($("#calendrier .mainspec").offset().top - $("#calendrier").offset().top) + $("#calendrier .mainspec").height() + 10;
		if(thesize < 241) thesize = 241;
		$("#calendrier").animate({height:thesize});
		$("#calendrier .mainspec").hide().fadeIn(500);
		$("#calendrier .date").hide().fadeIn(500);
		$("#calendrier .spec a").remove();
		
		$("#calendrier .spec").hover(function()
		{
			$(this).addClass('active');
		}, function()
		{
			$(this).removeClass('active');
		});
		
		$("#calendrier .spec").click(function()
		{
			closeCalendar();
			loadUniq($(this).attr("rel"));
		});
	});
}

function closeCalendar()
{
	//$("#calendrier").css('overflow','hidden');
	$("#calendrier").animate({height:0}, 500, function()
	{
		$(".calendrier a").removeClass('active');
	});
	backpop.unbind('click');
	backpop.fadeOut(500);
	backpop.click(closeCalendar);
}

$(function()
{
	$("li.calendrier").click(openCalendar);
	$("#calendrier").hide();
	
	$(".caload").live('click', function(e)
	{
		var $a = $(this);
		e.preventDefault();
		$("#calendrier table").fadeOut(500, function()
		{
			$(this).remove();
			$.get($a.attr('href')+"&nometa=true", function(data)
			{
				$("#calendrier .nav-cal").remove();
				$("#calendrier").prepend(data);
				$("#calendrier table").hide().fadeIn(500);
				linkCalendar();
			});
		})
	});
});