var mf=0;		// Индикатор откр. пункта меню (1 уровень)
var msf=0;	// Индикатор откр. пункта меню (2 уровень)
var tID;
var nm=0;		// количество элементов
var block,tg,fID,sID;

function init(b,t,f,s) {
	block=b;	// блок меню
	tg=t;				// пункт меню
	fID=f;			// ID меню 1-го уровня
	sID=s;			// ID меню 2-го уровня
	tag=document.getElementById(block).getElementsByTagName(tg);
	for (i=0;i<tag.length;i++) {
		if (tag[i].id.indexOf(fID)!=-1) {
			tag[i].onmouseover=sMenu; tag[i].onmouseout=hMenu; id=tag[i].id.substring(1);
			if (document.getElementById(sID+id)) {
				sPos(block,tag[i],document.getElementById(sID+id));
				document.getElementById(sID+id).onmouseover=clTime;
				document.getElementById(sID+id).onmouseout=hsMenu;
			}
		}
	}
}

function sPos(block,fmenu,smenu) {
	x=y=fx=0; var el,temp; el=fmenu.offsetParent; fblock=document.getElementById(block); pw=document.body.offsetWidth;
	pw=(navigator.appVersion.indexOf("MSIE 5")>-1)?pw-20:pw;
	if (el.offsetParent) { temp=el;
		while(temp.offsetParent) { temp=temp.offsetParent; x+=temp.offsetLeft; y+=temp.offsetTop; }
	}
	x+=el.offsetLeft; y+=el.offsetTop+el.offsetHeight;
	if (fblock.offsetParent) { temp=fblock;
		while(temp.offsetParent) { temp=temp.offsetParent; fx+=temp.offsetLeft; }
	}
	fx+=fblock.offsetLeft;
	if ((fx+x+smenu.offsetWidth)>pw) { x=x-smenu.offsetWidth+el.offsetWidth; }
	smenu.style.left=x+"px"; smenu.style.top=25+"px";
}

function sMenu() {
	id=this.id.substring(1);
	if (mf!=id&&mf>0) {
		document.getElementById(fID+mf).className="off"; (msf>0)?hhMenu(msf):null; this.className="on"; mf=id;
		document.getElementById(sID+mf)?ssMenu(mf):null;
	} else { this.className="on"; mf=id; document.getElementById(sID+mf)?ssMenu(mf):null; }
}

function hMenu() {
	id=this.id.substring(1);
	if (id==msf) { tID=setTimeout("hhMenu("+id+")",2000); } else { clearTimeout(tID); this.className="off"; mf=0; }
}

function clTime() { clearTimeout(tID); }

function ssMenu(id) { clearTimeout(tID); document.getElementById(sID+id).style.visibility="visible"; msf=id; }

function hsMenu() { id=this.id.substring(1); tID=setTimeout("hhMenu("+id+")",500); }

function hhMenu(id) {
	clearTimeout(tID);
	document.getElementById(fID+id).className="off";
	document.getElementById(sID+id).style.visibility="hidden"; msf=0;
}

onresize=function() { init(block,tg,fID,sID) }
