var MenuPos = 0;
var menuMatrix = new Array();
var menuHeight=400;
var menuWidth=975;
var enableMenuFx = true;
var enableTreeFx= false;
var enableTreeFx2= false;
var myMenuHeight = new Array();
var MenuPos2 = 0;
var firstCheck=false;

function getMenuHeight()
{

	var t;
	var xid = 'zoneNavigationGlobaleHaut1';
	var hx = document.getElementById(xid);
	var h;
	var sib;
	if (hx)
	{

		h = document.getElementById(xid).clientHeight;
		//try {
		//sib = document.getElementById(xid).siblings();
		sib = $(xid).siblings();;

		myMenuHeight[0] = h;

		document.getElementById(xid).style.position='relative';
		document.getElementById(xid).style.display='none';

		for (x=0;x<sib.length;x++)
		{
			t = sib[x].clientHeight;
			myMenuHeight[x+1] = t;
			if (t > menuHeight)
				menuHeight = t;

			sib[x].style.position='relative';
			sib[x].style.display='none';
		}	

		if (oldIE)
		{
			menuHeight=menuHeight;
		}
		else
			menuHeight=menuHeight-25;


		//} catch(e) {}

	}
	//alert(menuHeight);

	//wd(myMenuHeight);

}

function highlight(etat)
{

	var etat;

	//wd('<li>Warning : '+MenuPos);
	if (MenuPos2 && !MenuPos)
	{
		var sib = $$('img.btclass'+MenuPos2);
		//alert(sib);
		if (sib)
		{
			//wd('<li>current bloc is '+sib[0].id);

			if (etat)
				sib[0].src = sib[0].src.replace('_on','_off');
			else
				sib[0].src = sib[0].src.replace('_off','_on');

			//wd('<li> setting '.MenuPos2);
		}
	}


}
function wd(m)
{

	$('debug').innerHTML+=m;

}


function findHigh()
{
	//wd('<li> checking');
	var sib = $$('img.btclass');
	for (var x=0; x<sib.length; x++)
	{
		if (sib[x].src.match('_on'))
		{
			MenuPos2 = x+1;		
			//wd('Hilighted is : '+MenuPos2);
		}

	}
	firstCheck = true;
}

function rollImage(e,t)
{

	var w;
	var e;



	if (!MenuPos2)
	{
		if (!firstCheck)
			findHigh();
	}
	//alert(MenuPos2);

	//wd('<li>Current position  '+MenuPos2);

	w = e.className;

	if (!menuMatrix[0])
	{
		var nbMenu = $$('img.btclass');
		for (x=1;x<=nbMenu.length;x++)
		{
			menuMatrix[x]=0;
		}
		menuMatrix[0]=true;
	}

	var w;
	var id;
	var t;


	id = w.replace('btclass btclass','');
	//alert(id);

	//alert(enableTreeFx2);	

	if (id)
	{

		var bt = $$('img.btclass'+id);


		if (t) 
		{
			if (!menuMatrix[id])
			{
				bt[0].src = bt[0].src.replace('_on','_off');
				//fadeIt(e);
				if (enableTreeFx2)
				{
					effect = new Fx.Styles(bt[0],{wait:true });
					effect.custom({
							'opacity': [0.0,1]
							});
				}
			}
			if (MenuPos)
			{
				bt = $$('img.btclass'+MenuPos);
				bt[0].src = bt[0].src.replace('_off','_on');
				if (enableTreeFx)
				{
					//alert('on');
					effect = new Fx.Styles(bt[0],{wait:true });
					effect.custom({
							'opacity': [0.0,1]
							});
				}
			}
		}
		else
		{
			if (bt[0].src)
			{
				if (bt[0].src.match('_on'))
				{
					if (!menuMatrix[id])
						bt[0].src = bt[0].src.replace('_on','_off');
					//fadeIt(e);
					if (enableTreeFx2)
					{
						effect.custom({
								'opacity': [1,0.0]
								});
					}
				}
				else
				{
					bt[0].src = bt[0].src.replace('_off','_on');
					//fadeIt(e,true);
					if (enableTreeFx)
					{
						effect = new Fx.Styles(bt[0],{wait:true });
						effect.custom({
								'opacity': [0.0,1]
								});
					}
				}
				var sib = $$('img.btclass');
				for (var x=0; x<sib.length; x++)
				{
					if (sib[x].className != 'btclass btclass'+id)
					{
						sib[x].src = sib[x].src.replace('_on','_off');
					}
				}


				highlight();
			}
		}
	}
}


