/**************************************************************************
 *
 *  Fenêtre popup
 *
 * -
 * -
 *
 ************************************************************************/

function ouvrirFenetre(arg_url){
	newwin = window.open(arg_url,"consultation","width="+screen.availWidth+",height="+screen.availHeight+",resizable=no");
	newwin.moveTo(0,0);
			}

$(document).ready(function() {
	$('ul.gestion_liste li').not('.membre').mouseover(function(){

		$(this).addClass('active')

		$(this).bind('mouseleave',
			function(){
				$(this).removeClass('active')
		});

		$(this).mouseout(function(){
				$(this).removeClass('active')
		});
	});
});

$(document).ready(function() {


//	var regex_courriel = /^(?:[a-z0-9!#$%&'*+/=?^_/`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i;
	var regex_courriel   = /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/gi;
	var regex_passwd   = /[a-z0-9]{5,}/gi;

	/**
	 * Effets de "Mouseover" et "Mouseout" sur les boutons
	 */

	$('p.bouton a').bind('mouseenter',function() {
		var img = $('img', this);
		img.attr('src', img.attr('src').replace(/\.gif/, '-hover.gif'));
	}).bind('mouseleave', function() {
		var img = $('img', this);
		img.attr('src', img.attr('src').replace(/-hover\.gif/, '.gif'));
	})

	$('input[@type=image]').not('.static').bind('mouseenter',function() {
		var img = $(this);
		img.attr('src', img.attr('src').replace(/\.gif/, '-hover.gif'));
	}).bind('mouseleave', function() {
		var img = $(this);
		img.attr('src', img.attr('src').replace(/-hover\.gif/, '.gif'));
	})

/**************************************************************************
 *
 * Formulaire de la page "Connexion"
 *
 * -
 * -
 *
 ************************************************************************/

	$('.connexion input').focus(function() {
		$(this).addClass("focus");
	}).blur(function() {
		$(this).removeClass("focus");
	});

	/* Champs normaux */
	$('.connexion').find('#surnom, #courriel').focus(function() {
		if ($(this).val() == $(this).attr('title') && $(this).is('.label')) {
			$(this).val("").removeClass("label");
		}
	}).blur(function() {
		if ($(this).val() == "") {
			$(this).addClass("label");
			var champ = this;
			setTimeout(function() { $(champ).val($(champ).attr('title')) }, 10)
		}
	})

	/* Mots de passe... */
	$('.connexion').find('#motdepasse').hide().blur(function() {
		if ($(this).val() == "") {
			$(this).hide();
			$(this).prev().show();
		}
	});
	$('.connexion').find('#motdepasse_label').focus(function () {
		$(this).hide();
		$(this).next().show()[0].focus();
	});


/**************************************************************************
 *
 * Formulaire de la page "Inscription"
 *
 * -
 * -
 *
 ************************************************************************/

	$('.inscription input').focus(function() {
		$(this).addClass("focus");
	}).blur(function() {
		$(this).removeClass("focus");
	});

	/* Champs normaux */
	$('.inscription').find('#nom, #prenom, #surnom, #courriel, #groupe').focus(function() {
		if ($(this).val() == $(this).attr('title') && $(this).is('.label')) {
			$(this).val("").removeClass("label");
		}
	}).blur(function() {
		if ($(this).val() == "") {
			$(this).addClass("label");
			var champ = this;
			setTimeout(function() {	$(champ).val($(champ).attr('title')) }, 10);
		}
	});

	/* Mots de passe... */

	$('.inscription').find('#motdepasse1, #motdepasse2').hide().blur(function() {
		if ($(this).val() == "") {
			$(this).hide();
			$(this).prev().show();
		}
	});
	$('.inscription').find('#motdepasse1_label, #motdepasse2_label').focus(function () {
		$(this).hide();
		$(this).next().show()[0].focus();
	});


	/*******************************************
	 *
	 * Gestion des cases à cocher
	 *
	 *****************************************/

	var champs_errones = {
		nom : true,
		prenom : true,
		surnom : true,
		courriel : true,
		motdepasse : true
	}

	/* Nom + Prénom */
	$('#nom, #prenom').keyup(function() {
		valider_nomprenom(this);
	}).blur(function() {
		$(this).trigger('keyup');
	});

	/* Surnom */
	if($('#contenu.concours').length < 0){
		$('#surnom').keyup(function() {
			valider_surnom(this);
		}).blur(function() {
			$(this).trigger('keyup');
		});
	}



	/* Courriel */
	$('#courriel').keyup(function() {
		valider_courriel(this);
	}).blur(function() {
		$(this).trigger('keyup');
	});

	/* Mots de passe */
	$('#motdepasse1, #motdepasse2').keyup(function() {
		valider_motsdepasse(this);
	}).blur(function() {
		$(this).trigger('keyup');
	});

	// Validations initiales
	if ($('body.inscription').length > 0) {
		$('#nom, #prenom').each(function(){
			valider_nomprenom(this);
		})
		$('#surnom').each(function(){
			valider_surnom(this);
		})
		$('#courriel').each(function(){
			valider_courriel(this);
		})
		$('#motdepasse1, #motdepasse2').each(function(){
			valider_motsdepasse(this);
		})
		$('#groupe').each(function(){
			valider_groupe(this);
		})
	}

	function gerer_bouton() {
		var bouton = $('.inscription p.bouton input');
		var valide = true;
		$.each(champs_errones, function(i,n) {
			if (n === true) {
				valide = false;
				return false;
			}
		});

		if (valide === false) {
			bouton.attr('src', 'img/btn-enregistrer.gif');
		} else {
			bouton.attr('src', 'img/btn-enregistrer-actif.gif');
		}
	}

	$('.inscription p.bouton input').mouseover(function() {
		this.oldSrc = $(this).attr('src');
		if (this.oldSrc != "img/btn-enregistrer.gif") {
			$(this).attr('src', 'img/btn-enregistrer-hover.gif');
		}
	}).mouseout(function() {
		$(this).attr('src', this.oldSrc);
	});



	// Validations des champs individuels (ou en paire)
	function valider_nomprenom(champ) {
		if (
			$('#nom').val() != "" &&
			$('#nom').val().toLowerCase() != $('#nom').attr('title').toLowerCase() &&
			!$('#nom').is('.label')
			){
				champs_errones.nom = false;
			} else {
				champs_errones.nom = true;
			}

		if ($('#prenom').val() != "" &&
			$('#prenom').val().toLowerCase() != $('#prenom').attr('title').toLowerCase() &&
			!$('#prenom').is('.label')
			) {
			champs_errones.prenom = false;
		} else {
			champs_errones.prenom = true;
		}
		gerer_bouton();
	}

	function valider_surnom(champ) {
		if (
			$(champ).val() != "" &&
			$(champ).val().toLowerCase() != $(champ).attr('title').toLowerCase() &&
			!$(champ).is('.label')
			) {
			champs_errones.surnom = false;
		} else {
			champs_errones.surnom = true;
		}
		gerer_bouton();
	}

	function valider_courriel(champ) {
		if (
			$(champ).val() != "" &&
			$(champ).val().match(regex_courriel) &&
			!$(champ).is('.label')
			) {
			champs_errones.courriel = false;
		} else {
			champs_errones.courriel = true;
		}
		gerer_bouton();
	}

	function valider_motsdepasse(champ) {
		if (
			$('#motdepasse1').val().match(regex_passwd) &&
			$('#motdepasse2').val().match(regex_passwd) &&
			$('#motdepasse2').val() == $('#motdepasse1').val()
			) {
			champs_errones.motdepasse = false;
		} else {
			champs_errones.motdepasse = true;
		}
		gerer_bouton();
	}

	function valider_groupe(champ) {
		if (
			$(champ).val() != "" &&
			$(champ).val().toLowerCase() != $(champ).attr('title').toLowerCase() &&
			!$(champ).is('.label')
			) {
			$(champ).parent().parent().find('em').addClass('pleine');
		} else {
			$(champ).parent().parent().find('em').removeClass('pleine');
		}
	}


});

$(window).load(function() {
//	$('div#badge').fadeIn('slow');
})


/**************************************************************************
 *
 * Mettre une valeur par défaut dans un élément de formulaire
 *
 * -
 * -
 *
 ************************************************************************/


 $(document).ready(function() {
	$(":input.valeurParDefaut").each(function() {

		var classeCSS = "couleurValeurParDefault"; // optionel
		var valeurParDefaut = $(this).attr("title");

		// Au chargement de la page
		var valeurActuel = $(this).val();
		if (valeurActuel == "") {
			$(this).val(valeurParDefaut);
			$(this).addClass(classeCSS); // optionel
		}
		// Sur le focus
		$(this).focus(function() {
			var valeurActuel = $(this).val();
			if (valeurActuel == valeurParDefaut) {
				$(this).val("");
				$(this).removeClass(classeCSS); // optionel
			}
		});
		// Sur le blur
		$(this).blur(function() {
			var valeurActuel = $(this).val();
			if (valeurActuel == "") {
				$(this).val(valeurParDefaut);
				$(this).addClass(classeCSS); // optionel
			}
		});
	});

});

$(document).ready(function() {

	$('#envoyer_ami label').hide();

});
