{
	var	fnOnUnload = function ()
	{
		var vel = document.getElementsByTagName ('DIV');

		for (var c = 0; c < vel.length; c++)
			if (vel[c].bIsDybanu || vel[c].oDybanu)
			{
				vel[c].onmouseout = function () {};
				
				var velA = vel[c].getElementsByTagName ('A');
				for (var i = 0; i < velA.length; i++)
					if (velA[i].bIsDybanu)
						velA[i].onfocus = velA[i].onmouseover = function () {}
			}
	};
	
	if (window.attachEvent)
		window.attachEvent ('onunload', fnOnUnload);
	else
		window.addEventListener ('unload', fnOnUnload, false);
}

function setAlpha (oElem, fPct)
{
	oElem.style.filter = 'alpha(opacity=' +
				((oElem.style.MozOpacity =
				  oElem.style.opacity = fPct) * 100) +
			')';
}
function getItID (sid)
{
	if (sid == null)
		return null;
	else if (document.getElementById)
		return document.getElementById (sid);
	else
		return document.all(sid);
}
function toElement (evt) { return evt ? evt.relatedTarget : event ? event.toElement : null; }
function srcElement (evt) { return evt ? evt.target : event ? event.srcElement : null; }
function containsInHierarchy (elem, oElem)
{
 	if (elem.childNodes)
		for (var c = 0; c < elem.childNodes.length; c++)
			if (elem.childNodes[c] == oElem)
				return true;
			else
			if (elem.childNodes[c])
				if (containsInHierarchy (elem.childNodes[c], oElem))
					return true;

	return false;
}

var HORIZONTAL = 0, VERTICAL = 1,
	ANIM_NONE = 0, ANIM_FADE = 1;

function Dybanu (nDir, nAnimation, vItems, sNameSelf, sClassName)
{
	this.m_nDir = nDir;
	this.m_vItems = vItems;
	this.m_nAnimation = nAnimation;
	this.m_sNameSelf = sNameSelf;
	this.nClosingTimer = 0;
	if (sClassName)
		this.sClassName = sClassName;
	else
		this.sClassName = 'dybanu';
}
Dybanu.prototype.initialize = function (sidDIV, velemAWrap)
{
	var c;

 	this.m_oDIV = getItID(sidDIV);
 	this.m_bOpen = true;
	this.m_oDIV.oDybanu = this;
	this.m_oDIV.onmouseout = function (e)
	{
		var el = toElement(e);
		var _this = this;

		clearTimeout(this.oDybanu.nClosingTimer);
		this.oDybanu.nClosingTimer = setTimeout (
			function ()
			{
				_this.oDybanu.nClosingTimer = 0;
				if (el)
					do
						if (el.bIsDybanu || el == _this)
							return;
				     while (el = el.parentNode);

				if (_this.oDybanu.oCurMenu)
					_this.oDybanu.oCurMenu.close();
				_this.oDybanu.oCurMenu = null;
			},
			1000
		);
	}

	for (c = 0; c < velemAWrap.length; c++)
	{
		var oElem = getItID(velemAWrap[c]);

		this.m_vItems[c].wrapAround(oElem, this);
		this.m_vItems[c].initialize (this, oElem);
		this.m_vItems[c].m_oASelf = oElem;
	}

	this.m_nTabIndexing = 0;
	for (c = 0; c < this.m_vItems.length; c++)
		this.m_vItems[c].initialize_tabs ();
}
Dybanu.prototype.stopTimer = function ()
{
	clearInterval (this.m_nTimerID);
	this.m_nTimerID = null;
}
Dybanu.prototype.startTimer = function ()
{
 	if (!this.m_nTimerID)
		this.m_nTimerID = setInterval ('dybanu_tick(' + this.m_sNameSelf + ')', 50);
}
function dybanu_tick (oThis)
{
	var 	bTicking = false, bVal

 	for (var c = 0; c < oThis.m_vItems.length; c++)
 		bTicking |= (bVal = oThis.m_vItems[c].tick());

	if (!bTicking)
		oThis.stopTimer ();
}

