/*
	ExclusiveSoft Menu
	Version 3.5
	IE & Opera & Mozilla FX compartible
*/

var cImages = 4;
var MENU_BAR = 0;
var MENU_POPUP = 2;
var MENU_HILIGHT = 1;

//var UserAgent = navigator.userAgent.toLowerCase();
var NN4 = document.layers?true:false;
var fVisible = false;				// Popup is shown
var idTimer = null;				// Timer Id
var nameVisible = "";				// Visible popup name
var nPopupTimeout = 0;				
var xPopupMenu = 0, yPopupMenu = 0;
var oParentButton = null;
var oImages;
var strColors;

function MoveTo(obj){
	var oCh = obj.firstChild;
	while( oCh && ( oCh.href == null ) )
		oCh = oCh.nextSibling;
	if( oCh ){
		obj = oCh;
		location.href	= obj.href;
	}
	else{
		oCh = obj.firstChild;
		location.href = oCh.nodeValue;
	}
	return true;
}

function menuWriteBar(nHeight, nWidth, arrMenuBar){
	var i, cItems;
	var strLine;
	var strId;
	var oMenuBarItem;

	cItems = arrMenuBar.length;
	strLine = '<table border="0" cellpadding="0" cellspacing="0"><tr>';
	for(i = 0; i < cItems; i++){
		if( arrMenuBar[i].length != 3 )
			break;
		strId = 'menubaritem'+i.toString();
		strLine+= '<td id="' + strId + '"';
		strLine+= ' height="' + nHeight + '" width="' + nWidth + '"';
		strLine+= ' class="MenuBarItem"';
		strLine+= ' onclick=MoveTo(this);';
		strLine+= ' onmouseover="menuShow('+"'" + arrMenuBar[i][0] +"'"+', this);"';
		strLine+= ' onmouseout="menuReset();">';
		document.writeln(strLine);
		strLine = '<a href="' + arrMenuBar[i][1] + '"';
		strLine+= ' style="text-decoration: none;">';
		strLine+= arrMenuBar[i][2] + '</a>';
		document.writeln(strLine);
		strLine = '</td>';
		document.writeln(strLine);
	}
	strLine = '</tr></table>';
	document.writeln(strLine);
}

function menuWritePopupItem(strLink, strTitle){
	var strLine;
	strLine = '<tr>';
	strLine+= ' <td class="PopupMenuItem"';
	strLine+= ' onclick="MoveTo(this);"';
	strLine+= ' onmouseover="menuDecorateItem(this, MENU_POPUP | MENU_HILIGHT);"';
	strLine+= ' onmouseout="menuDecorateItem(this, MENU_POPUP);">';
	strLine+= ' <a style="text-decoration: none;" href="'+ strLink;
	strLine+= '">' + strTitle + '</a></td></tr>';
	document.writeln(strLine);
}

function menuWritePopup(PopupId, arrPopupMenu){
	var i, cItems;
	var strLine;
	strLine = '<div class="PopupMenu" id="'+ PopupId + '"';
	strLine+= ' onmouseover="KillTimer();" onmouseout="menuReset();">';
	document.writeln(strLine);
	document.writeln('<table cellspacing="1" cellpadding="0">');
	cItems = arrPopupMenu.length;
	for(i = 0; i < cItems; i++){
		if( arrPopupMenu[i].length != 2 )
			break;
		menuWritePopupItem(arrPopupMenu[i][0], arrPopupMenu[i][1]);
	}
	document.writeln('</table></div>');
}

function menuSetAttributes(arrMenuAttr){
	var i;
	if( arrMenuAttr.length != cImages )
		return;
	oImages = new Array(cImages);
	strColors = new Array(cImages);
	for( i=0; i < cImages; i++){
		oImages[i] = new Image();
		oImages[i].src = arrMenuAttr[i][0];
		strColors[i] = arrMenuAttr[i][1];
	}
}

function menuSetProperties(nTimeout)
{
	nPopupTimeout = nTimeout;
}

function menuShow(name, oMenuBarItem)
{
	// Stop timer
	KillTimer(); 

	// Do nothing if parent button not changed, if menu is already activeted
	if( fVisible &&	oParent && (oMenuBarItem == oParent) )
		return true;

	menuDecorateItem(oMenuBarItem, MENU_BAR | MENU_HILIGHT);

	// Hide shown popup
	if( fVisible )
		menuHide(); 

	CalcPopupMenuPos(oMenuBarItem);

	// set offset
	var oPopupMenu = document.getElementById(name);

	oPopupMenu.style.top = yPopupMenu.toString()+'px';
	oPopupMenu.style.left = xPopupMenu.toString()+'px';

//	alert(oPopupMenu.nodeName)

	oPopupMenu.style.visibility =  NN4 ? 'show' : 'visible';

	oParent=oMenuBarItem;
	fVisible=true;
	nameVisible=name;
	return true;
}

function menuHide()
{
	if( idTimer != null )
		KillTimer();

	if( fVisible == false )
		return;

	var oPopupMenu = document.getElementById(nameVisible);
	oPopupMenu.style.visibility =  NN4 ? 'hide' : 'hidden';

	menuDecorateItem(oParent, MENU_BAR);

	fVisible=false;
	nameVisible="";
	return true;
}

function KillTimer()
{
	if( idTimer != null )
	{
		 // если таймер установлен он обнуляется
		clearTimeout(idTimer);
		idTimer=null;
	}
	return true;
}

function menuReset()
{ 
	KillTimer();
	idTimer = setTimeout('menuHide()', nPopupTimeout);
}

function menuSetColor(obj, strAttrValue){
	var oCh = obj.firstChild;
	while( oCh && ( oCh.style == null ) )
		oCh = oCh.nextSibling;
	if( oCh )
   	obj = oCh;
	obj.style.color = strAttrValue;
}

function menuDecorateItem(obj, nType)
{
	obj.style.backgroundImage = 'url('+oImages[nType].src+')';
	menuSetColor(obj, strColors[nType]);
}

// Calculates position of the popup menu
function CalcPopupMenuPos(oMenuBarItem)
{
	var iHeight;
	xPopupMenu = 0;
	yPopupMenu = 0;

	iHeight = oMenuBarItem.offsetHeight;
	while (oMenuBarItem.offsetParent != null) 
	{
			xPopupMenu+= oMenuBarItem.offsetLeft;
			yPopupMenu+= oMenuBarItem.offsetTop;
			oMenuBarItem = oMenuBarItem.offsetParent;
	}
	yPopupMenu+= iHeight;
}