function showMenu(id,c)
{

	var id;
	var c;

	/*
	   var bt = $$('img.btclass'+id);
	   if (bt[0])
	   {
	   alert(bt[0].src);
	   }
	 */

	//alert(id);

	//wd('<li> Menu '+MenuPos+'@'+c+' for '+menuMatrix);

	xid = 'zoneNavigationGlobaleHaut'+id;
	xid2 = 'zoneNavigationGlobaleHaut'+MenuPos;


	//var myElements = getElementsByClassName(xid);

	var parent = document.getElementById('zoneNavigationGlobaleHaut');
	var h = $$('div.'+xid);

	var h2;

	if (MenuPos)
		h2  = $$('div.'+xid2);


	//h[0].style.visibility = 'visible';

	/*
	   for(var i=0;i<myElements.length;i++)
	   {
	   myElements[i].style.display = 'block';
	   myElements[i].style.visibility = 'visible';
	   document.images.nav_les_dossiers.src = '/president/root/bank/images_elysee_interface/zoneNavigationHorizontale/les_dossiers_on.gif';
	   }
	 */

	//alert(h[0].id);

	var sib = h[0].siblings();

	//wd('<li>Open::Start layer '+id+' Height : '+parent.style.visibility);


	//wd('<hr><li>Current value is MenuPos is <b>'+MenuPos+'</b> and clicked is <b>'+id+'</b> (c is '+c+')  ['+menuMatrix+']');
	//alert(MenuPos);

	/* pas selectionne */
	if ((!menuMatrix[id]) && (!c) )
	{


		//wd('<li> '+id+' This item is not selected');

		/* pas de menu ouvert */
		if (!MenuPos)
		{

			//wd('Menu Pos to zero : no menu');

			h[0].style.opacity='0';
			parent.style.opacity='0';
			//parent.style.visibility='hidden';		
			//parent.style.opacity='0';

			//alert('t0');
			highlight(true);

			if (MenuPos2)
			{


			}

			if (enableMenuFx)
			{


				//wd('<li> opening '+id+' @ '+ myMenuHeight[id-1]);
				if (ie)
				{
					parent.style.visibility= 'hidden';
				}

				effect = new Fx.Styles(parent, {duration:800, wait: true, onComplete:function() 
						{
						h[0].style.opacity='1';
						h[0].style.display='block';
						h[0].style.visibility='visible';
						new Fx.Styles(parent,{duration:500, wait:true}).custom({'opacity': [0,1]});
						}
						});
				effect.custom({
						'height': [0, myMenuHeight[id-1]]
						});


			}
			else
			{

				h[0].style.display = 'block';
				h[0].style.visibility = 'visible';
				h[0].style.opacity = '1';
				parent.style.display = 'block';
				parent.style.visibility = 'visible';
				parent.style.opacity = '1';




			}


			//parent.style.display = 'block';
			//parent.style.visibility = 'visible';
			//parent.style.opacity = '1';



		}

		/* deja selectionne */
		else
		{
			//alert('already');

			//wd('<li> already opened : '+id+'@'+myMenuHeight[id-1]);

			//wd('<li>Matrix in '+menuMatrix);

			if (enableMenuFx)
			{

				//alert('switch pane \n'+h[0]);

				//alert(h[0].id);
				//wd('<li> resize from '+MenuPos+' to '+id+' @ '+h[0].id);
				wd('<li> morphing from '+myMenuHeight[MenuPos-1]+' to '+myMenuHeight[id-1]);


				/*
				   new Fx.Styles(h2[0],{ duration: 600, wait: true, onComplete:function()
				   {
				   new Fx.Styles(parent,{ duration: 5000, wait: true, onComplete:function()
				   {
				   h[0].style.opacity='0';
				   h[0].style.display='block';
				//h[0].style.visibility='visible'; // beware IE 
				//wd('<li> Sibling : ');
				for (var x=0; x<sib.length; x++)
				{
				tid = sib[x].id.replace('zoneNavigationGlobaleHaut','');
				//wd(tid+', ');
				sib[x].style.display='none';
				sib[x].style.visibility='hidden';
				menuMatrix[tid] = 0;
				//MenuPos=0;
				}
				//wd('<li>Matrix out '+menuMatrix);
				//new Fx.Styles(h[0],{duration: 600, wait: true, onComplete:function(){removeFilter(h[0])} }).custom( {'opacity':[0,1]} );

				}

				}).custom({
				'height': [ myMenuHeight[MenuPos-1], myMenuHeight[id-1] ],
				'opacity': [1,0]
				});

				}

				}
				).custom({'opacity': [1,0]});

				 */


				if (ie)
				h2[0].style.visibility='hidden';

				//new Fx.Styles(h2[0],{ duration: 500, wait: true, onComplete:function(){

				new Fx.Styles(parent,{ duration: 500, wait: true, onComplete:function(){

						h[0].style.opacity='0';
						h[0].style.display='block';
						for (var x=0; x<sib.length; x++)
						{
						tid = sib[x].id.replace('zoneNavigationGlobaleHaut','');
						//wd(tid+', ');
						sib[x].style.display='none';
						sib[x].style.visibility='hidden';
						menuMatrix[tid] = 0;
						//MenuPos=0;
						}

						new Fx.Styles(parent,{ duration: 500, wait: true, onComplete:function(){
						
							new Fx.Styles(h[0],{duration: 500, wait: true, onComplete:function(){removeFilter(h[0])} }).custom( {'opacity':[0,1]} );

						} }).custom({'opacity': [0,1]});

						} }).custom({'opacity': [1,0], 'height': [ myMenuHeight[MenuPos-1], myMenuHeight[id-1] ]});


				//} }).custom({'opacity': [1,0]});


				menuPos=id;
				menuMatrix[id]=id;

			}
			else
			{


				h2[0].style.display='none';
				h2[0].style.opacity='0';

				h[0].style.opacity='1';
				h[0].style.display='block';
				h[0].style.visibility='visible';

				//parent.style.opacity='0';
				//parent.style.display='none';
				//parent.style.visibility='hidden';



			}



			/*
			   h[0].style.visibility='visible';
			   h[0].style.opacity='1';
			 */


			//alert('T1');



		}


		if (!enableMenuFx)
		{	

			parent.style.display='block';
			h[0].style.display='block';
			parent.style.visibility='visible';
			h[0].style.visibility = 'visible';
			//h[0].style.opacity = '1';

			//h[0].style.opacity = '1';
			//parent.style.opacity = '1';



		}


		//wd('<li>Close::Start layer '+id+' Height : '+h[0].style.height);

		var bt = $$('img.btclass'+id);
		if (bt[0])
		{
			bt[0].src = bt[0].src.replace('_off','_on');
		}


		//wd('<li>Setting MenuPos to '+id);
		MenuPos = id;
		menuMatrix[id] = id;

		var total = menuMatrix.length;

		for (x=0;x<=total;x++)
		{
			if (x != id)
				menuMatrix[x] = 0;
		}
		//alert(MenuPos);


	}
	else
	{

		//alert('reclick');
		//wd('<li>layer '+id+' : '+h[0].style.visibility);

		//h[0].style.display='none';
		//h[0].style.visibility='hidden';


		if (enableMenuFx)
		{
			effect = new Fx.Styles(parent, {onComplete:function() 
					{
					new Fx.Styles(parent,{duration:500, wait:true}).custom({'height': [menuHeight,0]});
					}});
			effect.custom({
					'opacity': [1,0]

					});

			h[0].style.display = 'none';
			h[0].style.visibility = 'hidden';
			h[0].style.opacity = '0';
		}
		else
		{

			h[0].style.display = 'none';
			h[0].style.visibility = 'hidden';
			h[0].style.opacity = '0';
			parent.style.display = 'none';
			parent.style.visibility = 'hidden';
			parent.style.opacity = '0';


		}


		//wd('<li>End layer '+id+' Height : '+h[0].style.height);
		/*
		   parent.style.display = 'none';
		   parent.style.visibility= 'hidden';
		 */

		//wd('<li>Setting menu to 0');
		MenuPos = 0;
		menuMatrix[id] = 0;

		//wd('Setting Menupos to ZERO');
		var bt = $$('img.btclass'+id);
		if (bt[0])
		{
			bt[0].src = bt[0].src.replace('_on','_off');
		}

		//alert(bt[0].src);
		highlight();

	}



}


