var common_top_menu_id = 'menu_top_';

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

var common_top_menu_activator_id = 'menu_top_cat_';

var top_top_menu = 0;
var top_menu_col_width = 170;

var incremented_top_menu = false;

var left_top_menu = 225;

var array_asked_hide = new Array();

var latest_id = false;

var colors_1 = new Array('','#FAD3DC', '#D0E4F4', '#D1EFE0', '#FFDBA4', '#DACFE9', '#C5E9FC');
var colors_2 = new Array('','#FDF2F4', '#ECF4FB', '#E9F8F0', '#FEF2D8', '#F2EEF7', '#EBF9FE');

var is_okay_right_arrow = true;
var is_okay_left_arrow = false;

var color_arrow = '#FFC060';


function showTopMenu( id , is_arrow) {

	

	var must_show = false;
	
		top_top_menu = 0;
		left_top_menu = 225;
	
	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 , is_arrow);
					
				}
				
			}
			
		}
		
		latest_id = id;
		
		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 += 40;
					
				} else {
					top_top_menu +=  42;
				}
	
			} 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';
			
			
			
			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' ) {
				//if ( array_li[i].className == 'main_option_' +id +'_1' || array_li[i].className == 'main_option_' +id +'_2') {	
					array_li[i].style.width = top_menu_col_width + 'px';
					cnt_root_elements++;
					if(id == '4') {
						array_li[i].style.width = 170 + 'px';
					}
				}
							
			}
			
			/* 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();
			}
			
			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";
					} 
				}
				
			}
			
			if(id == '4') {
				document.getElementById( common_top_menu_id + id ) . style . width = (top_menu_col_width * cnt_root_elements) + ( 30 * cnt_root_elements ) + 'px';
			} else {
				document.getElementById( common_top_menu_id + id ) . style . width = (top_menu_col_width * cnt_root_elements) + ( 90 * cnt_root_elements ) +  'px';			
			}
			document.getElementById( common_top_menu_id + id ).style.display = 'inline';

			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'){
						if (id == 4)
							document.getElementById( common_top_menu_id + id ).childNodes[j].style.height = '552px';
						else
							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)? colors_1[id] : colors_2[id];
						else  document.getElementById( common_top_menu_id + id ).childNodes[j].style.backgroundColor = (j%2==0)? colors_1[id] : colors_2[id];
					}
				
				}
				tab_exec_hauteur[id] = true;
				
			}
		}

	
	}

}

function hideTopMenu( id , is_arrow) {
	
	array_asked_hide[id] = window.setTimeout("doHideTopMenu('" + id + "', '" + is_arrow+ "')",250);
	
}

function doHideTopMenu( id, is_arrow ) {

// pour gerer l'affichage du defilement pour la boutique tous les heros
	if (id == '4' && is_arrow == '1') {
		top_top_menu = 0;

		document.getElementById( 'div_'+common_top_menu_id + id ).style.display = 'none';
		document.getElementById( 'left_arrow' ).style.display = 'none';
		document.getElementById( 'right_arrow' ).style.display = 'none';
		document.getElementById( 'left_arrow' ).style.background  =  color_arrow + ' 0px center';
		document.getElementById( 'right_arrow' ).style.background = 'url(../img/menu_suivant.png) no-repeat ' + color_arrow + ' 0px center';
		document.getElementById( 'right_arrow' ).onload = function() {
			fixElement('right_arrow');
		}
		is_okay_right_arrow = true;
		is_okay_left_arrow = false;

	}
	else if (id == '4') {
		document.getElementById( 'div_'+common_top_menu_id + id ).style.display = 'none';
	}
	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';
		//document.getElementById(common_top_menu_activator_id + id + '_txt' ).style.color="#FFFFFF";
		
	}

}

function doMoveDiv( id, sens) {

	var position = document.getElementById( common_top_menu_id + id ).offsetLeft
	var pos_min = top_menu_col_width * 4;
	var pos_max = - (top_menu_col_width * 8);
	
	if (sens == 'right')
		position -= top_menu_col_width * 4;
	else
		position += top_menu_col_width * 4 ;

	if (position == 0) {
		document.getElementById( 'left_arrow' ).style.background = 'url("") no-repeat ' + color_arrow ;
		document.getElementById( 'right_arrow' ).style.background = 'url(../img/menu_suivant.png) no-repeat '+ color_arrow + ' 0px center';
		document.getElementById( 'right_arrow' ).onload = function() {
			fixElement('right_arrow');
		}
		is_okay_right_arrow = true;
		is_okay_left_arrow = false;
	}
	if (position == -pos_min) {
		document.getElementById( 'left_arrow' ).style.background = 'url(../img/menu_precedent.png) no-repeat ' + color_arrow +' 0px center';
		
		document.getElementById( 'right_arrow' ).style.background = 'url("") no-repeat ' +color_arrow ;
		document.getElementById( 'right_arrow' ).onload = function() {
			fixElement('right_arrow');
		}
		
		is_okay_left_arrow = true;
		is_okay_right_arrow = false;
	}
	
	if (position >= pos_min) 
		position = 0;
	if (position <= pos_max)
		position = -pos_min;
	
	document.getElementById( common_top_menu_id + id ) . style . left = position + 'px';
}

function changeColor(flag, sens) {
	var url_img;

	if (is_okay_right_arrow == true)
	{
			url_img = 'url(../img/menu_suivant.png)';
		
		if (flag == 0 && sens == 'right') {
			document.getElementById( 'right_arrow' ).style.background = url_img + ' no-repeat #FF9900  0px center';
			document.getElementById( 'right_arrow' ).onload = function() {
				fixElement('right_arrow');
			}
		}
		else if (flag == 1 && sens == 'right') {
			document.getElementById( 'right_arrow' ).style.background = url_img + ' no-repeat ' + color_arrow + ' 0px center';
			document.getElementById( 'right_arrow' ).onload = function() {
				fixElement('right_arrow');
			}
		}
	}
	if (is_okay_left_arrow == true) {
			url_img = 'url(../img/menu_precedent.png)';

		if (flag == 0 && sens == 'left') {
			document.getElementById( 'left_arrow' ).style.background = url_img + ' no-repeat #FF9900  0px center';
			document.getElementById( 'left_arrow' ).onload = function() {
				fixElement('left_arrow');
			}
		}
		else if (flag == 1 && sens == 'left') {
			document.getElementById( 'left_arrow' ).style.background = url_img + ' no-repeat ' + color_arrow + ' 0px center';
			document.getElementById( 'left_arrow' ).onload = function() {
				fixElement('left_arrow');
			}
		}
	}
}

