

function ObjRBSMenu(id) 
{
	var menu = RBS.Dom.GetElement(id)
	this.objMenu = menu;
	if (menu == null) return;
	
	this.Id = id;	
	
	this.activeButton = null;
	this.Menu = new Array();
	
	this.pageMousedown = ObjRBSMenu_pageMousedown;
	this.resetButton = ObjRBSMenu_resetButton;
	this.closeSubMenu = ObjRBSMenu_closeSubMenu;
	this.buttonClick = ObjRBSMenu_buttonClick;
	this.buttonMouseover = ObjRBSMenu_buttonMouseover;
	this.depressButton = ObjRBSMenu_depressButton;
	this.menuMouseover = ObjRBSMenu_menuMouseover;
	this.menuItemMouseover = ObjRBSMenu_menuItemMouseover;
	
	this.menuInit = ObjRBSMenu_menuInit;
	RBS.Event.AddEvent(document, "mousedown", this.pageMousedown);
	
	RBS.System.RegisterNamespace("RBS.Menu");
	RBS.Menu.active = null;
	
	for(i = 0; i < menu.childNodes.length; i++) 
	{
		var button = menu.childNodes[i];
		if (button.tagName == "A" && button.className == "menuButton") 
		{
			RBS.Event.AddEvent(button, "mouseover", this.buttonMouseover);
		    if (button.getAttribute("submenu") != null) 
		    {
				button.mastermenu = this;
				RBS.Event.AddEvent(button, "click", this.buttonClick);
		    }
		}
	}
}


//----------------------------------------------------------------------------
// Fonction general
//----------------------------------------------------------------------------

ObjRBSMenu_pageMousedown = function(evt) 
{
	var This = RBS.Menu.active;
	if (This == null || This.activeButton == null) return true;
	
	new RBS_Event_Event(evt);
	var el = RBS.Event.lastEvent.src;
	
	if (el == This.activeButton) return true;
	
	while(el != null) 
	{
		if (el.tagName == "DIV" && el.id == This.Id)
		{
			return true;
		}
		el = el.parentNode;
	}	

	This.resetButton();
	This.activeButton = null;
	
	//Réactivation des listes déroulantes
	RBS.Form.DisplaySelect(true);
	return false;
}

function ObjRBSMenu_resetButton() 
{
	var This = RBS.Menu.active;
	RBS.Dom.RemoveClass(This.activeButton, "menuButtonActive");
	if (This.activeButton.menu != null) {
		This.closeSubMenu(This.activeButton.menu);
		RBS.Dom.Hide(This.activeButton.menu);
	}
}

function ObjRBSMenu_closeSubMenu(menu) 
{
	if (menu == null || menu.activeItem == null) return;
	var This = RBS.Menu.active;

	if (menu.activeItem.subMenu != null) 
	{
		This.closeSubMenu(menu.activeItem.subMenu);
		RBS.Dom.Hide(menu.activeItem.subMenu);
		menu.activeItem.subMenu = null;
	}

	RBS.Dom.RemoveClass(menu.activeItem, "menuItemHighlight");
	menu.activeItem = null;
}

function ObjRBSMenu_buttonClick(evt) 
{
	new RBS_Event_Event(evt);
	var button = RBS.Event.lastEvent.src;
	while (button != null && button.tagName != "A") 
	{
		button = button.parentNode;
	}
	var submenu = button.getAttribute("submenu");
	var oMenu = RBS.Dom.GetElement(submenu)
	if (oMenu == null) return RBS.Event.lastEvent.Stop();
	
	RBS.Menu.active = button.mastermenu;
	button.blur();
	if (button.menu == null)
	{
		button.menu = oMenu;  
		if (button.menu.initialized == null)
		RBS.Menu.active.menuInit(button.menu);
	}

	if (RBS.Menu.active.activeButton != null) 
		RBS.Menu.active.resetButton();

	if (button != RBS.Menu.active.activeButton) 
	{
		RBS.Menu.active.depressButton(button);
		RBS.Menu.active.activeButton = button;
		RBS.Form.DisplaySelect(false);
	}
	else
	{
		RBS.Menu.active.activeButton = null;
		RBS.Menu.active = null;
		RBS.Form.DisplaySelect(true);
	}
	    
	return RBS.Event.lastEvent.Stop();
}

function ObjRBSMenu_buttonMouseover(evt) 
{
	new RBS_Event_Event(evt);
	var button = RBS.Event.lastEvent.src;
	var This = RBS.Menu.active;

	if (This != null && This.activeButton != null && This.activeButton != button)
    This.buttonClick(evt);
}

function ObjRBSMenu_depressButton(button) 
{
	var x, y;
	button.className += " menuButtonActive";
	  
	x = ObjRBSMenu_GetLeft(button);
	y = ObjRBSMenu_GetTop(button) + RBS.Dom.GetHeight(button);
	
	RBS.Dom.MoveTo(button.menu, x, y);
	RBS.Dom.Show(button.menu);
}

