var common_top_menu_id = 'menu_top_';

var common_top_menu_activator_id = 'menu_top_cat_';
var tab_exec_hauteur = {47:false,48:false,49:false,50:false,51:false};

var top_top_menu = 32;
var top_menu_col_width = 165;

var incremented_top_menu = false;

var left_top_menu = 0;

var array_asked_hide = new Array();

var latest_id = false;

function showTopMenu( id , stay_display ) {
	
	var must_show = false;
	
	if ( (typeof array_asked_hide[id]) == "undefined") {
	
		must_show = true;
		
	} else  {
		
		if ( array_asked_hide[id] == false ) {
			
			must_show = true;
			
			
		} else {
			
			 window.clearTimeout(array_asked_hide[id]);
			 array_asked_hide[id] = false;
			
		}
		
		
	}
	
	if ( must_show ) {
		
		if ( latest_id != false ) {
			
			if ( (typeof array_asked_hide[latest_id]) != "undefined" ) {
				
				if (array_asked_hide[latest_id] != false) {
				
					window.clearTimeout(array_asked_hide[latest_id]);
					array_asked_hide[latest_id] = false;
					doHideTopMenu( latest_id, latest_display );
					
				}
				
			}
			
		}
		
		latest_id = id;
		latest_display = stay_display;
		
		if ( document.getElementById( common_top_menu_id + id ) ) {
	
			if ( navigator.userAgent.indexOf('MSIE') != -1 && incremented_top_menu == false ) {
	
				incremented_top_menu = true;
				
				if ( navigator.userAgent.indexOf('6.') != -1 ) {
					
					top_top_menu += 0;
					
				} else {
					top_top_menu +=  0;
				}
	
			} else if ( navigator.userAgent.indexOf('Opera') != -1 && incremented_top_menu == false ) {
	
				incremented_top_menu = true;
				top_top_menu += 16;
	
			}
			
			document.getElementById( common_top_menu_id + id ) . style . left = left_top_menu + 'px';
			document.getElementById( common_top_menu_id + id ) . style . top = top_top_menu + 'px';
			document.getElementById( common_top_menu_id + id ) . style . padding = 0 + 'px';
			/*if ( navigator.userAgent.indexOf('Firefox') != -1){
				document.getElementById( common_top_menu_id + id ) . style . marginTop = '-10px';
			}*/
			
			var array_li = document.getElementById(common_top_menu_id + id).getElementsByTagName('li');
			var cnt_li = array_li.length;
			
			var cnt_root_elements = 0;
			
			for (i = 0; i < cnt_li; i++) {
			
				if ( array_li[i].className == 'main_option' ) {
					
					array_li[i].style.width = top_menu_col_width + 'px';
					cnt_root_elements++;
					
				}
				
			}
			
			/* pour gerer le positionnement du menu quand il deborde a droite */
			var elem_width = (top_menu_col_width * cnt_root_elements + (cnt_root_elements*15));
			var elem_left = document.getElementById( common_top_menu_id + id ).parentNode.offsetLeft;
			
			var bloc_parent = document.getElementById( common_top_menu_id + id ).parentNode.parentNode;
			var x = 0;
			while (bloc_parent != null)
			{
			 x += bloc_parent.offsetLeft;
			 bloc_parent = bloc_parent.offsetParent;
			}
			
			var limit_bloc_parent = x + document.getElementById( common_top_menu_id + id ).parentNode.parentNode.offsetWidth;
			
			if((elem_left + elem_width) > limit_bloc_parent) document.getElementById( common_top_menu_id + id ) . style . left = '-'+(elem_width - document.getElementById( common_top_menu_activator_id + id) . offsetWidth) + 'px';
			
			//on abandonne le focus pour les select qui en avait
			for(var i=0;i<document.getElementsByTagName("SELECT").length;i++)
			{
				document.getElementsByTagName("SELECT")[i].blur();
			}
			
			//on abandonne également le focus de la recherche
			if(document.getElementById('completeDiv')){
				document.getElementById('completeDiv').innerHTML='';
				document.getElementById('completeDiv').style.visibility = 'hidden';
				if(document.getElementById('keyword').value!='' && document.getElementById('keyword').value!='Recherche') document.getElementById('keyword').value = '';
			}
			
			if ( navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('6.') != -1 ) {
			
				var svn=document.getElementsByTagName("SELECT");
				for (a=0;a<svn.length;a++){
					if (svn[a].id.indexOf(common_top_menu_id + id)>=0){
					svn[a].style.visibility="visible";
					} else {
					svn[a].style.visibility="hidden";
					} 
				}
				
			}
			
			document.getElementById( common_top_menu_id + id ) . style . width = (top_menu_col_width * cnt_root_elements + (cnt_root_elements*15)) +  'px';
			document.getElementById( common_top_menu_id + id ).style.display = 'inline';
			
			//attibution des couleurs de fond
			if(!tab_exec_hauteur[id]){
			
				var noeuds = document.getElementById( common_top_menu_id + id ).firstChild;
				var max_height = 0;
				
				while (noeuds!=null) {
					if((typeof noeuds) != "undefined"){
						if(noeuds.offsetHeight>max_height) max_height = noeuds.offsetHeight;
						noeuds = noeuds.nextSibling;
					}
				}
				
				var nb_li = document.getElementById( common_top_menu_id + id ).childNodes.length;
				
				for(var j=0 ; j<nb_li ; j++){
					
					if(document.getElementById( common_top_menu_id + id ).childNodes[j].className == 'main_option'){
						document.getElementById( common_top_menu_id + id ).childNodes[j].style.height = max_height+'px';
						if(navigator.userAgent.indexOf('MSIE') != -1) document.getElementById( common_top_menu_id + id ).childNodes[j].style.backgroundColor = (j%2==1)? '#F3EAE2' : '#FCF9F5';
						else  document.getElementById( common_top_menu_id + id ).childNodes[j].style.backgroundColor = (j%2==0)? '#F3EAE2' : '#FCF9F5';
					}
				
				}
				tab_exec_hauteur[id] = true;
				
			}
		}
	
	}

}

