function buildMenu(){
	
	var menuDiv = document.getElementById('menu');
	
	if (!menuDiv) setTimeout('buildMenu', 1000);

	else {
		var elements = menuDiv.getElementsByTagName('li');
		
		for (var i=0; i<elements.length; i++) {
			var img = elements[i].getElementsByTagName('img');
			if (img) {
				var currentImg = img[0];
				var exp = new RegExp('(.*)normal(.*)', 'gi');
				var match = exp.exec(currentImg.src);
				
				if (match && match.length > 1) {
					for (j = 0; j < match.length; j++) {
						currentImg.overIMG     = RegExp.$1 + 'over' + RegExp.$2;
						currentImg.normalIMG   = currentImg.src;
						currentImg.onmouseover = function(){ this.src = this.overIMG   }
						currentImg.onmouseout  = function(){ this.src = this.normalIMG }
					}
				}
			}
		}
	}
	
	// ROLLOVER LISTE LIENS
	var ulArr = document.body.getElementsByTagName('ul');
	for (i=0; i<ulArr.length; i++) {
		var ulElm = ulArr[i];
		if (ulElm.className == 'doc-list') {
			var li = ulElm.getElementsByTagName('li');
			for (j=0; j<li.length; j++) {
				var currentLi = li[j];
				currentLi.onmouseover = function(){
					this.getElementsByTagName('img')[0].src = '/images/divers/picto-doc-active.gif';
				}
				currentLi.onmouseout = function(){
					this.getElementsByTagName('img')[0].src = '/images/divers/picto-doc-grey.gif';
				}
			}
		}
	}
	
	if (document.getElementById('spacer-sitemap') && 
		document.getElementById('div-agenda') &&
		document.getElementById('sitemap')) {
		var hauteurAgenda = document.getElementById('div-agenda').offsetHeight;
		var posYSitemap   = window.document.getElementById('sitemap').offsetTop;
		var posYAgenda    = window.document.getElementById('div-agenda').offsetTop;
		var newHeight     = hauteurAgenda - (posYSitemap - posYAgenda) + 80;
		document.getElementById('spacer-sitemap').style.height = newHeight+"px";
	}
}

