Menu = function(stg)
{
	//alert ('ok');
	this.stg = stg;
	this.isIE = isIE();
}

Menu.prototype = 
{
	draw: function()
	{
		var parentElement = document.getElementById(this.stg.common.idBlock);
		if (!parentElement)
			return false;

		if (this.stg.common.menuBorderWidth)
			parentElement.style.border = this.stg.common.menuBorderWidth + ' ' + this.stg.common.menuBorderColor  + ' solid';

		var left = 0;

		var main = this.getMain();

		for (var i in this.stg.elem)
		{
			var elem = this.getElem(i, left);
						
			if (elem)
			{
				main.appendChild(elem);
				left += parseInt(this.stg.elem[i].width) - parseInt(this.stg.common.elemBorderWidth);
				//if (!this.isIE)

				//	left += 2*parseInt(this.stg.common.elemBorderWidth);

				if (isSet(this.stg.subelem[i]))
				{
					var subelem = this.getSubelem(i);
					if (subelem)
						elem.appendChild(subelem);
				}
			}
		}
		parentElement.appendChild(main);

		return true;
	},
	
	getElem: function(elemId, left)
	{
		var stg = this.stg.elem[elemId];
		if (!stg)
			return false;

		var colorOff = (isSet(stg.colorOff)) ? stg.colorOff : this.stg.common.elemColorOff;
		var colorOn = (isSet(stg.colorOn)) ? stg.colorOn : this.stg.common.elemColorOn;
		var bgColorOff = (isSet(stg.bgColorOff)) ? stg.bgColorOff : this.stg.common.elemBgColorOff;
		var bgColorOn = (isSet(stg.bgColorOn)) ? stg.bgColorOn : this.stg.common.elemBgColorOn;
		var fontSizeOff = (isSet(stg.fontSizeOff)) ? parseInt(stg.fontSizeOff) : parseInt(this.stg.common.elemFontSizeOff);
		var fontSizeOn = (isSet(stg.fontSizeOn)) ? parseInt(stg.fontSizeOn) : parseInt(this.stg.common.elemFontSizeOn);
		var fontWeightOff = (isSet(stg.fontWeightOff)) ? stg.fontWeightOff : this.stg.common.elemFontWeightOff;
		var fontWeightOn = (isSet(stg.fontWeightOn)) ? stg.fontWeightOn : this.stg.common.elemFontWeightOn;

		var ret = document.createElement ('div');
		ret.style.position = 'absolute';
		ret.style.zIndex = 5;
		ret.style.left = parseInt(left) + 'px';
		ret.style.top = '0px';
		ret.style.width = parseInt(stg.width) + 'px';
		ret.style.height = '100%';
		ret.style.color = colorOff;
		ret.style.fontSize = fontSizeOff + 'px';
		ret.style.fontWeight = fontWeightOff;
		ret.style.backgroundColor = bgColorOff;
		ret.style.textAlign = this.stg.common.elemTextAlign;
		ret.style.paddingLeft = parseInt(this.stg.common.elemPaddingLeft) + 'px';

		var instance = this;
		ret.onmouseover = function(event)
		{
			this.style.color = colorOn;
			this.style.fontSize = fontSizeOn + 'px';
			this.style.fontWeight = fontWeightOn;
			this.style.backgroundColor = bgColorOn;
			show(instance.getElemBlockId(elemId));
		}

		ret.onmouseout = function(event)
		{
			this.style.color = colorOff;
			this.style.fontSize = fontSizeOff + 'px';
			this.style.fontWeight = fontWeightOff;
			this.style.backgroundColor = bgColorOff;
			hide(instance.getElemBlockId(elemId));
		}

		var onclick = (isSet(stg.href)) ? ' onclick="document.location=\'' + stg.href + '\'"' : '';

		ret.innerHTML = '<div style="height:100%;z-index:5;border-left:' + parseInt(this.stg.common.elemBorderWidth) + 'px ' + this.stg.common.elemBorderColor + ' solid;border-right:' + parseInt(this.stg.common.elemBorderWidth) + 'px ' + this.stg.common.elemBorderColor + ' solid;cursor:' + (isSet(stg.href) ? 'pointer' : 'default') + '"' + onclick + '>' + stg.name + '</div>';

		return ret;
	},
	
	getElemBlockId: function(elemId)
	{
		return this.stg.common.idElemPrefix + elemId;
	},

	getMain: function()
	{
		var ret = document.createElement ('div');

		var height = parseInt(this.stg.common.height);
		if (this.isIE)
			height -= 2*parseInt(this.stg.common.menuBorderWidth);

		ret.style.position = 'relative';
		ret.style.zIndex = this.stg.common.zIndex;
		ret.style.backgroundColor = this.stg.common.bgColor;
		ret.style.fontFamily = this.stg.common.fontFamily;
		
		var borderWidth = parseInt(this.stg.common.elemBorderWidth);
		if (borderWidth > 0)
		{
			if (this.isIE)
				height += 2*parseInt(this.stg.common.elemBorderWidth);
			ret.style.border = borderWidth + 'px ' + this.stg.common.elemBorderColor + ' solid';
			ret.style.borderLeft = 'none';
		}

		ret.style.height = height + 'px';

		return ret;
	},
	
	getSubelem: function(elemId)
	{
		var stg = this.stg.elem[elemId];

		if (!stg)
			return false;

		var subelemSeparate = (isSet(stg.subelemSeparate)) ? stg.subelemSeparate : this.stg.common.subelemSeparate;
		var borderWidth = parseInt(this.stg.common.subelemBorderWidth);
		var borderColor = this.stg.common.subelemBorderColor;
		
		var ret = document.createElement ('div');
		ret.style.display = 'none';
		ret.style.zIndex = 1;
		ret.style.backgroundColor = '';
		ret.style.position = 'absolute';
		ret.style.top = (parseInt(this.stg.common.height) - parseInt(this.stg.common.elemBorderWidth)) + 'px';
		ret.style.paddingTop = (parseInt(this.stg.common.elemBorderWidth)) + 'px';
		ret.style.left = '0px';
		ret.style.width = (parseInt(stg.subelemWidth)) + 'px';
		ret.id = this.getElemBlockId(elemId);
		if (!subelemSeparate)
			ret.style.borderBottom = borderWidth + 'px ' + borderColor + ' solid';
		
		var subelems = this.stg.subelem[elemId];
		if (!subelems)
			return false;
		
		var isFirst= true;
		for (var i in subelems)
		{
			subelem = this.getSubelemElem(elemId, i, isFirst);
			if (!subelem)
				return false;
			ret.appendChild(subelem);
			isFirst = false;
		}

		return ret;
	},
	
	getSubelemElem: function(elemId, subelemId, isFirst)
	{
		var stgElem = this.stg.elem[elemId];
		var stgSubelem = this.stg.subelem[elemId][subelemId];
		if (!stgElem || !stgSubelem)
			return false;

		var colorOff = (isSet(stgSubelem.colorOff)) ? stgSubelem.colorOff : (isSet(stgElem.subelemColorOff) ? stgElem.subelemColorOff : this.stg.common.subelemColorOff);
		var colorOn = (isSet(stgSubelem.colorOn)) ? stgSubelem.colorOn : (isSet(stgElem.subelemColorOn) ? stgElem.subelemColorOn : this.stg.common.subelemColorOn);
		var bgColorOff = (isSet(stgSubelem.bgColorOff)) ? stgSubelem.bgColorOff : (isSet(stgElem.subelemBgColorOff) ? stgElem.subelemBgColorOff : this.stg.common.subelemBgColorOff);
		var bgColorOn = (isSet(stgSubelem.bgColorOn)) ? stgSubelem.bgColorOn : (isSet(stgElem.subelemBgColorOn) ? stgElem.subelemBgColorOn : this.stg.common.subelemBgColorOn);
		var fontSizeOff = (isSet(stgSubelem.fontSizeOff)) ? parseInt(stgSubelem.fontSizeOff) : (isSet(stgElem.subelemFontSizeOff) ? parseInt(stgElem.subelemFontSizeOff) : this.stg.common.subelemFontSizeOff);
		var fontSizeOn = (isSet(stgSubelem.fontSizeOn)) ? parseInt(stgSubelem.fontSizeOn) : (isSet(stgElem.subelemFontSizeOn) ? parseInt(stgElem.subelemFontSizeOn) : this.stg.common.subelemFontSizeOn);
		var fontWeightOff = (isSet(stgSubelem.fontWeightOff)) ? stgSubelem.fontWeightOff : (isSet(stgElem.subelemFontWeightOff) ? stgElem.subelemFontWeightOff : this.stg.common.subelemFontWeightOff);
		var fontWeightOn = (isSet(stgSubelem.fontWeightOn)) ? stgSubelem.fontWeightOn : (isSet(stgElem.subelemFontWeightOn) ? stgElem.subelemFontWeightOn : this.stg.common.subelemFontWeightOn);
		var subelemSeparate = (isSet(stgElem.subelemSeparate)) ? stgElem.subelemSeparate : this.stg.common.subelemSeparate;
	
		var ret = document.createElement ('div');
		ret.style.color = colorOff;
		ret.style.fontSize = fontSizeOff + 'px';
		ret.style.fontWeight = fontWeightOff;
		ret.style.backgroundColor = bgColorOff;
		ret.style.textAlign = this.stg.common.subelemTextAlign;
		ret.style.paddingLeft = parseInt(this.stg.common.subelemPaddingLeft) + 'px'; 
		ret.style.cursor = (isSet(stgSubelem.href)) ? 'pointer' : 'default';
		ret.style.border = parseInt(this.stg.common.subelemBorderWidth) + 'px ' + this.stg.common.subelemBorderColor + ' solid';
		if (!isFirst)
			ret.style.borderTop = 'none';
		if (!subelemSeparate)
			ret.style.borderBottom = 'none';

		ret.onmouseover = function(event)
		{
			this.style.color = colorOn;
			this.style.fontSize = fontSizeOn + 'px';
			this.style.fontWeight = fontWeightOn;
			this.style.backgroundColor = bgColorOn;
		}

		ret.onmouseout = function(event)
		{
			this.style.color = colorOff;
			this.style.fontSize = fontSizeOff + 'px';
			this.style.fontWeight = fontWeightOff;
			this.style.backgroundColor = bgColorOff;
		}

		if (isSet(stgSubelem.href))
		{
			var instance = this;
			ret.onclick = function(event)
			{
				hide (instance.getElemBlockId(elemId));

				document.location = stgSubelem.href;
			}
		}

		ret.innerHTML = stgSubelem.name;

		return ret;
	}
}