function hideTopMenu( id , stay_display ) {
	
	array_asked_hide[id] = window.setTimeout("doHideTopMenu('" + id + "',"+stay_display+")",250);
	
	if ( navigator.userAgent.indexOf('MSIE') != -1 ) var chaine = document.getElementById(common_top_menu_activator_id + id).firstChild.src;
	else var chaine = document.getElementById(common_top_menu_activator_id + id).childNodes[1].src;
		
	if ( navigator.userAgent.indexOf('MSIE') != -1 ) document.getElementById(common_top_menu_activator_id + id).firstChild.src = chaine.replace(/off.gif/,"on.gif");
	else document.getElementById(common_top_menu_activator_id + id).childNodes[1].src = chaine.replace(/off.gif/,"on.gif");
	
}

function doHideTopMenu( id , stay_display ) {
	
	if (array_asked_hide[id] != false ) {
	
		window.clearTimeout(array_asked_hide[id]);
		array_asked_hide[id] = false;
		
	}
	if ( document.getElementById( common_top_menu_id + id ) ) {
		
		if ( navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('6.') != -1 ) {
			
			var svn=document.getElementsByTagName("SELECT");
			
			for (a=0;a<svn.length;a++){
				if (svn[a].id.indexOf(common_top_menu_id + id)>=0){
				
					svn[a].style.visibility="hidden";
					
				} else {
					
					svn[a].style.visibility="visible";
					
				}
				
			}
			
		}
		document.getElementById( common_top_menu_id + id ).style.display = 'none';
		
	}
	
	if(stay_display==0){
	
		if ( navigator.userAgent.indexOf('MSIE') != -1 ) var chaine = document.getElementById(common_top_menu_activator_id + id).firstChild.src;
		else var chaine = document.getElementById(common_top_menu_activator_id + id).childNodes[1].src;
		
		if ( navigator.userAgent.indexOf('MSIE') != -1 ) document.getElementById(common_top_menu_activator_id + id).firstChild.src = chaine.replace(/on.gif/,"off.gif");
		else document.getElementById(common_top_menu_activator_id + id).childNodes[1].src = chaine.replace(/on.gif/,"off.gif");
	
	}

}