window.onload = function(){

	buildMenu();
	
	/********************** AGENDA **************************/
	// FORMATION
	if (document.getElementById('agenda-btn-formation')) {
		document.getElementById('agenda-btn-formation').onmouseover = function(){ this.src = "/images/divers/btn-agenda-over/agenda-btn-formation.gif" }
		document.getElementById('agenda-btn-formation').onmouseout = function(){ this.src = "/images/divers/agenda-btn-formation.gif" }
	}
	// R et D
	if (document.getElementById('agenda-btn-r-d')) {
		document.getElementById('agenda-btn-r-d').onmouseover = function(){ this.src = "/images/divers/btn-agenda-over/agenda-btn-r-et-d.gif" }
		document.getElementById('agenda-btn-r-d').onmouseout = function(){ this.src = "/images/divers/agenda-btn-r-et-d.gif" }
	}
	// LIENS UTILES
	if (document.getElementById('agenda-btn-liens-utiles')) {
		document.getElementById('agenda-btn-liens-utiles').onmouseover = function(){ this.src = "/images/divers/btn-agenda-over/agenda-btn-liens-utiles.gif" }
		document.getElementById('agenda-btn-liens-utiles').onmouseout = function(){ this.src = "/images/divers/agenda-btn-liens-utiles.gif" }
	}
	// HEADER
	if (document.getElementById('agenda-btn-header')) {
		document.getElementById('agenda-btn-header').onmouseover = function(){ this.src = "/images/divers/btn-agenda-over/agenda-header-01.gif" }
		document.getElementById('agenda-btn-header').onmouseout = function(){ this.src = "/images/divers/agenda-header-01.gif" }
	}
	/******************* FIN AGENDA *************************/
	
	/********************* ACTEURS *******************************/
	// INDUSTRIE DE LA VIANDE
	if (document.getElementById('btn-industrie-viande')) {
		document.getElementById('btn-industrie-viande').onmouseover = function(){ this.src = "/images/accueil/over/btn-industrie-viande.gif" }
		document.getElementById('btn-industrie-viande').onmouseout = function(){ this.src = "/images/accueil/btn-industrie-viande.gif" }
	}
	// EAUX MINERALES
	if (document.getElementById('btn-eaux-minerales')) {
		document.getElementById('btn-eaux-minerales').onmouseover = function(){ this.src = "/images/accueil/over/btn-eaux-minerales.gif" }
		document.getElementById('btn-eaux-minerales').onmouseout = function(){ this.src = "/images/accueil/btn-eaux-minerales.gif" }
	}
	// ALIMENTATION ANIMALE
	if (document.getElementById('btn-alimentation-animale')) {
		document.getElementById('btn-alimentation-animale').onmouseover = function(){ this.src = "/images/accueil/over/btn-alimentation-animale.gif" }
		document.getElementById('btn-alimentation-animale').onmouseout = function(){ this.src = "/images/accueil/btn-alimentation-animale.gif" }
	}
	// AUTRES INDUSTRIES
	if (document.getElementById('btn-industries')) {
		document.getElementById('btn-industries').onmouseover = function(){ this.src = "/images/accueil/over/btn-industries.gif" }			
		document.getElementById('btn-industries').onmouseout = function(){ this.src = "/images/accueil/btn-industries.gif" }
	}
	// PRODUITS LACTES
	if (document.getElementById('btn-produits-lactes')) {
		document.getElementById('btn-produits-lactes').onmouseover = function(){ this.src = "/images/accueil/over/btn-produits-lactes.gif" }
		document.getElementById('btn-produits-lactes').onmouseout = function(){ this.src = "/images/accueil/btn-produits-lactes.gif" }
	}
	// CHOCOLAT...
	if (document.getElementById('btn-chocolaterie')) {
		document.getElementById('btn-chocolaterie').onmouseover = function(){ this.src = "/images/accueil/over/btn-chocolaterie.gif" }
		document.getElementById('btn-chocolaterie').onmouseout = function(){ this.src = "/images/accueil/btn-chocolaterie.gif" }
	}
	// PLATS CUISINES
	if (document.getElementById('btn-plats-cuisines')) {
		document.getElementById('btn-plats-cuisines').onmouseover = function(){ this.src = "/images/accueil/over/btn-plats-cuisines.gif" }
		document.getElementById('btn-plats-cuisines').onmouseout = function(){ this.src = "/images/accueil/btn-plats-cuisines.gif" }
	}
	// EQUIPEMENTIERS
	if (document.getElementById('btn-equipements')) {
		document.getElementById('btn-equipements').onmouseover = function(){ this.src = "/images/accueil/over/btn-equipements.gif" }
		document.getElementById('btn-equipements').onmouseout = function(){ this.src = "/images/accueil/btn-equipements.gif" }
	}
	/***************** FIN ACTEURS ***************************/
	
	/***************** POLE AGROALIMENTAIRE ******************/
	// ENTREPRISES
	if (document.getElementById('btn-entreprises')) {
		document.getElementById('btn-entreprises').onmouseover = function(){ this.src = "/images/accueil/over/btn-entretiens.gif" }
		document.getElementById('btn-entreprises').onmouseout = function(){ this.src = "/images/accueil/btn-entretiens.gif" }
	}
	// ACTIONS
	if (document.getElementById('btn-actions')) {
		document.getElementById('btn-actions').onmouseover = function(){ this.src = "/images/accueil/over/btn-actions.gif" }
		document.getElementById('btn-actions').onmouseout = function(){ this.src = "/images/accueil/btn-actions.gif" }
	}
	/*************** FIN POLE AGROALIMENTAIRE ****************/
	// CHIFFRES CLES
	if (document.getElementById('btn-chiffres-cles')) {
		document.getElementById('btn-chiffres-cles').onmouseover = function(){ this.src = "/images/accueil/over/btn-chiffres-cles.gif" }
		document.getElementById('btn-chiffres-cles').onmouseout = function(){ this.src = "/images/accueil/btn-chiffres-cles.gif" }
	}
	// LOIRE ET SAVEURS
	if (document.getElementById('btn-loire-saveurs')) {
		document.getElementById('btn-loire-saveurs').onmouseover = function(){ this.src = "/images/accueil/over/btn-loire-saveurs.gif" }
		document.getElementById('btn-loire-saveurs').onmouseout = function(){ this.src = "/images/accueil/btn-loire-saveurs.gif" }
	}
	// LOIRE AUX 3 VIGNOBLES
	if (document.getElementById('btn-loire-3-vignobles')) {
		document.getElementById('btn-loire-3-vignobles').onmouseover = function(){ this.src = "/images/accueil/over/btn-loire-3-vignobles.gif" }
		document.getElementById('btn-loire-3-vignobles').onmouseout = function(){ this.src = "/images/accueil/btn-loire-3-vignobles.gif" }
	}
	// CUISINIERS DE LA LOIRE
	if (document.getElementById('btn-cuisiniers-de-la-loire')) {
		document.getElementById('btn-cuisiniers-de-la-loire').onmouseover = function(){ this.src = "/images/accueil/over/btn-cuisiniers-de-la-loire.gif" }
		document.getElementById('btn-cuisiniers-de-la-loire').onmouseout = function(){ this.src = "/images/accueil/btn-cuisiniers-de-la-loire.gif" }
	}
	// COMITE
	if (document.getElementById('btn-comite')) {
		document.getElementById('btn-comite').onmouseover = function(){ this.src = "/images/accueil/over/btn-comite.gif" }
		document.getElementById('btn-comite').onmouseout = function(){ this.src = "/images/accueil/btn-comite.gif" }
	}
	// PANIERS GOURMANDS
	if (document.getElementById('btn-paniers')) {
		document.getElementById('btn-paniers').onmouseover = function(){ this.src = "/images/accueil/over/btn-paniers.gif" }
		document.getElementById('btn-paniers').onmouseout = function(){ this.src = "/images/accueil/btn-paniers.gif" }
	}
	// A TABLE
	if (document.getElementById('btn-atable')) {
		document.getElementById('btn-atable').onmouseover = function(){ this.src = "/images/accueil/over/btn-atable.gif" }
		document.getElementById('btn-atable').onmouseout = function(){ this.src = "/images/accueil/btn-atable.gif" }
	}
	//VOIR ACTUALITES
	if (document.getElementById('btn-voir-actus')) {
		document.getElementById('btn-voir-actus').onmouseover = function(){ this.src = "/images/accueil/over/btn-voir-actus.gif" }
		document.getElementById('btn-voir-actus').onmouseout = function(){ this.src = "/images/accueil/btn-voir-actus.gif" }
	}
};

