//------------------- DEFINITION DES  CLASSES -------------------//

//CLASSE DE STYLE
function style() {
	this.class_css=''; //class css à utiliser
	this.bg_im=''; //background image propre à la case
	this.bg_color=''; //background color propre à la case
	this.text_color='';//couleur du texte
	this.roll=''; //rollover propre à la case
	this.roll_color='';//couleur bg du rollover
	this.roll_text_color='';//couleur du texte quand rollover
	this.margin=''; //  margin : top right bottom left;
	this.width='';
	this.height='';
	this.border_size='';
	this.pref=''; //s'affichera avant l'intitule du menu
	this.css_perso='';
	
	
	//code javascript pour rollover
	this.get_roll=function() {
	var roll=''; back='';
	
		if (this.roll_color!='') { 
			roll+=' this.style.backgroundColor=\''+this.roll_color+'\'; ';
			back+=' this.style.backgroundColor=\''+this.bg_color+'\'; ';
		}
		
		if (this.roll!='') { 
			roll+=' this.style.backgroundImage=\'url(\\\''+this.roll+'\\\')\'; ';
			back+=' this.style.backgroundImage=\'url(\\\''+this.bg_im+'\\\')\'; ';
		}
		
		
		if (this.roll_text_color!='') { 
			roll+=' this.style.color=\'url(\\\''+this.roll_text_color+'\\\')\'; ';
			back+=' this.style.color=\'url(\\\''+this.text_color+'\\\')\'; ';
		}
	
		return 'onmouseover="'+roll+'" onmouseout="'+back+'"';
	}
	
	this.est_vide= function() {
		return true;//a revoir
	}
	
	this.duplicate = function() {
		var t=new style();
		
		for (var i in this) {
			t[i]=this[i];
		}
		
		return t;
	}
}

//CLASSE DE CONFIGURATION
function config() {
	this.ouvert=false; //false - true
	this.deroulant=false; //false - true
	this.head_visible=true; //true - false ; affiche ou non l'entête quand ouvert
	
	this.ouverture=''; // '' - click - mouseover - ext_bouton
	this.forme='horizontale'; //horizontale - verticale 
	this.sens=''; // '' : default - droite - gauche -haut - bas
	
	this.limite=-1; //-1 : aucune;
	this.intercalaire=''; //cellule speciale de separation
	
	this.delay_ouverture=10; //durée du delai de sécurité pour les ouvertures onmouseover  (en dixieme de seconde)
	this.pas_deroulement=5;
	
	
	this.heritage_style=''; //'' - 'pere' - 'fils'; definit l'heritage du style 
	this.heritage_config=''; //'' - 'pere' - 'fils'; definit l'heritage de la config
	
	this.duplicate = function() {
		var t=new config();
		
		for (var i in this) {
			t[i]=this[i];
		}
		
		return t;
	}
}


