/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \
|		
|		Copyright (c) 2008
|		Design + HTML/CSS/DOM JavaScript : Smart Agence
|		http://www.smartagence.com/
|		
\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */


function $id(sID) {
	return document.getElementById(sID);
};


function hasCSS() {
    var _d = document.createElement('div');
    _d.id = 'css_test';
    document.getElementsByTagName("body")[0].appendChild(_d);
    $id('css_test').style.width="1px";
	$id('css_test').style.height="1px";
	$id('css_test').style.display="none";
    var _v = ($id('css_test').style.width != "1px") ? false : true;
	document.getElementsByTagName("body")[0].removeChild($id('css_test'));
	return _v;
};


function AddToolButton(sContainerID, oParams){
	var DOM = document.createElement && document.getElementById && document.getElementById(sContainerID);
	if(DOM){
		var cible=document.getElementById(sContainerID).getElementsByTagName('UL');
		
		// no container list? let's create it
		if(cible.length == 0) {
			var oUL = document.createElement('UL');
			document.getElementById(sContainerID).appendChild(oUL);
		}
		cible = document.getElementById(sContainerID).getElementsByTagName('UL');
		
		if(cible){
			var oLi = document.createElement("LI");
			var oLink = document.createElement("A");
			var oImg = document.createElement("IMG");
			
			with(oLink){
				id = oParams.linkId;
				title = oParams.imgAlt;
				href = oParams.linkHREF? oParams.linkHREF : "javascript:;";
				onclick = oParams.onclick? oParams.onclick : "";
			};
			with(oImg){
				src = oParams.imgSrc;
				alt = oParams.imgAlt;
				className = oParams.imgClass;
			};
			oLink.appendChild(oImg);
			oLi.appendChild(oLink);
			
			// insertion&hellip; avant un element existant
			if(oParams.insertBefore && document.getElementById(oParams.insertBefore)) {
				var oNext = document.getElementById(oParams.insertBefore).parentNode;
				cible[0].insertBefore(oLi,oNext);
			}
			else{
				// ou en fin de liste
				cible[0].appendChild(oLi);
			}
		}	// end if cible
	}	// end if DOM
};	// end AddToolButton()

/* ______________________[ 01 | Interactivité du menu principal (menu horizontal) ]________________________ */

/* A special thanks goes to Eric Shepherd for his ALA article about “Hybrid CSS Dropdowns”: http://www.alistapart.com/articles/hybrid/ 
and to Patrick Griffiths and Dan Webb for their htmldog.com article “Sons of Suckerfish”: http://www.htmldog.com/articles/suckerfish/ */

function SmartHover(who) {
	if (document.all&&document.getElementById&&document.getElementsByTagName&&$id(who)) {
		navRoot=$id(who);
		for (i=0;i<navRoot.childNodes.length;i++) {
			node=navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {this.className+=" over";}
				node.onmouseout=function() {this.className=this.className.replace(" over", "");}
			}
		}
	}
	
	if($id(who)) {
		SmartFocus(who);
	}
};

function SmartFocus(who) {
	var navLnk=$id(who).getElementsByTagName("A");
	var navItm=$id(who).getElementsByTagName("LI");
	for (var n=0;n<navItm.length;n++) {
		if (navItm[n].className!="on") navItm[n].className="y";
	}
	for (var i=0;i<navLnk.length;i++) {
		navLnk[i].onfocus=function() {
			if (this.parentNode.parentNode.id==who) {
				this.parentNode.className+=" over";
			} else {
				this.parentNode.parentNode.parentNode.className+=" over";
			}
		}
		navLnk[i].onblur=function() {
			if (this.parentNode.parentNode.id==who) {
				this.parentNode.className=this.parentNode.className.replace(" over", "");
			} else {
				this.parentNode.parentNode.parentNode.className=this.parentNode.parentNode.parentNode.className.replace(" over", "");
			}
		}
	}
}


function IMGRollover() {
	var imgs,i;
	imgs=document.getElementsByTagName('img');
	for(i=0;i<imgs.length;i++) {
		if (/roll/.test(imgs[i].className)) {
			imgs[i].onmouseover=function(){roll(this);};
			imgs[i].onmouseout=function(){roll(this);};
		}
	}
};

