/****************** init *****************/
var DB = false; // indique les appels de fonctions
var EI = false; // voir fonction a() : indique les états de l'UI
var MS = {rollAffich: false, timeout: false}; // tous rolls invisibles, pas de timeout
// ts divs de class 'menuDown' hidden
// creat timeout variables for list item
// it's for avoid some warning with IE


roll2 = $('roll_2');

roll2.hide();
roll2.addClassName('roll');
roll2.removeClassName('roll_preload'); // pour éviter l'affichage momentané on loading
// roll2.setStyle("{visibility:visible}");
var rollIndexes = ['2'];
function init(){
	//alert('fonction init');
	// MENU_H : roll-over
	Event.observe($('body'), 'mouseover', handleBody, false);
	var menu_h = $('menu_h');
	Event.observe(menu_h, 'mouseover', stopPropagation, false);
	var liens = menu_h.getElementsByClassName('menu_over_lien');
	/*var Debug = _parcourirTout(liens);
	alert(Debug);*/
	liens.each(function(lien){
		//var c = _parcourirTout(lien);
		//alert(c);
		Event.observe(lien, 'mouseover', handleA, false);

		Event.observe(roll2, 'mouseover', handleRoll, false);
	});
	
	// ZOOM + / -
	Event.observe($("zoomPlus"), 'click', handleZoom, false);
	Event.observe($("zoomMoins"), 'click', handleZoom, false);
	}
Event.observe(window, 'load', init, false);
/************************ ZOOM **************************************/
/************* fonctions principales *****************************/
/**
*	gère le zoom + et -
*
* global textes array : liste des id OU classes de style (! avec un '.' au moins !!!) et leurs valeurs de départ (0=>fontSize, 1=>lineHeight)
*/
/*** VALEURS DEPART DES ELTS TEXTE <= BUG 1 : GETSTYLE NE MARCHE PAS AU DEPART ! ***/
var textes = {'texteChapeau': {0:14, 1:25}, 'texteH1': {0:18, 1:25}, 'texteCentre': {0:12, 1:15}, 'h3.q': {0:12, 1:15}, 'h6.texteH6': {0:12, 1:15}, 'presTexte': {0:14, 1: 25}};
function handleZoom(e){
	var action = Event.element(e).id;/*** + OU - ? ***/
	
	for(prop in textes){
		var isClass= prop.match(/\./);
		if(!isClass){/*** recherche directe par ID ***/
			var elt = prop;
			var n = $(elt);
			if((n)){ _zoom(action, n, textes[prop]);}/*** TOUS LES ELTS NE SONT PAS PRESENTS SUR TOUTES LES PAGES ! ***/
		}else{/*** rech de l'id via la CLASSE ***/
			var classes = $$(prop);
			if(classes.length){classes.each(function(c){/*** IDEM ***/
				//alert('question : ' + q.id);
				_zoom(action, c, textes[prop]);
				});<!-- /each -->
			}
		}<!-- /if(isClass) -->
	}<!-- /for -->
}<!-- /function handleZoom -->
/**************** fonctions internes *******************/
/**
* f interne à handleZoom
*
* action String
* elt Object
* tab Array : 0=> fontSize de départ / 1=> lineHeight
*/
function _zoom(action, elt, tab){
	var taille = elt.getStyle('fontSize');
	var line = elt.getStyle('lineHeight');//alert('old pour ' + elt + ' => taille : ' + taille + ' line : ' + line);
	if('' == taille) taille = tab[0];// voir BUG 1 ds handleZoom
	if('' == line) line =  tab[1];
	var a = {'zoomPlus': '+', 'zoomMoins': '-'};
	eval("var nTaille = parseInt('" + taille + "') " + a[action] + " 2;");
	eval("var nLine = parseInt('" + line + "') " + a[action] + " 2;");

	//alert('new taille : ' + nTaille);
	eval("Element.setStyle('" + elt.id + "', {fontSize:'" + nTaille + "px', lineHeight:'" + nLine + "px'});");
}

