//*****************************************************************************
// Warning!!
// There is a relation between main menu border and submenu position.
// Current implementation may not work correctly with wide borders.

function StyleSet(MenuItem, MenuItemSelected, FirstMenuItem, FirstMenuItemSelected, TableStyle)
{
	this.m_MenuItem					= MenuItem;
	this.m_MenuItemSelected			= MenuItemSelected;
	this.m_FirstMenuItem			= FirstMenuItem;
	this.m_FirstMenuItemSelected	= FirstMenuItemSelected;
	this.m_TableStyle				= TableStyle;
}

//--- Menu constructor - use for root and submenus ---

function Menu(Title, isVertical, StyleSet)
{
	this.m_ID				= null;				// item id - will be filed during menu HTML generation
	this.m_SubMenuID		= null;				// sub menu id - will be filed during menu HTML generation
	this.m_Title			= Title;			// text to be shown as menu caption - ignored for root menu
	this.m_isVertical		= isVertical;		// menu orientation if true menu will be horizontal
	this.m_isSubmenu		= false;			// true if root menu - update at the menu construction time
	this.m_parent			= null;				// reference to parent
	this.m_nodes			= new Array();		// menu children
	this.m_HTML				= "";				// HTML representation of menu
	this.m_isFirst			= false;			// indicates that item is last in the menu
	this.m_Top				= -1;				// absolute positions for sub menus
	this.m_Left				= -1;				// absolute positions for sub menus
	this.m_StyleSet			= StyleSet;			// set of styles to be used for menu rendering
	this.m_ChildStyleSet	= StyleSet;			// set of styles to be used for menu rendering
}

//--- Method to build hierarchy ---

Menu.prototype.AddItem = function(item)
{with(this){

//--- Set the child's parent ---
	item.m_parent = this;
	
//--- If submenu set flag ---
	if(Menu.prototype.isPrototypeOf(item))		
	{
		item.m_isSubmenu = true;
		item.m_StyleSet	= m_ChildStyleSet;
	}
	else
	{
		item.m_StyleSet = m_ChildStyleSet;
	}

	if(m_nodes.length == 0) item.m_isFirst = true;
	m_nodes[m_nodes.length] = item;
}}

//--- Method to render the menu ---

Menu.prototype.PrepareMenu = function(insertAtElement, MenuVariableName)
{with(this){
//--- Check if root menu ---
	if (m_isSubmenu == true) 
	{	
		alert('Cannot render independent submenus');
		return;
	}

//--- Generate Menus HTML ---
	PrepareMenuInternal(MenuVariableName, MenuVariableName)
	
//--- Set menu ---
	insertAtElement.innerHTML = m_HTML;
}}

Menu.prototype.PrepareMenuInternal = function(ID, Reference)
{with(this){
//--- Generate menu/item ID ---
	m_SubMenuID = ID;
	if(!m_isSubmenu)	m_ID = ID;
	
	var sStr = '';
	sStr += '<DIV ID="' + m_SubMenuID + '" ';
	if(m_isSubmenu)	
	{
		sStr += 'style="display:none" ';
	}
	sStr += '>';
//	sStr += '<TABLE ID="' + m_SubMenuID + '" cellSpacing="0" cellpadding="0" ';
	sStr += '<TABLE cellSpacing="0" cellpadding="0" ';
	sStr += 'class="' + m_StyleSet.m_TableStyle + '" ';
/*	if(m_isSubmenu)	
	{
		sStr += 'style="display:none" ';
	}*/
	sStr += '>';
	
//--- Horizontal menu has only one row ---
	if(!m_isVertical)	sStr += '<TR>';
	
	var nItem = 0;
	for(nItem = 0; nItem < m_nodes.length; nItem++)
	{
	//--- Vertical menu - each item in separate row ---
		if(m_isVertical)	sStr += '<TR>';

	//--- Generate item id and reference ---
		var ChildID	= m_ID + '_' + nItem;
		var ChildReference = Reference + '.m_nodes[' + nItem + ']';
		m_nodes[nItem].m_ID = ChildID;
		
	//--- Genrate submenu HTML ---
		if(Menu.prototype.isPrototypeOf(m_nodes[nItem]))
		{
			m_nodes[nItem].PrepareMenuInternal(ChildID+'_Sub', ChildReference);
		}

	//--- Render item in HTML ---
		sStr += m_nodes[nItem].ItemHTML(ChildID, ChildReference);
				
	//--- Vertical menu - each item in separate row ---
		if(m_isVertical)	sStr += '</TR>';
	}
	
//--- Horizontal menu has only one row ---
	if(!m_isVertical)	sStr += '</TR>';
	
	sStr += '</TABLE></DIV>';
	
	m_HTML = sStr;
}}

Menu.prototype.GetSubMenusHTML = function()
{with(this){
	var sStr = '';
	 
	if(m_isSubmenu) sStr += m_HTML;
	
	var nItem = 0;
	for(nItem = 0; nItem < m_nodes.length; nItem++)
	{
		if(Menu.prototype.isPrototypeOf(m_nodes[nItem]))
		{
			sStr += m_nodes[nItem].GetSubMenusHTML();
		}
	}
	
	return sStr;
}}

//--- Item - menu leaf node constructor ---

function Item(Title, link, targetframe)
{
	this.m_ID			= null;			// item id - will be filed during menu HTML generation
	this.m_Title		= Title;		// menu caption
	this.m_link			= link;			// link to page
	this.m_targetframe	= targetframe;	// frame in which to load the link
	this.m_parent		= null;			// reference to parent
	this.m_StyleSet		= null;
}

