
var SEPARATOR = "<hr>";

function CMenuItem(sObjName, sTitle, sURL, sOnImgURL, sOffImgURL, sDisImgURL,
	nLeft, nTop, nWidth, nHeight,
	sAltName)
{
	CMenuItem_Constructor(this,
		sObjName, sTitle, sURL, sOnImgURL, sOffImgURL, sDisImgURL, 
		nLeft, nTop, nWidth, nHeight, sAltName);
}

function CMenuItem_Constructor(_this,
	sObjName, sTitle, sURL, sOnImgURL, sOffImgURL, sDisImgURL, 
	nLeft, nTop, nWidth, nHeight,
	sAltName)
{
	CVisualObject_Constructor(_this, sObjName,
		nLeft, nTop, nWidth, nHeight);
	_this.SetClassName("CMenuItem");

	_this.virtual("SetHeight");
	_this.virtual("SetWidth");
	_this.virtual("SetLeft");
	_this.virtual("SetTop");

	_this.virtual("OnDraw");
	_this.virtual("DrawImage");
	_this.virtual("DrawTitle");

	_this.virtual("SetFocus");
	_this.virtual("KillFocus");

	_this.CreateImage = CMenuItem_CreateImage;
	_this.GetImageObject = CMenuItem_GetImageObject;

	_this.GetTitle = CMenuItem_GetTitle;
	_this.SetTitle = CMenuItem_SetTitle;
	_this.IsSeparator = CMenuItem_IsSeparator;
	_this.SetTitleOfs = CMenuItem_SetTitleOfs;

	_this.SetSubMenu = CMenuItem_SetSubMenu;

	_this.$GetImgTag = CMenuItem_$GetImgTag;
	_this.$GetLinkTag = CMenuItem_$GetLinkTag;

	_this.m_sTitle = "";
	_this.SetTitle(sTitle);

	_this.m_sURL = sURL;
	_this.m_sAlt = (sAltName == null) ? "" : sAltName;

	_this.m_sCSS = "class=MenuItemCSS";
	_this.m_sDisabledCSS = "class=DisabledMenuItemCSS";

	_this.m_nImgHeight = 0;
	_this.m_nImgWidth = 0;
	_this.m_arImgs = null;
	_this.CreateImage("on", sOnImgURL);
	_this.CreateImage("off", sOffImgURL);
	_this.CreateImage("disabled", sDisImgURL);

	_this.m_submenu = null;

	_this.m_nBorder = 0;
	_this.m_nTitleOfs = 5;
	_this.m_sBgColor = (_this.IsSeparator() ? 0 : "#E0E0E0");
	_this.m_sTextHAlign = "left";
	_this.m_sTextVAlign = "middle";
	_this.m_sImgHAlign = "left";
	_this.m_sImgVAlign = "middle";

	_this.Enable(!_this.IsSeparator());
      
}

function CMenuItem_SetLeft(nLeft)
{
	this.Base_SetLeft(nLeft);
}

function CMenuItem_SetTop(nTop)
{
	this.Base_SetTop(nTop);
}

function CMenuItem_SetHeight(nHeight)
{
	this.Base_SetHeight(nHeight);

	if (this.m_nHeight < this.m_nImgHeight)
		this.m_nHeight = this.m_nImgHeight;
}

function CMenuItem_SetWidth(nWidth)
{
	this.Base_SetWidth(nWidth);

	if (this.m_nWidth < this.m_nImgWidth)
		this.m_nWidth = this.m_nImgWidth;
}

function CMenuItem_SetSubMenu(menu)
{
	this.m_submenu = menu;

	menu.Visible(false);
	menu.SetParent(this);

	if (this.m_owner.IsKindOf("CMenu"))
	{
		var nLeft = this.GetAbsLeft();
		var nTop = this.GetAbsTop();

		if (!this.m_owner.IsVertical())
			nTop += this.m_nHeight;
		else
		{
			nLeft += this.m_nWidth;
			if (nLeft + menu.m_nWidth > g_nScreenWidth)
				nLeft = g_nScreenWidth - menu.m_nWidth;
			nLeft -= 10;
		}

		menu.AbsMoveTo(nLeft, nTop);
	}
}

function CMenuItem_CreateImage(pos, sImageURL)
{
	if (sImageURL == null || sImageURL == "")
		return;

	if (!this.m_arImgs)
		this.m_arImgs = new Array();

	this.m_arImgs[pos] = img = new Image();
	img.src = sImageURL;

	return;	

	if (this.m_nHeight < img.height)
		this.m_nHeight = img.height;
	if (this.m_nWidth < img.width)
		this.m_nWidth = img.width;

	if (this.m_nImgHeight < img.height)
		this.m_nImgHeight = img.height;
	if (this.m_nImgWidth < img.width)
		this.m_nImgWidth = img.width;
}

