window.onload = function () {
	// Fix PNG for Ie 6
	if ( (/MSIE 6\.0/).test(navigator.userAgent) && !(/MSIE 7\.0/).test(navigator.userAgent) ) {		
		//DD_belatedPNG.fix(".FixPng");
	}	
	
	/* Admin */
		$("#error_message").slideDown( "slow" );
		setTimeout( function (){ $("#error_message").slideUp( "slow" ); }, 10000 );
		$("#error_CROSS").click( function (){
			$("#error_message").slideUp( "slow" );
		});
	
		$("#error_message").slideDown( "slow" );
		setTimeout( function (){ $("#error_message").slideUp( "slow" ); }, 10000 );
		$("#error_CROSS").click( function (){
			$("#error_message").slideUp( "slow" );
		});
		
}

function defilementAutoRefresh(etat,choix) {

	if ( etat == "marche" ) {
		defilementAuto(choix);
		timerDefilement = setInterval("defilementAuto('"+choix+"')",25);
	} else {
		clearInterval(timerDefilement);
	}
	
}

function changerSaison(saison) {
	
	if ( saison == 1 ) {
		
		/* On change le lien */
			$(".item2").css("background","#BB6D0F none repeat scroll 0 0");
			$(".item1").css("background","none");
		
		/* On appel la première vidéo de la saison 1 */
			selectMediaGtAcademy(1,'','video','player','','');
			
			defilementAutoRefresh();
		
	} else {
	
		/* On change le lien */
			$(".item1").css("background","#BB6D0F none repeat scroll 0 0");
			$(".item2").css("background","none");
				
		/* On appel la première vidéo de la saison 1 */
			selectMediaGtAcademy(2,'','video','player','','');
			
			defilementAutoRefresh();
			
	}
}

function changerCategoryFreerace(idCat) {
	
	$("li[class^='item']").css("background","#BB6D0F none repeat scroll 0 0");
	$("#cat_"+idCat).css("background","none");
					
	/* On appel la première vidéo de la saison 1 */
		selectMediaFreeRace(idCat,'','video','player','','');
		defilementAutoRefresh();
		
}

isPopupMessage = false;

function alertPopup(texte)
{
	isPopupMessage = true;
	screenFlyover( "alert", texte, "", "" );
	setTimeout("closeFlyover()",4000);
}


function openWindow(theURL,winName,width, height) { 
  window.open(theURL, winName, "width=" + width + ", height=" + height + ",scrollbars=yes");
}

/* Permet de calculer le nombre de caractères restant pour un commentaire */
	function countCaracComment(div,idResult) {
		var textLength = $("#"+div).val().length;
		
		if ( ( 500 - parseInt(textLength) ) > 1 ) {
			$("#"+idResult).html( 500 - parseInt(textLength));
		} else {
			$("#"+div).val( $("#"+div).val().substr(0,500) );
			$("#"+idResult).html( 0 );
		}
	}
	
	function countCaracComment1000(div,idResult) {
		var textLength = $("#"+div).val().length;
		
		if ( ( 1000 - parseInt(textLength) ) > 1 ) {
			$("#"+idResult).html( 1000 - parseInt(textLength));
		} else {
			$("#"+div).val( $("#"+div).val().substr(0,1000) );
			$("#"+idResult).html( 0 );
		}
	}
	
	function divModifExprimezVous() {
	
		$("#exprimezVous").html("<input type='text' onclick=\"$(this).val(''); return false;\" id='textExprimezVous' value='Exprimez-vous' /> <a href='#' onclick='exprimezVous(); return false;' title='Valider'>Valider</a>");
	
	}
	
/* Permet de gérer le parser pour le retour de l'upload */
	function verifResultUpload(chaine) {

		var idMedia	= /id\>([0-9]+)\<\//.exec(chaine);
		
		if ( idMedia ) {
			
			/* On vérifie si le media a déjà été upload */
			var duplicateMedia	= /DuplicateAssetException/.exec(chaine);
			
			if ( duplicateMedia ) {
				return "duplicate";
			} else {
				idMediaRecup = idMedia[1];
				return idMediaRecup;
			}
			
		} else {
			return false;
		}
	
	}
	
/* Permet de gérer le parser pour le retour de l'upload */
	function verifResultUploadPath(chaine) {

		var idMedia	= /path\>(.*)\<\/path/.exec(chaine);
		
		if ( idMedia ) {
			
			/* On vérifie si le media a déjà été upload */
			var duplicateMedia	= /DuplicateAssetException/.exec(idMedia);
			
			if ( duplicateMedia ) {
				return "duplicate";
			} else {
				idMediaRecup = idMedia[1];
				return idMediaRecup;
			}
			
		} else {
			return false;
		}
	
	}

function choixKeyframe(id) {
	
	$('.keyframe').removeAttr('checked');
	$('#keyframe_'+id).attr('checked','checked');
	
}

