/* ****************************************************

	@file         global.js
	@description  Comportements globaux
	@author       Rafaël (ixmedia.com)
	vim: set noet ts=4 fdm=marker fenc=utf-8:

***************************************************** */

(function($) {

	$(function() {
		
		var $div = $('div#nav-etapes-wrap');
		var $ul = $('ul#nav-etapes');
		var divHeight = $div.height();
		
		$div.after('<p id="afficher-masquer-continuum"><img src="/img/interface/bouton-continuum-afficher.gif" alt="Afficher le continuum" /></p>');
		
		/*if ( !$('body').hasClass('section-accueil') ) {
			if ( readCookie('continuum') && readCookie('continuum') === 'affiche' ) {
				$div.data('cache', false);
				boutonMasquer();
			} else {
				$div.css('height', 50);
				$ul.css('top', -131);
				$div.data('cache', true);
				boutonAfficher();
			}
		} else {
			$div.data('cache', false);
			boutonMasquer()
		}*/
		
		/*$('#afficher-masquer-continuum').click(function() {
			if ($div.data('cache')) {
				createCookie('continuum', 'affiche');
				$div.animate( {height: divHeight}, 500, 'easeInBack' );
				$ul.animate( {top: 0}, 500, 'easeInBack', function() {
					boutonMasquer()
				} );
			} else {
				createCookie('continuum', 'cache');
				$div.animate( {height: 50}, 500, 'easeInBack' );
				$ul.animate( {top: -131}, 500, 'easeInBack', function() {
					boutonAfficher();
				} );
			}
			$div.data('cache', !$div.data('cache'));
			return false;
		});*/
		
		
		function boutonAfficher() {
			$('#afficher-masquer-continuum').find('img').attr({
				src: '/img/interface/bouton-continuum-afficher.gif',
				alt: 'Afficher le continuum'
			})
		}
		function boutonMasquer() {
			$('#afficher-masquer-continuum').find('img').attr({
				src: '/img/interface/bouton-continuum-masquer.gif',
				alt: 'Masquer le continuum'
			})
		}
		
		
		$('p.toggle:not(.post) a').each(function() {
			$( $(this).attr('href') ).hide();
			$(this).click(function() {
				$( $(this).attr('href') ).slideDown();
				return false;
			});
		});
		
		
		$('form#tri_publications select').change(function() {
			
			var idPublication = $(this).val();
			
			if (idPublication != 0) {
				var $listes     = $('ul[id^="liste-categorie"]');
				var $bonneListe = $listes.filter('ul[id$="'+idPublication+'"]');
				
				$listes.add( $listes.prev('h2') ).animate( {'opacity': 0}, 200, function() {
					$listes.add( $listes.prev('h2') ).hide();
					$bonneListe.add( $bonneListe.prev('h2') ).show().animate( {'opacity': 1}, 200 );
				});
			} else {
				var $listes = $('ul[id^="liste-categorie"]');
				$listes.add( $listes.prev('h2') ).animate( {'opacity': 0}, 200, function() {
					$listes.add( $listes.prev('h2') ).hide();
					$listes.add( $listes.prev('h2') ).show().animate( {'opacity': 1}, 200 );
				});
			}
			
		});
				
		$('form').submit(function() {
			
			var erreurs = [];
			
			if ( $('#prenom').val() === '' ) {
				erreurs[0] = "Votre prénom est obligatoire.";
				$('#prenom').parent().addClass('erreur');
			} else {
				$('#prenom').parent().removeClass('erreur');
			}
			if ( $('#nom').val() === '' ) {
				erreurs[1] = "Votre nom est obligatoire.";
				$('#nom').parent().addClass('erreur');
			} else {
				$('#nom').parent().removeClass('erreur');
			}
			if ( !$('#courriel').val().match(/^[_a-zA-Z0-9.\-]+@[a-zA-Z0-9]([_a-zA-Z0-9\-]+\.)+([a-zA-Z]{2,10})$/) ) {
				erreurs[2] = "Le courriel est invalide.";
				$('#courriel').parent().addClass('erreur');
			} else {
				$('#courriel').parent().removeClass('erreur');
			}
			if ( erreurs.length > 0 ) {
				$liste = $('<ul class="erreur">');
				$('ul.erreur').remove();
				for (var i in erreurs) {
					$liste.append('<li>'+ erreurs[i] +'</li>');
				}
				$('form p:first').after( $liste );
				return false;
			}
		});
		
		if ( $('.calendrier-wrap').length ) {
			
			if ( ($journeeSelectionnee = $('.journee-selectionnee')).length ) {
				$('.calendrier-wrap').not( $journeeSelectionnee.parents('.calendrier-wrap') ).hide();
			} else {
				$('.calendrier-wrap:gt(0)').hide();
			}
			
			$('.calendrier-wrap').each(function(i) {
				var $this = $(this);
				
				$this.find('.lien-suivant:not(.desactive)').click(function() {
					$this.hide();
					$('.calendrier-wrap:eq('+ (i+1) +')').show();
				});
				
				$this.find('.lien-precedent:not(.desactive)').click(function() {
					$this.hide();
					$('.calendrier-wrap:eq('+ (i-1) +')').show();
				});
			});
		}
	})
})(jQuery);

/*
 *	fonctions de cookies par Peter-Paul Koch (@ppk)
 *	http://www.quirksmode.org/js/cookies.html
 */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

