function trimNodes(elt) {
	for(i=0; i<elt.childNodes.length; i++) {
		if(elt.childNodes[i].nodeType == 3) {
			// remover le node
			elt.removeChild(elt.childNodes[i]);
		}
	}
}

/****************************************************/
/*					CUSTOM MENU PRINCIPAL			*/
/****************************************************/
function customMenuPrincipal() {
	// cette fonction sert juste à supprimer la bordure du dernier élément de la liste du menu
	if(document.getElementById('menu_menuprincipal')) {
		trimNodes(document.getElementById('menu_menuprincipal'));
		trimNodes(document.getElementById('menu_menuprincipal').lastChild);
		trimNodes(document.getElementById('menu_menuprincipal').lastChild.lastChild);
		var tabMenu = document.getElementById('menu_menuprincipal').lastChild.lastChild;
		tabMenu.firstChild.firstChild.style.borderRight = 'none';
	}
}


/****************************************************/
/*				FIX MENU DEROULANT IE				*/
/****************************************************/
sfHover = function() {
	var sfEls = document.getElementById("menu_menuprincipal").lastChild.getElementsByTagName("LI");
	var allMenus = document.getElementById("menu_menuprincipal").lastChild.childNodes;
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
function showActiveMenu(elt) {
	elt.style.display = 'block';
}
function hideAllMenus(elt) {
	for(jLU=0; jLU<elt.length; jLU++) {
		elt[jLU].style.display = 'none';
	}
}


/****************************************************/
/*					GESTION DU ZOOM					*/
/****************************************************/
var offsetfrommouse=[15,15]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var currentimageheight = 300;
var currentimagewidth = 500;
var hScreenSecurity = 20;
var vScreenSecurity = 100;

function prepareZoom() {
	if((document.getElementById('con_descriptif')) || (document.getElementById('galerie_contact'))) {
		if(document.getElementById('con_descriptif'))
			var tabPhotos = document.getElementById('con_descriptif').getElementsByTagName('a');
		if(document.getElementById('galerie_contact'))
			var tabPhotos = document.getElementById('galerie_contact').getElementsByTagName('a');
		
		var tabPhotosZoom = new Array;
		for(iP=0; iP<tabPhotos.length; iP++){
			if(tabPhotos[iP].getAttribute('rel') == 'lightbox[roadtrip]')
				tabPhotosZoom.push(tabPhotos[iP]);
		}
		for(iPZ=0; iPZ<tabPhotosZoom.length; iPZ++) {
			var urlZoom = tabPhotosZoom[iPZ].href;
			tabPhotosZoom[iPZ].urlZoom = urlZoom;
			tabPhotosZoom[iPZ].onmouseover = function() {
				// si l'on est dans le cas d'une galerie il faut supprimer les éléments vides
				if(document.getElementById('galerie_contact')) trimNodes(this);
				var w = this.firstChild.width;
				var h = this.firstChild.height;
				if( w>0 && h>0 )  {
					if(this.firstChild.width>this.firstChild.height) {
						w = 450;
						h = this.firstChild.height*(w/this.firstChild.width);
					}
					else {
						h = 450;
						w = this.firstChild.width*(h/this.firstChild.height);
					}
					//alert(w+'|'+h);
					w = Math.round(w);
					h = Math.round(h);
					showtrail(this.urlZoom,this.firstChild.getAttribute('alt'),'',w,h);
					}
			}
			tabPhotosZoom[iPZ].onmouseout = function() {
				hidetrail();
			}
			tabPhotosZoom[iPZ].href = 'javascript:void(0)';
		}
	}
}

function gettrailobj(){
	return document.getElementById("trailimageid").style;
}

function gettrailobjnostyle(){
	return document.getElementById("trailimageid");
}

function truebody(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function showtrail(imagename,title,description,zwidth,zheight){
	
	// création du conteneur de zoom
	var conteneurZoom = document.createElement('div');
	conteneurZoom.id = 'trailimageid';
	document.body.appendChild(conteneurZoom);
	// création du titre s'il est rempli
	if(title != '') {
		var titreZoom = document.createElement('h2');
		conteneurZoom.appendChild(titreZoom);
		var txtTitreZoom = document.createTextNode(title);
		titreZoom.appendChild(txtTitreZoom);
	}
	// création du descriptif s'il est rempli
	if(description != '') {
		var descZoom = document.createElement('p');
		conteneurZoom.appendChild(descZoom);
		var txtDescZoom = document.createTextNode(description);
		descZoom.appendChild(txtDescZoom);
	}
	// création de l'image
	
	var imgZoom = document.createElement('img');
	imgZoom.setAttribute('src',imagename);
	imgZoom.id = 'imgZoom';
	imgZoom.setAttribute('alt',description);
	conteneurZoom.appendChild(imgZoom);

	currentimagewidth = zwidth;
	currentimageheight = zheight;
	
	//alert(zwidth+'-'+zheight);
	hScreenSecurity = currentimagewidth;
	//vScreenSecurity = currentimageheight;
	document.onmousemove=followmouse;
}

function hidetrail(){
	document.onmousemove=killfollowmouse;
	var eltTrail = document.getElementById('trailimageid');
	if(eltTrail) document.body.removeChild(eltTrail);
}

function killfollowmouse(e) {
}

function followmouse(e){
	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]

	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)

	if (typeof e != "undefined"){	// MOZILLA
		if (docwidth - e.pageX < hScreenSecurity){
			xcoord = e.pageX - xcoord - hScreenSecurity - 60; // Move to the left side of the cursor
		} else {
			xcoord += e.pageX;
		}
		if (docheight - e.pageY < (currentimageheight + vScreenSecurity)){
			ycoord += e.pageY - Math.max(0,(vScreenSecurity + currentimageheight + e.pageY - docheight - truebody().scrollTop));
		} else {
			ycoord += e.pageY;
		}
	} else if (typeof window.event != "undefined"){		// IE
		if (docwidth - event.clientX < hScreenSecurity){
			xcoord = event.clientX + truebody().scrollLeft - xcoord - hScreenSecurity - 60; // Move to the left side of the cursor
		} else {
			xcoord += truebody().scrollLeft+event.clientX
		}
		if (docheight - event.clientY < (currentimageheight + vScreenSecurity)){
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(vScreenSecurity + currentimageheight + event.clientY - docheight));
		} else {
			ycoord += truebody().scrollTop + event.clientY;
		}
	}

	if(ycoord < 0) { ycoord = ycoord*-1; }
	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"
}


