//
// WEBADMIN	Система управления web сайтом.
//
// Модуль:	menu.js
//
// Версия:	1.1
//
// Авторы:	(c) TWB Group, 2002
//
// История:
//
//	1.0,	15.06.2002
//		Написание модуля.
//
//	1.1,	21.07.2002
//		Добавление подсветки всех родительских пунктов меню.
//		Увеличение времени, перед тем как исчезнет всплывшее меню до 180мс, чтобы
//		избежать моргание, когда возвращаеш курсор от всплывшего к родительскому пункту.
//
//
//	Этот модуль описывает работу всплывающего меню. В том числе содержит функции для
//	генерирования.
//
//

var g_MenuHeight = "25px";

//--------------------------------------------------------------------------------------

var pObjs = new Array();
var pStatus = new Array();
var cObjs = 0;
var bTimer = 0;

var killTimer;

//--------------------------------------------------------------------------------------

var g_idMenu;
var g_objParent;

function showMenu(idMenu, objParent)
{
	var obj = getDiv(idMenu);
	if(bTimer) {
		g_idMenu = idMenu;
		g_objParent = objParent;
		setTimeout("showMenu(g_idMenu, g_objParent)", 20);
		return false;
	}
	if(typeof(obj) != "object") return false;
	
	var posX, posY;
	posX = 0;
	posY = 0;
	if(typeof(objParent) == "object") {
		var objElement = objParent;
		while(objElement.tag != 0) {
			posX += objElement.offsetLeft;
			posY += objElement.offsetTop;
			objElement = objElement.offsetParent;
		}
		if (objParent.offsetParent.offsetParent.scrollTop) posY -= objParent.offsetParent.offsetParent.scrollTop;
		if(objParent.titled == "yes") posY += parseInt(objParent.style.height)+2;
		else 
			if(objParent.clientWidth != "") {
				if(getDiv(objParent.ltag)) posX += parseInt(getDiv(objParent.ltag).clientWidth)-2;
				else posX += parseInt(objParent.clientWidth);
			}
			else {
				posX += obj.x;
			}
	}
	
	if(posX + parseInt(obj.clientWidth) > document.body.clientWidth) {
		posX = document.body.clientWidth - parseInt(obj.clientWidth);
		if(posX < 0) posX = 0;
		posY += 15;
	}
	if(posY + parseInt(obj.clientHeight) > document.body.clientHeight + document.body.scrollTop) {
		posY = document.body.clientHeight + document.body.scrollTop - parseInt(obj.clientHeight);
		if(posY < 0) posY = 0;
	}
	setPosition(obj, posX, posY);

	regMenu(obj);
}

function hideMenu(idMenu)
{
	var obj = getDiv(idMenu);
	if(typeof(obj) == "object") {
		setPosition(obj, -2000, -2000);
		unregMenu(obj);
	}
}

//--------------------------------------------------------------------------------------

function setPosition(obj, posX, posY)
{
	if(typeof(obj) != "object") return false;

	obj.style.left = posX;
	obj.style.top = posY;
}

//--------------------------------------------------------------------------------------

function getDiv(idMenu)
{
	return document.all["div"+idMenu];
}

//--------------------------------------------------------------------------------------

function regMenu(objMenu)
{
	if(typeof(objMenu) == "object") {
		for(i = 0; i < cObjs; i++) 
			if(pObjs[i].tag == objMenu.tag) return false;
		
		pObjs[cObjs] = objMenu;
		pStatus[cObjs] = 0;
		cObjs++;
	}
}

function unregMenu(objMenu)
{
	if(pObjs[cObjs-1].tag == objMenu.tag) cObjs--;
}

function setStatus(obj, intStatus)
{
	for(i = 0; i < cObjs; i++) {
		if(pObjs[i].tag == obj.tag) {
			pStatus[i] = intStatus;
			break;
		}
	}
}

//--------------------------------------------------------------------------------------

function lookMenu()
{
	for(i = cObjs-1; i >= 0; i--) {
		if(pStatus[i] == 0) hideMenu(pObjs[i].tag);
		if(pStatus[i] == 1) break;
	}
	bTimer = 0;
	
	if(cObjs == 0 && !bItemStatus) unregItem(0);
}

//--------------------------------------------------------------------------------------

var pItems = new Array(), cItems = 0, bItemStatus = 0;

function regItem(obj)
{
	var nDeep = 0;
	if(typeof(obj) == "object") {
		if(obj.ltag == 0) nDeep = 0;
		else
			for(i = 0; i < cObjs; i++) {
				if(pObjs[i].tag == obj.ltag) { nDeep = i+1; break; }
			}
		if(cItems > nDeep+1) for(i = cItems-1; i > nDeep; i--) {
			pItems[i].style.background="#B74F4D"; 
		}
		if(typeof(pItems[nDeep]) == "object") {
			if (pItems[nDeep].className=='menuitem') {
				tr=pItems[nDeep].parentElement;
				td1=tr.children(0);
				td1.style.background="";
				td2=tr.children(1);
				td2.style.background="";
				td2.style.color="#62000C";
			} else if (pItems[nDeep].className=='tdsubmenu') {
				tr=pItems[nDeep].parentElement;
				td1=tr.children(1);
				td1.style.background="";
				td2=tr.children(2);
				td2.style.background="";
				td2.style.color="#FFFFFF";
			} else {
				pItems[nDeep].style.background="#B74F4D"; 
			}
			
		}
		pItems[nDeep] = obj;
		if (pItems[nDeep].className=='menuitem') {
				tr=pItems[nDeep].parentElement;
				td1=tr.children(0);
				td1.style.background="#980012";
				td2=tr.children(1);
				td2.style.background="#980012";
				td2.style.color="#FFFFFF";
		} else if (pItems[nDeep].className=='tdsubmenu') {
				tr=pItems[nDeep].parentElement;
				td1=tr.children(1);
				td1.style.background="#CD6462";
				td2=tr.children(2);
				td2.style.background="#CD6462";
				td2.style.color="#FFFFFF";
		} else {
			pItems[nDeep].style.background="#CD6462";
		}
		cItems = nDeep+1;
		bItemStatus = 1;
	}
}

