/*
 * layout.js
 * requiert jQuery - http://jquery.com
 */

$(document).ready(function() {

	/*
	Pages liens :
	les blocs doivent tous avoir la meme hauteur,
	sinon il y a de grands espaces vides
	*/
	$('div[@class=bloc-liens]').each(function() {
		var h = $(this).height();
		if (h > $(this).prev().height()) {
			$(this).siblings('bloc-liens').height(h);
		}
	});

	/*
	Accueil :
	equilibrage de la hauteur des 2 colonnes
	*/
	$('div[@class=rubArticles]').each(function() {
		var h = $(this).next().height();
		if ($(this).height() < h) {
			$(this).height(h);
		}
	});

	// Article : notes marginales (fix superpositions)
	var docText = $('#docText'),
	    prevBottom = 0,
	    sidenotes = $('ul.sidenotes', docText),
	    sidenotesLastIndex = sidenotes.length - 1;

	sidenotes.each(function() {

		// Ajustement de la position verticale
		// selon celle de la note precedente

		var self = $(this),
		    thisTop = self.offset().top,
		    diffPos = prevBottom - thisTop;

		prevBottom = thisTop + self.height();

		if (diffPos > 0) {
			self.css('top', diffPos);
			prevBottom += diffPos;
		}

		// Ajustement du bloc texte si les notes sont plus hautes
		if (sidenotes.index(this) == sidenotesLastIndex) {
			var textLastChild = docText.children(':last'),
			    textBottom = textLastChild.offset().top + textLastChild.height(),
			    diffBottom = prevBottom - textBottom;
			if (diffBottom > 0) {
				docText.css('padding-bottom', diffBottom + 'px');
			}
		}

	});

});