function getAsciiMonth(month){
	var monthArr = new Array();
	monthArr[0] = 'Janvier';
	monthArr[1] = 'Février';
	monthArr[2] = 'Mars';
	monthArr[3] = 'Avril';
	monthArr[4] = 'Mai';
	monthArr[5] = 'Juin';
	monthArr[6] = 'Juillet';
	monthArr[7] = 'Août';
	monthArr[8] = 'Septembre';
	monthArr[9] = 'Octobre';
	monthArr[10] = 'Novembre';
	monthArr[11] = 'Décembre';
	if (month[0] == '0') month = month[1];
	return monthArr[parseInt(month) - 1];
}

function updateActuInfos(actuID){
	$.get('/pages/actualites/actu.ajax.php', { 'actu_id': actuID }, function(actu){
		var dateSplit = actu.date.split('/');
		if (actu.image == 1) {
			actu.content += '<br /><img id="actu-highlight-content-image" src="/pages/actualites/actu.image.php?id='+actuID+'" alt="" style="border:8px solid #ddd;" />';
		}
		$('#actu-highlight-loader').hide();
		$('#actu-highlight-big-date span').first().html(getAsciiMonth(dateSplit[1]) + ' ' + dateSplit[2]);
		$('#actu-highlight-date').hide().html(actu.date);
		$('#actu-highlight-title').hide().html(actu.title);
		$('#actu-highlight-content').hide().html(actu.content);
		$('#actu-highlight-date').fadeIn(300, function(){
			$('#actu-highlight-title').fadeIn(300, function(){
				$('#actu-highlight-content').slideDown(500);
			});
		});
	});
}

function showActu(actuId){
	
	var overlayDIV = $('#overlay');
	var actuDIV    = $('#actu-highlight');
	
	if (actuId) {
		
		location.hash = '/article/'+actuId;
		$('body,html').animate({ scrollTop: 200 }, 500);
		
		var actu = '';
		
		$('#actu-highlight-big-date span').first().html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
		$('#actu-highlight-date').html('');
		$('#actu-highlight-title').html('');
		$('#actu-highlight-content').html('');
		
		// On prend la taille de bottom-div qui est à la taille réelle du contenu
		// alors que body ne l'est pas
		overlayDIV.height(document.body.clientHeight);
		//actuDIV.height(actuDIV.height());
			
		$('#actu-highlight-loader').show();
		
		if (overlayDIV.css('display') == 'none') {
			
			overlayDIV.hide().fadeTo(300, .6, function(){
													   
				actuDIV.css({
					'position':   'absolute',
					'visibility': 'hidden'
				});
				actuDIV.css({
					'display':    'block',
					'visibility': 'visible',
					'left':       ((document.body.clientWidth - actuDIV.width()) / 2) - 14 + 'px'
				});

				actuDIV.fadeIn(500, function(){
					$('#actu-highlight-close-btn').hover(function(){
						$(this).fadeTo(300, .5);
					}, function(){
						$(this).fadeTo(300, 1);
					});
					updateActuInfos(actuId);
				});
				
				$('#actu-highlight').css('cursor', 'pointer').click(function(){
					overlayDIV.fadeOut(300);
					actuDIV.fadeOut(300);
				});
			});
		}
		
		else {
			actuDIV.fadeOut(500, function(){
				updateActuInfos(actuId);
				actuDIV.fadeIn(500);
			});
		}
	}
}

