(function($) {

	$(function() {

		var $body = $('body');

		// Liens externes
		$('a[href*="pdf"], a[href^=http://]').not('a[href*='+window.location.host+']').not('a[href*='+AQPSCONGRES_URL+']').addClass('externe').attr('target','_blank');


		$('.grise').css('opacity', 0.4);

		$('input#interesse_transport_collectif').change(function() {
			switch ( $('input#interesse_transport_collectif').is(':checked') ) {
				case false:
					$('input#choix1_transport_collectif').attr('disabled', 'disabled');
					$('input#choix2_transport_collectif').attr('disabled', 'disabled');
					$('input#nombre_places').attr('disabled', 'disabled');
					break;
				case true:
					$('input#choix1_transport_collectif').removeAttr('disabled');
					$('input#choix2_transport_collectif').removeAttr('disabled');
					$('input#nombre_places').removeAttr('disabled');
					break;
			}
		});

		$('input#interet_autre').change(function() {
			switch ( $('input#interet_autre').is(':checked') ) {
				case false:
					$('input#autre_interet_precision').attr('disabled', 'disabled');
					break;
				case true:
					$('input#autre_interet_precision').removeAttr('disabled');
					break;
			}
		});

		$('input#titre_autre').change(function() {
			switch ( $('input#titre_autre').is(':checked') ) {
				case false:
					$('input#autre_titre_precision').attr('disabled', 'disabled');
					break;
				case true:
					$('input#autre_titre_precision').removeAttr('disabled');
					break;
			}
		});



		/*
		 * Calcul des frais selon les options choisies
		 */
		compterFrais();
		$('input.frais').change(compterFrais);
		function compterFrais() {
			var totalFraisInscription = 0;
			var totalFraisSupplementaires = 0;

			$('input.frais').each(function() {

				var frais = 0;

				if ( $(this).is('input[type="checkbox"]') ) {

					frais = parseInt( $(this).parent().find('label').text(),10 ) || 0;
					if ( $(this).is(':checked') ) {
						$(this).hasClass('inscription') ? totalFraisInscription += frais : totalFraisSupplementaires += frais;
					}

				} else {

					if ( $(this).is('#nombre_enfants') ) {
						frais = $('#nombre_enfants').val() * $('#nombre_jours').val() * parseInt( $('#prix_service_garde').text(),10 ) || 0;
					} else if ($(this).is('#nombre_billets_spectacle')) {
						frais = $('#nombre_billets_spectacle').val() * parseInt( $('#prix_spectacle').text(),10) || 0;
					}
					$(this).hasClass('inscription') ? totalFraisInscription += frais : totalFraisSupplementaires += frais;

				}

			});

			$('span#frais_inscription').text( totalFraisInscription );
			$('span#frais_supplementaires').text( totalFraisSupplementaires );
			$('span#total_frais').text( totalFraisInscription + totalFraisSupplementaires );
		};


/*
 * @section Compte à rebour de l'entete
******************************************************************************/
/* Commenté pendant l'événement
		var congres = new Date();
		congres = new Date("October 26, 2010 17:00:00");
		$('#compteur-temps .compteur').countdown({
			until: congres,
			description: '',
			format: 'dhms',
			layout:'<p class="jours">{dn} {dl}</p><p class="heures">{hnn} : {mnn} : {snn}</p>',
			labels: ['années', 'mois', 'semaines', 'jours', 'heures', 'minutes', 'secondes']
		});

		$("input[type=text],textarea").focus(function (){
			$(this).addClass("focus");
			$(this).removeClass("plein");
		}).blur(function (){
			$(this).removeClass("focus");
			if ($(this).val() == '') {
				$(this).removeClass("plein");
			}
			else{
				$(this).addClass("plein");
			}
		}).each(function (i){
			if ($(this).val() == '') {
				$(this).removeClass("plein");
			}
			else{
				$(this).addClass("plein");
			}
		});
*/
/*
 * @section Accueil
******************************************************************************/
		if($body.is('.template-accueil')){

			var messageAlerte = 'Vous pouvez selectionner un maximum de 2 choix par question';

			$("input[name^='confQuestionA']").click(function(){
				if($("input[name^='confQuestionA']:checked").size() > 2){
					alert(messageAlerte);
					return false;
				}
			});
			$("input[name^='confQuestionB']").click(function(){
				if($("input[name^='confQuestionB']:checked").size() > 2){
					alert(messageAlerte);
					return false;
				}
			});
			$("input[name^='confQuestionC']").click(function(){
				if($("input[name^='confQuestionC']:checked").size() > 2){
					alert(messageAlerte);
					return false;
				}
			});
			
			
			//visionneuse de l'accueil
			// combien?
			var nbSlides=$("div.visionneuse ul li").length;
			// on se facilite le travail pour la navigation
			for(var c=0; c<=nbSlides; c++){
				$("div.visionneuse ul li:eq("+c+")").attr("id","s-"+(c+1));
			}
			
			// timer
			var delais = 7000;
			var timerSlider = setTimeout(faireChangerImage, delais);
			var animationEnCours = false;
			// écouteurs 
			$("div.visionneuse a#prec, div.visionneuse a#suiv").click(function(e){
					e.preventDefault();
				 var sens=1;
				if($(this).attr("id")=="prec"){
					sens=-1;
				}
				changerImage(sens, nbSlides);
			});
		}//slider accueil
			
			function faireChangerImage(){
				changerImage(1, nbSlides);
			}
			// changerImage
			function changerImage(sens, nbSlides){
				// s'il y a un timer, on l'arrête
				clearTimeout(timerSlider);
				var actif = parseFloat($("div.visionneuse ul li.actif").attr("id").substr(2));
				var prochaineSlide=actif+sens;
				if(actif+sens==nbSlides+1){
					prochaineSlide=1;
				}
				else if(actif+sens==0){
					prochaineSlide=nbSlides;
				}
				
				//changer de slide
				var adresse="";
				if(!animationEnCours){
					animationEnCours=true;
					$("div.visionneuse ul li").clearQueue();
					$("div.visionneuse ul li.actif").fadeOut('slow', function(){
							$(this).removeClass("actif");
							$("div.visionneuse ul li#s-"+prochaineSlide.toString()).addClass("actif");
							$("div.visionneuse ul li.actif").fadeIn('slow', function(){
									animationEnCours=false;																										 
							});																											
					});
				}
				
				// redémarrer le timer
				timerSlider = setTimeout(faireChangerImage, delais);
			
		}

	});

})(jQuery);

