var animationSpeed = 'slow';

var animateRightBlock = function(toSelecteur) {
	$('#ads, .login-box').hide();
	$(toSelecteur).show();
}

$(document).ready(function() {
	
	if ($('#ads').length) {
		/*
		 * Boite de login/inscription
		 */
		$('.step0-1').click(function() {
			animateRightBlock('#step-1');
			return false;
		});
		
		// Retour � la boite originale
		$('.login-box .cancel').click(function() {
			animateRightBlock('#ads');
			return false;
		});
	
		$('#step-1 .connect, .menu .connect').click(function() {
			animateRightBlock('#step-2');
			return false;
		});
		
		$('#step-1 .register, .menu .register').click(function() {
			animateRightBlock('#step-3');
			return false;
		});
		
		$('form#login').validate({
			messages: { 
	            rplogin: { 
	                required: "Merci de bien vouloir renseigner une valeur pour ce champ.", 
	                email: "Merci de bien vouloir renseigner une adresse mail valide"
	            },
	            rppass: "Merci de bien vouloir renseigner une valeur pour ce champ."
	        }
		});
		
		$('form#signin').validate({
			messages: { 
	            registerlogin: { 
	                required: "Merci de bien vouloir renseigner une valeur pour ce champ.", 
	                email: "Merci de bien vouloir renseigner une adresse mail valide"
	            },
	            registerpass: "Merci de bien vouloir renseigner une valeur pour ce champ.",
	            registerpassconfirm: "Merci de bien vouloir renseigner une valeur pour ce champ."
	        }
		});
	}

	$('#commentaires .answer-link, #commentaires .post-comment-link').click(function() {
		$(".form-reagissez-souscommentaire").slideUp(animationSpeed);
		$("#commentaires .answer-link").slideDown(animationSpeed);
		$("#form-reagissez").slideUp(animationSpeed);
		$("#commentaires .post-comment-link").slideDown(animationSpeed);
		
		$(this).slideUp(animationSpeed);
		$($(this).next()).slideDown(animationSpeed);
		return false;
	});

	$('#form-reagissez .cancel, .form-reagissez-souscommentaire .cancel, #form-reagissez .form-title, .form-reagissez-souscommentaire .form-title').click(function() {
		$(".form-reagissez-souscommentaire").slideUp(animationSpeed);
		$("#form-reagissez").slideUp(animationSpeed);
		$("#commentaires .answer-link").slideDown(animationSpeed);
		$("#commentaires .post-comment-link").slideDown(animationSpeed);
		
		return false;
	});
	
	if($('#job-picker').length) {
		$('#job-picker').slideDown(animationSpeed);
		$('#job-picker .cancel').click(function() {
			$('#job-picker').slideUp(animationSpeed);
			_gaq.push(['_trackEvent', 'Professions', 'Choisir plus tard (fermer le bandeau bleu)']);
			return false;
		});
		$('#job-picker form').submit(function() {
			_gaq.push(['_trackEvent', 'Professions', 'Choisir', $('#job-picker option:selected').text()]);
		});
	};
	
	if($('#job-picker-doc').length) {
		$('#job-picker-doc').slideDown(animationSpeed);
		$('#job-picker-doc form').submit(function() {
			_gaq.push(['_trackEvent', 'Professions', 'Choisir', $('#job-picker-doc option:selected').text()]);
		});
		$('#job-picker-doc a.barlink').click(function() {
			_gaq.push(['_trackEvent', 'Professions', 'Choisir', $(this).html()]);
		});
	};
	
	if($('#job-picker-doc-exist').length) {
		$('#job-picker-doc-exist').slideDown(animationSpeed);
	};
	
	if($('#job-picker-doc-not-exist').length) {
		$('#job-picker-doc-not-exist').slideDown(animationSpeed);
		$('#job-picker-doc-not-exist a.barlink').click(function() {
			_gaq.push(['_trackEvent', 'Professions', 'Choisir', $(this).html()]);
		});
	};
	
	if($('#user-details').length) {
		if ($('.change-job').length) {
			$('.change-job').click(function(){ _gaq.push(['_trackEvent', 'Professions', 'Changer de profession (annulee la profession courante)', $('#user-details span.job strong').html()]); });
		}
	}
	
	if ($('#rubNiv3Ajax').length) {
		$('span.readership a, #section-alt ul.readership a').click(function(){ _gaq.push(['_trackEvent', 'Professions', 'Choisir', $(this).html()]); });
		$('a.jobdirectlychosen').click(function(){ _gaq.push(['_trackEvent', 'Professions', 'Choisir', 'Choisi directement au clic sur un article mono-profession']); });
	}
	
	/*
	 * Choix de profession sur une rubrique niveau 2
	 */
	/* La classe "link" est là pour différencier avec les autres */
	$('.section .link.choose-job').click(function() {
		var currentSection = $(this).parent().parent();
		var currentJobChoose = $(currentSection).next();
		
		$(currentSection).hide();
		$(currentJobChoose).show();
		return false;
	});
	$('.section .section-title .choose-job').click(function() {
		var currentSection = $(this).parent().parent();
		var currentJobChoose = $(currentSection).next();
		
		$(currentSection).hide();
		$(currentJobChoose).show();
		return false;
	});
	$('.section .abstract .choose-job').click(function() {
		var currentSection = $(this).parent().parent().parent();
		var currentJobChoose = $(currentSection).next();
		
		$(currentSection).hide();
		$(currentJobChoose).show();
		return false;
	});
	$('.section-alt .cancel').click(function() {
		var currentJobChoose = $(this).parent();
		var currentSection = $(currentJobChoose).prev();
		
		$(currentSection).show();
		$(currentJobChoose).hide();
		return false;
	});
	
	if ($('#navigation').length) {
		$('.level-2-category .level-2-title').click(function(){
			$(this).next().toggle();
			return false;
		});
	}
});

jQuery(function(){
	others('#pre-page dl');
});
/* -------------------------------------------------------------------------- */
var others = function(selector){
	
	if(!jQuery(selector).length) return;
	
	var active = 'active';
	
	jQuery(selector).find('dt').find('span').wrapInner('<button />');	
	jQuery(selector).find('button').click(function(e){
		if(jQuery(selector).hasClass(active)){
			jQuery(selector).removeClass(active);
		}else{
			jQuery(selector).addClass(active);		
		}
	});
	jQuery(document).click(function(){
		jQuery(selector).removeClass(active);
	});
	jQuery(selector).click(function(e){
		e.stopPropagation();
	});
}
