$(document).ready(function(){
	// Vars
	tempsLecture 	= 7000; 	// 7s
	tempsEffet	 	= 400; 		// 7s
	eqEnCours		= 0;		// Par défaut, premier article.
	mode			= "auto";	// Mode : Auto ou Manuel.
	
	// Nombre d'articles FootNat
	nbArticleExpress = $('.GabPageHeaderLeftRightInfosL2_Actu').size();
	iEnCours = 0;
	
	$('#GAB_PAGE_HEADER_LEFT_RIGHT_INFOSL2 p').html($('.GabPageHeaderLeftRightInfosL2_Actu:first').html());
	
	// Démarrage des timers
	tTimerFN = setTimeout(TimerFN, tempsLecture+1000);
	timer = setTimeout(changerArticle, tempsLecture);
	
	// Masquage d'éléments & Initalisations diverses
	$('.PostHome').hide();
	$('#SIDEBAR_BLOC_INS').hide();
	$('#MS_Option_Cnx').attr('checked','checked');
	
	// Initialisation de la pagination
	initPagination("FilinfoListeInformation", "FILINFO_PAGINATION", 10, "unePagination", "gPAGINATION");
	
	// Sélection du premier article et parametrage du cadre réception
	$('#ACTUALITE_AFFICHAGE').html($('.PostHome:first').html());
	$('#ACTUALITE_AFFICHAGE').css("min-height", "310px");
	$('#ACTUALITE_AFFICHAGE').css("height", "310px");
	$('.ActualiteSelectionListePuce:first').css("background", "#FFFFFF");
	
	// Opacité des articles avec photos centrés
	$('.PostContentBarre').css("opacity", 0.5);
	
	// Si curseur positioné sur le cadre.
	$('#ACTUALITE_AFFICHAGE').hover(function(){ clearTimeout(timer);},function(){ if (mode != "manuel") { timer = setTimeout(changerArticle,tempsLecture); }});
	$('#GAB_PAGE_HEADER_LEFT_RIGHT_INFOSL2').hover(function(){ clearTimeout(tTimerFN);},function(){ tTimerFN = setTimeout(TimerFN,tempsLecture+1000); });

	// Au changement de statut Connexion/Inscription dans la sidebar
	$('input[name=MySpace_Option]').click(function(){
		if($(this).attr('id') == "MS_Option_Cnx") {
			$('#SIDEBAR_BLOC_INS').hide();
			$('#SIDEBAR_BLOC_CNX').show();
			$(this).attr('checked','checked');
			$('#MS_Option_Ins').removeAttr('checked');
		} else {
			$('#SIDEBAR_BLOC_CNX').hide();
			$('#SIDEBAR_BLOC_INS').show();
			$(this).attr('checked','checked');
			$('#MS_Option_Cnx').removeAttr('checked');
		}
	});
	// Au clic sur l'une des puces de sélection
	$('.ActualiteSelectionListePuce').click(function(){
		var idSelect = $(this).attr('rel');
		
		mode = "manuel";
		clearTimeout(timer);
		
		$('#ACTUALITE_AFFICHAGE').fadeOut(tempsEffet, function(){ 
			$('#ACTUALITE_AFFICHAGE').html($('.PostHome:eq(' + idSelect + ')').html()); 
			$('#ACTUALITE_AFFICHAGE').fadeIn(tempsEffet);
			$('.ActualiteSelectionListePuce:eq(' + eqEnCours +  ')').removeAttr("style");
			$('.ActualiteSelectionListePuce:eq(' + idSelect + ')').css("background", "#FFFFFF");
			eqEnCours = idSelect;
		});
	});

	$('#GAB_PAGE_HEADER_LEFT_RIGHT_RECHERCHE input').css('opacity', 0.5);
	$('#GAB_PAGE_HEADER_LEFT_RIGHT_RECHERCHE_LOUPE').click(function() {
		$('#GAB_PAGE_HEADER_LEFT_RIGHT_RECHERCHE form:first').submit();
	});
});

function changerArticle() {
	var newId	= eqEnCours+1;
	$('#ACTUALITE_AFFICHAGE').fadeOut(tempsEffet, function(){ 
		if ($('.PostHome:eq(' + newId + ')').size() != 1) newId = 0; 
		$('#ACTUALITE_AFFICHAGE').html($('.PostHome:eq(' + newId + ')').html()); 
		$('#ACTUALITE_AFFICHAGE').fadeIn(tempsEffet);
		$('.ActualiteSelectionListePuce:eq(' + eqEnCours +  ')').removeAttr("style");
		$('.ActualiteSelectionListePuce:eq(' + newId + ')').css("background", "#FFFFFF");
		eqEnCours = newId;
		timer = setTimeout(changerArticle, tempsLecture);
		return false;
	});
}
function TimerFN(){
	if ( iEnCours+1 < nbArticleExpress) {
		$('#GAB_PAGE_HEADER_LEFT_RIGHT_INFOSL2 p').fadeOut(function(){
			$('#GAB_PAGE_HEADER_LEFT_RIGHT_INFOSL2 p').html($('.GabPageHeaderLeftRightInfosL2_Actu:eq(' + (iEnCours+1) + ')').html()).fadeIn();
			iEnCours = iEnCours + 1;
		});
	} else {
		$('#GAB_PAGE_HEADER_LEFT_RIGHT_INFOSL2 p').fadeOut(function(){
			$('#GAB_PAGE_HEADER_LEFT_RIGHT_INFOSL2 p').html($('.GabPageHeaderLeftRightInfosL2_Actu:eq(0)').html()).fadeIn();
			iEnCours = 0;
		});
	}
	tTimerFN = setTimeout(TimerFN, tempsLecture+1000);
}
/* Correction IE */
var _st = window.setTimeout;
window.setTimeout = function(fRef, mDelay) {
	if(typeof fRef == 'function'){
		var argu = Array.prototype.slice.call(arguments,2);
		var f = (function(){ fRef.apply(null, argu); }); 
		return _st(f, mDelay);
	}
	return _st(fRef,mDelay); 
};