/*Dybanu.prototype.onMouseOut =
	elOver = toElement(e);

	if ((elOver == null) ||
		(elOver != this.m_oDIV &&
		!containsInHierarchy (this.m_oDIV, elOver) &&
		this.oCurMenu &&
		this.oCurMenu != elOver.oThisMenu))
	{
		if (this.oCurMenu)
			this.oCurMenu.close ();
		this.oCurMenu = null;
	}
}*/

function MenuItem (sCaption, sImg, sHref, sOnClick, sAccessKey, vItems)
{
	this.m_sCaption = sCaption;
	this.m_sImg = sImg;
	this.m_sHref = sHref;
	this.m_sOnClick = sOnClick;
	this.m_sAccessKey = sAccessKey;
	this.m_vItems = vItems;
	this.m_bOpen = false;
	this.m_bYesClosingNotOpening = false;
	this.m_nClock = 0;
}
MenuItem.prototype.initialize = function (oDybanu, oASelf)
{
	this.m_oDybanu = oDybanu;
	if (this.hasChildren ())
	{
		this.createMe (oASelf, VERTICAL);
		for (var c = 0; c < this.m_vItems.length; c++)
			this.m_vItems[c].initialize (oDybanu, this.m_vItems[c].m_oASelf);
	}
}
MenuItem.prototype.initialize_tabs = function ()
{
	this.m_oASelf.tabIndex = this.m_oDybanu.m_nTabIndexing++;
	if (this.m_vItems)
		for (var c = 0; c < this.m_vItems.length; c++)
			this.m_vItems[c].initialize_tabs();
}
MenuItem.prototype.hasChildren = function () { return this.m_vItems != null; }
MenuItem.prototype.createItem = function (oParentMenu)
{
 	var oInner;

	this.m_oASelf = document.createElement('A');
	this.m_oASelf.bIsDybanu = true;

	if (this.m_sImg)
	{
		oInner = document.createElement('IMG');
		oInner.src = this.m_sImg;
		oInner.border = 0;
	} else if (this.m_sCaption)
		oInner = document.createTextNode (this.m_sCaption);

	this.m_oASelf.appendChild(oInner);
	this.wrapAround (this.m_oASelf, oParentMenu);
    	this.m_oASelf.style.display = 'block';
}

MenuItem.prototype.wrapAround = function (oA, oParentMenu)
{
	if (this.m_sAccessKey)
		oA.accessKey = this.m_sAccessKey;
	if (this.m_sOnClick)
		oA.onclick = this.m_sOnClick;
	if (this.m_sHref)
		oA.href = this.m_sHref;

    this.oParentMenu = oA.oParentMenu = oParentMenu;
    oA.oThisMenu = this;

    oA.style.textDecoration = 'none';
    if (!this.m_sImg)
	    oA.innerHTML = oA.innerHTML.replace (this.m_sAccessKey, '<U>' + this.m_sAccessKey + '</U>');
	else
		oA.childNodes[0].alt = this.m_sCaption;


	oA.onfocus = oA.onmouseover = function ()
	{
		clearTimeout(this.oThisMenu.m_oDybanu.nClosingTimer);
		this.oThisMenu.m_oDybanu.nClosingTimer = 0;

		if (this.oParentMenu.oCurMenu &&
			this.oParentMenu.oCurMenu != this.oThisMenu &&
			this.oParentMenu.oCurMenu.hasChildren())
			this.oParentMenu.oCurMenu.close();

		this.oParentMenu.oCurMenu = this.oThisMenu;
		if (this.oThisMenu.hasChildren() && this.oParentMenu.m_bOpen)
			this.oThisMenu.open ();
	};
}

