// Dean Edwards/Matthias Miller/John Resig

/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
    document.write("<script id='__ie_onload' defer src='javascript:void(0)'><\/script>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
        if (this.readyState == "complete") {
            init(); // call the onload handler
        }
    };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            init(); // call the onload handler
        }
    }, 10);
}

/* for other browsers */
window.onload = init;

function init() {
    // quit if this function has already been called
    if (arguments.callee.done) return;

    // flag this function so we don't do the same thing twice
    arguments.callee.done = true;

    // kill the timer
    if (_timer) clearInterval(_timer);

    // do stuff
	initOuvrirFermer();
	initImprimer();
	initPresdechezvousArea();
	initFocusOnLoad();
	initTickerHome();
	initClearSearch();
	initRollEtreApelle();
	initRollNumeroTelephone();
	initRollHomeActualites();
	
};

function gestionFormulaire(elt, etat,valStr) {
	if (etat=='on' && elt.value == valStr)
		elt.value = "";
	if (etat=='off' && elt.value == "")
		elt.value = valStr;
}

function initOuvrirFermer() {
	
	function getNextElementNode(elmt) {
		var next = elmt.nextSibling;
		if (!next) return false;
		if (next.nodeType != 1) next = getNextElementNode(next);
		return next;
	}
	
	function switchDisplayOfElementNodeByTagName(elmt, tag, display) {
		if (elmt.tagName == tag) {
			elmt.style['display'] = display;
			return true;
		}
		else {
			return false;
		}
	}
	
	function getURLParamKeyAndValue(key) {
		var paramsArray = new Array();
		if (location.href.split('?').length > 1) {
			paramsArray = location.href.split('?')[1].split('&');
		}
		var paramsArrayCount = paramsArray.length;
		var resultArray = new Array();
		resultArray.push(paramsArrayCount);
		if (paramsArrayCount != 0) for (var i = 0; i<paramsArrayCount; i++) {
			var param = paramsArray[i];
			var keyAndValue = param.split('=');
			if (key != "" && keyAndValue[0]==key) return keyAndValue[1];
			resultArray.push(keyAndValue);
		}
		return resultArray;
	}
	
	/* On cherche un éventuel paramètre 'offre' indiqué dans l'URL */
	offre = getURLParamKeyAndValue('offre');
	
	var serviceList = $$CN('service','DT');
	serviceNb = serviceList.length;
	for (var i = 0; i<serviceNb ; i++) {
		var serviceItem = serviceList[i];
		var switchNode = document.createElement("dd");
		switchNode.className = 'switch';
		if(offre != serviceItem.id) {
			switchNode.innerHTML = "\n						<p class=\"ouvrir\">En savoir plus</p>\n					";
		}
		else {
			switchNode.innerHTML = "\n						<p class=\"fermer\">Fermer</p>\n					";
		}
		var suivant = getNextElementNode(serviceItem);
		if (suivant.className == 'objectif') suivant = getNextElementNode(suivant);
		insertSwitchNode = suivant.parentNode.insertBefore(switchNode, suivant);
		/* Si une ancre est passée dans l'URL et qu'elle correspond à un de nos élements, il doit être déplié */
		if(offre != serviceItem.id) {
			while (switchDisplayOfElementNodeByTagName(suivant, 'DD', 'none')) {
				suivant = getNextElementNode(suivant);
			}
		}
		switchNode.onclick = function() {
			switchP = this.getElementsByTagName('P');
			if (switchP[0].className == 'ouvrir') {
				suivant = getNextElementNode(this);
				while (switchDisplayOfElementNodeByTagName(suivant, 'DD', 'block')) {
					suivant = getNextElementNode(suivant);
				}
				this.innerHTML = "\n						<p class=\"fermer\">Fermer</p>\n					";
			} else {
				suivant = getNextElementNode(this);
				while (switchDisplayOfElementNodeByTagName(suivant, 'DD', 'none')) {
					suivant = getNextElementNode(suivant);
				}
				this.innerHTML = "\n						<p class=\"ouvrir\">En savoir plus</p>\n					";
			}
		}
	}
}

function initImprimer() {
	var imprimer = $('imprimer');
	if (imprimer) imprimer.onclick = function() {
		window.print();
		return false;
	}
}

function initPresdechezvousArea() {
	if ($('presdechezvous')) {
		areaArray = $$TN('AREA', 'presdechezvous');
		areaArrayLength = areaArray.length;
		if (areaArrayLength > 0) for (var i=0; i < areaArrayLength; i++) {
			area = areaArray[i];
			area.onclick = function() {
				end = this.href.split('/')[this.href.split('/').length-1];
				filename = end.split('.')[0];
				param = end.split('.')[1].split('?')[1];
				if ( window.open(filename+'_popup.php3?'+param, 'contact_popup' ,'toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizable=1,copyhistory=0,menuBar=0,height=500,width=324,top=100,left=250') ) return false;
			}
		}
	}
}

function initFocusOnLoad() {
	if ($('contact_popup')) {
		window.focus();
	}
}