/************************ gestion du roll-over **************************/
/************** fonctions principales *****************/
function handleA(e){
	//
	if(DB){alert('handleA');a();}
	if(MS.rollAffich){// SI >1 roll affiché
		if(MS.timeout){// SI timeout en attente
			// annuler timeout
			_annulerTimeout();
		}
		// hide tous les rolls
		_hideAll();
	}
	// afficher le 'div_menu<N>' = e
	_affichRoll(e);
	a();
}

function handleBody(e){
	if(DB)alert('evt : ' + e.type + ' sur body ! / timeout == ' + MS.timeout + ' / rollAffich == ' + MS.rollAffich);
	if(MS.rollAffich){ // 
		_hideAll();
	}
	//	a();
}

function handleRoll(e){
	if(DB)alert('handleRoll');
	if(MS.timeout){// SI timeout en attente
		// annuler timeout
		_annulerTimeout();
	}
	a();
	// s'assurer ke le roll associé est affiché ? superflu <= si over = visible !-)
}



/**
 * stop la propagation from menu_h
 */
function stopPropagation(e){
	//alert('stop !');
	Event.stop(e);
}

/********************** fonctions internes *********************/
function _hideAll(e){
	if(DB)alert('_hideAll');
	// cacher tous les rolls
	for(var i = 1; i <  7; i++){
		if(!_isNotIn(i)){
			eval('var roll = $(\'roll_' + i + '\')');
			MS.timeout = setTimeout(function(){roll.hide();}, 300);
		}
	}
	MS.rollAffich = false;
}

function _affichRoll(e){	
	do{
		// afficher le roll correspondant
		//var ev = Event.element(e);
		var ev = Event.findElement(e, 'A');
		var n = ev.id;
		n = n.split('_');
		var m = n[1];
		if(_isNotIn(m)){ //alert('NOT roll'+m);
			break;
		}// vérif que roll+m existe !
				
		if(DB)alert('afficher roll_'+m);
		eval('$(\'roll_'+m+'\').style[\'left\'] = ev.offsetLeft + \'px\'');
		eval('$(\'roll_'+m+'\').show()');
		MS.rollAffich = true;
	}while(false);
	
	Event.stop(e);
}

function _annulerTimeout(e){
	//alert('timeout_2 annulé');
	clearTimeout(MS.timeout);
	MS.timeout = false;
	MS.rollAffich = true;
}
/**
 * global rollIndexes array : indices des rolls existants
 */
function _isNotIn(i){
	for(prop in rollIndexes){
		//alert(prop + ' == ' + i + ' ?');
		if(i == rollIndexes[prop]){ return false;}
	}
	return true;
}
/****************************** DEBUG ***************************************/

function a(){
	if(EI) alert('timeout : ' + MS.timeout + ' / ' + MS.rollAffich);
}
function _parcourirTout(obj){
	//		alert('parcourirTout');
	var n = '';
	var c = 0;
	for(prop in obj){
		n += prop + ' => ' + obj[prop] + '<BR />';
		if(prop == 'style'){ 
			var j = 0;
			var style = obj[prop];
			var nT = '';
			for(prop1 in style){
				nT += prop1 + ' => ' + style[prop1] + '<BR />';
			j++;
			}
			n += '<H4>STYLE  ( ' + j + ' ): </H4>' + nT;
		}
		c++;
	}
	n = '<H4>TOUT : ( ' + c + ' ) </H4>' + n;
	return n;/**/
}

/*** odl 
function afficherRoll(e){
	// cacher tous les rolls
	if(rollAffich){// si roll déja affiché : un timeout a certainement été déclenché en passant sur body en venant sur l'élt...
		if(DB)alert('annuler timeout');
		annulerTimeout();
	} // si le roll n'est pas déja affiché
	hideAll(e);
	rollAffich = true;
	do{
		// afficher le roll correspondant
		var ev = Event.element(e);
		var n = ev.id;
		n = n.split('_');
		var m = n[1];
		if(_isNotIn(m)){ //alert('NOT roll'+m);
			break;
		}// vérif que roll+m existe !
		//		
		if(DB)alert('afficher roll'+m);
		eval('roll'+m+'.style[\'left\'] = ev.offsetLeft + \'px\'');
		eval('roll'+m+'.show()');
	}while(false);
	
	Event.stop(e);
}***/
