/*
 * common.js
 * requiert jQuery - http://jquery.com
 */

// Langue du document
var lang = $('html').attr('lang');

$(document).ready(function() {
	
	// Article : résumés
	$('#abstract div[@id^=abstract]:not(:eq(0))').hide();
	$('#abstract a:first-child').addClass('active');
	$('#abstract a').bind('click', function() {
		var lg = $(this).attr('id').substr(7);
		$('#abstract div[@lang=' + lg + ']').show().siblings('div[@id^=abstract]').hide();
		$(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')));
		}
		
		// Calcul des positions verticales
		var thisIdx = $('*').index($(this)[0]);
		var thisTop = $(this).offset().top;
		var diffPos = prevNoteBottom - thisTop;
		prevNoteBottom = thisTop + $(this).height();
		
		// Ajustement du bloc #text si les notes sont plus hautes que le texte
		if (thisIdx === lastSidenotes) {
			var textSection = $('#text');
			var textPadding = parseInt(textSection.css('padding-bottom'));
			var textBottom  = textSection.offset().top + textSection.height();
			var diffBottom  = (prevNoteBottom - textBottom);
			if (diffBottom > 0) {
				textSection.css('padding-bottom', (textPadding + diffBottom) + 'px');
			}
		}
		
		// Correction des positions verticales
		if (diffPos > 0) {
			$(this).css('top', diffPos);
			prevNoteBottom += diffPos;
		}
		
	});
	
	
});
