/*
	contient tous les scripts javascripts n�cessaires au fonctionnement du site
*/
var DEBUG = true;


$(document).ready(function(){
	/* affiche le div pour affichage des infos de d�bogage */
	if (DEBUG) {
		$('#debug').css('display', 'block');
		$('#debug').append('id_secteur : ' + $('#id_secteur').val() + '<br />');
		$('#debug').append('id_rubrique : ' + $('#id_rubrique').val() + '<br />');
		$('#debug').append('id_article : ' + $('#id_article').val() + '<br />');
	}

	/* �vite les cadres autours des liens sous Firefox */
	$('a').bind('focus', function(){
		this.blur();
	});

	/* int�gration des infobulles d�finitions et accronymes dans le texte des articles */
	arr_defs = document.getElementsByTagName('dfn');
	arr_acrs = document.getElementsByTagName('acronym');
	insere_infobulles(arr_defs);
	insere_infobulles(arr_acrs);

	/* attribut la classe en cours � tous les li parents de la classe en-cours */
	has_current_children($('#menu-rubrique ul'));

	/* affichage, masquage sous rubrique menu gauche */
	/*$('#menu-rubrique ul:eq(0)').children().each(function (){
		if ($(this).find('ul').length > 0) {
			$(this).children('a').bind('click', function (){
				if ($(this).parent().find('ul').css('display') == 'none') {
					$('#menu-rubrique ul li ul').hide();
					$(this).parent().find('ul').show();
				} else {
					$(this).parent().find('ul').hide();
				}
				return false;
			});
		}
	});*/

	if ($('body').attr('class') != undefined) {
		var body_classes = $('body').attr('class').split(' ');
		for (i = 0; i < body_classes.length; i++) {
			init(jQuery.trim(body_classes[i]));
		}
	}
});

/* fonction appel�e � l'initialisation de la page, permet une ex�cution conditionnelle en fonction de la rubrique en cours
l'id du tag "body" est de la forme rubrique-x o� x est l'identifiant du secteur en cours
sauf pour la page d'acceuil o� l'id est "index" */
function init(body_class){

	switch(body_class){
	case 'rubrique-1':
		/* page d'accueil
		centrage vertical du texte */
		margin_top = parseInt((($('#conteneur-article').height() / 2) - ($('#bloc-texte').height() / 2)));
		$('#bloc-texte').css('margin-top', parseInt((($('#conteneur-article').height() / 2) - ($('#bloc-texte').height() / 2))) + 'px');

	case 'secteur-2' :
		/* collections */
		/* masque le menu mots si aucun article */
		if ($('.liste-articles li').length == 0 && $('#menu-mots').children(0).html() == $('#menu-mots span').html()) {
			$('#menu-mots').hide();
		}

		/* tout le catalogue */
		$('.liste-articles li .vignettes-sup li a').bind('click', function (){
			var conteneur = $(this.parentNode.parentNode.parentNode).find('.img-article');
			if (conteneur.length > 0) {
				$(conteneur).find('img').remove();
				$(conteneur).find('a').append('<img src="' + $(this).attr('href') + '"/>')
			}
			return false;
		});

		$('.liste-articles li .img-article a').bind('mouseover', function (e){
			var id_str = $(this).parent().parent().attr('id');
			var id_match	= id_str.match(/article\-([0-9]+)$/);
			if (id_match != null) {
				affiche_detail_article(id_match[1]);
			}
			return false;
		});

		/* espace au dessus de la rubrique "nouveaut�s de la semaine" */
		$('#menu-rubrique').children('li:last').css('margin-top', '20px');
	break;
	case 'secteur-7' :
		var url	= window.location.href;
		url_match = url.match(/id_article\=([0-9]+)/);
		
		$('.conteneur-frm-reponse').each(function (){
			$(this).find('a:first').bind('click', function (){
				var mon_form = $(this).parent().parent().find('.frm-reponse');
				if ($(mon_form).css('display') == 'none') {
					$('.frm-reponse').hide();
					$(mon_form).show();
				} else {
					$(mon_form).hide();
				}
				return false;
			});
		});

		/* si prévisualisation de commentaire */
		if ($('fieldset.previsu').length) {
			/* si aucune erreur */
			if ($('.reponse_formulaire').length == 0) {				
				/* masque ce qui suit la prévisualisation */
				$('fieldset.previsu').nextAll().hide();
			}
		} else {			
			// on n'est pas en prévisualisation, masque le formulaire
			$('.frm-reponse').hide();
		}
		
		url_match = url.match(/#forum[0-9]+/)
		if (url_match != null) {
			alert('Votre message a bien été pris en compte.');
		}
	break;
	}
}

function affiche_detail_article(id_article){
	id_article = parseInt(id_article);
	if (isNaN(id_article) || id_article == 0) {
		return false;
	}

	var ajax_url = '/spip.php?page=ajax-description-article&var_mode=calcul&id_article=' + id_article + '&lang=' + $('#lang').val();
	$.get(ajax_url, function(data){
		$('#vignettes-ambiance').html(data);
		var scroll_top = $(window).scrollTop();
		$('#vignettes-ambiance').css('margin-top', scroll_top + 'px');
	});
}

/* d?termine si l'un des noeuds enfants li contenus
dans obj ont la classe current */
function has_current_children(obj){
	var val_return_hcc = false;
	/* parcours les enfants */
	$(obj).children().each(function (){
		/* l'enfant ou un de ses descendants a la class "current" */
		//$('#debug').append('<br />' + $(this).attr('nodeName') + ' ' + $(this).hasClass('current') + ' ' + has_current_children(this));

		if ($(this).hasClass('en-cours')) {
			if ($(this).find('li.en-cours').length == 0) {
				//$(this).find('a').css('background-image', 'none');
				//$(this).find('a').css('font-weight', 'bold');
				//$(this).removeClass('current');
				//$('#debug').append('ICI');
			}
			val_return_hcc = true;
		} else {
			if (has_current_children(this)) {
				//if ($(this).attr('nodeName') == 'LI') {
					$(this).addClass('en-cours');
				//}
				val_return_hcc = true;
			} else {
				$(this).find('a').css('font-weight', 'normal');
				$(this).find('a').css('color', '#727272');
			}
		}
	});
	return val_return_hcc;
}

/* effet pour affichage / masquage du menu gauche */
jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};

