var page_breves_actu=1;
var max_pages_breves=0;
var game="all";
var image_liste=new Image();
image_liste.src="/tpl/default/design/images/indexvnew/aaa_popuphp.png";
var request=false;

var url=window.location.href.split("#");
var game_load="";
if(url.length>1){
	game_load=url[url.length-1];
	if(game_load!="all" && game_load!=""){
		
	}
}

jQuery(document).ready(function(){
	timeoutAffiche=setTimeout('affiche_next()', 5000);
	jQuery(".link_affiche").hover(function(){
		var id_temp=jQuery(this).attr("id").split("link_affiche_");
		id_temp=id_temp[1];
		toggle_affiche(id_temp);
	});
	jQuery("#link_brev_prev").click(go_prev_brev);
	jQuery("#link_brev_next").click(go_next_brev);
	max_pages_breves=jQuery("#max_pages_breves").val();
	jQuery(".breves_search input").click(function(){
		if(jQuery(this).val()=="Rechercher...") jQuery(this).val("");
	});
	
	// La liste d'affiches
	jQuery(".slc_link_affiche").hover(function(){ liste_affiche(this); });
	jQuery(".slideshow").hover(function(){ affiche_liste_affiches(); }, function(){ cache_liste_affiches(); });
	jQuery(".slc_affiche").hover(function(){ liste_affiches=true; }, function(){ cache_liste_affiches(); });
	
	// Les tooltips
	jQuery(".foot_games ul li a").tooltip({delay:0,showURL:false});
	
	// Les blocs drag'n'dropables
	if(place_blocs!=''){
		place_blocs=place_blocs.split("-");
		for(var i=0;i<place_blocs.length;i++){
			place_blocs[i]=jQuery("#bloc_"+place_blocs[i]);
		}
		jQuery(".col_gauche").html("");
		for(var i=0;i<2;i++){
			jQuery(".col_gauche").append(place_blocs[i]);
		}
		jQuery(".col_droite").html("");
		for(var i=2;i<place_blocs.length;i++){
			jQuery(".col_droite").append(place_blocs[i]);
		}
		jQuery(".col_gauche").fadeIn("slow");
		jQuery(".col_droite").fadeIn("slow");
	}
	else {
		jQuery(".col_gauche").fadeIn("slow");
		jQuery(".col_droite").fadeIn("slow");
	}
	jQuery(".col_gauche").sortable({ connectWith: ".col_gauche", handle:".move_bloc", opacity:0.8, placeholder: "move_is_possible", forcePlaceholderSize:true, revert:true, scroll:false, update:update_blocs });
	jQuery(".col_droite").sortable({ connectWith: ".col_droite", handle:".move_bloc", opacity:0.8, placeholder: "move_is_possible", forcePlaceholderSize:true, revert:true, scroll:false, update:update_blocs });

	
	// Filtre du jeu
	jQuery(".filtre_jeu").click(function(){
		switch_game(this);
	});
	
	if(game_load!="" && game_load!="all") {
		switch_game(jQuery("#game_"+game_load).get(0));
	}
	else {
		jQuery(".loading_blocs").remove();
	}


	// Le calendrier
	ini_calendar();
	jQuery(".prev_mois").click(prev_mois);
	jQuery(".next_mois").click(next_mois);
	jQuery("#cal_slc_mois").change(change_date_cal);
	jQuery("#cal_slc_year").change(change_date_cal);

});

function update_blocs(){
	if(!request){
		request=true;
		setTimeout("request=false;",1000);
		jQuery.post("/apps_save_blocs.php", { col_gauche: jQuery(".col_gauche").sortable("serialize"), col_droite: jQuery(".col_droite").sortable("serialize")});
	}
}

function prev_mois(){
	if(!request){
		var boolgo=true;
		
		var mois_actu=parseInt(jQuery("#cal_slc_mois").val())+1;
		var annee_actu=parseInt(jQuery("#cal_slc_year").val());
		
		var min_annee=jQuery("#cal_slc_year option:first").val();
		
		if(mois_actu-1<1){
			if(annee_actu-1>=min_annee){
				mois_actu=12;
				annee_actu--;
			}
			else boolgo=false;
		}
		else mois_actu--;
		
		jQuery("#cal_slc_mois").val(mois_actu-1);
		jQuery("#cal_slc_year").val(annee_actu);
		
		if(boolgo){
			request=true;
			setTimeout("request=false;",1000);
			jQuery.post("/apps_get_cal.php",{mois:mois_actu, annee:annee_actu}, function(data){
				jQuery("#tbl_calendar").html(data);
				ini_calendar();
			});
		}
	}
}

function next_mois(){
	if(!request){
		var boolgo=true;
		
		var mois_actu=parseInt(jQuery("#cal_slc_mois").val())+1;
		var annee_actu=parseInt(jQuery("#cal_slc_year").val());
		
		var max_annee=jQuery("#cal_slc_year option:last").val();
		
		if(mois_actu+1>12){
			if(annee_actu+1<=max_annee){
				mois_actu=0;
				annee_actu++;
			}
			else boolgo=false;
		}
		else mois_actu++;
		
		jQuery("#cal_slc_mois").val(mois_actu-1);
		jQuery("#cal_slc_year").val(annee_actu);
		
		if(boolgo){
			request=true;
			setTimeout("request=false;",1000);
			jQuery.post("/apps_get_cal.php",{mois:mois_actu, annee:annee_actu}, function(data){
				jQuery("#tbl_calendar").html(data);
				ini_calendar();
			});
		}
	}
}

