var popupStatus = 0;  
var buttonMax = 0;
var buttonStatus = 0;
var uptimeStatus = false;
var checkStatus = false;

$(document).ready(function(){

	
	/* Popup du r�glement */
	$("#reglespop").click(function(){
	
				$.ajax({
				type: "POST",
				async: false,
				
				url: "/default/index/rules",
					success:function(result){
						$("#middle-popup").html(result);
						$("#about_slide").width('703px');
						$("#about_slide").height('565px');
						$("#spacer_corps").height('725px');
					}
				});
			
				centerPopup();
				loadPopup();
				buttonMax = 5;

				$("#about_slide").wslide({
						width: 703,
						height: 565,
						pos: 0,
						horiz: true,
						autolink: false,
						duration: 800	});

				gestion_boutons();
			
	});
	
	/* Pop-up About swim challenge */
	$("#aboutlink").click(function(){
				
				$.ajax({
				type: "POST",
				async: false,
				url: "/default/index/about",
					success:function(result){
						$("#middle-popup").html(result);
						$("#about_slide").width('703px');
						$("#about_slide").height('320px');
						$("#spacer_corps").height('470px');
					}
				});
	
				centerPopup();
				loadPopup();
				buttonMax = 3;

				$("#about_slide").wslide({
						width: 703,
						height: 320,
						pos: 0,
						horiz: true,
						autolink: false,
						duration: 800				});
				
				gestion_boutons();
		

	});
	$("#aboutlink2").click(function(){
				
				$.ajax({
				type: "POST",
				async: false,
				url: "/default/index/about",
					success:function(result){
						$("#middle-popup").html(result);
						$("#about_slide").width('703px');
						$("#about_slide").height('320px');
						$("#spacer_corps").height('470px');
					}
				});
	
				centerPopup();
				loadPopup();
				buttonMax = 3;

				$("#about_slide").wslide({
						width: 703,
						height: 320,
						pos: 0,
						horiz: true,
						autolink: false,
						duration: 800				});
				
				gestion_boutons();
		

	});
	
	/*Pop-up pr?inscription */
	$(".register_link").click(function(){
				
				$.ajax({
				type: "POST",
				async: false,
			
				url: "/personne/index/mafiche",
					success:function(result){
						
						var testdata = result;
						
						$("#middle-popup").html(result);
						
						$("#about_slide").width('703px');
						$("#about_slide").height('383px');
						$("#spacer_corps").height('1200px');
						
					}
				});
				
				centerPopup();
				loadPopup();
	});
	
	/*Pop-up profil*/
	$(".profil_link").click(function(){
				
				$.ajax({
				type: "POST",
				async: false,
				url: "/personne/index/profils",
					success:function(result){
						$("#bottom-popup").css("background-image", "url(/medias/images/bottom-popup2.png)");
						$("#middle-popup").html(result);
						$("#about_slide").width('703px');
						$("#about_slide").height('383px');
						$("#spacer_corps").height('1200px');
						
					
						
						
					}
				});
				
				centerPopup();
				loadPopup();
	});
	
	/*Pop-up envoyer � un ami */
	$("#sendfriend").click(function(){
				
				$.ajax({
				type: "POST",
				async: false,
				url: "/default/index/friend",
					success:function(result){
						$("#middle-popup").html(result);
						$("#spacer_corps").height('366px');
					}
				});
				
				centerPopup();
				loadPopup();
				
	});
	
	/*Pop-up widget */
	
	$("#widget").click(function(){
				
				$.ajax({
				type: "POST",
				async: false,
				url: "/default/index/widget",
					success:function(result){
						$("#middle-popup").html(result);
						$("#about_slide").width('703px');
						$("#about_slide").height('310px');
						$("#spacer_corps").height('670px');
					}
				});
	
				centerPopup();
				loadPopup();
				buttonMax = 3;

				$("#about_slide").wslide({
						width: 703,
						height: 310,
						pos: 0,
						horiz: true,
						autolink: false,
						duration: 800				});
				
				gestion_boutons();
		

	});
	
	/*Pop-up login */
	$("#login").click(function(){
				
				$.ajax({
				type: "POST",
				async: false,
				dataType: "html",
				url: "/default/index/login",
					success:function(result){
						$("#middle-popup").html(result);
						$("#spacer_corps").height('380px');
					}
				});
				
				centerPopup();
				loadPopup();
				
	});
	
	/* Click ext�rieur pour fermer la pop-up*/
	$("#popupbg").click(function(){
		disablePopup();
	});
	
	
	$(".lien_preinsc").live('click',function()
	{
		disablePopup();
		$(this).delay(500,function()
		{
			$.ajax({
					type: "POST",
					async: false,
					url: "/personne/index/mafiche",
						success:function(result){
							$("#middle-popup").html(result);
							$("#about_slide").width('703px');
							$("#about_slide").height('383px');
							$("#spacer_corps").height('757px');
						}
					});
					
					centerPopup();
					loadPopup();
					return false;
		});
		
	});
	
	$(".lien_about").live('click',function()
	{
		disablePopup();
		$(this).delay(500,function()
		{		
			$.ajax({
					type: "POST",
					async: false,
					url: "/default/index/about",
						success:function(result){
							$("#middle-popup").html(result);
							$("#about_slide").width('703px');
							$("#about_slide").height('383px');
						}
			});
		
			centerPopup();
			loadPopup();
			buttonMax = 3;

			$("#about_slide").wslide({
					width: 703,
					height: 273,
					pos: 0,
					horiz: true,
					autolink: false,
					duration: 800				});
			
			gestion_boutons();
		});
	});
	
	$(".lien_rules").live('click',function()
	{
		disablePopup();
		
		$(this).delay(500,function()
		{				
			$.ajax({
			type: "POST",
			async: false,
			url: "/default/index/rules",
				success:function(result){
					$("#middle-popup").html(result);
					$("#about_slide").width('703px');
					$("#about_slide").height('383px');
				}
			});
				
				centerPopup();
				loadPopup();
				buttonMax = 4;

				$("#about_slide").wslide({
						width: 703,
						height: 383,
						pos: 0,
						horiz: true,
						autolink: false,
						duration: 800	});

				gestion_boutons();
		});
	});
	
	

//	function lienCreate()
	window.lienCreate = function()
	{
		disablePopup();
			
			$.ajax({
			type: "POST",
			async: false,
			url: "/groupe/index/form",
				success:function(result){
					$("#middle-popup").html(result);
					$("#about_slide").width('703px');
					$("#about_slide").height('110px');
				}
			});
				
				centerPopup();
				loadPopup();
				buttonMax = 4;

				$("#about_slide").wslide({
						width: 703,
						height: 110,
						pos: 0,
						horiz: true,
						autolink: false,
						duration: 800	});

				gestion_boutons();
	
		return false;
	}
	
//	function lienJoin()
window.lienJoin = function()
	{
		disablePopup();
			
			$.ajax({
			type: "POST",
			async: false,
			url: "/groupe/index/join",
				success:function(result){
					$("#middle-popup").html(result);
					$("#about_slide").width('703px');
					$("#about_slide").height('383px');
				}
			});
				
				centerPopup();
				loadPopup();
				buttonMax = 4;

				$("#about_slide").wslide({
						width: 703,
						height: 383,
						pos: 0,
						horiz: true,
						autolink: false,
						duration: 800	});

				gestion_boutons();
	
	}

	
	$("#close").live('click',function(){
		disablePopup();
	});
	
	$("#enter_swimming_distance_activate").live("click",function()
	{
	
		if(!$("#form_distance").is(":visible"))
			$("#form_distance").slideDown();
	});
	
	timerinc = setInterval(function()
	{
							
			$.ajax({
					type: "POST",
					url: "/default/index/updatexml/",
					success:function(result){
						var cdh =  parseInt($("#cpt_j_h").html());
						var chh =  parseInt($("#cpt_h_h").html());
						var cmh =  parseInt($("#cpt_m_h").html());
						
						var cdf =  parseInt($("#cpt_j_f").html());
						var chf =  parseInt($("#cpt_h_f").html());
						var cmf =  parseInt($("#cpt_m_f").html());
						
						
						var currentH = hmsec(cdh,chh,cmh);
						var nextH = hmsec(result.jh,result.hh,result.mh);
						var minH = nextH - currentH;
						
						var currentF = hmsec(cdf,chf,cmf);
						var nextF = hmsec(result.jf,result.hf,result.mf);
						var minF = nextF - currentF;
						
						
						if(minH >0){
							timerh = setInterval( function() {								
								if(minH > 0){
									animateCpt("h");
									minH--;
								}else{
									clearInterval(timerh);
								}
								
							}, 15 );
						}
						
						if(minF >0){
							timerf = setInterval( function() {								
								if(minF > 0){
									animateCpt("f");
									minF--;
								}else{
									clearInterval(timerf);
								}
								
							}, 15 );
						}
						
				
					},
					dataType: "json"
						
			});
			
			$.ajax({
				type: "POST",
				url: "medias/flash/compteur.xml",
				dataType: "xml",
				success:function(result){
					$(result).find("temps").each(
							function(){
								
								var tpsh = $(this).text();
								var tpsf = 100 - tpsh;
								var menhtml = $("#civilite .homme").html();
								var womenhtml = $("#civilite .femme").html();								
								
								ms = menhtml.split(" ");
								ws = womenhtml.split(" ");
								$("#civilite .homme").html(ms[0] + " "+ tpsh + "%");
								$("#civilite .femme").html(ws[0] + " "+ tpsf + "%");
								
							}
					);					
				}
			});
							
	},20000);
	
});