MenuItem.prototype.createMe = function (oASelf)
{
	var	oDIV = document.createElement ('DIV'), c;

	oDIV.bIsDybanu = true;
	oDIV.style.position = 'absolute';
	oDIV.style.cursor = 'pointer';

	oDIV.className = this.m_oDybanu.sClassName;
	for (c = 0; c < this.m_vItems.length; c++)
	{
		this.m_vItems[c].createItem(this);
		oDIV.appendChild (this.m_vItems[c].m_oASelf);
	}

	oDIV.oThisMenu = this;
	oDIV.oParentMenu = oASelf.parentNode.oThisMenu;
	oDIV.onmouseout = function (evt)
	{
		var el = toElement(evt);
		var _this = this;

		clearTimeout(this.oThisMenu.m_oDybanu.nClosingTimer);
		this.oThisMenu.m_oDybanu.nClosingTimer = setTimeout (
			function ()
			{
				_this.oThisMenu.m_oDybanu.nClosingTimer = 0;

				if (el)
					do
						if (el.bIsDybanu)
							return;
			          while (el = el.parentNode);

				_this.oThisMenu.close();
				if (_this.oParentMenu)
					_this.oParentMenu.m_oDIV.onmouseout(evt);
			},
			1000
		);
	};
	return this.m_oDIV = oDIV;
}

MenuItem.prototype.open = function ()
{
	if (!this.m_bOpen)
	{
		this.m_oDIV.style.left = this.m_oDIV.style.top = '0px';
		document.body.appendChild(this.m_oDIV);
		this.m_bOpen = true;

		var 	nX, nY;

		if (this.m_oASelf.parentNode.style == 'absolute')
		{
			nX = this.m_oASelf.parentNode.offsetLeft;
			nY = this.m_oASelf.parentNode.offsetTop;
		} else
		{
			var p = this.m_oASelf.parentNode;

	        nY = nX = 0;
			do
			{
    				switch (p.nodeName)
				{
     			case 'TR':
				case '#document':
				case 'BASEFONT':
				case 'TBODY':
					break;
				default:
					if (p.offsetLeft)
						nX += p.offsetLeft;
					if (p.offsetTop)
						nY += p.offsetTop;
					break;
				}
			} while (p = p.parentNode);
		}

	   	if (this.oParentMenu instanceof Dybanu)
	   		nWhere = this.oParentMenu.m_nDir;
	   	else
	   		nWhere = VERTICAL;

		switch (nWhere)
		{
		case VERTICAL:
			this.m_oDIV.style.left = nX + this.m_oASelf.parentNode.offsetWidth + 'px';
			this.m_oDIV.style.top = nY + this.m_oASelf.offsetTop + 'px';
			break;
		case HORIZONTAL:
			this.m_oDIV.style.left = nX + this.m_oASelf.offsetLeft + 'px';
			this.m_oDIV.style.top = nY + this.m_oASelf.parentNode.offsetHeight + 'px';
			break;
		}
		this.switchAnimation (false);
	}
}
MenuItem.prototype.onAnimStart = function ()
{
 	setAlpha (this.m_oDIV, this.m_bYesClosingNotOpening ? this.m_nClock / 5.0 : (5 - this.m_nClock) / 5.0);
}
MenuItem.prototype.onAnimDone = function ()
{
 	if (this.m_bYesClosingNotOpening)
		document.body.removeChild(this.m_oDIV);
}
MenuItem.prototype.close = function ()
{
 	if (this.m_bOpen)
 	{
		if (this.oCurMenu)
			this.oCurMenu.close ();

		this.m_bOpen = false;
		this.oCurMenu = null;

		this.switchAnimation (true);
	}
}
MenuItem.prototype.switchAnimation = function(bYesClosingNotOpening)
{
	this.m_bYesClosingNotOpening = bYesClosingNotOpening;
	if (this.m_oDybanu.m_nAnimation == ANIM_NONE)
		this.onAnimDone ();
	else
	{
		this.m_nClock = 5 - this.m_nClock;
		this.onAnimStart ();
		this.m_oDybanu.startTimer ();
	}
}
MenuItem.prototype.tick = function()
{
	var bTicking = false;

	if (this.m_nClock > 0)
	{
		switch (this.m_oDybanu.m_nAnimation)
		{
		case ANIM_NONE: return false;
		case ANIM_FADE:
			setAlpha (this.m_oDIV, (this.m_bYesClosingNotOpening ? this.m_nClock : 5 - this.m_nClock) / 5.0);
			break;
		}

		if (--this.m_nClock)
			bTicking = true;
		else
			this.onAnimDone ();
	}

	if (this.m_vItems)
		for (var c = 0; c < this.m_vItems.length; c++)
			bTicking |= this.m_vItems[c].tick();

	return bTicking;
}

