/* code pour jQuery */
$(document).ready(function() {

	// apparition du titre de la section
	$('span.js_color')
		.delay(2500)
		.animate({'color': '#bd73b0'}, 2500);

	// cosmétique
	$('.liste li:odd').addClass('odd');

	// logo 
	$('#logo a').hover(
		function () {
			$('#logo a span').stop(false, true).fadeOut(100);
		},
		function () {
			$('#logo a span').stop(false, true).fadeIn(500);
		}
	);

	// initialisation de la liste des événements
	var el_start = '-105px';
	var el_height = 605; // 11 * 55px
	$('#events_list>ul>li').css({'height': '0px'});
	if ($('#programme').parent().is('.active')) {
		$('#events_list>ul>li').css({'height': '55px'});
		$('#events_list').show();
	} else {
		$('#events_list').hide().css({'top': el_start}).show();
	}

	// animation du menu principal (accordéon)
	$('.sub').hide();
	$('.active .sub').show();
	$('#menu li:has(ul) > a').click(function (event) {
		if ($(this).next()[0].style.display == 'none') {
			$('.sub').animate({'height': 'hide'}, 300);
			$(this).next().animate({'height': 'show'}, 300);
			// modifier pour garder toujours le programme ouvert sur les pages events ?
			if ($(this).is('#programme')) {
				$('#events_list').animate({'top': '0px'}, 1000);
				$('#events_list>ul>li').animate({'height': '55px'}, 1000);
			} else {
				$('#events_list').animate({'top': el_start}, 500);
				$('#events_list>ul>li').animate({'height': '0px'}, 500);
			}
		} else {
			$('.sub').animate({'height': 'hide'}, 300);
			if ($(this).is('#programme')) {
				$('#events_list').animate({'top': el_start}, 500);
				$('#events_list>ul>li').animate({'height': '0px'}, 500);
			}
		}
		$(this).blur();
		event.preventDefault();
	})

	var mainH = $('#main').height();
	var sidebarH = $('#sidebar').height();
	var centerH = (mainH > sidebarH) ? mainH : sidebarH;
	$('#events_list .events').each(function () {
		$(this).attr('rel', $(this).height());
	});
	var calcHeights = function () { // pour tenir compte du fait que la hauteur de la sidebar varie en fonction du sous-menu ouvert
		sidebarH = $('#sidebar').height();
		centerH = (mainH > sidebarH) ? mainH : sidebarH;
		$('#events_list .events').css({'max-height': (centerH - 30) + 'px'}); // -30 pour bordure + padding
	}
	$('#events_list .events').css({'max-height': (centerH - 30) + 'px'}); // -30 pour bordure + padding

	// survol de la liste des événements
	// avec ajustement de la position du bloc si celui-ci descend trop bas
	$('#events_list > ul > li:not(.active)').hover(
		function () {
			calcHeights();
			var absPos = $(this).index() * $(this).height();
			var eventsH = Number($(this).children('.events').eq(0).attr('rel'));
			var adjust = 0;
			if ((absPos + eventsH + 30) > centerH) { // le bloc events affiché déborde vers le bas de la page
				adjust = (centerH - (absPos + eventsH + 30)) + 'px'; // 30 = padding + border (events)
			}
			$(this).children('.events')
				.stop(false, true)
				.animate({'opacity': 'show', 'top': adjust}, 300);
		},
		function () {
			$(this).children('.events').stop(false, true).animate({'opacity': 'hide', 'top': '0px'}, 300);
		}
	);

	var violet_els = '#content :not(.sign) a, #content h3, #content .js_color, #content .heure, #main .event strong, body.communiques #main strong';
	var black_els = '';
	$('#events_list > ul > li:not(.active)').hover(
		function (e) {
			$('#content, #main a.fred').stop(true, false).animate({'color': '#ccc'}, 300);
			$('#content p.mosaique').stop(true, false).animate({'background-color': '#ccc'}, 300);
			$('#main .day').stop(true, false).animate({'border-top-color': '#ccc'}, 300);
			$(violet_els).stop(true, false).animate({'color': '#f2e3ef'}, 300);
			$('#content img').stop(true, false).fadeTo(300, 0.2);
		},
		function (e) {
			$('#content, #main a.fred').stop(true, true).delay(100).animate({'color': '#000'}, 300);
			$('#content p.mosaique').stop(true, true).delay(100).animate({'background-color': '#000'}, 300);
			$('#main .day').stop(true, true).delay(100).animate({'border-top-color': '#000'}, 300);
			$(violet_els).stop(true, true).delay(100).animate({'color': '#bd73b0'}, 300);
			$('#content img').stop(true, true).delay(100).fadeTo(300, 1);
		}
	);

	// "crypter" les adresses e-mail
	// Pour tous les liens commençant par "mailto" ou les span ayant une class "wmail"
	// remplace "[arobase]" par "@" et "[point]" par "."
	$('a[href^="mailto"], span.wmail').each(function (i) {
			//Remplacment du texte dans l'élément
			var temp = $(this).html();
			temp = temp.replace("[arobase]","@");
			temp = temp.replace("[point]",".");
			$(this).html(temp);
			//Si il y a un attribut "href", on remplace le texte dans l'attribut
			if($(this).attr("href")){
					var temphref = $(this).attr("href");
					temphref = temphref.replace("[arobase]","@");
					temphref = temphref.replace("[point]",".");
					$(this).attr("href",temphref);
			}
	})

	// initialisation prettyPhoto (plug-in de type LightBox)
	// http://www.no-margin-for-errors.com/projects/prettyPhoto-jquery-lightbox-clone/
	$('a[rel^="prettyPhoto"]').prettyPhoto({
		animationSpeed: 'fast', 		/* fast/slow/normal */
		padding: 2, 					/* padding for each side of the picture */
		opacity: 0.5, 					/* Value between 0 and 1 */
		showTitle: true, 				/* true/false */
		allowresize: true, 				/* true/false */
		counter_separator_label: '/', 	/* The separator for the gallery counter 1 "of" 2 */
		theme: 'facebook', 				/* light_rounded / dark_rounded / light_square / dark_square / facebook */
		overlay_gallery: false,
		callback: function(){}
	});

	// blocs "dépliants"

	if ($('.depliant').length) {
		$('div.depliant').hide();
		$('h3.depliant').click(function () {
			$(this)
				.toggleClass('depliant_open')
				.next()
				.animate({'height': 'toggle', 'opacity': 'toggle'}, 350);
		});
		// si le hash de l'url existe, on déplie automatiquement le bloc correspondant
		if (location.hash) {
			var hash = location.hash.substring(1);
			$('h3.depliant[rel=' + hash + ']')
				.toggleClass('depliant_open')
				.next()
				.delay(500)
				.animate({'height': 'toggle', 'opacity': 'toggle'}, 750);
		}
	}



}); // fin jQuery