function insere_infobulles(html_elements){
	nb = typeof(html_elements) == 'object' ? html_elements.length : 0;
	for (i = 0; i < nb; i++) {
		if (html_elements[i].getAttribute('title')) {

			html_elements[i].insertBefore(document.createElement('span'), html_elements[i].firstChild);
			html_elements[i].firstChild.setAttribute('className', 'cartouche-def');
			html_elements[i].firstChild.setAttribute('class', 'cartouche-def');

			html_elements[i].firstChild.appendChild(document.createElement('span'));
			html_elements[i].firstChild.appendChild(document.createElement('span'));

			html_elements[i].firstChild.getElementsByTagName('span')[0].setAttribute('className', 'cartouche-def-haut');
			html_elements[i].firstChild.getElementsByTagName('span')[0].setAttribute('class', 'cartouche-def-haut');
			html_elements[i].firstChild.getElementsByTagName('span')[1].setAttribute('className', 'cartouche-def-int');
			html_elements[i].firstChild.getElementsByTagName('span')[1].setAttribute('class', 'cartouche-def-int');
			html_elements[i].firstChild.getElementsByTagName('span')[1].appendChild(document.createTextNode(html_elements[i].getAttribute('title')));
			html_elements[i].setAttribute('title', '');

			html_elements[i].onmouseover = function () {
				this.firstChild.style.display = 'block';
				//alert(this.nodeName + this.getAttribute('title') + this.firstChild.nodeName);
			}

			html_elements[i].onmouseout = function () {
				this.firstChild.style.display = 'none';
				//alert(this.getAttribute('title'));
			}
		}
	}
}


function visibilite(thingId)
{
var targetElement;
targetElement = document.getElementById(thingId) ;
if (targetElement.style.display == "none")
{
targetElement.style.display = "" ;
} else {
targetElement.style.display = "none" ;
}
}