//init
window.onload= function(){
	//	alert('init : ' + cat);
	var adresse = window.document.URL;
	//alert(adresse);
	if(adresse.match(/imprimer/))window.print();// impression si demandée
	affich(0);// affichage des liens courants lors de PREMIERE ARRIVEE (page affichée par défaut : 0)
	
	var pagination = $$("a.lien_page");// liens de PAGINATION
	Event.observe($('_0'), "click", affich, false);// page en cours à l'arrivée : page 1 (id=_0) / class="lien_page_crs"
	if(pagination.length){
		for(prop in pagination){
			if(typeof pagination[prop] == 'object') {
				Event.observe(pagination[prop], "click", affich, false); // autres liens : class="lien_page"
			}
		}
	}
	var liens_p = $$("a.a_cat1");// liens MENU GAUCHE
	// "a.a_cat1_crs" observé lors de affich(0) de onload	
	if(liens_p.length){
		for(prop in liens_p){
			if(typeof liens_p[prop] == 'object') {
				Event.observe(liens_p[prop], "click", affich, false);
			}
		}
	}
	var divs = $$("a.titre_2");// liens MENU HAUT
	if(divs.length){
		$H(divs).each(function(item){
			var str = String(item.value);
			//alert(str + ' = ' + cat + ' ? ');
			if(str.match(eval(cat))){
				//alert(item.value);
				var enfants = item.value.childNodes;
				var mess = '';
				for(prop in enfants){
					if(typeof enfants[prop] == "object"){
						var obj = enfants[prop];
						var j = obj['src'].lastIndexOf('.');// ! il pt y avoir plusieurs '.' (http://127.0.0.1/...)
						var chemin = obj['src'].substring(0, j);// chemin + nom de fichier sans extension
						new_img = chemin + '_roll.gif';
						//alert(new_img);
						obj['src'] = new_img;
					}
				}
				//alert('enfants ' + mess);
			}else{
				Event.observe(item.value, "mouseover", hover, false);
				Event.observe(item.value, "mouseout", hover_out, false);
			}
		});
	}
	//menu gauche : gestion du chgt de puce avec mouse
	var liens = $$("a.dossier");
	//var enfants = liens.childNodes
	var n = 0;
	$H(liens).each(function(item, value){
		n++;
		if(n < 14){
			//			alert(item.value + ' => ' + item.value.childNodes.length);
			var enfants = item.value.childNodes;
			Event.observe(item.value, "mouseover", hover2, false);
			Event.observe(item.value, "mouseout", hover2_out, false);
		}
	});
}