/* Permet d'afficher les flyovers */
	function screenFlyover( popup, param, param2, param3 ) {

		/* On récupére en ajax les données et on les affiches dans la div contenuAjoutePopup */
		//$.scrollTo( "contenuPopup", {duration:10} );
		$(".loading").show();
		$("#background_Flyover").show();
		
			$(".bg_white").css({
				"left"				:	"50%",
				"margin-left"		:	"-"+(parseInt( $(".bg_white").width()/2 )+"px"),
				"top"				:	"50%",
				"margin-top" 		: 	"-"+(parseInt( $(".bg_white").height() / 2 )+"px")
			});

			$(".bg_white").css("margin-top", ( parseInt($(".bg_white").css("margin-top")) + ($(window).scrollTop()) )+"px");
					
		$(".bg_white").show();
			
		$.ajax({
			type: "post",
			async: true,
			url: "/"+popup+".popup",
			data: { "param":param, "param2" : param2, "param3" : param3 },
			success: function(res) {
						
				
				var res = $.trim(res);
					
				if ( res == "nosession" ) {
					
					/* Si la personne n'est pas connecté */
						screenFlyover( "identifiez_vous", ''+param+'', '','' );
					
				} else if ( res == "duplicateFreeRace" ) {
					alertPopup("Vous êtes déjà inscrit à la Free RACE. <br /> Vous serez tenu informé par email si vous êtes sélectionné pour les <br /> qualifications nationales. ");
				} else {

					$(".loading").hide();

					if( isPopupMessage )
					{
						if( $.browser.msie && $.browser.version.substr(0,1) == 6 )
						{
							document.getElementById("content_Flyover").innerHTML = res;
							if( $.browser.msie && $.browser.version.search( /6/ ) != -1 ) {
								pngFix();
							}
						} else {
							$("#content_Flyover").html(res);
							if( $.browser.msie && $.browser.version.search( /6/ ) != -1 ) {
								pngFix();
							}
						}
						isPopupMessage = false;
					}else{
						$("#content_Flyover").html(res);
						if( $.browser.msie && $.browser.version.search( /6/ ) != -1 ) {
							pngFix();
						}
					}

					$("#content_Flyover").css({
						"left"				:	"50%",
						"margin-left"		:	"-"+(parseInt( $("#content_Flyover").width()/2 )+"px"),
						"top"				:	"50%",
						"margin-top" 		: 	"-"+(parseInt( $("#content_Flyover").height() / 2 )+"px")
					});

					/* Permet de mettre la popup au coin supèrieure si cette dernière est plus grande que la taille de l'écran */
					if ( $(window).height() <= $("#content_Flyover").height() ) {
						var diffSize = ( parseInt($("#content_Flyover").height() / 2) - parseInt($(window).height() / 2 ) );
						$("#content_Flyover").css("margin-top",parseInt($("#content_Flyover").css("margin-top")) + diffSize + 20);
						$("#content_Flyover").css("padding-bottom","20px");
					} 
					
					/* Permet de le centrer par rapport au scrool */
					$("#content_Flyover").css("margin-top", ( parseInt($("#content_Flyover").css("margin-top")) + ($(window).scrollTop()) )+"px");
					
					/* Permet d'agrandir le background_Flyover */
						$("#background_Flyover").height( $("body").height() );
					
					$(".bg_white").hide();
					$(".loading").hide();
					$(".flyover").show();
					
				}

			}
		});
		
		/*
		$("#background_Flyover").click( function () {
			closeFlyover();
		});
		*/
	}

/* Permet de fermer un flyover */
	function closeFlyover() {
		$(".flyover").hide();
	}
	
function defilement(direc) {
	
	/* Correspond à ma hauteur de l'image + le texte */
		var tailleZone = 104;	
		var heightListingVideo = $("#listingVideo").height();
	
	if ( direc == "+" ) {
		var topDefil = parseInt($("#listingVideo").css("top"))+tailleZone;
	} else {
		var topDefil = parseInt($("#listingVideo").css("top"))-tailleZone;
	}

	if ( ( Math.abs(topDefil)  < ( heightListingVideo - tailleZone ) ) && topDefil <= 0 )
		$("#listingVideo").css("top",topDefil+"px");
}

function defilementAuto(direc) {
	
	/* Correspond à ma hauteur de l'image + le texte */
		var tailleZone = 104;	
		var vitesse = 3;
		var heightListingVideo = $("#listingVideo").height();
	
	if ( direc == "+" ) {
		var topDefil = parseInt($("#listingVideo").css("top"))+vitesse;
	} else {
		var topDefil = parseInt($("#listingVideo").css("top"))-vitesse;
	}

	if ( ( Math.abs(topDefil)  < ( heightListingVideo - ( tailleZone * 2 ) ) ) && topDefil <= 0 )
		$("#listingVideo").css("top",topDefil+"px");
		
}
	