/* ****************************************************

	@file		  global.js
	@description  Comportements globaux
	@version	  20090909

***************************************************** */

(function($){

	$(function() {
/*
 * @section Événements
******************************************************************************/

		var evenementsPosition;
		evenementsPosition = 0;

		var evenementsTotal;
		$('.evenements .viewport').css('overflow', 'hidden');
		evenementsTotal = $('.evenements .viewport li').length;
		$('.evenements .nav a').click(function() {
			if ( $(this).is('.inactive') ) { return false; }

			if ( $(this).parent().is('.next') ) {
				evenementsPosition++;
				deplacerEvenements( true );
			} else {
				evenementsPosition--
				deplacerEvenements( true );
			}

		});

		function deplacerEvenements( animate ) {

			var newMargin = evenementsPosition*168;

			if ( animate === true ) {
				$('.evenements .viewport ul').stop().animate({
					marginLeft : ( -1 * newMargin ) + 'px'
				},{
					duration: 400,
					easing: 'easeOutQuart'
				});
			} else {
				$('.evenements .viewport ul').stop().css({
					marginLeft : ( -1 * newMargin ) + 'px'
				});
			}

			updaterNavEvenements();

		}

		function updaterNavEvenements() {
			if ( evenementsPosition+3 >= evenementsTotal ) { $('.evenements .next a').addClass('inactive'); }
			else { $('.evenements .next a.inactive').removeClass('inactive'); }
			if ( evenementsPosition == 0 ) { $('.evenements .prev a').addClass('inactive'); }
			else { $('.evenements .prev a.inactive').removeClass('inactive'); }
		}

		updaterNavEvenements();

		// Déplacement vers le prochain événement
		if ( $('.evenements .viewport ul li.futur').length ) {
			evenementsPosition = $('.evenements .viewport ul li').index( $('.evenements .viewport ul li.futur')[0] );
			if ( evenementsPosition > 0 ) {
				deplacerEvenements( false );
			}
		}

/*
 * @section Sidebar
******************************************************************************/


		$('#sidebar #nav li .nav-item ul').hide();
		$('#sidebar .carte a').click(function() {
			$(this).parent().next().stop().slideToggle();
			var strong = $(this).find('strong');
			if ( strong.is('.actif') ) {
				strong.html('Afficher la liste des arrondissements').removeClass('actif');
			} else {
				strong.html('Cacher la liste des arrondissements').addClass('actif');
			}
		});


		$('#sidebar .carte').next().find('a').bind('mouseenter', function() {
			$(this).addClass('hover');
		}).bind('mouseleave', function() {
			$(this).removeClass('hover');
		});


		$('#sidebar .carte a').bind('mouseenter', function() {
			$(this).find('strong').show();
		}).bind('mouseleave', function() {
			$(this).find('strong').hide();
		});

/*
 * @section Trouvez
******************************************************************************/

$('#trouvez .trouvez-overlay').css('opacity', 0.7);

$('a.trouvez').click(function() {
	$('#trouvez').show();

	if ( $.browser.msie && $.browser.version <= 6 ) {
		window.scrollTo(0,1);
	}

	$('#trouvez .trouvez-fg').css( 'top', (100+$(window).scrollTop()) + 'px' );
	
	document.getElementById('codepostal').focus();

});

$('#trouvez .fermer a').click(function() {
	$('#trouvez').hide();
});

$('#trouvez form').submit(function() {

	var url = $(this).attr('action');
	$('#trouvez .resultats:visible, #trouvez .erreurs:visible').slideUp();
	$('#trouvez .loading').show();
	
	$.ajax({
		url : url,
		data : { codepostal : $('#codepostal').val() },
		success : function(data) {
			data = data.split('|');
			$('#trouvez .loading').hide();
			if ( data[0] == 'resultat' ) {
				$('#trouvez .resultats .resultat strong').html( data[1] );
				$('#trouvez .erreurs:visible').slideUp();
				$('#trouvez .resultats').slideDown();
			} else {
				$('#trouvez .erreurs .resultat').html( data[1] );
				$('#trouvez .resultats:visible').slideUp();
				$('#trouvez .erreurs').slideDown();
			}
		}
	});
	return false;

});


/*
 * @section Faire un don
******************************************************************************/

	$('.termes').hide().prev().find('a').click(function() {
		$(this).parent().next().slideToggle();
	});

	$('a.accept').click(function() {
		$(this).parent().parent().slideUp();
		$('#condition_o').show();
		$('#condition_n').hide();
	})

	$('a.refuse').click(function() {
		$(this).parent().parent().slideUp();
	})

/*
 * @section Focus Recherche
******************************************************************************/

	$('#s').not('.plein').focus(function() {
		$(this).parent().addClass('focus');
	}).blur(function() {
		$(this).parent().removeClass('focus');
	});

/*
 * @section Entête au hasard
******************************************************************************/

	var rand=1+(Math.floor(Math.random()*7));
	$('body.blog-principal #entete').addClass('fond'+rand);


/*
 * @section Galerie Flickr
******************************************************************************/

	$('.flickr-galerie li a').fancybox({
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 300,
		'overlayShow': true,
		'padding' : 0,
		'callbackOnStart' : function() {
			$('object, embed').addClass('to_restore').css('visibility', 'hidden');
		},
		'callbackOnClose' : function() {
			$('object, embed').filter('.to_restore').removeClass('to_restore').css('visibility', 'visible');
		}
	});

	});

})(jQuery);

function calculTotal( lang, id ) {

	var a1;
	if ( $('#periode-1').is(':checked') ) { a1 = 5; $('input[name=amount_1]').val(5); $('b#amount_1').text('5'); }
	if ( $('#periode-2').is(':checked') ) { a1 = 10; $('input[name=amount_1]').val(10); $('b#amount_1').text('10'); }
	if ( $('#periode-3').is(':checked') ) { a1 = 15;$('input[name=amount_1]').val(11); $('b#amount_1').text('15'); }

	var c = parseInt( $('input[name=contribution]').val() );

	if ( isNaN(a1) ) { a1 = 0; }
	if ( isNaN(c) ) { c = 0; }
	
	if (a1 == 0 && c == 0) {
		alert('Veuillez sélectionner votre période d\'adhésion et/ou le montant de votre contribution.');
		return false;
	}

	if ( c > 1000 ) {
		alert('La contribution ne peut excéder 1000 $.');
		c = 0;
	} else {
		$('input[name=amount_2]').val(c); $('b#amount_2').text(c);
	}

	var total = a1 + c;
	$('b#amount').text(total);
	$('input[name=amount]').val(total);


}