
var VERTICAL_MENU = 1;
var HORIZONTAL_MENU = 2;

function CMenu(sObjName,
	nWidth, nHeight, nDirection, nSpan)
{
	CMenu_Constructor(this, sObjName, nWidth, nHeight, nDirection, nSpan);
}

function CMenu_Constructor(_this, sObjName,
	nWidth, nHeight, nDirection, nSpan)
{
	CVisualObject_Constructor(_this, sObjName, 0, 0, nWidth, nHeight);
	_this.SetClassName("CMenu");

	_this.virtual("SetBgColor");

	_this.virtual("InsertItem");
	_this.IsMenuItem = CMenu_IsMenuItem;

	_this.SetTitleOfs = CMenu_SetTitleOfs;
	_this.SetItemsSpan = CMenu_SetItemsSpan;
	_this.IsVertical = CMenu_IsVertical;

	_this.virtual("OnDraw");

	_this.m_nItemsSpan = SafeNumber(nSpan);

	_this.m_nImgWidth = 0;
	_this.m_nImgHeight = 0;

	_this.m_nBorder = 1;
	_this.m_sBgColor = "#C4E1FF";
	_this.m_nPosX = _this.m_nPosY = 0;
	_this.m_nDirection =
		(nDirection == null || nDirection == "") ?
		VERTICAL_MENU : nDirection;
}

function CMenu_SetTitleOfs(nOfs)
{
	for (i in this.m_arItems)
		this.m_arItems[i].SetTitleOfs(nOfs);		
}

function CMenu_SetItemsSpan(nSpan)
{
	this.m_nItemsSpan = nSpan;
}

function CMenu_IsVertical()
{
	return this.m_nDirection == VERTICAL_MENU;
}

function CMenu_SetBgColor(nBgColor)
{
	this.Base_SetBgColor(nBgColor);

	for (i in this.m_arItems)
	{
		var item = this.m_arItems[i];
		if (item.IsKindOf("CMenuItem") && !item.IsSeparator())
			item.SetBgColor(nBgColor);
	}
}

function  CMenu_InsertItem(item)
{
	item.m_nBorder = 0;
	if (item.IsKindOf("CMenuItem") && !item.IsSeparator())
		item.m_sBgColor = this.m_sBgColor;

	if (this.IsVertical())
	{
		if (this.m_nHeight < this.m_nPosY)
			this.m_nHeight = this.m_nPosY;

		item.SetLeft(this.m_nPosX);
		item.SetTop(this.m_nPosY);
		item.SetWidth(this.m_nWidth);

		if (item.IsKindOf("CMenuItem") && item.IsSeparator())
		{
			item.SetHeight(1);
			this.m_nPosY += 1;
		}
		else
		{
			if (item.m_nHeight < this.m_nItemsSpan)
				item.SetHeight(this.m_nItemsSpan);

                	this.m_nPosY += (this.m_nItemsSpan > item.m_nHeight) ?
					this.m_nItemsSpan : item.m_nHeight;
		}

		if (this.m_nTop + this.m_nHeight < 
		    this.m_nTop + item.m_nTop + item.m_nHeight)
			this.SetHeight(item.m_nTop + item.m_nHeight);
	}
	else
	{
		if (this.m_nWidth < this.m_nPosX)
			this.m_nWidth = this.m_nPosX;

		item.SetLeft(this.m_nPosX);
		item.SetTop(this.m_nPosY);
		item.SetHeight(this.m_nHeight);

		if (item.m_nWidth < this.m_nItemsSpan)
			item.SetWidth(this.m_nItemsSpan);

		this.m_nPosX += (this.m_nItemsSpan > item.m_nWidth) ?
				this.m_nItemsSpan : item.m_nWidth;

		if (this.m_nLeft + this.m_nWidth < 
		    this.m_nLeft + item.m_nLeft + item.m_nWidth)
			this.SetWidth(item.m_nLeft + item.m_nWidth + 1);
	}

	if (item.IsKindOf("CMenuItem"))
	{
		if (this.m_nImgWidth < item.m_nImgWidth)
			this.m_nImgWidth = item.m_nImgWidth;
		if (this.m_nImgHeight < item.m_nImgHeight)
			this.m_nImgHeight = item.m_nImgHeight;
	}

	this.Base_InsertItem(item);
}

function CMenu_IsMenuItem(obj)
{
	if (obj)
		return this.m_arItems[obj.GetObjectName()] != null;
	else
		return false;
}

function CMenu_OnDraw()
{
	document.write(
		"<table border=" + this.m_nBorder +
		" cellpadding=0 cellspacing=0" +
		" width=" + this.m_nWidth + 
		" height=" + this.m_nHeight + " " +
		this.m_sCSS + " " +
		">\n"+
		"<tr>\n<td>\n\n");

	for (i in this.m_arItems)
	{
		var item = this.m_arItems[i];
		if (item.IsKindOf("CMenuItem"))	
			item.m_nImgWidth = this.m_nImgWidth; 
		item.Draw();
	}

	document.write("\n</td>\n</tr>\n</table>\n");
}