function CMenuItem_GetImageObject()
{
	var img = null;
	var sImgName = this.GetObjectName() + "Img";
	if (IsIE())
		img = document.images[sImgName];
	else
	if (IsNN())
	{
		var obj = this.GetElement();
		if (obj)	
			img = obj.document.images[sImgName];
	}

	return img;
}

function CMenuItem_SetTitle(sTitle)
{
	if (!sTitle)
		this.m_sTitle = "";
	else
		this.m_sTitle = new String(sTitle);
}

function CMenuItem_GetTitle()
{
	return !this.IsSeparator() ? 
		ToHtmlSafeString(this.m_sTitle) : SEPARATOR;
}

function CMenuItem_IsSeparator()
{
	return this.m_sTitle == SEPARATOR;
}

function CMenuItem_SetTitleOfs(nOfs)
{
	this.m_nTitleOfs = nOfs;	
}

function CMenuItem_$GetImgTag(img)
{
	if (img != null)
		return	"<img "+
			" name=\"" + this.GetObjectName() + "Img\""+
	  	 	" id=\"" + this.GetObjectName() + "Img\""+
			" alt=\"" + this.m_sAlt + "\"" +
			" src=\"" + img.src + "\"" +
			" border=0" +
			">";
	else
		return "";
}

function CMenuItem_$GetLinkTag()
{
	return  "<a href=\"" + this.m_sURL + "\" target=\"_top\" " +
		" name=\""+this.GetObjectName() + "Lnk\" "+
	 	" id=\"" + this.GetObjectName() + "Lnk\" "+
	 	this.m_sCSS + 
		this.$GetMouseFuncTags() + 
		">"; 
}

function CMenuItem_DrawImage()
{
	var sImgTag = "";
	if (this.m_arImgs)
		sImgTag = this.$GetImgTag(this.IsEnabled() ?
			this.m_arImgs["off"] : this.m_arImgs["disabled"]);

	if (sImgTag != "")
	{
		if (this.IsEnabled())
			sImgTag = this.$GetLinkTag() + sImgTag + "</a>";
	}
	else
		sImgTag = "&nbsp;";

	var sTag = 
        	"<td "+
		" align="+ this.m_sImgHAlign +
		" valign=" + this.m_sImgVAlign + 
		(this.m_nImgWidth ? (" width=" + this.m_nImgWidth):"") + 
		(this.m_nImgHeight ? (" height="+ this.m_nImgHeight):"") + 
		">" + 
		sImgTag +
		"</td>\n";
//		"<td width="+this.m_nTitleOfs+">&nbsp;</td>\n";
//	alert(sTag);
	document.write(sTag);
}

function CMenuItem_DrawTitle()
{
	var sTitleTag = this.GetTitle(); 
	if (sTitleTag == "")
		return;

	if (this.IsEnabled())
		sTitleTag = this.$GetLinkTag() + sTitleTag + "</a>";

	var sTag = 
		"<td width=100%" +
		" align=" + this.m_sTextHAlign +
		" valign=" + this.m_sTextVAlign + " " +
	 	this.GetCSS() + 
		">" + (!this.IsSeparator() ? ("&nbsp;"+sTitleTag) : "") +
		"</td>\n";

//	alert(sTag);
	document.write(sTag);
}

function CMenuItem_OnDraw()
{
	document.write(
		"<table border=" + this.m_nBorder +
		" cellpadding=0 cellspacing=0" +
		(this.m_nWidth ? (" width=" + this.m_nWidth) : "") + 
		(this.m_nHeight ? (" height=" + this.m_nHeight) : "") + 
		">\n"+
		"<tr>\n");

	if ((!this.IsSeparator() && this.m_arImgs) || 
	    (this.m_owner && this.m_owner.IsVertical()))
		this.DrawImage();

	this.DrawTitle();

	document.write("</tr>\n</table>\n");
}

function CMenuItem_SetFocus()
{
	this.Base_SetFocus();

	if (this.IsEnabled())
	{
		var img = this.GetImageObject();
		if (img != null)
			img.src = this.m_arImgs["on"].src;

		var owner = this.m_owner;
		if (owner && owner.IsKindOf("CMenu"))
			for (i in owner.m_arItems)
			{
				var item = owner.m_arItems[i];
				if (item.IsKindOf("CMenuItem") && item.m_submenu)
					item.m_submenu.Visible(false);		
			}

		if (this.m_submenu && !this.m_submenu.IsVisible())
			this.m_submenu.Visible(true); 	
	}
}

function CMenuItem_KillFocus(selectedItem)
{
	if (this.IsEnabled()) 
	{
		var img = this.GetImageObject();
		if (img != null)
			img.src = this.m_arImgs["off"].src;

		if (this.m_submenu)
                        this.m_submenu.Visible(
				selectedItem == this.m_submenu ||
				this.m_submenu.IsMenuItem(selectedItem));
	}

	this.Base_KillFocus(selectedItem);
}