Item.prototype.ItemHTML = function(ID, Reference)
{with(this){
//--- Generate item HTML ---
	var sStr = '';
	sStr += '<TD NOWRAP ';
	sStr += 'class="' + GetClass(false, this) + '" ';
	sStr += 'ID="' + m_ID + '" ';
	sStr += 'onMouseOver="' + Reference + '.MouseOver();" ';
	sStr += 'onMouseOut="' + Reference + '.MouseOut();" ';
	sStr += 'onClick="' + Reference + '.MouseClick();" ';
	sStr += '>';
	sStr += m_Title;
	sStr += '</TD>';
	
	return sStr;
}}

Menu.prototype.ItemHTML = function(ID, Reference)
{with(this){
//--- Generate item HTML ---
	var sStr = '';
	sStr += '<TD NOWRAP ';
	sStr += 'class="' + GetClass(false, this) + '" ';
	sStr += 'ID="' + m_ID + '" ';
	sStr += 'onMouseOver="' + Reference + '.MouseOver();" ';
	sStr += 'onMouseOut="' + Reference + '.MouseOut();" ';
	sStr += '>';
	sStr += '<DIV style="width:100%">' + m_Title + '</DIV>';
	sStr += m_HTML;
	sStr += '</TD>';
	
	return sStr;
}}

function GetClass(isOver, item)
{
var sClass ='';

	if(isOver)
	{
		if(item.m_isFirst)	sClass = item.m_StyleSet.m_FirstMenuItemSelected;
		else				sClass = item.m_StyleSet.m_MenuItemSelected;
	}
	else
	{
		if(item.m_isFirst)	sClass = item.m_StyleSet.m_FirstMenuItem;
		else				sClass = item.m_StyleSet.m_MenuItem;
	}
	return sClass;
}

function SetClass(isOver, item)
{	
	document.all(item.m_ID).className = GetClass(isOver, item);
}

//--- Mouse moves over menu ---

Menu.prototype.MouseOver = function()
{
//--- Set the style ---
	SetClass(true, this);

with(this){
//--- Show submenu ---
	document.all(m_SubMenuID).style.display = 'block';
	
//	alert(document.all(m_SubMenuID).outerHTML);
//--- Position submenu ---
	document.all(m_SubMenuID).style.position = 'absolute';
	document.all(m_SubMenuID).style.zIndex = 100;
	document.all(m_SubMenuID).style.overflowY = 'visible';
	document.all(m_SubMenuID).style.overflowX = 'visible';
	
//	document.all(m_SubMenuID).innerHTML='<IFRAME FRAMEBORDER=0 SCROLLING=NO SRC="indexnoframes.htm"></IFRAME>';

	with(document.all(m_ID))
	{
		var xCP = 3;
		var yCP = 3;
		if(m_parent.m_isVertical)	
		{
			var xBW	 = 0;
			var yBW	 = 0;
			if(currentStyle.borderLeftStyle != 'none')	xBW	 = parseInt(currentStyle.borderLeftWidth);
			if(currentStyle.borderTopStyle != 'none')	yBW	 = parseInt(currentStyle.borderTopWidth);
			var xOff = offsetWidth;
			var yOff = offsetHeight;
			
			if(m_Left == -1)	m_Left	= document.all(m_SubMenuID).offsetLeft + xOff - xCP - xBW;
			if(m_Top == -1)		m_Top	= document.all(m_SubMenuID).offsetTop - yOff + yCP + 2*yBW;
		}
		else
		{
			var xBW	= 0
			var yBW	= 0
			if(currentStyle.borderRightStyle != 'none')		xBW = parseInt(currentStyle.borderRightWidth);
			if(currentStyle.borderBottomStyle != 'none')	yBW	 = parseInt(currentStyle.borderBottomWidth);
			var xOff = (offsetWidth-1)/2;
			
			if(m_Left == -1)	m_Left	= document.all(m_SubMenuID).offsetLeft - xOff + xBW;
			if(m_Top == -1)		m_Top	= document.all(m_SubMenuID).offsetTop + yCP + yBW;
		}
	}
	document.all(m_SubMenuID).style.left = m_Left;
	document.all(m_SubMenuID).style.top = m_Top;
	document.all(m_SubMenuID).style.width = '100%';
}}

Item.prototype.MouseOver = function()
{
//--- Set the style ---
	SetClass(true, this);
}

//--- Mouse moves out of menu ---

Menu.prototype.MouseOut = function()
{
//--- Set the style ---
	SetClass(false, this);

with(this){
	document.all(m_SubMenuID).style.display = 'none';
}}

Item.prototype.MouseOut = function()
{
//--- Set the style ---
	SetClass(false, this);
}

Item.prototype.MouseClick = function()
{with(this){

var wDestination = window;

	if(m_targetframe != '') wDestination = FindFrame(top, m_targetframe);
	wDestination.navigate(m_link);
}}

function FindFrame(obj, name)
{
var nFrmInd = 0;
var	wFrame	= null;

	for(nFrmInd = 0; nFrmInd < obj.frames.length; nFrmInd++)
	{
		if(obj.frames[nFrmInd].name == name)
			return obj.frames[nFrmInd];
			
		wFrame = FindFrame(obj.frames[nFrmInd], name)
		if(wFrame != null)
			return wFrame;
	}
	
	return wFrame;
}
