/***** интерфейс настроик для классов Item и Submenu *****/
SubmenuIterface				= function() {}

SubmenuIterface.borders		= function(oObject, sBorder, sStyle, sWidth, sColor) {
	if (oObject && oObject.style) {
		switch(sBorder.toUpperCase()) {
			case 'LEFT' :
				oObject.style.borderLeftStyle		= sStyle;
				oObject.style.borderLeftWidth		= sWidth;
				oObject.style.borderLeftColor		= sColor;
			break;
			case 'TOP' :
				oObject.style.borderTopStyle		= sStyle;
				oObject.style.borderTopWidth		= sWidth;
				oObject.style.borderTopColor		= sColor;
			break;
			case 'RIGHT' :
				oObject.style.borderRightStyle	= sStyle;
				oObject.style.borderRightWidth	= sWidth;
				oObject.style.borderRightColor	= sColor;
			break;
			case 'BOTTOM' :
				oObject.style.borderBottomStyle	= sStyle;
				oObject.style.borderBottomWidth	= sWidth;
				oObject.style.borderBottomColor	= sColor;
			break;
		}
	}
}

SubmenuIterface.padding		= function(oObject, sDirect, sPad) {
	if (oObject && oObject.style) {
		switch(sDirect.toUpperCase()) {
			case 'LEFT'		: oObject.style.paddingLeft		= sPad; break;
			case 'TOP'		: oObject.style.paddingTop		= sPad; break;
			case 'RIGHT'	: oObject.style.paddingRight	= sPad; break;
			case 'BOTTOM'	: oObject.style.paddingBottom	= sPad; break;
		}
	}
}

SubmenuIterface.objectShow	= function(oObject) {oObject.show();}
SubmenuIterface.objectHide	= function(oObject) {oObject.hide();}

SubmenuIterface.absolutePos = function(oObject) {
	var oPos = {x : oObject.offsetLeft, y : oObject.offsetTop};
	if (oObject.offsetParent) {
		var oTemp = SubmenuIterface.absolutePos(oObject.offsetParent);
		oPos.x += oTemp.x;
		oPos.y += oTemp.y;
	}
	return oPos;
}

/***** класс Item - пункт в меню *****/
Item = function(sText) {
	this.line = document.createElement('DIV');
	this.text(sText);
}

Item.prototype.text				= function(sText)	{this.line.innerHTML				= sText;	}
Item.prototype.className		= function(sClass)	{this.line.className				= sClass;	}
Item.prototype.bgColor			= function(sColor)	{this.line.style.backgroundColor	= sColor;	}

Item.prototype.borders			= function(sStyle, sWidth, sColor) {
	this.border('LEFT',		sStyle, sWidth, sColor);
	this.border('TOP',		sStyle, sWidth, sColor);
	this.border('RIGHT',	sStyle, sWidth, sColor);
	this.border('BOTTOM',	sStyle, sWidth, sColor);
}

Item.prototype.border			= function(sBorder, sStyle, sWidth, sColor) {
	SubmenuIterface.borders(this.line, sBorder, sStyle, sWidth, sColor);
}

Item.prototype.paddingLeft		= function(sPad)	{SubmenuIterface.padding(this.line, 'LEFT',		sPad)}
Item.prototype.paddingTop		= function(sPad)	{SubmenuIterface.padding(this.line, 'TOP',		sPad)}
Item.prototype.paddingRight		= function(sPad)	{SubmenuIterface.padding(this.line, 'RIGHT',	sPad)}
Item.prototype.paddingBottom	= function(sPad)	{SubmenuIterface.padding(this.line, 'BOTTOM',	sPad)}
Item.prototype.padding			= function(sPad)	{this.paddingLeft	(sPad);
													 this.paddingRight	(sPad);
													 this.paddingTop	(sPad);
													 this.paddingBottom	(sPad);						}

