/*
	contient tous les scripts javascripts necessaires au fonctionnement du site
*/
var DEBUG = false; // affiche dans la page les ids de secteur, rubrique, article

/* execute au chargement */
$(document).ready(function(){
	/* affiche le div pour affichage des infos de debogage */
	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 />');
	}

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

	/* Transformation du menu langues */
	$('#menu-lang a').each(function (){
		$(this).html($(this).attr('title'));
	});

	/*
	$('#navigation span').css('border', '1px solid red');
	$('#navigation span').each(	function (){
		$(this).html('&gt;' + $(this).html());
	});
	*/


	/* attribut la classe en cours a 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;
			});
		}
	});

	/* centrage nav haut */
	//$('ul#navigation').halign();
	//ab$('ul#navigation').show();

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

	/* affichage des mini-fiches sur la mise en scène */

	$('.mes-repere').bind('mouseover', function(evt){
		var mini_fiche  = $(this).parent().find('.mes-article-fiche');

		$('.mes-article-fiche').each(function(){
			if ($(this).attr('id') != mini_fiche.attr('id')) {
				$(this).hide();
			}
		});

		if (mini_fiche.length) {
			var conteneur	= $(this).parents('#conteneur-mes');
			var max_x		= conteneur.width() - 10;
			var max_y		= conteneur.width() - 60;
			var pos = $(this).position();
			var post = pos.top;
			var posl = pos.left;
			var mf_w = mini_fiche.width();
			var mf_h = mini_fiche.height();
			/*post += 40;*/
			if ((posl + mf_w) > max_x) {
				posl = max_x - mf_w;
			}

			if ((post + mf_h) > max_y) {
				post = max_y - mf_h;
			}

			mini_fiche.css('top', post + 'px');
			mini_fiche.css('left', posl + 'px');
			mini_fiche.css('z-index', '2');
			mini_fiche.show();


			// si pas de survol dans un certain laps de temps, ferme la fiche
			$(this).bind('mouseleave', function(){
				var mini_fiche  = $(this).parent().find('.mes-article-fiche');
				cmd = 'if(!$("#' + mini_fiche.attr('id') + '").hasClass("mes-fiche-mouseover")){$("#' + mini_fiche.attr('id') + '").hide();}';
				setTimeout(cmd, 1000);
			})


			mini_fiche.bind('mouseenter', function(){
				$(this).addClass('mes-fiche-mouseover');
			});

			//mini_fiche.addClass('mes-en-cours');
			mini_fiche.bind('mouseleave', function(){
				$(this).removeClass('mes-fiche-mouseover');
				$(this).hide();
			});
		}
	})
});

/**
 *
 * @access public
 * @return void
 **/
function ouvre_fiche(id_fiche, x, y){
	/* ferme les autres fiches */
	//$('.mes-article-fiche').each(function(){
	//	if ($(this).attr('id') != id_fiche) {
	//		$(this).hide();
	//		$(this).removeClass('mes-en-cours');
	//	}
	//});
	//mes-fiche-mouseover
}


/*
	fonction appelee a l'initialisation de la page, permet une execution conditionnelle
	en fonction de la rubrique en cours


*/
function init(body_class){

	switch(body_class){
	case 'secteur-1' :
		/* accueil */
		if ($('#actu-gauche-sommaire').length == 0) {
			$('#bg-actu-gauche-sommaire').hide();
		}
	break;
	case 'secteur-2' :
		/* collections */
		$('#collection-couleurs li a').fancybox({
		'hideOnContentClick': true,
		'titleShow': true,
		'titlePosition'  		: 'inside'
		});

		$('#pp-image a').fancybox({
		'hideOnContentClick': true,
		'titleShow': false
		});
	break;
	case 'secteur-3' :
		/* services */

	break;
	case 'secteur-5' :
		/* services */
		$('.img-news').fancybox({
		'hideOnContentClick': true,
		'titleShow': true,
		'titlePosition'  		: 'inside'
		});
	break;
	case 'secteur-4' :
		/* contact */
		/* masque le formulaire de contact lors de la prévisualisation du message avant l'envoi */
		if ($('form .previsu').length) {
			$('form .previsu').parent().find('fieldset:eq(1)').hide();
		}

		//$('.spip_form_label_obligatoire').parent().css('border', '1px solid #000');
		$('.spip_form_label_obligatoire').parent().each(function (){
			//$(this).css('border', '1px solid #000');
			$(this).html('*' + $(this).html());
		})
	break;
	}
}

/*
	determine si l'un des noeuds enfants li contenus
	dans obj ont la classe "en-cours",
	si c'est le cas retourne true
*/
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" */
		if ($(this).hasClass('en-cours')) {
			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;
}