function animateCpt(type){
	
	var cdh =  parseInt($("#cpt_j_"+type).html());
	var chh =  parseInt($("#cpt_h_"+type).html());
	var cmh =  parseInt($("#cpt_m_"+type).html());
	
	
	if(cmh == 59){
		$("#cpt_m_"+type).html("0");
		if(chh == 23){
			$("#cpt_h_"+type).html("0");
			$("#cpt_j_"+type).html(cdh + 1);
		}else{
			$("#cpt_h_"+type).html(chh + 1);
		}
	}else{
		$("#cpt_m_"+type).html(cmh + 1);
	}
	
	if(parseInt($("#cpt_j_"+type).html()) == 30)
	{
		$("#cpt_j_"+type).html(0);
		$("#cpt_y_"+type).html( parseInt($("#cpt_j_"+type).html()) + 1);
	}
	
	
}

function hmsec(j,h,m)
{
	var cpt_tps;
	cpt_tps = j * 1400;
	cpt_tps += h * 60;
	cpt_tps += m ;
	
	return cpt_tps;
}


	
/*Fonction pour charger la pop-up*/
function loadPopup(){  
			
			
		   $("#corps1-1").fadeIn("800");
		   $("#corps1-2").fadeIn("800");
			if($.browser.msie)
			{	
				$(this).delay(800,function()
				{
						$("#top-popup").css({ 'background-color': "transparent" });
						$("#middle-popup").css({ 'background-color': "transparent" });
						$("#bottom-popup").css({ 'background-color': "transparent" });
				});
			}
		

		   $("#challenge_hommes").hide();
		   $("#challenge_femmes").hide();
		   $("#lien_aquas").hide();
		   $("#play_game").hide();
		   $("#pub").hide();
		   $("#ab").hide();
		   $("#slogan").hide();
		   $("#signup").hide();
		   $("#dashboard").hide();
		   $("#donation").hide();
		   $("#compteur").hide();
			if(popupStatus==0)
			{  
				$("#popupbg").css({ "opacity": "0.2" }); 
				$("#popupbg").fadeIn("slow");  
				$("#popup").fadeIn("slow");  
				popupStatus = 1;  
			}  
}

