var speed = 1000;
		
var timerRunning = false;
var myTimer = null;
var timerSpeed = 7000;


$(document).ready(function(){
	
	var domain = $('body').attr('domain');
	var baseUrl = $('body').attr('baseUrl');
	
	if ($("#latest-news ul li").length > 1) {
		$("#latest-news ul").newsTicker(5000);
	}
	
	if ($("#entraineurs_slideshow li").length > 1) {
		$("#entraineurs_slideshow").newsTicker(5000);
	}
	
	
	 $("ul.demo").ytplaylist({addThumbs:true, playerWidth:'670', playerHeight:'400', autoPlay: true, holderId: 'ytvideo'});
	
	var descriptionopen = null;
	
	
	var couleur = "";
	$('.popup_horaires_calendrier .popup-content .cours').hover(
	  function () {
		$(this).addClass("hover");
	  },
	  function () {
		$(this).removeClass("hover");
	  }
	);
	
	
	var couleur2 = "";
	$('.popup_horaires_calendrier .popup-content .cours').click(function() {
	  		
		var hauteurbulle = $('#descriptions').offset();
		
		$("body").animate({ scrollTop: hauteurbulle.top }, "slow");
		
		var horaire = $('.popup_horaires_calendrier').attr('name');
		
		if (descriptionopen != $(this).attr('target')) {
			if (descriptionopen == null)  {
				
				var cliqued = $(this).attr('target')
				
				$('.popup_horaires_calendrier .popup-content .cours').each(function(i){
					
					$('#descriptions #'+$(this).attr('target')).find('.description-button').remove();
					
					if ($(this).attr('target') == cliqued) {
						$(this).addClass("selected");
					}
				});
				
				var cour = $(this).attr('target');
			
				$('#descriptions #'+$(this).attr('target')).append('<div class="description-button"><a href="'+domain+baseUrl+'inscription/'+horaire+'/'+cour+'/">Inscrivez-vous à ce cours ici</a></div>');
				
				$('#descriptions #'+$(this).attr('target')).slideDown('fast');
				
				descriptionopen = $(this).attr('target');
			} else {
				
				var cliqued = $(this).attr('target')
				
				$('#descriptions #'+ descriptionopen).slideUp('fast', function() {
					
					$('.popup_horaires_calendrier .popup-content .cours').each(function(i){
						
						$('#descriptions #'+$(this).attr('target')).find('.description-button').remove();
						
						if ($(this).attr('target') == cliqued) {
							$(this).addClass("selected");
						} else {
							$(this).removeClass("selected");
						}
					});
					
					var cour = cliqued;
					
					$('#descriptions #'+cliqued).append('<div class="description-button"><a href="'+domain+baseUrl+'inscription/'+horaire+'/'+cour+'/">Inscrivez-vous à ce cours ici</a></div>');
					
					$('#descriptions #'+cliqued).slideDown('fast');
					descriptionopen = cliqued;
				});			
			}

		}
	
	});
	
	initAlerte('#alerte');
	
});



function startTimer(id) {
	if (!timerRunning) {
		myTimer = setTimeout( "alerte('"+id+"')", timerSpeed);
		timerRunning = true;
	}
}

function initAlerte(id) {
	if ($(id + ' li').size() > 0) {
		if ($(id + ' li').first().attr('warning') == 'true') {
			$(id).addClass('red');
		}
		$(id + ' li').first().show();
		$(id).fadeIn(speed);
		
		if ($(id + ' li').size() > 1) {
			startTimer(id);
		}
	}
}

function alerte(id) {
	
	$(id + ' li').each(function() {
		
		var current = $(this);
		
		if (current.hasClass('selected')) {
			var nextInList = current.next();
			if (current.next().length == 0) {
				nextInList = $(id + ' li').first();
			}
			
			current.fadeOut(speed, function() {
				if ($(id).hasClass('red')) {
					$(id).removeClass('red');
				}
				if (nextInList.attr('warning') == 'true') {
					$(id).addClass('red');
				}
				nextInList.fadeIn(speed);
			});
			
			current.removeClass('selected');
			nextInList.addClass('selected');
			
			return false;
			
		}
		
	});
	
	timerRunning = false;
	startTimer(id);
}
