var isDOM = (document.getElementById ? true : false); 
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNN4 = (document.layers ? true : false);
// переменная для установки текущего меню
var activeMenu;
function setActiveMenu(index){
activeMenu=index;
}
function updateMenu(){
for (count = 0; count < menu.length; count++)
menu[count][0].ref.visibility = 'hidden';
writeMenus();
}
function getRef(id) {
if (isDOM) return document.getElementById(id);
if (isIE4) return document.all[id];
if (isNN4) return document.layers[id];
}
function getSty(id) {
return (isNN4 ? getRef(id) : getRef(id).style);
} 
// время скрытия меню
var popTimer = 0;
function popOver(menuNum, itemNum) {
clearTimeout(popTimer);
hideAllBut(menuNum);
targetNum = menu[menuNum][itemNum].target;
if (targetNum > 0) {
thisX = parseInt(menu[menuNum][0].ref.left) + parseInt(menu[menuNum][itemNum].ref.left);
thisY = parseInt(menu[menuNum][0].ref.top) + parseInt(menu[menuNum][itemNum].ref.top);
with (menu[targetNum][0].ref) {
left = parseInt(thisX + menu[targetNum][0].x);
top = parseInt(thisY + menu[targetNum][0].y);
visibility = 'visible';
      }
   }
}
function popOut(menuNum, itemNum) {
if ((menuNum == activeMenu) && !menu[menuNum][itemNum].target)
hideAllBut(activeMenu)
else
popTimer = setTimeout('hideAllBut(activeMenu)', 100);
}
function hideAllBut(menuNum) {
for (count = 0; count < menu.length; count++)
menu[count][0].ref.visibility = 'hidden';
menu[activeMenu][0].ref.visibility = 'visible';
menu[menuNum][0].ref.visibility = 'visible';
}
// *** КОНСТРУКТОР МЕНЮ ***
function Menu(x, y, width, bkGrnd) {
// позиция и размеры элемента меню
this.x = x;
this.y = y;
this.width = width;
// 1 - использовать битмэп для фона; 0 - черный фон
this.bkGrnd =bkGrnd;
// родительское меню и кол-во его элементов (зарезервированно)
this.parentMenu = null;
this.parentItem = null;
// указатель на стиль объекта (зарезервированно)
this.ref = null;
}
function Item(text, href, frame, length, spacing, target) {
this.text = text;
this.href = href;
this.frame = frame;
this.length = length;
this.spacing = spacing;
this.target = target;
// указатель на стиль объекта (зарезервированно)
this.ref = null;
}
function writeMenus() {
if (!isDOM && !isIE4 && !isNN4) return;
for (currMenu = 0; currMenu < menu.length; currMenu++) with (menu[currMenu][0]) {
// переменные для генерации свойств HTML-блока и позиции след. элемента меню
var str = '', itemX = 0, itemY = 0;
for (currItem = 1; currItem < menu[currMenu].length; currItem++) with (menu[currMenu][currItem]) {
var itemID = 'menu' + currMenu + 'item' + currItem;
var w =width;
var h =length;
// генерация свойств DIV-слоя 
if (isDOM || isIE4) {
str += '<div id="' + itemID + '" style="position: absolute; left: ' + itemX + '; top: ' + itemY + '; width: ' + w + '; height: ' + h + '; visibility: inherit;" ';
}
if (isNN4) {
str += '<layer id="' + itemID + '" left="' + itemX + '" top="' + itemY + '" width="' + w + '" height="' + h + '" visibility="inherit"; ';
}
// добавления обработчика mouseover и завершение DIV-тега
str += 'onMouseOver="popOver(' + currMenu + ',' + currItem + ')" onMouseOut="popOut(' + currMenu + ',' + currItem + ')">';
// проверка: нужен ли фон меню или подчеркивание элементов?
// 0 - вывод меню черного цвета с синими границами элементов
// 1 - вывод меню с фоном 
// 2 - вывод меню без фона
if (bkGrnd == 0){
	str += '<table bgcolor="#000000" width="' + w + '"border="0" cellspacing="1" cellpadding="4"' + '"><tr><td  bgcolor="#FFFFFF"  height="' + h + '" align="right">' + '<a class="menulinks"' + 'href="' + href + '"' + (frame ? ' target="' + frame + '">' : '>') + text + '</a></td>';
}
if (bkGrnd == 1){
	str += '<table background="menufon.jpg" width="' + w + '"border="0" cellspacing="0" cellpadding="4"' + '"><tr><td height="' + h + '" align="right">' + '<a class="menulinks"' + 'href="' + href + '"' + (frame ? ' target="' + frame + '">' : '>') + text + '</a></td>';
}
if (bkGrnd == 2){
	str += '<table width="' + w + '"border="0" cellspacing="0" cellpadding="4"' + '"><tr><td height="' + h + '" align="right">' + '<a class="menulinks"' + 'href="' + href + '"' + (frame ? ' target="' + frame + '">' : '>') + text + '</a></td>';
}

if (target > 0) {
menu[target][0].parentMenu = currMenu;
menu[target][0].parentItem = currItem;
}
str += '</tr></table>' + (isNN4 ? '</layer>' : '</div>');
itemY += length + spacing;
}
// вставка DIV-тега с меню в конец тега BODY
// *** для тестирования ***
//if (isDOM) {
//var newDiv = document.createElement('div');
//document.getElementsByTagName('body').item(0).appendChild(newDiv);
//newDiv.innerHTML = str;
//ref = newDiv.style;
//ref.position = 'absolute';
//ref.visibility = 'hidden';
//}
if (isDOM || isIE4) {
document.body.insertAdjacentHTML('beforeEnd', '<div id="menu' + currMenu + 'div" ' + 'style="position: absolute; left:0px; top:0px; visibility: hidden">' + str + '</div>');
ref = getSty('menu' + currMenu + 'div');
}
if (isNN4) {
ref = new Layer(0);
ref.document.write(str);
ref.document.close();
}

for (currItem = 1; currItem < menu[currMenu].length; currItem++) {
itemName = 'menu' + currMenu + 'item' + currItem;
if (isDOM || isIE4) menu[currMenu][currItem].ref = getSty(itemName);
if (isNN4) menu[currMenu][currItem].ref = ref.document[itemName];
   }
}
with(menu[activeMenu][0]) {
ref.left = x;
ref.top = y;
ref.visibility = 'visible';
   }
}
// *** ОПРЕДЕЛЕНИЕ СВОЙСТВ МЕНЮ ***
var menu = new Array();
// главное меню
menu[0] = new Array();
// Menu(<координата X>, <координата Y>, <ширина>, <есть ли фон?>);
menu[0][0] = new Menu( 2, 123, 170, 2);
// Item(<надпись ссылки>, <адрес ссылки>, <указание фрейма (если есть)>, <высота элемента 
// меню>, <пространстов м/д элементами>, <индекс вложенного меню(если 0 - нет таких), т.е. 
// указатель массива menu[индекс] - это вложенное меню>);
menu[0][1] = new Item('главная &nbsp;', 'index.htm', '', 20, 8, 0);
menu[0][2] = new Item('фильмы &nbsp;', '#', '', 20, 4, 1);
menu[0][3] = new Item('биография &nbsp;', 'Bio.htm', '', 20, 7, 0);
menu[0][4] = new Item('тексты &nbsp;', 'Text.htm', '', 20, 2, 0);
menu[0][5] = new Item('форум &nbsp;', 'http://castalia.ru/viewforum.php?f=9&sid=eb87e5c9562b77312707fc194e592d8c', '', 20, 2, 0);
menu[0][6] = new Item('связь &nbsp;', 'mailto:pallet@pochta.ru?subject=Письмо администрации', '', 20, 2, 0);
// подменю фильмов
menu[1] = new Array();
menu[1][0] = new Menu(170, 0, 170, 1);
menu[1][1] = new Item('Фандо и Лис', 'FandoLis.htm', '', 20, 1, 0);
menu[1][2] = new Item('Крот',  'ElTopo.htm', '', 20, 1, 0);
menu[1][3] = new Item('Святая гора',  'Holy.htm', '', 20, 1, 0);
menu[1][4] = new Item('Бивень',  'Biv.htm', '', 20, 1, 0);
menu[1][5] = new Item('Святая кровь',  'Sangre.htm', '', 20, 1, 0);
menu[1][6] = new Item('Вор радуги',  'Thief.htm', '', 20, 1, 0);

// *** ОПТИМИЗАЦИЯ ПОД NN4 ***
if (isNN4) document.captureEvents(Event.CLICK);
document.onclick = clickHandle;

function clickHandle(evt)
{
 if (isNN4) document.routeEvent(evt);
 hideAllBut(activeMenu);
}