function unregItem(nMustExist)
{
	for(i = cItems-1; i >= nMustExist; i--) 
	{
		if (pItems[i].className=='menuitem') {
			tr=pItems[i].parentElement;
			td1=tr.children(0);
			td1.style.background="";
			td2=tr.children(1);
			td2.style.background="";
			td2.style.color="#62000C";	
		} else if (pItems[i].className=='tdsubmenu') {
			tr=pItems[i].parentElement;
			td1=tr.children(1);
			td1.style.background="";
			td2=tr.children(2);
			td2.style.background="";
			td2.style.color="#FFFFFF";
		} else {
			pItems[i].style.background="#B74F4D"; 
		}
	}
	cItems = nMustExist;
}

//--------------------------------------------------------------------------------------

var g_obj;

function tdOver(obj)
{
	if(typeof(obj) != "object") return false;
	showMenu(obj.tag, obj);
	regItem(obj);
}

function tdOut(obj)
{
	if(typeof(obj) != "object") return false;

	bItemStatus = 0;
	if(bTimer) {
		g_obj = obj;
		setTimeout("tdOut(g_obj)", 30);
		return false;
	}
	
	clearTimeout(killTimer);
	killTimer = setTimeout("lookMenu()", 30);
	bTimer = 1;
}

var g_objDiv;

function divOut(obj)
{
	if(typeof(obj) != "object") return false;

	setStatus(obj, 0);
	clearTimeout(killTimer);
	killTimer = setTimeout("lookMenu()", 30);
	bTimer = 1;
}

function divOver(obj)
{
	if(typeof(obj) != "object") return false;
	
	setStatus(obj, 1);
}

//--------------------------------------------------------------------------------------

function MoveTo(strURL)
{
	location.href = strURL;
}

//--------------------------------------------------------------------------------------

var lastPopupMenu = 0, cItem = 0;
var strResult =  '';

function spm(idMenu)
{
	strResult += "<DIV id='div"+idMenu+"' tag='"+idMenu+"' onmouseout='divOut(this)'"+
		" onmouseover='divOver(this)' style='width: "+(g_PopupMenuWidth+23)+"px;' class='popupmenu'>"+
		"<TABLE cellpadding='0' cellspacing='0' border='0' bgcolor='#FFF6DC'>";
		// Добавка в 26 пикселей происходит из-за стиля: отступы и границы

	cItem = 0;
	lastPopupMenu = idMenu;
}

function epm()
{
	strResult += "</TABLE></DIV>";
	document.write(strResult);
	strResult = '';

	setPosition(getDiv(lastPopupMenu), -2000, -2000);
	if(cItem > 17) {
		document.all["div"+lastPopupMenu].style.height = "200px";
		document.all["div"+lastPopupMenu].style.overflowY = "scroll";
		document.all["div"+lastPopupMenu].style.scrollbarBaseColor="#B74F4D";
		document.all["div"+lastPopupMenu].style.scrollbarArrowColor="#FFFFFF";		
	} else {
		document.all["div"+lastPopupMenu].style.height = cItem * parseInt(g_MenuHeight) + 2;
	}
	lastPopupMenu = 0;
}

function ami(id, strText, strURL, nextid)
{
	strResult += "<TR><TD nowrap tag='"+id+"' ltag='"+lastPopupMenu+"' onmouseout='tdOut(this);' onmouseover='tdOver(this);' "+
		"class='floatmenuitem' style='width: "+(g_PopupMenuWidth+20)+"px; height: "+g_MenuHeight+"; padding-left:10px; padding-right:2px;'";
	
	if(strURL != "") strResult += " onclick='MoveTo(\""+strURL+"\")'";
	strResult += ">"+strText;
	if (nextid) strResult +=  "<img src='/images/fc.gif' align=right width=3 height=3>";
	strResult += "</TD></TR><TR><TD background='images/point_separate_h.gif' height='1'><img src='/images/s.gif' width=1 height=1></TD></TR>";
	cItem++;
}



function addTitleMenuItem(id, strText, strURL)
{
	strResult += "<DIV tag='"+id+"' titled='yes' onmouseout='tdOut(this)' onmouseover='tdOver(this)' "+
		"class='itemoff' style='width: "+g_MenuWidth+"; height: "+g_MenuHeight+"'";
	
	if(strURL != "") strResult += " onclick='MoveTo(\""+strURL+"\")'";
	strResult += ">"+strText+"</DIV>";
}

function menu_on(x) {
tr=x.parentElement;
td1=tr.children(0);
td1.style.background="#980012";
td2=tr.children(1);
td2.style.background="#980012";
a=td2.children(0);
a.style.color="#FFFFFF";
}

function menu_off(x) {
tr=x.parentElement;
td1=tr.children(0);
td1.style.background="#D8C293";
td2=tr.children(1);
td2.style.background="#D8C293";
a=td2.children(0);
a.style.color="#62000C";
}

function submenu_on(x) {
x.style.background="#CD6462";
}

function submenu_off(x) {
x.style.background="#B74F4D";
}