function menu() {
	this.id=''; //numero de menu
	this.nom=''; //nom du menu
	this.intitule=''; //intitule de la case / de l'header
	this.lien=''; //lien / js 
	
	
	this.nbe=0;
	this.cases=new Array();
	
	this.style=new style(); //class css ; background/rollover; etc..
	this.child_style=new style(); //class css ; background/rollover; etc..
	
	this.config=new config(); //limites, orientation, etat d'origine
	this.child_config=new config(); //class css ; background/rollover; etc..
	
	this.father='';
	
	//TYPE DE L'OBJET (CASE ou MENU)
	this.est_case=function() {
		return this.nbe==0;
	}
	
	//TAILLE DE L'OBJET
	this.taille_case=function (type) {
		var taille=0;
		if (type=='width') { taille=px_to_int(this.style.width); }else{ taille=px_to_int(this.style.height); }
		if (taille==0) {taille=100;}
		
		if (txt_to_number(this.style.margin)) {
			taille+=2*txt_to_number(this.style.margin);
		}
		

		return taille;
	}
	
	this.taille_max=function (type) {
		var taille=0; var i=0; var t=new menu(); var tt=0;
			if (this.config.head_visible) {taille=this.taille_case(type);}
			
			while (i<this.nbe) {
				t=this.cases[i];
				if (t.est_case()) { tt=t.taille_case(type); }else{	tt=t.taille_max(type); 	}
				if (tt>taille) {taille=tt;}
				i++;
			}
		return taille;
	}
	
	this.taille_menu=function(type,ouvert) {
		//facultatif : on prend en compte tous les menus comme ouverts
		if(!ouvert){ouvert=false;}
		//variables internes
		var taille=0; var i=0; var t=new menu();
		//on regarde s'il faut cumuler les tailles ou non
		var cumul=(((type=='width')&&(this.config.forme=='horizontale')) || ((type=='height')&&(this.config.forme=='verticale')));
		
		if ((ouvert) || (this.config.ouvert)) {
			if (cumul) {
				if (this.config.head_visible) {taille=this.taille_case(type);}
				while (i<this.nbe) {
					t=this.cases[i];
					if (t.est_case()) { taille+=t.taille_case(type); } else { taille+=t.taille_menu(type,ouvert);	}
					i++;
				}
			}else{
				return this.taille_max(type);
			}
			
		}else{
			
			return this.taille_case(type);
		}
		
		
		
		return taille;
	}
	
	//PRESENCE DU MENU DANS LE DOM
	this.dans_DOM=function() {
		
		if (document.getElementById(this.id+'_cadre')) {return true;}else{return false;}
	}
	
	//AJOUTS / DEFINITION
	this.definir_case=function(id, nom, intitule, lien) {
		//definition de la case
		this.id=id; this.nom=nom; this.intitule=intitule; this.lien=lien;
	}
	
	this.heriter=function(menu) {
		//importation du style si demandé
		var he=this.config.heritage_style;
		switch (he) {
			case 'pere' :
				menu.style=this.style.duplicate();
			break;
			case 'fils' :
				menu.style=this.child_style.duplicate();
			break;
		}
		
		//importation de la config si demandé
		var he=this.config.heritage_config;
		switch (he) {
			case 'pere' :
				menu.config=this.config.duplicate();
			break;
			case 'fils' :
				menu.config=this.child_config.duplicate();
			break;
		}
		
	}
	
	this.ajouter_case=function(id, nom, intitule, lien) {
		//creation de la case
		var temp=new menu();
		
		//definition de la case
		temp.id=id; temp.nom=nom; temp.intitule=intitule; temp.lien=lien;
		temp.father=this.id;
		
		this.heriter(temp);
		
		//ajout de la case au menu
		this.cases[this.nbe]=temp; this.nbe++;  	
	}
	
	this.importer_case= function(obj,heritage) {
		//par default, l'heritage est inactif
		if (!heritage) {heritage=false;}
		
		//recuperation de la case (ou menu)
		var temp=new menu(); temp=obj; temp.father=this.id;
		
		if (heritage) { this.heriter(temp); }
		
		//ajout de l'objet au menu
		this.cases[this.nbe]=temp; 	this.nbe++;
	}
	
	//OBTENIR UNE CASE
	this.getcase= function() {
	var t_id=''; var t_class=''; var t_style=''; var t_roll=''; var t_href=''; var before=''; var after=''; var t_open=''; 
	var link_a=''; var link_b=''; var delay_report=''; var tfather=new menu(); var new_id=remplace_id(this.id);
	var mdeb='<div style="position:relative; text-align:center; margin:auto; background-color:blue;">'; var mfin='</div>';
	
		//identifiant DOM
		
		if (this.id!='')  { t_id='id="'+new_id+'" '; }
		
		//class css
		if (this.style.class_css!='') { t_class='class="'+this.style.class_css+'" '; }
		//proprieté de la case
		if (this.style.width!='') { t_style=t_style+' width:'+this.style.width+'; '; }
		if (this.style.height!='') { t_style=t_style+' height:'+this.style.height+'; '; }
		if (this.style.bg_im!='') { t_style=t_style+' background-image:url(\''+this.style.bg_im+'\'); '; }
		if (this.style.bg_color!='') { t_style=t_style+' background-color:'+this.style.bg_color+'; '; }
		if (this.style.text_color!='') { t_style=t_style+' color:'+this.style.text_color+'; '; }
		if (this.style.border_size!='') { t_style=t_style+' border-width:'+this.style.border_size+'; '; }
		if (this.style.margin!='') { t_style=t_style+' margin:'+this.style.margin+'; '; }
		if (this.style.css_perso!='') { t_style=t_style+' '+this.style.css_perso+' '; }
		if (t_style!='') {t_style=' style="'+t_style+' " '; }
		//rollover
		if (this.style.get_roll()!='') { t_roll=this.style.get_roll(); }
		//linkage/ouverture
		t_open=' id="'+new_id+'_openner" class="menu_openner"';
		//report de l'affichage onmouseover
		if (this.father!='') {
			//tfather=objet_menu(this.father,menus);
			//if (tfather.config.ouverture=='mouseover') {	
			//delay_report=' onmouseover="if (menu_delay[\''+tfather.id+'\']>0) { menu_delay[\''+tfather.id+'\']='+tfather.config.delay_ouverture+';}" ';	
			delay_report=' onmouseover="delay_report(\''+this.id+'\');" ';
			before='<div '+delay_report+'>'; after='</div>';
			//}
		}
		
		if (this.est_case()) {
			t_href=' href="'+this.lien+'" ';
			link_a='<a '+t_open+t_href+' >'; link_b='</a>';
		}else{ 
			switch (this.config.ouverture) {
				case 'mouseover' :
				if (this.config.deroulant) {var delay_ouverture=this.config.delay_ouverture+temps_deroulement(this.id);}
				else{var delay_ouverture=this.config.delay_ouverture;}
				t_href=' onmouseover="delayed_ouverture(\''+new_id+'\','+delay_ouverture+');" ';
				break;
				
				case 'over_click' :
				if (this.config.deroulant) {var delay_ouverture=this.config.delay_ouverture+temps_deroulement(this.id);}
				else{var delay_ouverture=this.config.delay_ouverture;}
				t_href=' onmouseover="clicked_ouverture(\''+new_id+'\');" ';
				break;
				
				case 'click_click' :
				if (this.config.deroulant) {var delay_ouverture=this.config.delay_ouverture+temps_deroulement(this.id);}
				else{var delay_ouverture=this.config.delay_ouverture;}
				t_href=' onclick="clicked_ouverture(\''+new_id+'\');" ';
				break;
				
				case 'mouseenter' :
				if (this.config.deroulant) {var delay_ouverture=this.config.delay_ouverture+temps_deroulement(this.id);}
				else{var delay_ouverture=this.config.delay_ouverture;}
				t_href=' onmouseenter="ouverture(\''+new_id+'\');" onmouseleave="ouverture(\''+new_id+'\');" ';
				break;
				
				
				default:
				t_href=' onclick="ouverture(\''+new_id+'\');" '; 
				break;
			}
			
			link_a='<div '+t_open+t_href+' >'; link_b='</div>';
		}
		
		
		
		if (this.config.forme!='liste') {
			return '<div  class="menu_class">'+before+link_a+'<div '+t_id+t_class+t_style+t_roll+' >'+this.style.pref+this.intitule+'</div>'+link_b+after+'</div>';
		}else{
			return '<div class="menu_class"><a '+t_href+t_id+t_class+t_style+t_roll+' >'+this.style.pref+this.intitule+'</a></div>';
		}
	}
	
	this.returncase= function(id) {
		var i=0; var temp=new menu(); var trouve=false;
		while ((i<this.nbe) && (trouve==false)) {
			temp=this.cases[i];
			if (temp.id==id) {trouve=true;}
			i++;
		}
		
		if (trouve) {return temp;}else{return null;}
	}

	//AFFICHER UNE CASE
	this.affiche_case=function() {
		document.writeln(this.getcase());
	}
	
	//OBTENIR LE MENU
	this.getmenu=function() {
		var id_div=!this.dans_DOM(); var i=0; var res=''; var resint=''; var int_before=false; var mini_res=''; 
		var delay_report=''; var setforme='';	var hide_style='';	var borne_a=''; var borne_b=''; var lborne_a=''; var lborne_b='';  
		var hborne_a=''; var hborne_b=''; var temp=new menu(); var new_id=remplace_id(this.id);
		
		//menu deroulant fermé
		if ((this.config.ouvert==false)&&(this.config.deroulant==true)) {
			var min_size=0; 
			//si head_visible
			if (this.config.head_visible) { 
				if (this.config.forme=="horizontale") { var type="width"}else{ var type="height";}
				min_size=this.taille_case(type);
			}
			
			hide_style=' style="'+type+':'+min_size+'px; overflow:hidden;" ';
		}
		
		//structure
		/*if (this.config.ouverture=='over_click') {
		if (this.config.deroulant) {var delay_ouverture=this.config.delay_ouverture+temps_deroulement(this.id);}
		else{var delay_ouverture=this.config.delay_ouverture;}
		var t_op=' onmouseover="ouverture(\''+new_id+'\');" onmouseout="ouverture(\''+new_id+'\');" ';
		}else{var t_op='';}*/
		
		if (id_div) {res+='<div   id="'+new_id+'_cadre" class="menu_cadre" '+hide_style+'  >'; is_div=false;}
				
		//gestion de l'apparition
		
			//si le menu est de type case
			if ((this.est_case()) || ((this.config.ouvert==false)&&(this.config.deroulant==false))) {
				res+=this.getcase();
				
			//si c'est un menu
			}else{
				
				
				
				res+='<table class="menu_class" id="'+new_id+'_table" cellspacing="0" cellpadding="0">';
				
				//code de structuration interne, définit en fonction de la forme et du sens du menu
					
					if (this.config.forme!='') {
						setforme=this.config.forme;
					}else{
						if ((this.config.sens=='droite') || (this.config.sens=='gauche')) {setforme='horizontale';}
						if ((this.config.sens=='haut') || (this.config.sens=='bas')) {setforme='verticale';}
					}
					
					switch (setforme) {
						case 'liste' :
						lborne_a='<br />';  lborne_b='';//<br /> initial
						borne_a=''; borne_b='<br />'; //pour chaque case... un retour à la ligne
						hborne_a='<span class="head_menu">'; hborne_b='</span><br />';
						if (this.config.sens=='haut') {int_before=true;}	//si on va contre courant
						break;
						
						case 'verticale' :
						lborne_a='';  lborne_b='';//il n'y aura un tr à chaque case
						borne_a='<tr><td '+delay_report+'>'; borne_b='</td></tr>'; //pour chaque case... un td
						hborne_a='<tr class="head_menu" ><td '+delay_report+'>'; hborne_b='</td></tr>';
						if (this.config.sens=='haut') {int_before=true;}	//si on va contre courant
						break;
						
						default ://horizontale
						lborne_a='<tr>';  lborne_b='</tr>';//il n'y aura qu'une seule ligne tr en tout
						borne_a='<td style="vertical-align:top;" '+delay_report+'>'; borne_b='</td>'; //pour chaque case... un td
						hborne_a='<td  class="head_menu" style="vertical-align:top;" '+delay_report+'>'; hborne_b='</td>'; //pour chaque case... un td
						if (this.config.sens=='gauche') {int_before=true;}	//si on va contre courant
						break;
					}
				
				//gestion de l'entete
				if (this.config.head_visible) { resint+=hborne_a+this.getcase()+hborne_b; }
				
				//generation du tableau
				while (i<this.nbe) {
					temp=this.cases[i];
					mini_res=borne_a+temp.getmenu()+borne_b;
					if (int_before) { resint=mini_res+resint; }else{ resint=resint+mini_res;}
					if ((this.config.intercalaire!='') && ((i+1)<this.nbe)) {resint+=borne_a+this.config.intercalaire+borne_b;}
					i++;
				}
				
				res+=lborne_a+resint+lborne_b;
				
				res+='</table>';
							
			}
			
		if (id_div) {res+='</div>';}

		return res;
	}
	
	//--ACTIONS--
	
	//AFFICHER LE MENU
	this.afficher=function() {
		document.writeln(this.getmenu());
	}
	
	//OUVRIR LE MENU
	
	
		
	//ouverture
	this.ouvrir=function() {
		var limite=0;
		
			if (this.config.deroulant) {
				if (this.config.forme=="horizontale") {var type="width";}else{var type="height";}
				
				var limite=this.taille_menu(type,true);
				
				if (this.config.head_visible) { limite=limite-this.taille_case(type); }
				
				//alert('o'+limite);
				derouler(this.id,limite,type,this.config.pas_deroulement);
			}else{

				document.getElementById(this.id+'_cadre').innerHTML=this.getmenu();
			}
	}
	
	//fermeture
	this.fermer=function() {
	
			if (this.config.deroulant) {
				if (this.config.forme=="horizontale") {var type="width";}else{var type="height";}
				var limite=this.taille_menu(type,true);
				
				if (this.config.head_visible) { limite=limite-this.taille_case(type); }
				
				//alert('f'+limite);
				derouler(this.id,(-1*limite),type,this.config.pas_deroulement);
			}else{

				document.getElementById(this.id+'_cadre').innerHTML=this.getmenu();
			}
	}
	
	this.change_etat=function() {		
			if (this.config.ouvert==true) {this.config.ouvert=false;}else{this.config.ouvert=true;}
			if (this.config.ouvert) {this.ouvrir();}else{this.fermer();}
	}
}