/***** класс Submenu - выпадающее подменю *****/
Submenu = function(sParentId, iLeft, iTop, iWidth, iHeight, bHandler) {
	if (sParentId) this.parent		= document.getElementById(sParentId);
	else this.parent				= false;
	this.left						= iLeft;
	this.top						= iTop;
	this.items						= new Array();
	this.menu						= document.createElement('DIV');
	var oTmpobject					= this;
	if (!bHandler) {
		this.menu.onmouseover			= function() {SubmenuIterface.objectShow(oTmpobject)};
		this.menu.onmouseout			= function() {SubmenuIterface.objectHide(oTmpobject)};
	}
	this.menu.style.display			= 'none';
	this.menu.style.position		= 'absolute';
	this.menu.style.zIndex			= 100;// + Submenu.count;
	
	if (iWidth	&& (iWidth > 0))	this.width(iWidth);
	if (iHeight && (iHeight > 0))	this.height(iHeight);
	this.borders('solid', '0', '');
	try {
		if (this.parent) this.parent.insertBefore(this.menu, this.parent.firstChild);
		else document.body.appendChild(this.menu);
	} catch(e) {}
}

Submenu.prototype.setParentLeft = function(iDelta) {
	if (this.parent) {
		var oParentPos = SubmenuIterface.absolutePos(this.parent);
		this.left = oParentPos.x + (iDelta || 0);
	}
}

Submenu.prototype.show			= function()		{
	if (this.left >= 0)	this.menu.style.left = this.left;
	else this.menu.style.left = '';
	if (this.top >= 0)	this.menu.style.top = this.top;
	else this.menu.style.top = '';
	this.menu.style.display = '';
}

Submenu.prototype.addItem		= function(sText)	{
	this.items[this.items.length] = new Item(sText);
	this.menu.appendChild(this.items[this.items.length-1].line);
}

Submenu.prototype.itemUp		= function(iNum)	{
	if (this.items[iNum] && this.items[iNum-1]) {
		var oTemp = this.items[iNum-1].line.cloneNode(true);
		this.menu.removeChild(this.items[iNum-1].line);
		this.items[iNum-1].line = this.items[iNum].line;
		if (this.items[iNum+1]) this.items[iNum].line = this.menu.insertBefore(oTemp, this.items[iNum+1].line);
		else this.items[iNum].line = this.menu.appendChild(oTemp);
	}
}

Submenu.prototype.itemDown		= function(iNum)	{
	if (this.items[iNum] && this.items[iNum+1]) {
		var oTemp = this.items[iNum+1].line.cloneNode(true);
		this.menu.removeChild(this.items[iNum+1].line);
		this.items[iNum+1].line = this.items[iNum].line;
		this.items[iNum].line = this.menu.insertBefore(oTemp, this.items[iNum+1].line);
	}
}

Submenu.prototype.itemUpCount	= function(iNum, iCount) {
	for (var i = iNum; i > iNum-iCount; i--) this.itemUp(i);
}

Submenu.prototype.itemDownCount	= function(iNum, iCount) {
	for (var i = iNum; i < iNum+iCount; i++) this.itemDown(i);
}

Submenu.prototype.hide			= function()		{this.menu.style.display			= 'none';	}

Submenu.prototype.width			= function(sWidth)	{this.menu.style.width				= sWidth;	}
Submenu.prototype.height		= function(sHeight)	{this.menu.style.height				= sHeight;	}
Submenu.prototype.bgColor		= function(sColor)	{this.menu.style.backgroundColor	= sColor;	}
Submenu.prototype.color			= function(sColor)	{this.menu.style.color				= sColor;	}

Submenu.prototype.borders		= function(sStyle, sWidth, sColor) {
	this.border('LEFT',		sStyle, sWidth, sColor);
	this.border('TOP',		sStyle, sWidth, sColor);
	this.border('RIGHT',	sStyle, sWidth, sColor);
	this.border('BOTTOM',	sStyle, sWidth, sColor);
}

Submenu.prototype.border		= function(sBorder, sStyle, sWidth, sColor) {
	SubmenuIterface.borders(this.menu, sBorder, sStyle, sWidth, sColor);
}

Submenu.prototype.paddingLeft	= function(sPad)	{SubmenuIterface.padding(this.menu, 'LEFT',		sPad)}
Submenu.prototype.paddingTop	= function(sPad)	{SubmenuIterface.padding(this.menu, 'TOP',		sPad)}
Submenu.prototype.paddingRight	= function(sPad)	{SubmenuIterface.padding(this.menu, 'RIGHT',	sPad)}
Submenu.prototype.paddingBottom	= function(sPad)	{SubmenuIterface.padding(this.menu, 'BOTTOM',	sPad)}
Submenu.prototype.padding		= function(sPad)	{this.paddingLeft	(sPad);
													 this.paddingRight	(sPad);
													 this.paddingTop	(sPad);
													 this.paddingBottom	(sPad);						}