function roll(o) {
	var src,ftype,newsrc;
	src=o.src;
	ftype=src.substring(src.lastIndexOf('.'), src.length);
	if(/_over/.test(src)) {
		newsrc=src.replace('_over','');
	} else {
		newsrc=src.replace(ftype, '_over'+ftype);
	}
	o.src=newsrc;
};


/* ______________________[ 02 | Gestion de la taille du texte d’un article ]________________________ */

function SmartSize() {
	var args=SmartSize.arguments;
	if (document.getElementById&&$id("Tplus")&&$id("Tmoins")) {
		var cibleplus=$id("Tplus");
		var ciblemoins=$id("Tmoins");
		cibleplus.onclick=function() {
			for (n=0;n<args.length;n++) {
				if ($id(args[n])) {
					var cibletxt=$id(args[n]);
					var sizestr=cibletxt.style.fontSize.substring(0,cibletxt.style.fontSize.length-2);
					var sizeinter=cibletxt.style.lineHeight.substring(0,cibletxt.style.lineHeight.length-2);
					sizestr=sizestr!=""?parseInt(sizestr):12;
					cibletxt.style.fontSize=sizestr+1+"px";
				}
			}
		}
		ciblemoins.onclick=function() {
			for (n=0;n<args.length;n++) {
				if ($id(args[n])) {
					var cibletxt=$id(args[n]);
					var sizestr=cibletxt.style.fontSize.substring(0,cibletxt.style.fontSize.length-2);
					var sizeinter=cibletxt.style.lineHeight.substring(0,cibletxt.style.lineHeight.length-2);
					sizestr=sizestr!=""?parseInt(sizestr):12;
					cibletxt.style.fontSize=sizestr-1+"px";
				}
			}
		}
	}
}


/* ______________________[ 03 | Lancement d’une impression pour les navigateurs compatibles ]________________________ */

function DirectPrint() {
	if (window.print) self.print();
}


/* ______________________[ 04 | Miscellaneous ]________________________ */

function OpenPopup(url,nom,option) {
	window.open(url,nom,option);
}

function SmartRescueForm() {
	var args=SmartRescueForm.arguments;
	for (n=0;n<args.length;n++) {
		var cibleSelectName=args[n];
		if (document.getElementById&&$id(cibleSelectName)) {
			var cibleSelect=$id(cibleSelectName);
			cibleSelect.className="show";
		}
	}
}


/* ______________________[ 05 | Gestion de l’ouverture/fermeture d’un <h2> » ]________________________ */

function SmartPlan(cible) {
	if (document.getElementById&&$id(cible)) {
		var root=$id(cible).getElementsByTagName("H2");
		for (a=0;a<root.length;a++) {
			if (root[a].nodeName=="H2") {
				SmartOpenClose(root[a]);
			}
		}
	}
}

function SmartOpenClose(who) {
	var args=SmartOpenClose.arguments;
	if (who.nextSibling&&who.nextSibling.className=="treeview") {
		var Sister=who.nextSibling;
		Sister.style.display="block";
		who.className="on";
	} 
	if (!document.all&&who.nextSibling.nextSibling&&who.nextSibling.nextSibling.className=="treeview") {
		var Sister=who.nextSibling.nextSibling;
		Sister.style.display="block";
		who.className="on";
	}
	who.onclick=function() {
		if (Sister) {
			Sister.style.display=Sister.style.display=="none"?"block":"none";
			who.className=who.className=="off"?"on":"off";
		}
	}
}


/* ______________________[ 06 | Ajout de la page courante aux favoris ]________________________ */

/* Mettre la page courante en favoris — MSIE and Firefox */
function AddBookmark(btn) {
	if (document.getElementById&&$id(btn)) {
		var el=$id(btn);
		var operaStr="« Ctrl + T » pour ajouter aux favoris.";
		if (document.all&&navigator.userAgent.indexOf("Opera")==-1) {
			el.onclick=function() {
				window.external.AddFavorite(document.location,document.title);
			}
		} else if (navigator.userAgent.indexOf("Firefox")!=-1) {
			el.onclick=function () {
				window.sidebar.addPanel(document.title,document.location,"");
			}
		} else if (navigator.userAgent.indexOf("Opera")!=-1) {
			el.title=operaStr;
			window.status=operaStr;
		}
	}
}

