/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \
|		
|		Copyright (c) 2008
|		Design + HTML/CSS/DOM JavaScript : Smart Agence
|		http://www.smartagence.com/
|		
\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */


/* ______________________[ 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&&document.getElementById(who)) {
		navRoot=document.getElementById(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", "");}
			}
		}
	}
	SmartFocus(who);
}

function SmartFocus(who) {
	var navLnk=document.getElementById(who).getElementsByTagName("A");
	var navItm=document.getElementById(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", "");
			}
		}
	}
}

/* ______________________[ 02 | Gestion de la taille du texte d’un article ]________________________ */

function SmartSize() {
	var args=SmartSize.arguments;
	var maxResizeCounter = 10;
	var currResizeCounter = 0;
	
	if (document.getElementById && document.getElementById("Tplus") && document.getElementById("Tmoins")) {
		var cibleplus=document.getElementById("Tplus");
		var ciblemoins=document.getElementById("Tmoins");
		cibleplus.onclick=function() {
		  if (currResizeCounter < maxResizeCounter) {
        redim(args, +1, +1, currResizeCounter, maxResizeCounter);
        currResizeCounter += 1;
      }//alert (currResizeCounter);
		};
		ciblemoins.onclick=function() {
		  if (currResizeCounter > -maxResizeCounter) {
        redim(args, -1, -2, currResizeCounter, maxResizeCounter);
        currResizeCounter -= 1;
      }//alert (currResizeCounter);
		};
	}
};

function redim(args, sign, signb, currResizeCounter, maxResizeCounter){
 for (n=0;n<args.length;n++) {
	  if (document.getElementById(args[n])){
			var cibletxt=document.getElementById(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+sign)+"px";
		}
	}
}
/*currResizeCounter==0?(sizestr-2)+"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);
}

/* ______________________[ 05 | insertion automatique de boutons, outils ]________________________ */

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 avant un element existant
			if(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()

/* ______________________[ 06 | Ajout de la page courante aux favoris ]________________________ */

/* Mettre la page courante en favoris — MSIE and Firefox */
function AddBookmark(btn) {
	if (document.getElementById&&document.getElementById(btn)) {
		var el=document.getElementById(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)
};

function SmartSelect(who) {
	var args = SmartSelect.arguments;
	for (n=0;n<args.length;n++) {
		if (document.getElementById(args[n])) {
			mySelect=document.getElementById(args[n]);
			myList=mySelect.getElementsByTagName("UL")[0];
			myInput=mySelect.getElementsByTagName("P")[0];
			ProcessSelect(mySelect, myList, myInput);
		}
	}  
};

function ProcessSelect (Sel, list, inp) {
	list.className="cache";
	inp.onclick = function() {
		affichSelect (Sel, list, inp);
	};	
	inp.onmouseout = null;
	list.onmouseover = function() { this.className="over";};
	list.onmouseout = function() { this.className = "cache"; }; 
};

function affichSelect (Sel, list, inp) {
	if (list.className=="cache"){
		list.className="over";
	}
	else {
		list.className="cache";
	}
}

function SmartMap(maps) {
  if (document.getElementsByTagName("MAP")){
    maps = document.getElementsByTagName("MAP");
    for (n=0;n<maps.length;n++) {
      areas = maps[n].getElementsByTagName("area");
      for (e=0; e<areas.length; e++){
        areas[e].onmouseover = function() {tooltip.show(this)};
        areas[e].onmouseout = function() {tooltip.hide(this)};
      };
    };
  };
};

/* ______________________[ 08 | Lancement des scripts ]________________________ */

window.onload=function() {
	SmartHover("NavigationPrincipale");
	AddToolButton("ToolBox",{linkId:"Print",linkHREF:"javascript:DirectPrint();",imgSrc:"img/outils/bt_print.gif",imgAlt:"Imprimer"});
	AddToolButton("outilsTexte",{linkId:"Tplus",imgSrc:"img/outils/text_bigger.gif",imgAlt:"Texte plus grand"});
  AddToolButton("outilsTexte",{linkId:"Tmoins",imgSrc:"img/outils/text_smaller.gif",imgAlt:"Texte plus petite",insertBefore:"Tplus"});
  AddToolButton("ContenuFooter",{linkId:"innerPrint",linkHREF:"javascript:DirectPrint();",imgSrc:"img/outils/bg_print.gif",imgAlt:"Imprimer l'article"});
  SmartSize("ColContenu", "ColContextuel");
  SmartMap();
  SmartSelect("ActuSelect");
  if(typeof SiteMapManager !== "undefined") {
		var AgefosSMM = new SiteMapManager("plansite").setToggleSwitch(true, "plansite").apply();
	}
  /* IE 5-6 background flicker fix */
	try {document.execCommand('BackgroundImageCache', false, true);}
	catch(e) { }
}
