/*
 * common.js
 * requiert jQuery - http://jquery.com
 */

$(document).ready(function() {
	
	// Article : resumes
	$('div.abstract:not(.shown)').addClass('hidden');
	$('#navAbstracts a:first-child').addClass('active');
	$('#navAbstracts a').click(function() {
		var thisLang = $(this).attr('id').substr(7);
		$('#abstract div[@lang=' + thisLang + ']').siblings('div.abstract').addClass('hidden').end().removeClass('hidden');
		$(this).addClass('active').siblings('a').removeClass();
		return false;
	});
	
	// Article : suppression des mises en forme locales sur les images
	$('img').removeAttr('style');
	
	// Article : notes marginales (fix superpositions)
	var prevNoteBottom = 0;
	var lastSidenotes  = $('*').index($('.sidenotes:last')[0]);
	$('.sidenotes').each(function() {
		// Alignement avec le premier paragraphe,
		// si le premier frere est un niveau de titre
		var heading = $(this).siblings('*[@id^=heading]:first-child');
		if (heading.length) {
			$(this).css('top', heading.height() + parseInt(heading.css('margin-bottom')));
		}
		// Correction des positions verticales
		var thisIdx = $('*').index($(this)[0]);
		var thisTop = $(this).offset().top;
		var diffPos = prevNoteBottom - thisTop;
		prevNoteBottom = thisTop + $(this).height();
		if (diffPos > 0) {
			$(this).css('top', diffPos);
			prevNoteBottom += diffPos;
		}
		// Ajustement du bloc #text
		// si le dernier paragraphe est court avec beaucoup de notes
		// (debordement sur la section suivante)
		if (thisIdx === lastSidenotes) {
			var textSection = $('#text');
			var textPadding = parseInt(textSection.css('padding-bottom'));
			var textBottom  = textSection.offset().top + textSection.height();
			var diffBottom  = ($(this).offset().top + $(this).height()) - textBottom;
			if (diffBottom > 0) {
				textSection.css('padding-bottom', (textPadding + diffBottom) + 'px');
			}
		}
	});
	
	// Acces restreint
	var loginError = $('#loginbox strong.error');
	var loginBlock = $('#loginbox div.togglable');
	if (loginError.length < 1 && $('#loginbox').hasClass('log-out') === false) {
		loginBlock.hide();
	}
	$('#loginbox.log-in legend > span')
		.hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		})
		.click(function() {
			loginBlock.toggle();
			return false;
		});
	$('#loginbox.log-in img.cancel')
		.click(function() {
			loginBlock.hide();
			loginError.remove();
		});
	
	// Signaler : verifications
	var isMail = /^[\w\-]+(\.[\w\-]+)*@[\w\-]+(\.[\w\-]+)*\.[\w\-]{2,}$/;
	$('#sendTo').submit(function() {
		var error = new Array();
		$(this).find('label').removeClass('error');
		if (false === isMail.test($('#to').val())) { error[0] = 'to'; }
		if (false === isMail.test($('#from').val())) { error[1] = 'from'; }
		if (error.length > 0) {
			for (e in error) {
				$('label[@for='+error[e]+']').addClass('error');
			}
			return false;
		}
		return true;
	});
	
});


