// JavaScript Document
/*
tested & OK:
MAC ie5, ns4, ns6
PC ie6, ns4, Opera7
*/
/***** MENUOBJ
- Creer des layers pour la têtes et les lignes du menu
- En faire des LyrObjs et les stocker dans une array
- Creer un menuObj en passant cette array

offsetS: decalage des items par rapport à la tête, par ex pour submenus
	! x DOIT etre positif, et il faut aussi que le bas du dernier item soit plus bas
	que celui de head, sinon mauvaise detection de sortie de survol
*/
var MENUS_OUVERTS;
var MENU_LIST;
	
 function MenuObj (itemS,x,y,visible,clickToUnfold, offsetS)
 {
	this.offsetS = offsetS;
 	this.type = 'MenuObj';
 	this.itemS = itemS;
	this.state = 'open';
	this.visible = visible;
	this.clickToUnfold = clickToUnfold;
	// fonction event de MenuObj
	this.MenuMouseMove = MenuObjMouseMove;
	this.MenuMouseDown = MenuObjMouseDown;
	// methodes
	this.MoveTo = MenuObjMoveTo;
	this.Close = MenuObjClose;
	this.Open = MenuObjOpen;
	this.Switch = MenuObjSwitch;
	this.Show = MenuObjShow;
	this.Hide = MenuObjHide;
	this.Area = MenuObjArea;
	this.Bottom = MenuObjBottom;	// retourne le bottom de l'item [0]
	this.Right = MenuObjRight;
	this. Height = MenuObjHeight;
	this.MoveTo(x,y);
	this.Close();
	if (this.visible)
		this.Show();

	if(!MENUS_OUVERTS)
		MENUS_OUVERTS = new PileObj();// ici s'empilent les pointeurs des MenuObjs ouverts
																	// (si submenu, il y en a plusieurs!)
	if(!MENU_LIST)
	{
		MENU_LIST = new Array();
	}
	MENU_LIST[MENU_LIST.length] = this;		// tous les menus crees sont inscrits ici. 
														// CloseAllMenus() etc... utilisent cette liste 
 	return this;
 }

function MenuObjMoveTo (x,y)
{
	limit = this.itemS.length;
	offsetY = 0;
	offsetX = 0;
	for(i = 0; i<limit; i++)
	{
		this.itemS[i].MoveTo (x+offsetX, y+offsetY);
		if (this.offsetS && (!offsetY)) // premier item
		{	// appliquer décalage des items par rapport à head
			offsetX = this.offsetS[0];
			offsetY = this.offsetS[1];
		}
		offsetY += this.itemS[i].Height();
	}
}

function MenuObjClose()
{
	if (this.state == 'open')
	{
		this.state = 'closed';
		for(i=1; i<this.itemS.length; i++)
		{
			this.itemS[i].Hide();
		}
	}
}

function MenuObjShow()
{
	this.itemS[0].Show();
}
function MenuObjHide()
{
	this.Close();
	this.itemS[0].Hide();
}

function MenuObjOpen()
{
	if (this.state == 'closed')
	{
		this.state = 'open';
		for(i=1; i<this.itemS.length; i++)
		{
			this.itemS[i].Show();
		}
		MOUSEMOVE_FUNCTS.push(this.MenuMouseMove);
//		if (this.MyMouseMove) // fonction custom supplémentaire?
//			MOUSEMOVE_FUNCTS.push(this.MyMouseMove);
		MENUS_OUVERTS.push(this);
		// gestionnaire d'event pour la sortie du survol
		StartEventCapture('mousemove');
	}
}

function MenuObjMouseMove(e)
{
	if (MENUS_OUVERTS.Length())
	{
		mymenu = MENUS_OUVERTS.GetTop();
		if (mymenu)
		{
		myrect = mymenu.Area();
		mymouse = GetMouse(e);
		myX = mymouse[0];
		myY = mymouse[1];
		if ( (myX<myrect[0]) || (myX>myrect[2]) || (myY<myrect[1]) || (myY>myrect[3]) )
		{
			mymenu.Close();
			MENUS_OUVERTS.pop();
			MOUSEMOVE_FUNCTS.pop();
		}
		}
	}
}

function MenuObjMouseDown(e)
{
	// pour l'intant on intervient pas...
}

function  MenuObjSwitch()
{
	if (this.state == 'closed')
		this.Open();
	else
		this.Close();
}

function MenuObjArea()
{
	mybottom = this.itemS[this.itemS.length - 1].Bottom();
	myright = this.itemS[this.itemS.length - 1].Right();
	myrect = new Array(this.itemS[0].Left(),this.itemS[0].Top(),myright,mybottom);
	return myrect;
}

function MenuObjBottom()
{	// necessaire si on est un sous-menu
	return this.itemS[0].Bottom();
}
function MenuObjHeight()
{	// idem
	return this.itemS[0].Height();
}
function MenuObjRight()
{	// idem
	return this.itemS[0].Right();
}
// fin methodes MenuObj


// pour manipuler tous les menus d'un coup
// (!! ce ne sont pas de methodes)
function CloseAllMenus()
{
	for (i = 0; i<MENU_LIST.length; i++)
		MENU_LIST[i].Close();
}
function HideAllMenus()
{
	for (i = 0; i<MENU_LIST.length; i++)
		MENU_LIST[i].Hide();
}
function ShowAllMenus()
{
	for (i = 0; i<MENU_LIST.length; i++)
		MENU_LIST[i].Show();
}
function MoveToAllMenus(x,y)
{
	for (i = 0; i<MENU_LIST.length; i++)
		MENU_LIST[i].MoveTo(x,y);
}
function MoveByAllMenus(x,y)
{
	for (i = 0; i<MENU_LIST.length; i++)
		MENU_LIST[i].MoveBy(x,y);
}