//------------------- REGISTRE DE MENUS -------------------//
//Menus
var menus;
menus=new Array(); 

//Styles
var  menu_style;
menu_style=new Array();

//Configs
var menu_config;
menu_config=new Array();

var menu_delay;
menu_delay=new Array();

function ajouter_menu(num,id,tab) {//on donne une id au menu (pour feuille de style), on associe aussi un numéro qui correspondra à la position du menu dans le tableau [menus]
	if (!tab){tab=menus;}
	var t=new menu();
	tab[num]=t;
	tab[num].id=id;
}

function trouve_menu(id,tab) {
	var i=0; var t=new menu();  var stop=false;
	
	while ((i < tab.length) && (stop==false)) {
		t=menus[i];
		if (t.id==id) { stop=true; }
		else{ if (!t.est_case()) { stop=trouve_menu(id,t.cases); } }
		i++;
	}
	
	return stop;
	
}

function objet_menu(id,tab) {
	
	var i=0; var o=0; var t=new menu(); var tp=new menu(); var stop=false;
	var res=new menu(); 
	
	while ((i < tab.length) && (stop==false)) {
		if (tab[i]) {
			 t=tab[i];
			 if (t.id==id) { stop=true; res=t;}
			 else{ 
				//si c'est un menu, on fait la recherche dans ses sous-menus
				if (!t.est_case()) { 
					tp=t.cases;
					res=objet_menu(id,tp);
					if (res.id!='') {stop=true;}
				} 
			 }
		}
		i++;
	}
	

	return res;
}