/****************************************************/
/*		GESTION DE L'OUVERTURE DE LIEN EN POPUP		*/
/****************************************************/
function addEvent(source, type, callback) {
  // fonction d'abstraction pour enregistrer un gestionnaire d'evenement
  // comprend le DOM standard, la syntaxe prorietaire MSIE, l'ancien modele HTML
  // source : objet sur lequel ajouter le gestionnaire d'evenement
  // type : type d'evenement
  // callback : fonction qui traitera l'evenement
  if (source.addEventListener){		// code standard DOM
    source.addEventListener(type, callback, false);
    return true;
  } else if (source.attachEvent){ 	// code propriétaire MSIE
    var r = source.attachEvent("on"+type, callback);
    return r;
  } else {        	// code navigateur sans support DOM-event
    eval('source.on' + type + '= callback') ;
  }
}

function getStandardEvent(e) {
 // abstraction pour recuperer un objet standard pour l'evenement en cours 
 // comprend le modele DOM standard et le modele proprietaire de MSIE
 // e : parametre recu lors de l'appel du gestionnaire d'evenement 
 // retour : objet d'evenement standard
 if (e == null && window.event) {
   // cas particulier de MSIE pour recuperer l'evenement en cours
   e = window.event ;
 }
 if (e.target == null && e.srcElement) {
   // cas particulier de MSIE pour recuperer la balise DOM cible
   e.target = e.srcElement ;
 }
 if (! e.preventDefault ){
   // cas particulier de MSIE pour empecher l'action par defaut du navigateur
   e.preventDefault = function () { this.returnValue = false ; } ;
 }
 return e ;
}

function openLinkInPopupWhenClick(e) {
	// gestionnaire d'evenement actif lors d'un clic sur les liens
	// ouvre le lien dans une popup et pas dans une page normale
	// e : evenement de clic
	e = getStandardEvent(e)  ;
	var link =  e.target ;
	(link.nodeName != 'A')?link=link.parentNode:link=link;	// traitement des cas où le 1er noeud n'est pas du texte
	var addr = link.getAttribute('href') ; 
	window.open(addr, '_blank')  ;
	e.preventDefault()  ;
	return false ;
}

function prepareHelpLinks() {
 // explore le document pour rechercher les liens d'aide
 // à chaque lien, on verifie s'il a "help" dans la liste de ses classes
 // si oui, on enregistre un gestionnaire d'evenement pour le clic de ce lien
 var link, list, i ;
 list = document.getElementsByTagName('a') ;
 for(i=0; i<list.length; i++) {
   link = list.item(i) ;
   if (link.getAttribute('href') && link.className) {
     if ((' '+link.className+' ').indexOf(' popup ') != -1) {
       addEvent(link, 'click', openLinkInPopupWhenClick) ;
     }
   }
 }
}
/****************************************************/
/*						DOM NEWS					*/
/****************************************************/
/* Variables, go nuts changing those! */
	// initial position 
	var dn_startpos=120; 			
	// end position
	var dn_endpos=-70; 			
	// Speed of scroller higher number = slower scroller 
	var dn_speed=70;				
	// ID of the news box
	var dn_newsID='actu_ticker';			
	// class to add when JS is available
	var dn_classAdd='hasJS';		
	// Message to stop scroller
	var dn_stopMessage='';	
	// ID of the generated paragraph
	var dn_paraID='DOMnewsstopper';
/****************************************************/
/*				INITIALISATION DE LA PAGE			*/
/****************************************************/
function addLoadListenerPage(func) {
	if (window.addEventListener) {
		window.addEventListener("load", func, false);
	} else if (document.addEventListener) {
		document.addEventListener("load", func, false);
	} else if (window.attachEvent) {
		window.attachEvent("onload", func);
	}
}

if (document.getElementById && document.createTextNode) {
	addLoadListenerPage(function() {
		customMenuPrincipal();
		if (window.attachEvent) sfHover();
		prepareHelpLinks();
		prepareZoom();
		if (document.getElementById('actu_ticker')) initDOMnews();
	});
}