var mf=0; // Индикатор откр. пункта меню (1 уровень)
var msf=0; // Индикатор откр. пункта меню (2 уровень)
var tID;
var nm=0; // количество элементов
var block,tg,fID,sID;
var ind=0;

function hSet(b,e,f,s,br) {
	tag=document.getElementById(b).getElementsByTagName(e);
	for (i=0;i<tag.length;i++) {
		fChild=tag[i].firstChild; lChild=tag[i].lastChild;
		if (fChild.tagName=="img"||fChild.tagName=="IMG") { tag[i].className="br"; }
		if (fChild.tagName=="a"||fChild.tagName=="A") { ind++; fChild.id="m"+ind;
			if (lChild.tagName=="div"||lChild.tagName=="DIV") lChild.id="ms"+ind;
		}
	}
	init(b,f,"m","ms");
}

function init(b,t,f,s) {
	block=b; tg=t; fID=f; sID=s;
	tag=document.getElementById(block).getElementsByTagName(t);
	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;
	ups=0;
	pw=(navigator.appVersion.indexOf("MSIE 5")>-1)?pw-20:pw;
	if (el.offsetParent) { temp=el;
		while(temp.offsetParent!=fblock&&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-ups)+"px"; smenu.style.top=(y-7)+"px";
}

function sMenu() {
	id=this.id.substring(1);
	if (mf!=id&&mf>0) {
		document.getElementById(fID+mf).className=""; (msf>0)?hhMenu(msf):null; this.className=""; mf=id;
		document.getElementById(sID+mf)?ssMenu(mf):null;
	} else {
		this.className=""; 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=""; 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(2); tID=setTimeout("hhMenu("+id+")",500); }

function hhMenu(id) {
	clearTimeout(tID);
	document.getElementById(fID+id).className="";
	document.getElementById(sID+id).style.visibility="hidden"; msf=0;
}

onresize=function() { init(block,tg,fID,sID) }