/*Fonction pour fermer la pop-up*/
function disablePopup(){
	if($.browser.msie)
	{
		$("#top-popup").css({ 'background-color': "white" });
		$("#middle-popup").css({ 'background-color': "white" });
		$("#bottom-popup").css({ 'background-color': "white" });
	}

	$("#corps1-1").fadeOut("800");
	$("#corps1-2").fadeOut("800");
    $("#challenge_hommes").fadeIn("800");
	$("#challenge_femmes").fadeIn("800");
	$("#signup").fadeIn("800");
	$("#swimmerday").fadeIn("800");
	$("#lien_aquas").fadeIn("800");
	$("#play_game").fadeIn("800");
	$("#pub").fadeIn("800");
	$("#ab").fadeIn(800);
	$("#slogan").fadeIn("800");
	$("#dashboard").fadeIn("800");
	$("#donation").fadeIn("800");
	$("#compteur").fadeIn("800");
	if(popupStatus==1)
	{
		$("#popupbg").fadeOut("slow");
		$("#popup").fadeOut("slow");
		popupStatus = 0;
	}
	$("html,body").animate({scrollTop:0},200);
	$("#spacer_corps").height('603px');
}

/*Donne les bonnes dimensions et positions des pop-up*/
function centerPopup(){

	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popup").height();
	var popupWidth = $("#popup").width();

	$("#popup").css({"position": "absolute","top": 288,"left": windowWidth/2-popupWidth/2});
	$("#popupbg").css({"height": windowHeight});
}

