var busyTV = false;
var busyOnglet = false;
$(document).ready(function() {
	
	/* Onglets */	
	$('.group_actus').append('<div class="c1"></div><div class="c2"></div>').addClass('javascript_ok');
	$('.onglet').append('<div class="c3"></div><div class="c4"></div>');
	
	//Onglet le plus haut = hauteur pour tous
	var top_height = 0;
	var onglets = [];
	$('.group_actus dd').each(function(i){
		var h = $(this).outerHeight();
		if(h > top_height)
			top_height = h;
		onglets.push($(this).parent().attr('id'));
		$(this).parent().data('repere', i);
		$(this).append('<div class="deco"></div>');
	});
	var currentOnglet = 0;
	$('.group_actus').css('height', top_height+'px');
	$('.group_actus .current dd').show();
	$('.group_actus dt a').click(function() 
	{
		currentOnglet = $(this).parents('dl').data('repere');
		ShowOnglet($(this).parents('dl').attr('id'));
		return false;
	});
	
	//Ajoute des fleches de navigation
	if($('.group_actus dl').length > 1)
	{
		//Ajout des fleches
		$('.group_actus').append('<div id="ongletsL"></div><div id="ongletsR"></div>');
		
		//Actions sur fleches
		$('#ongletsL').click(function()
		{
			if(busyOnglet)
				return false;
			currentOnglet--;
			if(currentOnglet < 0)
				currentOnglet = onglets.length-1;
			ShowOnglet(onglets[currentOnglet]);
			
			$(this).stop().addClass('hover').animate({
				left: '20px'
			}, 100, function() {
				$(this).animate({
					left: '32px'
				}, 200).removeClass('hover');
			});

		});
		
		$('#ongletsR').click(function() 
		{
			if(busyOnglet)
				return false;
			currentOnglet++;
			if(currentOnglet > onglets.length-1)
				currentOnglet = 0;
			ShowOnglet(onglets[currentOnglet]);
			
			$(this).stop().addClass('hover').animate({
				right: '20px'
			}, 100, function() {
				$(this).animate({
					right: '32px'
				}, 200).removeClass('hover');
			});
		});
	}else if($('.group_actus dl').length == 1){	//Enleve le seul onglet existant
		$('.group_actus').css('margin-bottom',0).append('<div class="c3"></div><div class="c4"></div>').find('dt').remove();
		
	}
	
	//Egalise les hauteurs de blocs
	var precs = [];
	var top_h = 0;
	$('.encadre_group:not(.group_actus) > dl').each(function(i){
		
		if($(this).hasClass('clearer') && $(precs).length>0)
		{
			$(precs).css('height', top_h+'px');
			precs = [];
			top_h = 0;
		}
		h = $(this).height();
		
		if(top_h < h)
			top_h = h;

		var h = $(this).height();
		if(h > top_height)
			top_height = h;
		precs.push(this);
	});
	if($(precs).length>0)
	{
		$(precs).css('height', top_h+'px');
	}
	
	
	/* CosneTv */
	
	$('#tv dl').not(':first').hide();
	$('#tv dl:first').addClass('current');
	
	var currentTv = 0;
	var totalTv = $('#tv dl').length-1;
	if(totalTv > 1)
	{
		/* Afficher fleche */
		$('#tv').append('<div id="tvL"></div><div id="tvR"></div>');
		$('#tvL').click(function() 
		{
			if(busyTV)
				return false;
			currentTv--;
			if(currentTv < 0)
				currentTv = totalTv;
			ShowTv(currentTv);
			
			$(this).stop().addClass('hover').animate({
				left: '26px'
			}, 100, function() {
				$(this).animate({
					left: '32px'
				}, 200).removeClass('hover');
			});

		});		
		$('#tvR').click(function() 
		{
			if(busyTV)
				return false;
			currentTv++;
			if(currentTv > totalTv)
				currentTv = 0;
			ShowTv(currentTv);
			
			$(this).stop().addClass('hover').animate({
				right: '26px'
			}, 100, function() {
				$(this).animate({
					right: '32px'
				}, 200).removeClass('hover');
			});
		});
	}
	
	/* Calendrier */
	$('.calendar td a').each(function(i){
		var pos = $(this).position();
		var tgt = $(this).next('.calendar_show');
		tgt.append('<div class="rond"></div><div class="filet"></div>');
		var p = $(this).position();
		$(tgt).children('.filet').css({'width': (p.left+22)+'px' , 'right':-(p.left+28)+'px'});
		var h = $(tgt).outerHeight();
		$(this).next('.calendar_show').css('top', (Math.round(pos.top-h/2+12))+'px');
	});
	$('.calendar td a').mouseenter(function() 
	{	
		
		$(this).addClass('calendar_current');
		var tgt = $(this).next('.calendar_show');
		calGo = $.timer(300,function(){
			tgt.fadeIn("fast");
		});
	}).mouseleave(function()
	{
		$(this).removeClass('calendar_current');
		var tgt = $(this).next('.calendar_show');
		$.clearTimer(calGo);
		tgt.hide();
	});
});
function ShowOnglet(id)
{
	busyOnglet = true;
	$('#'+id).parents('.group_actus').find('.current dd').fadeOut('fast', function() {
		$(this).parent().removeClass('current');
		$('#'+id).addClass('current').find('dd').fadeIn(200, function() {
			busyOnglet = false;
		});
	});
	return false;
}
function ShowTv(id)
{
	busyTV = true;
	$('#tv dl.current').fadeOut('fast', function() {
		$(this).removeClass('current');
		$("#tv dl:eq("+id+")").addClass('current').fadeIn(200, function() {
			busyTV = false;
		});
	});


}