function change_date_cal(){
	var mois_selected=parseInt(jQuery("#cal_slc_mois").val())+1;
	var annee_selected=parseInt(jQuery("#cal_slc_year").val());
	
	request=true;
	setTimeout("request=false;",1000);
	jQuery.post("/apps_get_cal.php",{mois:mois_selected, annee:annee_selected}, function(data){
		jQuery("#tbl_calendar").html(data);
		ini_calendar();
	});
}

function ini_calendar(){
	jQuery('.date_has_event').each(function () {
		// options
		var distance = 5;
		var time = 250;
		var hideDelay = 150;

		var hideDelayTimer = null;

		// tracker
		var beingShown = false;
		var shown = false;

		var trigger = jQuery(this);
		var popup = jQuery('.events ul', this).css('opacity', 0);

		// set the mouseover and mouseout on both element
		jQuery([trigger.get(0), popup.get(0)]).mouseover(function () {
			// stops the hide event if we move from the trigger to the popup element
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			// don't trigger the animation again if we're being shown, or already visible
			if (beingShown || shown) {
				return;
			} else {
				beingShown = true;

				// reset position of popup box
				popup.css({
					bottom: 20,
					left: -76,
					display: 'block' // brings the popup back in to view
				})

				// (we're using chaining on the popup) now animate it's opacity and position
				.animate({
					bottom: '+=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					// once the animation is complete, set the tracker variables
					beingShown = false;
					shown = true;
				});
			}
		}).mouseout(function () {
			// reset the timer if we get fired again - avoids double animations
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			// store the timer so that it can be cleared in the mouseover if required
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				popup.animate({
					bottom: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					// once the animate is complete, set the tracker variables
					shown = false;
					// hide the popup entirely after the effect (opacity alone doesn't do the job)
					popup.css('display', 'none');
				});
			}, hideDelay);
		});
	});
}

// Les brèves
function go_prev_brev(){
	if(page_breves_actu!=1 && !request){
		page_breves_actu--;
		jQuery("#breves_liste_ul").after("<div class=\"loading_news\"></div>");
		request=true;
		setTimeout("request=false;",1000);
		jQuery.post("/apps_liste_breves.php",{ page:page_breves_actu, game:game},function(data){
			jQuery("#breves_liste").html(data);
		});
	}
}

function go_next_brev(){
	if(page_breves_actu!=max_pages_breves && !request){
		page_breves_actu++;
		jQuery("#breves_liste_ul").after("<div class=\"loading_news\"></div>");
		request=true;
		setTimeout("request=false;",1000);
		jQuery.post("/apps_liste_breves.php",{ page:page_breves_actu, game:game},function(data){
			jQuery("#breves_liste").html(data);
		});
	}
}
// Fin brèves


// Les affiches
var numAffiche = 1;
var timeoutAffiche;
function toggle_affiche(num){
	if(num!=numAffiche){
		if(num>8) num=1;
		
		jQuery(".slideshow_div").stop(true, true);
		jQuery(".img_slc_affiche img").stop(true, true);
		
		// Le slideshow
		jQuery(".link_affiche").removeClass("on");
		jQuery("#link_affiche_"+num).addClass("on");
		jQuery("#affiche_"+numAffiche).fadeOut("slow");
		jQuery("#affiche_"+num).fadeIn("slow");
		
		// La liste
		jQuery(".slc_liste_affiches a").removeClass("on");
		jQuery("#slc_link_affiche_"+num).addClass("on");
		jQuery("#slc_affiche_"+numAffiche).fadeOut("slow");
		jQuery("#slc_affiche_"+num).fadeIn("slow");
		
		numAffiche=num;
		clearTimeout(timeoutAffiche);
		timeoutAffiche=setTimeout('affiche_next()', 5000);
	}
}
function affiche_next(){
	toggle_affiche(numAffiche+1);	
}
function liste_affiche(obj){
	var num=jQuery(obj).attr("id").split("slc_link_affiche_");	
	num=num[1];
	toggle_affiche(num);
}
function affiche_liste_affiches(){
	jQuery(".slc_affiche").slideDown(200);
}
function cache_liste_affiches(){
	jQuery(".slc_affiche").slideUp(200);
}
// Fin affiches




// Recherche rapide
function go_search(){
	var terme=jQuery("#search_input").val();
	
	if(terme!=""){
		var url="/news/recherche/";
		if(game!="all") url+=game+"/";
		url+=terme+".html";
		window.location.href=url;
	}
}

// Fin recherche rapide


// Sélection du jeu
function switch_game(obj){
	if(!request){
		jQuery(".loading_blocs").remove();
		var next_game=jQuery(obj).attr("id").split("game_");
		next_game=next_game[1];
		jQuery(".filtre_jeu").removeClass("on");
		jQuery(obj).addClass("on");
		
		if(next_game!=game){
			window.location.href="#"+next_game;
			jQuery("#centre").append("<div class=\"loading_blocs\"></div>");
			
			page_breves_actu=1;
			
			request=true;
			setTimeout("request=false;",1000);
			
			jQuery.post("/apps_liste_bloc.php",{ game:next_game }, function(data){
				data=data.split("|||");
				jQuery("#breves_liste").html(data[0]);
				jQuery("#liste_rencontres").html(data[1]);
				jQuery("#liste_dossiers").html(data[2]);
				jQuery("#liste_compets").html(data[3]);
				jQuery("#liste_videos").html(data[4]);
				jQuery("#liste_dls").html(data[5]);
				jQuery(".loading_blocs").remove();
			});
			
			
			game=next_game;
		}
	}
}
// Fin sélection du jeu