function initTickerHome() {
	
	if (!$('bandeau')) return;
	
	var nbMinLettres = 100; /*Nombre minimal de lettres*/
	var nbMinLettres = 0; /*Nombre minimal de lettres*/
	var delais = 50; /*Temps en milisecondes avant changement du texte*/
	var index = 0; /*Décalage du texte*/
	var taux = 2; /*Largeur du décalage*/

	var bandeau = $('bandeau'); /*Le div qui contient le texte*/
	var p = $$TN('P', 'bandeau')[0];
	var span = $$TN('SPAN', 'bandeau')[0]; /*Le span qui contient le texte*/
	var spanWidth = span.offsetWidth;
	spanLeftValue = span.style['left'].split('px')[0];
	
	var texteComplet = span.innerHTML; /*Le texte*/
	var texteCompletLength = texteComplet.length; /*Nombre de lettres du texte*/
	if (texteCompletLength = 0) return;
	
	//On double le texte pour 'simuler' une boucle de texte.
	span.innerHTML = texteComplet+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+texteComplet;

		function changeText() {
		spanLeftValue = spanLeftValue-taux;
		var absoluteSpanLeftValue = Math.abs(spanLeftValue);
		span.style['left'] = spanLeftValue+'px';
		if (spanLeftValue < 0 && absoluteSpanLeftValue > spanWidth-30) {
			//spanLeftValue = '363';
			// Attention aux signes des variables !! //
			// +95 pout corriger les 110 ou 120 px de large des espaces insécables. //
			spanLeftValue = spanLeftValue+spanWidth+95;
		}
	}
	
	var ticker = setInterval(changeText,delais);
	
	/*Gestion de l'arrêt et de la reprise du défilement*/
	bandeau.onmouseover = function() {
		clearInterval(ticker);
	}
	bandeau.onfocus = function() {
		clearInterval(ticker);
	}
	bandeau.onmouseout = function() {
		ticker = setInterval(changeText, delais);
	}
	bandeau.onblur = function() {
		ticker = setInterval(changeText, delais);
	}
}

function initClearSearch() {
	var recherche = $('zone_recherche');
	if (recherche) {
		recherche.onfocus= function() {
			this.value= '';
		}
		recherche.onblur = function() {
			if (this.value == '') {
				this.value = 'Rechercher';
			}
		}
	}
}

function initRollEtreApelle() {
	var img = $('etre_appele');
	if (img) {
		var src = img.src;
		var srcSplit = src.split('.');
		var ext = srcSplit.pop();
		var debut = srcSplit.join('.');
		document.preload = new Image();
		document.preload.src = debut+'_on.'+ext;
		img.onmouseover = function() {
			if ((this.runtimeStyle)&&(this.runtimeStyle.filter)) // sous IE
			{
				var filterOrig=this.runtimeStyle.filter.split('_img/');
				var ssFilterOrig=filterOrig[1].split("'");
				var basenameExt=ssFilterOrig[0].split(".");
				var baseName=basenameExt[0];
				var ext2=basenameExt[1];
				this.runtimeStyle.filter = "";
				this.runtimeStyle.filter = filterOrig[0]+"_img/"+baseName+"_on."+ext2+"'"+ssFilterOrig[1]+"'"+ssFilterOrig[2]+"'"+ssFilterOrig[3];
			}else
				this.src = debut+'_on.'+ext;
		}
		img.onmouseout = function() {
			if ((this.runtimeStyle)&&(this.runtimeStyle.filter)) // sous IE
			{
					
				var filterOrig=this.runtimeStyle.filter.split('_img/');
				var ssFilterOrig=filterOrig[1].split("'");
				var basenameExt=ssFilterOrig[0].split(".");
				var baseName=basenameExt[0].split("_on")[0];
				var ext2=basenameExt[1];
					this.runtimeStyle.filter = filterOrig[0]+"_img/"+baseName+"."+ext2+"'"+ssFilterOrig[1]+"'"+ssFilterOrig[2]+"'"+ssFilterOrig[3];
			}else
				this.src = debut+'.'+ext;
		}
	}
}

function initRollNumeroTelephone() {
	var imgZoom = $('zoomnumero');
	if(imgZoom) {
		var div = $$CN('contact_telephone','div','annexes')[0];
		if (div) {
			var img = div.getElementsByTagName('IMG')[0];
			var annexes = $('annexes');
			if (img && annexes) {
				annexesTop = annexes.offsetTop;
				annexesLeft = annexes.offsetLeft;
				initialTop = getTop(imgZoom);
				initialLeft = getLeft(imgZoom);
				img.onmouseover = function(e) {
					var initialX = getLeft(this);
					var initialY = getTop(this);
					var x = ((navigator.appName.substring(0,3) == "Net") ? e.pageX : window.event.x+document.body.scrollLeft+getLeft($('annexes')));
					var y = ((navigator.appName.substring(0,3) == "Net") ? e.pageY : window.event.y+document.body.scrollTop+getTop($('annexes')));
					imgZoom.style['top'] = (y-initialY+getTop(img, 'annexes')+20)+'px';
					imgZoom.style['left'] = (x-initialX+getLeft(img, 'annexes')-255)+'px';
					document.onmousemove = function(e) {
						var x = ((navigator.appName.substring(0,3) == "Net") ? e.pageX : window.event.x+document.body.scrollLeft+getLeft($('annexes')));
						var y = ((navigator.appName.substring(0,3) == "Net") ? e.pageY : window.event.y+document.body.scrollTop+getTop($('annexes')));
						imgZoom.style['top'] = (y-initialY+getTop(img, 'annexes')+20)+'px';
						imgZoom.style['left'] = (x-initialX+getLeft(img, 'annexes')-255)+'px';
					}
					imgZoom.style['display'] = 'block';
				}
				img.onmouseout = function() {
					imgZoom.style['display'] = 'none';
					imgZoom.style['top'] = initialTop+'px';
					imgZoom.style['left'] = initialLeft+'px';
					document.onmousemove = 'NULL';
				}
			}
		}
	}
}

function initRollHomeActualites() {
	var divActu = $('actualites');
	if (divActu) {
		divActu.onmouseover = function() {
			this.style['backgroundColor'] = '#FDEDEE';
		}
		divActu.onmouseout = function() {
			this.style['backgroundColor'] = '';
		}
	}
}