/* 
function derouler(id,decompte,type,pas) {
	alert(id+' - '+decompte+' - '+type+' - '+pas); 
	alert(decompte+51); alert(pas+51);
	alert(objet_menu(id,menus).config.pas_deroulement);
}
*/


function derouler(id,decompte,type,pas) {
	//alert(id+' - '+decompte+' - '+type+' - '+pas); 
	var new_size=0; //alert(document.getElementById(id+'_cadre').style.height);

	//par defaut
	if (!pas) { pas=5; }
	
	if (decompte != 0) {
		if (decompte < 0) {
		
			if (decompte+pas >0) { pas=-1*decompte; decompte=0;}else{decompte=decompte+pas;}
			
			if (type=='width') {
				new_size=px_to_int(document.getElementById(id+'_cadre').style.width)-pas;
				document.getElementById(id+'_cadre').style.width=new_size+'px';
			}else{
				new_size=px_to_int(document.getElementById(id+'_cadre').style.height)-pas;
				document.getElementById(id+'_cadre').style.height=new_size+'px';
			}
			
				//alert(new_size);
		}else{
			
			if (decompte-pas <0) { pas=decompte; decompte=0;}else{decompte=decompte-pas;}
			
			if (type=='width') {
				new_size=px_to_int(document.getElementById(id+'_cadre').style.width)+pas;
				document.getElementById(id+'_cadre').style.width=new_size+'px';
			}else{
				new_size=px_to_int(document.getElementById(id+'_cadre').style.height)+pas;
				//alert('new :'+(new_size-3));
				document.getElementById(id+'_cadre').style.height=new_size+'px';
			}
				
				//alert('2_'+new_size);
			
		}
		
		
		setTimeout("derouler('"+id+"',"+decompte+",'"+type+"',"+pas+");",50);
	}
}