function hideActulisation(){
	var overlayDIV = $('#overlay');
	var actuDIV = $('#actu-highlight');
	actuDIV.fadeOut(200, function(){
		overlayDIV.fadeOut(200);
	});
}

(function($){
	$(document).ready(function(){
		
		$('#historique-entretiens-toggler').click(function(){
			$('#historique-entretiens-panel').slideToggle();
		});

		$('.btn-programme-entretiens').click(function(e){
			e.preventDefault();
			var elt = $(this);
			var iframe = $('<iframe src="'+elt.attr('href')+'" width="800" height="1140" border="0" frameborder="0"></iframe>').css({
				'width': '800px',
				'height': '1140px',
				'padding': '0',
				'margin': '0 auto',
				'border': '0',
				'background': '#fff',
				'overflow': 'visible',
				'text-align': 'left'
			});
			var wrapper = $('<div></div>').css({
				'width': '800px',
				'height': '1140px',
				'padding': '20px 50px 50px 50px',
				'margin': '0 0 50px -450px',
				'background': '#fff',
				'text-align': 'center',
				'position': 'absolute',
				'z-index': 101,
				'top': '20px',
				'left': '50%',
				'-webkit-box-shadow': '0 0 20px rgba(0,0,0,.6)',
				'-moz-box-shadow': '0 0 20px rgba(0,0,0,.6)',
				'box-shadow': '0 0 20px rgba(0,0,0,.6)',
				'filter':'alpha(opacity=0)',
				'-moz-opacity': '0',
				'-webkit-opacity': '0',
				'opacity': '0'
			});
			var overlay = $('<div></div>').css({
				'width': '100%',
				'height': '100%',
				'background': '#000',
				'overflow': 'hidden',
				'position': 'fixed',
				'z-index': 100,
				'top': '0',
				'left': '0',
				'filter':'alpha(opacity=0)',
				'-moz-opacity': '0',
				'-webkit-opacity': '0',
				'opacity': '0'
			});
			$('<a href="javascript:void(); return false;">fermer cette fenêtre</a>').click(function(){
				overlay.fadeTo(350, 0, function(){
					overlay.remove();
				});
				wrapper.fadeTo(500, 0, function(){
					iframe.remove();
					wrapper.remove();							
				});
			}).css({
				'font-size': '14px',
				'line-height': '14px',
				'color': '#111',
				'font-variant': 'small-caps',
				'display': 'block',
				'text-align': 'right',
				'padding': '10px 0 10px 0'
			}).appendTo(wrapper);
			overlay.appendTo(document.body);
			wrapper.appendTo(document.body);
			iframe.appendTo(wrapper);
			overlay.fadeTo(500, .5, function(){
				wrapper.fadeTo(750, 1);
			});
		});
		
		// Colonne "Agenda" : créer une popup avec le texte étendu de l'événement lors du rollover
		$('#right-col li').mouseenter(function(){
			$('.agenda-item-overlay').remove();
			var listItem = $(this);
			var position = listItem.position();
			$('<div class="agenda-item-overlay">'+listItem.html()+'</div>').hide().appendTo('.agenda-item-receiver').css({
				'top': position.top - 1,
				'left': position.left + 25
			}).fadeIn(300).mouseleave(function(){
				$(this).remove();
			});
		});
		$('.agenda-item-receiver').mouseleave(function(){
			$('.agenda-item-overlay').remove();
		});
		
		// Les boutons "lire la suite" ont un effet d'opacité
		$('.fade-on-rollover').hover(function(){
			$('.fade-on-rollover').trigger('mouseleave');
			$(this).animate({ opacity:.5 }, 300);
		}, function(){
			$(this).stop(true,true).animate({ opacity:1 }, 300);
		});
		
		// Le bouton "Haut de page" scroll vers le haut
		$('#scroll-to-top').click(function(){
			$('body,html').animate({ scrollTop: 0 }, 500);
			return false;
		});
		
		// ACTUALITES : montrer l'article demandé dans l'URL (hash du type "#/article/{id}")
		var actuExpression = new RegExp("#/article/([0-9]+)", "g");
		var actuResult  = (actuExpression.exec(location.hash));
		if (actuResult != null && actuResult[1]) {
			var actuId = actuResult[1];
			showActu(actuId);
		}
	});
})(jQuery);