/********************* functions *************************/
/**
@ public
@ purpose : gère les liens en cours pour menu gauche ET pagination + affiche la page courante
@ return cible : array : numéro de page / C_cat(s) / A_article(s)
*/
function affich(e){
	// j'afficher la page voulue
	//alert('e : ' + typeof e);
	if(typeof e == 'object'){
		var elt = Event.element(e).id;
	}else{
		var elt = '_0';// 1ère arrivée : page 0 à afficher
	}

	var pagination = 0;
	//alert($(elt).className + ' == ' + $(elt).className.substring(0, 9));
	if($(elt).className.substring(0, 9) == 'lien_page'){// s agit-il d un clic sur un lien de pagination ?
		//alert('pagination : YES');
		pagination = 1;
		elt = elt.substring(1, elt.length);// pour clic sur lien de pagination : enlever le _
	};

	var pages = $$("div.page");
	//	alert('lien : ' + elt);
	var ok = 0;
	if(pages.length){
		var cible = new Array();
		$H(pages).each(function(item, value){
			var id = pages[value].id;
			var compts = id.split('_');
			for(prop in compts){
				if(compts[prop] == elt){
					//alert('id de page : ' + compts.toString());
					cible = compts; // récup numéro/titres cat et art de page affichée
					ok = 1;
				}
			}
			if(ok){
				pages[value].style.display = '';
			}else{
				pages[value].style.display = 'none';
				//$('_' + compts[0]).className = 'lien_page';
			}
			ok = 0;
		});

		// je modifie les styles du menu gauche en conséquence
		affich_pagination(cible);
		// je modifie les styles de la pagination en conséquence
		affich_menu_g(cible);
	}
}
/**
@ private <= function affich
@ purpose : affichage lien en cours de liens de pagination
*/
function affich_pagination(cible){ 
	var pagination = $$("div.pagination"); // je récupère le fragment DOM où se trouvent les liens de pagination
	if(pagination.length){
		$H(pagination).each(function(item, value){
			//	alert(pagination[value].childNodes.toString());
			var pages = pagination[value].childNodes;
			$H(pages).each(function(item, value){
				//alert(typeof pages[value]);
				if(typeof pages[value] == 'object'){
					var num = pages[value];
					if(num.tagName == 'A'){// je lis tous les liens de pagination
						if(num.id == '_' + cible[0]){
							//alert(num.id + ' == _' + cible[0] + ' ? ');
							$(num).className = 'lien_page_crs';
						}else{
							$(num).className = 'lien_page';
						}
					}
				}
			});
		});
	}
}
/**
@ private <= function affich
@ purpose : affichage lien en cours de liens de menu gauche
*/
function affich_menu_g(cible){
	// 	alert ('récup tous les liens de menu_gauche : ');
	var frag = $$('div.cadre_gauche');
	if(frag.length){
		$H(frag).each(function(item, value){
			//				alert(frag[value].childNodes.toString());
			var pages = frag[value].childNodes;
			$H(pages).each(function(item, value){
				//alert(typeof pages[value]);
				if(typeof pages[value] == 'object'){
					var num = pages[value];
					if(num.tagName == 'P' || num.tagName == 'DIV'){
						//alert(num.innerHTML);
						if(num.childNodes.length > 0){
							var liens = num.childNodes;
							$H(liens).each(function(item, value){
								var a = liens[value];
								if(typeof a == 'object'){
									if(a.tagName == 'A'){
										//alert(a.id);
										for(prop in cible){
											if(typeof cible[prop] != 'function' && a.id != ''){// ne prend pas en cpte les a sans id (titres dossiers)
												//	alert(a.id + ' == ' + cible[prop] + ' ? ');
												if(a.id == cible[prop]){
													//alert('en cours : ' + a.id + ' => ' + a.innerHTML + ' => class : ' + a.className);
													a.className = 'a_cat1_crs';
													Event.observe(a, "click", affich, false);
													//alert('en cours : ' + a.id + ' => ' + a.innerHTML + ' => class : ' + a.className);
													break;// finir de lire si style en cours appliqué !
												}else{
													a.className = 'a_cat1';
												}
											}
										}
									}
								};
							});
						}
					}
				}
			});
		});
	}
}

var img_src = '';
function hover(e){
	//alert('hover : ' + Event.element(e).src);
	img_src = Event.element(e).src;
	var i = img_src.lastIndexOf('.');// ! il pt y avoir plusieurs '.' (http://127.0.0.1/...)
	var chemin = img_src.substring(0, i);// chemin + nom de fichier sans extension
	new_img = chemin + '_roll.gif';
	//alert(new_img);
	Event.element(e).src = new_img;
}
function hover_out(e){
	//alert('out');
	Event.element(e).src = img_src;
}
/*********************** hovers menu gauche **********************/
function hover2(e){
	var enfants = Event.element(e).childNodes;
	for(prop in enfants){
		//			alert('nodeType : ' + enfants[prop]["nodeType"] + " => " + enfants[prop]["tagName"]);

		if(enfants[prop]["tagName"] == 'IMG'){
			enfants[prop]["src"] = 'img/LMC/cat/sup_rouge.jpg';
		}/**/

	}
}
function hover2_out(e){
	var enfants = Event.element(e).childNodes;
	for(prop in enfants){
		//alert('nodeType : ' + enfants[prop]["nodeType"] + " => " + enfants[prop]["tagName"]);
		if(enfants[prop]["tagName"] == 'IMG'){
			enfants[prop]["src"] = 'img/LMC/cat/sup_noir.jpg';
		}/**/

	}
}