function temps_deroulement(id) {
	//detection du menu
	var t=new menu();	t=objet_menu(id,menus); var i=0;

	if (t.config.forme=="horizontale") {var type="width";}else{var type="height";}
				
	var limite=t.taille_menu(type,true);
				
	if (t.config.head_visible) { limite=limite-t.taille_case(type); }
	
	i=Math.ceil(limite/t.config.pas_deroulement/2);
	
	return i;
}

function delay_report(id) {
	var t=new menu(); t=objet_menu(id,menus);
	
	while (t.father!='') {
		
		t=objet_menu(t.father,menus);
		
	
		
		if (menu_delay[t.id]>0) { 
		
		if (t.config.ouverture=='mouseover') {
		menu_delay[t.id]=t.config.delay_ouverture; 
		}
		
		}//alert(t.id+' delay ok :'+menu_delay[t.id]+'='+t.config.delay_ouverture); 
		
		
		
	}
}

function clicked_fermeture(id) {
		setTimeout("ouverture('"+id+"');",400);
		souris['gauche']='';
}

function clicked_ouverture(id) {
		
		if (!objet_menu(id,menus).config.ouvert) {
			eval(souris['gauche']);
			ouverture(id);
			souris['gauche']='clicked_fermeture(\''+id+'\');';
		}
}

function delayed_fermeture(id) {
	if (menu_delay[id]>0) {
		menu_delay[id]=menu_delay[id]-1;
		setTimeout("delayed_fermeture('"+id+"');",100);
		
	}else{
		ouverture(id);
	}
}

function delayed_ouverture(id,delay) {
	//creation du delay si inexistant
	if (!menu_delay[id]) {menu_delay[id]=0;}
	
	
	if (menu_delay[id]<=0) {
		menu_delay[id]=delay;
		ouverture(id);
		delayed_fermeture(id);
	}else{
		menu_delay[id]=delay;
	}
}

function ouverture(id,tab) {
if (!tab) {tab=menus;}
var t=new menu();
t=objet_menu(id,tab);
	if (t.id!='') {
		t.change_etat();
		if (!t.config.ouvert){menu_delay[id]=0;}
	}
}