/* Get elements by class name(s) */
function getElementsByClassName(oElm, strTagName, oClassNames){		//src element, target element tag, class name(s)
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
	if(typeof oClassNames == "object"){
		for(var i=0; i<oClassNames.length; i++){
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
		}
	}
	else{
		arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
	}
	var oElement;
	var bMatchesAll;
	for(var j=0; j<arrElements.length; j++){
		oElement = arrElements[j];
		bMatchesAll = true;
		for(var k=0; k<arrRegExpClassNames.length; k++){
			if(!arrRegExpClassNames[k].test(oElement.className)){
				bMatchesAll = false;
				break;
			}
		}
		if(bMatchesAll){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
};

/* ______________________[ 07 | <select/> au clavier ]________________________ */

/* Thanks goes to Cameron Adams from http://www.themaninblue.com/ */

function initSelect(whichSelect) {
	if (document.getElementById&&$id(whichSelect)) {
		var theSelect=$id(whichSelect);
		theSelect.changed=false;
		theSelect.onfocus=selectFocussed;
		theSelect.onchange=selectChanged;
		theSelect.onkeydown=selectKeyed;
		theSelect.onclick=selectClicked;
		return true;
	}
}

function selectChanged(theElement) {
	var theSelect;
	if (theElement&&theElement.value) {
		theSelect=theElement;
	} else {
		theSelect=this;
	}
	if (!theSelect.changed) {
		return false;
	}
	if (theSelect.value&&theSelect.value!="0") window.location=theSelect.value;
	return true;
}

function selectClicked() {
	this.changed=true;
}

function selectFocussed() {
	this.initValue=this.value;
	return true;
}

function selectKeyed(e){
	var theEvent;
	var keyCodeTab="9";
	var keyCodeEnter="13";
	var keyCodeEsc="27";
	if (e) {
		theEvent=e;
	} else {
		theEvent=event;
	}
	if ((theEvent.keyCode==keyCodeEnter||theEvent.keyCode==keyCodeTab)&&this.value!=this.initValue) {
		this.changed=true;
		selectChanged(this);
	} else if (theEvent.keyCode==keyCodeEsc) {
		this.value=this.initValue;
	} else {
		this.changed=false;
	}
	return true;
}


function strippedList(sID) {
	var oList = $id(sID);
	var counter = 0;
	
	if(oList) {
		for(i=0; i<oList.childNodes.length;i++) {
			if(oList.childNodes[i].nodeType == 1) {
				oList.childNodes[i].className = (counter%2 == 0 ? "even":"");
				counter += 1;
			}
		}
	}
};

function AddSearchBehaviour(sElementID) {
	if(document.getElementById && document.getElementById(sElementID)) {
		var oSearchInput = document.getElementById(sElementID);
		var sInitValue = oSearchInput.value;
		oSearchInput.onclick = function() {
			if(this.value === sInitValue) {
				this.value = "";
			}
		};
		oSearchInput.onblur = function() {
			if(this.value === "") {
				this.value = sInitValue;
			}
		};
	}
};


/* ______________________[ 08 | Lancement des scripts ]________________________ */

window.onload=function() {
	/* IE 5-6 background flicker fix */
	AddToolButton("ToolBox",{linkId:"Print",linkHREF:"javascript:DirectPrint();",imgSrc:"img/layout/toolbox/print.gif",imgAlt:"Imprimer cette page"});
	if(hasCSS()) {
		try {document.execCommand('BackgroundImageCache', false, true);}
		catch(e) { }
		SmartHover("NavigationPrincipale");
		AddToolButton("ToolBox",{linkId:"Tmoins",imgSrc:"img/layout/toolbox/smaller.gif",imgAlt:"Texte plus petit",insertBefore:"Print"});
		AddToolButton("ToolBox",{linkId:"Tplus",imgSrc:"img/layout/toolbox/bigger.gif",imgAlt:"Texte plus grand",insertBefore:"Tmoins"});
		SmartSize("ColMenu","ColContenu","ColContextuelle");
		IMGRollover();
		
		if($id("SlideMenu")) {
			slideMenu.build('SlideMenu',410,10,5);
		}
		
		if($id("plansite")) {
			var SiteMap = new SiteMapManager("plansite").setToggleSwitch(true,"plansite").apply();
		}
		
		AddSearchBehaviour("SearchQuery");
		
		strippedList("Actus");
	}
};