ObjRBSMenu_GetLeft = function(Element)
{
	var result = 0;	
	while (Element && Element.className != "menuBar")
	{
		result += parseInt(Element.offsetLeft);
		Element = Element.offsetParent;
	}
	return result;
}

ObjRBSMenu_GetTop = function(Element)
{
	var result = 0;
	while (Element && Element.className != "menuBar")
	{
		result += parseInt(Element.offsetTop);
		Element = Element.offsetParent;
	}
	return result;
}


function ObjRBSMenu_menuMouseover(evt) {
	new RBS_Event_Event(evt);
	var menu = RBS.Event.lastEvent.src;
	var This = RBS.Menu.active;
	
	menu = RBS.Dom.GetContainerWith(menu, "DIV", "menu");

	if (menu.activeItem != null)
		RBS.Menu.active.closeSubMenu(menu);
}

function ObjRBSMenu_menuItemMouseover(evt) {
	
	new RBS_Event_Event(evt); 
	var item, menu, x, y;
	
	var This = RBS.Menu.active;
	item = RBS.Event.lastEvent.src;
	while (item.tagName != "A") 
	{
		item = item.parentNode;
	}
	
	var SubMenu = RBS.Dom.GetElement(item.getAttribute("submenu"))
	if (SubMenu == null) return RBS.Event.lastEvent.Stop();
	
	menu = item.parentNode;
	while (menu.tagName != "DIV") 
	{
		menu = menu.parentNode;
	}	
	
	if (menu.activeItem == item) return RBS.Event.lastEvent.Stop();
	
	// Close any active sub menu and mark this one as active.
	if (menu.activeItem != null) 
	{
		This.closeSubMenu(menu);
	}
    
	menu.activeItem = item;
	RBS.Dom.AddClass(item, "menuItemHighlight");
	
	// Initialize the sub menu, if not already done.
	if (item.subMenu == null) 
	{
		item.subMenu = SubMenu;
		if (item.subMenu.initialized == null)
		This.menuInit(item.subMenu);
	}

	// Get position for submenu based on the menu item.

	// x = RBS.Dom.GetLeft(item)  + RBS.Dom.GetWidth(item);
	// y = RBS.Dom.GetTop(item);
	
	x = item.offsetParent.offsetLeft + item.offsetParent.offsetWidth;
	y = item.offsetParent.offsetTop + item.offsetTop;
	
	// Adjust position to fit in view.
	var maxX, maxY;

	maxX = RBS.Dom.GetInsideWindowWidth();
	maxY = RBS.Dom.GetInsideWindowHeight();

	maxX -= RBS.Dom.GetWidth(item.subMenu);
	maxY -= RBS.Dom.GetHeight(item.subMenu);

	if (x > maxX)
		x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth + (menu.offsetWidth - item.offsetWidth));
		
	y = Math.max(0, Math.min(y, maxY));

	// Position and show it.
	RBS.Dom.MoveTo(item.subMenu, x, y);
	RBS.Dom.Show(item.subMenu);
	dump("Show " + item.subMenu.id);
		
	// Stop the event from bubbling.
	return RBS.Event.lastEvent.Stop();
}

function ObjRBSMenu_menuInit(menu) 
{
	var itemList, spanList
	var textEl, arrowEl;
	var itemWidth;
	var w, dw;
	var i, j;

	RBS.Event.AddEvent(menu, "mouseover", ObjRBSMenu_menuMouseover);

	// Find the width of a menu item.
	itemList = menu.getElementsByTagName("A");
	if (itemList.length > 0)
		itemWidth = RBS.Dom.GetWidth(itemList[0]);
	else
		return;

	// For items with arrows, add padding to item text to make the
	// arrows flush right.
	for (i = 0; i < itemList.length; i++) 
	{
		if (itemList[i].getAttribute("submenu") != null) 
		{
			RBS.Event.AddEvent(itemList[i], "mouseover", ObjRBSMenu_menuItemMouseover);	
		}
		spanList = itemList[i].getElementsByTagName("SPAN")
		textEl  = null
		arrowEl = null;

		for (j = 0; j < spanList.length; j++) 
		{
			if (RBS.Dom.HasClass(spanList[j], "menuItemText"))
				textEl = spanList[j];
			if (RBS.Dom.HasClass(spanList[j], "menuItemArrow"))
				arrowEl = spanList[j];
		}
		if (textEl != null && arrowEl != null)
			textEl.style.paddingRight = (itemWidth - (textEl.offsetWidth + arrowEl.offsetWidth)) + "px";
	}

	

	w = itemList[0].offsetWidth;
	itemList[0].style.width = w + "px";
	
	dw = itemList[0].offsetWidth - w;
	
	w -= dw;
	itemList[0].style.width = w + "px";

	// Mark menu as initialized.
	menu.initialized = true;
}

