/*
 * common.js
 * requiert jQuery - http://jquery.com
 */

// Langue du document
var lang = $('html').attr('lang');

$(document).ready(function() {
	
	// Cleobarre
	$('#cleoForm').bind('submit', function() {
		window.location.href = $('#listeRevues option:selected').val();
		return false;
	});
	
	// Accès abonnés
	//if ($('#loginBox > form').is('form')) {
	if ($('#loginBox > form').hide()) {
		$('#loginBox > a:first-child')
			.bind('click', function() {
				$('#loginBox > form').toggle();
				return false;
			});
		$('#loginBox :button')
			.bind('click', function() {
				$('#loginBox > form').hide();
				return false;
			});
	}
	
	fixNotesPosition();
	//fixBlockHeight('toto');
	
	/*
	if ($('#entries').is('div')) {
		$('#entries > div.hidden').hide();
		$('#A').show();
		$('#alphabet > a')
			.attr('href', '#')
			.bind('click', function(){
				$('#entries > div.hidden').hide();
				$('div#' + $(this).text()).toggle();
			});
	}
	*/
	
	// Hauteur contenu/navigation :
	// le contenu doit toujours être plus haut que la nav
	var navHeight = $('#nav').height();
	if (navHeight > $('#main').height()) {
		$('#main').css({'height': navHeight + 100});
	}
	
	// Hauteur de la colonne syndication
	var contentHeight = $('#content').height();
	$('#syndication').height(contentHeight);
	
	// Suppression des mises en forme locales
	var tagsToCleanUp = ['img', 'table', 'tr', 'td'];
	for (var tag in tagsToCleanUp) {
		$(tagsToCleanUp[tag]).removeAttr('style');
	}
	
	// Vignettes de sommaire de médiathèque
	var h = 0;
	$('.fichiers').each(function() {
		h = ($(this).height() > h) ? $(this).height() : h;
		$(this).siblings('.fichiers').height(h);
	});
	
	/*
	$('div.thumb').each(function() {
		w = $(this).width();
		h = $(this).height();
		if (w > wMax) {wMax = w;}
		if (h > hMax) {hMax = h;}
		$('div.thumb').width(wMax).height(hMax);
	});
	*/
	
	
	
	// Notes marginales
	//$('ul[@class=sidenotes]//a');
	
	// Suppression notes marginales dans titre d'article
	$('h1 > ul').remove();
	
	// Formatage des appels de notes
	$('a[@class$=notecall]//span').removeAttr('style');
	$('a[@class$=notecall]').parents('span').removeAttr('style');
	//$('a[@class$=notecall]').prepend('&nbsp;(').append(')');
	
	// Rustines diverses
	//$('h2//ul[@class=sidenotes]').remove();
	
	// tpl/image.html : lien Fermer la fenêtre
	if ($('#popUpImage').is('body')) {
		if (window.opener) {
			$('#backLink')
				.attr('href', '#')
				.html('Fermer la fenêtre')
				.click(function() { window.close(); return false; });
		}
	}
	
	// Article : équilibrage des entrées d'index
	//if ($('#docIndex').is('div')) {
	//	fixBlockHeight('entries');
	//}
	
	// Présentation
	//$('#publiPresentation > p[@lang=' + lang + ']').addClass('first');
	$('#publiPresentation > p:eq(0)').addClass('first');
	
	// Sommaire : effet de survol
	/*
	$('#summary li.textes').hover(function() {
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
	*/
	
	// Article : sections dépliantes
	$('div.togglable').not('.shown').hide();
	$('span.toggle').addClass('JSon').bind('click', function() {
		$(this).toggleClass('active').parent().next('div').toggle();
	});
	
	/*
	$('#abstract > div:not(#abstract-' + lang + ')').hide();
	$('a[@id=toggle-abstract-' + lang).addClass('active').css({'color':'red'});
	$('a[@id^=toggle-abstract]').bind('click', function() {
		$('#abstract-' + $(this).attr('id').substr(15)).show().siblings().hide();
		$(this).addClass('active').siblings('a').removeClass();
		return false;
	});
	*/
	$('#abstract p:not(:eq(0))').hide();
	$('#abstract a:first-child').addClass('active');
	$('#abstract a').bind('click', function() {
		var lg = $(this).attr('id').substr(7);
		$('#abstract p[@lang=' + lg + ']').show().siblings('p').hide();
		$(this).addClass('active').siblings('a').removeClass();
		return false;
	});
	
	// Liens de navigation dans l'article (raccourcis)
	$('a[@href=#abstract]').bind('click', function() {
		$('#abstract').show().prev('h2').addClass('active');
	});
	$('a[@href=#entries]').bind('click', function() {
		$('#entries').show().prev('h2').addClass('active');
	});
	$('a[@href=#toc]').bind('click', function() {
		$('#toc').show().prev('h2').addClass('active');
	});
	
	
	// Corrections de stylage (propositions pour Discours)
	$('*[@id^=heading]').next('p[@class=texte]').addClass('paragraphesansretrait');
	$('p.citationbis > span:first-child').removeAttr('style').addClass('numCitation');
	
	
	// Appels de notes
	$('#docText a.footnotecall')
		.prepend('[').append(']')
		.bind('click', function() {
			$('a#' + $(this).attr('id').substr(4))
				.parent().addClass('noteHighlight')
				.siblings().removeClass('noteHighlight');
		});
		
	// Notes de bas de page
	$('#notes a[@class*=ootnote]')
		.prepend('[').append(']')
		.bind('click', function() {
			$(this).parent().removeClass('noteHighlight');
		});
	
	
	
});

// Retourne la position d'un élément
// http://www.dyve.net/jquery/?autocomplete
function findPos(obj) {
	var curLeft = obj.offsetTop || 0;
	var curTop = obj.offsetTop || 0;
	while (obj = obj.offsetParent) {
		curLeft += obj.offsetTop;
		curTop += obj.offsetTop;
	}
	return {x:curLeft,y:curTop};
}

// Dompte les notes marginales
function fixNotesPosition() {
	// La position du bas de la note précédente
	var prevOffsetBottom = 0;
	// Traitement des positions
	$('ul[@class=sidenotes]').each(function() {
		var thisPos = findPos($(this).get(0));
		var diffPos = prevOffsetBottom - thisPos.y;
		prevOffsetBottom = thisPos.y + $(this).height();
		if (diffPos > 0) {
			//$(this).css({'top': diffPos, 'background': '#FFFFB3'});
			$(this).css({'top': diffPos});
			prevOffsetBottom += diffPos;
		}
	});
}

// Équilibre la hauteur d'un ensemble de blocs
function fixBlockHeight(objClass) {
	var h = 0, hMax = 0;
	/*
	$('div[@class=' + objClass + ']').each(function() {
		h = $(this).height();
		if (h > $(this).prev().height()) {
			$(this).siblings().height(h);
		}
	});
	*/
	
}

/* http://happygiraffe.net/blog/articles/2007/09/26/jquery-logging */
jQuery.fn.log = function (msg) {
	console.log("%s: %o", msg, this);
	return this;
};