function envoi_mail()
{
	
	$.ajax({
			type: "POST",
			url: "/includes/php-int.php",
			data:"int=mail&expediteur="+$("#fexpediteur").val()+"&destinataire="+$("#fdestinataire").val(),
				success:function(result){
					if(result.length > 0)
						$("#p_error").html(result);
					else
						disablePopup();
				}
		});
}
   
function gestion_boutons()
{
	$("#slide-right_link").click(function()
		{
		
			if(parseInt($("#slide-right_link").attr('name')) == buttonMax)
					$("#slide-right_link").hide();
			else
			{
				$("#slide-right_link").attr('name',parseInt($("#slide-right_link").attr('name')) +1) ;
				$("#slide-right_link").attr('href',"#about_slide-"+$("#slide-right_link").attr('name')) ;
			}
			
			if(!$("#slide-left_link").is(":visible"))
					$("#slide-left_link").show();
			
				$("#slide-left_link").attr('name',parseInt($("#slide-left_link").attr('name')) +1) ;
				$("#slide-left_link").attr('href',"#about_slide-"+$("#slide-left_link").attr('name')) ;
			
	
			
			
		});
		
		$("#slide-left_link").click(function()
		{
		
		
				$("#slide-left_link").attr('name',parseInt($("#slide-left_link").attr('name')) -1) ;
				$("#slide-left_link").attr('href',"#about_slide-"+$("#slide-left_link").attr('name')) ;
			
			
			if(!$("#slide-right_link").is(":visible"))
				$("#slide-right_link").show();
			else
			{
				$("#slide-right_link").attr('name',parseInt($("#slide-right_link").attr('name')) -1) ;
				$("#slide-right_link").attr('href',"#about_slide-"+$("#slide-right_link").attr('name')) ;}
			if(parseInt($("#slide-left_link").attr('name')) ==0)
			{
				$("#slide-left_link").hide();
			}
			
		});
}
 
 function isNumeric(form_value) 
{ 
    if (form_value.match(/^\d+$/) == null) 
        return false; 
    else 
        return true; 
} 



function testphpcallback(){
	$("#spacer_corps").height('466px');
	$.ajax({
		type: "GET",
		async: false,
		url: "/default/index/headerajax",
			success:function(result){
				$("#infologinzone").html(result);
				
			}
		});
}

function afterlogin(){
	window.location = "/home/login";
}
