// 	menu.js типа меню и все такое...
//	Взято с http://www.cooking.ru.
//

nn =  (navigator.appName == "Netscape");
ie = (navigator.appName == "Microsoft Internet Explorer");
loader = window;
isLoaded = false;
areCreated = false;
menuLoc = null;

function Test_loaded(){	
	if (menu_loaded && menudata_loaded &&menu_setting_loaded ){
		clearInterval(timerID);
		isLoaded = true;
		startIt;
	}
}
timerID =setInterval("Test_loaded()", 250);
if(nn) {
	origWidth = loader.innerWidth;
	origHeight = loader.innerHeight;
	loader.onresize = reDo;
	NSresized = false;
}


function popUpExt(menuName, event, statusString) {
	if(parseInt(navigator.appVersion) >= 4) {
		if (isLoaded){
		popUp(menuName, event);
		window.status = eval('strMenuName' + menuName.substr(6));
		return true;
		}
	}
}

function popDownExt(menuName) {
	if(parseInt(navigator.appVersion) >= 4) {
		if (isLoaded){
		MyTimer = window.setTimeout("popDown('"+menuName+"')",2200);
//		popDown(menuName);
		window.status = '';
		}
	}
}

function menuSetup(hasParent,openCont,openItem) {
	this.onmouseover = menuOver;
	this.onmouseout = menuOut;
	this.showIt = showIt;
	this.hideTree = hideTree;
	this.hideParents = hideParents;
	this.hideTop = hideTop;
	this.isOn = false;
	this.hideTimer = null;
	this.currentItem = null;
	this.hideSelf = hideSelf;
	this.hasParent = false;
	if(nn) {
		this.bgColor = this.menuBorCol;
		this.fullHeight = this.lastItem.top + this.lastItem.clip.bottom;
		this.clip.right = this.menuWidth+2;
		this.clip.bottom = this.fullHeight;
	} else if(ie) {
		with (this.style) {
			width = this.menuWidth;
			borderWidth = 1;
			borderColor = this.menuBorCol;
			borderStyle = "solid";
		}
		this.lastItem.style.border="";
		this.fullHeight = this.scrollHeight;
		this.showIt(false);
		this.onselectstart = cancelSelect;
		this.moveTo = moveTo;
		this.moveTo(0,0);
	}	
}

function itemSetup(whichItem,whichArray) {
	this.onmouseover = itemOver;
	this.onmouseout = itemOut;
	this.container = (ie) ? this.offsetParent : this.parentLayer;
	arrayPointer = (this.container.hasParent) ? (whichItem-1)*menuitemLength : ((whichItem-1)*menuitemLength);
	this.dispText = whichArray[arrayPointer + menuitemDispTextID];
	this.iconSrc = whichArray[arrayPointer + menuitemIconSrcID];
	this.linkText = whichArray[arrayPointer + menuitemLinkTextID];
	this.hasMore = whichArray[arrayPointer + menuitemHasMoreID];
	this.cat = whichArray[arrayPointer + menuitemCatID];	
	if(ie) {
		if (this.hasMore) {
			this.child = eval("elMenu" + this.id.substr(4));
			this.child.parentMenu = this.container;
			this.child.parentItem = this;
		}
		if (this.linkText) {
				this.onclick = linkIt;
				this.style.cursor = "hand";
		}
		with (this.style) {
			padding = menuitemPadding;
			color = this.container.menuFontColor;
			fontSize = menuitemFontSize;
			fontWeight = "normal";
			fontStyle =	"normal";
			fontFamily = menuitemFontFamily;
			backgroundColor = this.container.menuBGColor;
		}
	} else if(nn) {
		if (this.linkText) {
			this.captureEvents(Event.MOUSEUP)
			this.onmouseup = linkIt;
		}
		htmStr = this.dispText; // (whichItem != whichArray.length/menuitemLength)
		htmStr =	"<font><span style=\"font-family: " + menuitemFontFamily + "; font-size: " + menuitemFontSize + 
						"\">&nbsp;&nbsp;" + 
						htmStr+ makeRight(this.hasMore) + "</span></font><br clear=all><img src=" + poloskaSrc + " height=1 width=" + menuWidth + " hspace=0 vspace=0 valign=bottom align=right>";
		htmStrOver = htmStr.fontcolor(this.container.menuFontOver);
		htmStr = htmStr.fontcolor(this.container.menuFontColor);
		this.htmStr = makeDisp(htmStr,this.hasMore,this.iconSrc);
		this.htmStrOver = (this.hasMore) ? htmStrOver : htmStrOver;		
		this.visibility = "inherit";
		this.bgColor = this.container.menuBGColor;
		if (whichItem == 1) {
			this.top = 1;
		}
		else {
			this.top = this.prevItem.top + this.prevItem.clip.height;
		}
		this.left = 1; // это определяет толщину рамки слева.
//		this.clip.top = this.clip.left = -2;
		this.clip.right = this.container.menuWidth;
		this.txtLyr = new Layer(this.container.menuWidth,this); 
		this.txtLyr.document.write(this.htmStr);
		this.txtLyr.document.close();
		this.txtLyr.visibility = "inherit";
		this.clip.bottom += this.txtLyr.document.height;
		this.dummyLyr = new Layer(this.container.menuWidth,this); // dummyLyr используется для событий вроде бы... Типа, здесь ничего не нарисовано, а если убрать то элементы меню перестанут работать как ссылки.
		this.dummyLyr.left = this.dummyLyr.top = -1;
		this.dummyLyr.clip.width = this.clip.width+2;
		this.dummyLyr.clip.height = this.clip.height+2;
		this.dummyLyr.visibility = "inherit";	
		color = this.parentLayer.parentLayer.bgColor;
		// // alert("RGB(" + parseInt((color/65536)%256) + "," + parseInt((color/256)%256) + "," + parseInt(color%256) + ")");
	}
}

function makeMenu(isChild,menuCount) {
	if(ie)
		makeMenuIE(isChild,menuCount);
	else if(nn)
		makeMenuNS(isChild,menuCount);
}

function makeMenuIE(isChild,menuCount,parMenu) {
	menu = makeElement("elMenu" + menuCount);
	menu.array = eval("arMenu" + menuCount);
	menu.setMenuTree = setMenuTree;
	menu.setMenuTree(isChild,parMenu);
//	if(exandoBarSrc || exandoBarHeight)
//		menu.itemStr = "<img src=" + exandoBarSrc + " width=" + menuWidth + " height=" + exandoBarHeight + " border=0><br clear=all>";
//	else
		menu.itemStr = "";
	while (menu.itemCount < menu.maxItems) {
		menu.itemCount++;
		status = "Building menu, please wait...";
		itemName = "item" + menuCount + "_" + menu.itemCount;
		arrayPointer = ((menu.itemCount-1)*menuitemLength); // 5 - в каждой строке
		iconSrc = menu.array[arrayPointer + menuitemIconSrcID]; // URI иконки если есть...
		dispText = menu.array[arrayPointer + menuitemDispTextID]; // название элемента меню
		hasMore = menu.array[arrayPointer + menuitemHasMoreID]; // hasMore 0 - если нету дочернего меню, иначе дочернее меню есть (не знаю как устанавливать в этом случае)
		menu.itemStr += "<span id=" + itemName + " style=\"height: " + (menuitemHeight-1) + "px; width:" + menuWidth + ((menu.itemCount != menu.array.length/menuitemLength) ? "; border: solid; border-width: 0px 0px 1px 0px; border-color: " + menu.menuBorCol: "") + "\">&nbsp;&nbsp;" + makeDisp(dispText,hasMore,iconSrc) + makeRight(hasMore) + "</span><br clear=all>";
		if (hasMore) {
			makeMenuIE(true,menuCount + "_" + menu.itemCount,menu);
			menu = menu.parentMenu;
		}	
	}

	menu.innerHTML = menu.itemStr;
	itemColl = menu.children.tags("SPAN");
	for (i=0; i<itemColl.length; i++) {
		it = itemColl(i);
		it.setup = itemSetup;
		it.setup(i+1,menu.array);
	}
	menu.lastItem = itemColl(itemColl.length-1);
	menu.setup(isChild,parMenu);
}

function makeMenuNS(isChild,menuCount,parMenu,parItem) {
	tempArray = eval("arMenu" + menuCount);
	tempWidth = menuWidth; // хм...
	menu = makeElement("elMenu" + menuCount,tempWidth,null,null);
	menu.array = tempArray;
	menu.setMenuTree = setMenuTree;
	menu.setMenuTree(isChild,parMenu);
	while (menu.itemCount < menu.maxItems) {
		menu.itemCount++;
		status = "Building Menus, Please Wait...";
		prevItem = (menu.itemCount > 1) ? menu.item : null;
		itemName = "item" + menuCount + "_" + menu.itemCount;
		menu.item = makeElement(itemName,null,null,menu);
		menu.item.prevItem = prevItem;
		menu.item.setup = itemSetup;
		menu.item.setup(menu.itemCount,menu.array);
		if (menu.item.hasMore) {
			makeMenuNS(true,menuCount + "_" + menu.itemCount,menu,menu.item);
			menu = menu.parentMenu;
		}
	}
	menu.lastItem = menu.item;
	menu.setup(isChild,parMenu,parItem);
}

function makeElement(whichEl,whichWidth,whichParent,whichContainer) {
	if(nn) {
		if (whichWidth)
			elWidth = whichWidth;
		else {
			elWidth = (whichContainer) ? whichContainer.menuWidth : whichParent.menuWidth;
			if (whichContainer) elWidth = elWidth-4; // Это, наверное, нужно, для дочерних меню.
		}
		if (!whichContainer)
			whichContainer = menuLoc;
		eval(whichEl + "= new Layer(elWidth,whichContainer)");
		eval(whichEl + ".zIndex = 256;");
	} else if(ie) {
		elStr = "<div id=" + whichEl + " style=\"position:absolute\"></div>";
		menuLoc.document.body.insertAdjacentHTML("BeforeEnd",elStr);
	}
	return eval(whichEl);
}

function makeTop() {
	beingCreated = true;
	while(eval("window.arMenu" + topCount)) {
		makeMenu(false,topCount);
		topCount++
	}
	status = "Done";
	areCreated = true;
	beingCreated = false;
}

function initVars() {
	if(areCreated) {
		for(i=1; i<topCount; i++) {
			cur = eval("elMenu"+i);
			clearTimeout(cur.hideTimer);
			cur.hideTimer=null;
		}
		clearTimeout(allTimer);
	}
	topCount = 1;
	areCreated = false;
	beingCreated = false;
	isOverMenu = false;
	currentMenu = null;
	allTimer = null;
}

initVars();

// нижние две функции не используются! (Почему непонятно...)
function NSunloaded() {
	if(ie) initVars();
	isLoaded = false;
	if(ie) setTimeout("keepTrack()",50)
}

function NSloaded(e){
	if (nn && e.target.name == mainFrName) {
		initVars();
		startIt();
	}
}

function keepTrack() {
	if (menuLoc.document.readyState == "complete") {
		initVars();
		startIt();
	}
	else
		setTimeout("keepTrack()",50);
}

function startIt() {
	isLoaded = true;
	menuLoc = window;
	menuLoc.nav = nav = window;
	makeTop();	
}

function setMenuTree(isChild,parMenu) {
	this.menuWidth = menuWidth; // хм...
	this.menuFontColor = menuFontColor;
	this.menuFontOver = menuFontOver;
	this.menuBGColor = menuBGColor;
	this.menuBGOver = menuBGOver;
	this.menuBorCol = menuBorCol; // цвет рамки меню
	this.treeParent = this;
	this.startChild = this;
	this.maxItems = (this.array.length)/menuitemLength;
	this.hasParent = isChild;
    this.setup = menuSetup;
	this.itemCount = 0;
}

function initVars() {
	if(areCreated) {
		for(i=1; i<topCount; i++) {
			cur = eval("elMenu"+i);
			clearTimeout(cur.hideTimer);
			cur.hideTimer=null;
		}
		clearTimeout(allTimer);
	}
	topCount = 1;
	areCreated = false;
	beingCreated = false;
	isOverMenu = false;
	currentMenu = null;
	allTimer = null;
}

initVars();

function IEunloaded() {
	initVars();
	isLoaded = false;
	setTimeout("keepTrack()",50)
}

function keepTrack() {
	if (menuLoc.document.readyState == "complete") {
		initVars();
		startIt();
	}
	else {
		setTimeout("keepTrack()",50);
	}
}

function startIt() {
	isLoaded = true;
	menuLoc = window;
	menuLoc.nav = nav = window;
	makeTop();	
}

function makeTop(){
	beingCreated = true;
	while(eval("window.arMenu" + topCount)) {
		makeMenu(false,topCount);
		topCount++
	}
	status = "Done";
	areCreated = true;
	beingCreated = false;
}

function popUp(menuName,e){//alert("popUp");
	if (nn && NSresized) startIt();
	if (!isLoaded) return;
	linkEl = (ie) ? event.srcElement: e.target;
	if (!beingCreated && !areCreated) startIt();
	linkEl.menuName = menuName;	
	popMenu(e);
}

function getRealLeft() {    
	xPos = this.offsetLeft;
    tempEl = this.offsetParent;    
	while (tempEl != null) {
        xPos += tempEl.offsetLeft;        
		tempEl = tempEl.offsetParent;    
	}
    return xPos;
}
	
function getTrueX() {
	link1.getRealLeft = getRealLeft;
	trueX = (link1.getRealLeft() + menuitemLeftOffset);
	return trueX;
}

function getRealTop() {    
	yPos = this.offsetTop;    
	tempEl = this.offsetParent;
    while (tempEl != null) {        
		yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
	}
	return yPos;
}

function popMenu(e) {//  alert("popMenu");
	if (!isLoaded || !areCreated) return true;
	eType = (ie) ? event.type : e.type;
	hideAll();
	linkEl = (ie) ? event.srcElement : e.target;
	currentMenu = eval(linkEl.menuName);
	currentMenu.hasParent = false;
	currentMenu.treeParent.startChild = currentMenu;
	if(ie) {
		linkEl.getRealTop = getRealTop;
		menuLocBod = menuLoc.document.body;
		xPos = getTrueX(); 
		yPos = linkEl.getRealTop();
	} else if(nn) {
		//xPos = (document.container.document.container2.pageX + menuitemLeftOffset);  // а было menuitemLeftOffset + 2
		xPos = (menuitemLeftOffset+2);
		yPos = ( document.fixPoint1 ? document.fixPoint1.document.fixPoint2.pageX : fixPointYOffset ) + menuElementHeight*(parseInt(linkEl.menuName.substr(6))-1)+170;
	}
//	alert ("==="+xPos+"x==y"+yPos+"="+fixPointYOffset);
	xPos += popupXOffset;
	yPos += popupYOffset;
	currentMenu.moveTo(xPos,yPos);
	currentMenu.isOn = true;
	currentMenu.showIt(true);
	return false;
}

function menuOver(e) { // alert("menuOver");
	this.isOn = true;
	isOverMenu = true;
	currentMenu = this;
	if (this.hideTimer) clearTimeout(this.hideTimer);
}

function menuOut() { // alert("menuOut");
	if(ie) {
		theEvent = menuLoc.event;
		if (theEvent.srcElement.contains(theEvent.toElement)) return;
	}
	this.isOn = false;
	isOverMenu = false;
	menuLoc.status = "";
	allTimer = setTimeout("currentMenu.hideTree()",50);  
}

function itemOver() { // alert("itemOver");
	if(ie) {
		theEvent = menuLoc.event;
		if (theEvent.srcElement.tagName == "IMG") return;
		this.style.backgroundColor = this.container.menuBGOver;
		this.style.color = this.container.menuFontOver;
	} else if(nn)
		this.bgColor = this.container.menuBGOver;
	this.container.currentItem = this;
}

function itemOut() { // alert("itemOut");
	if(nn) {
		this.bgColor = this.container.menuBGColor;
		if (!isOverMenu) {
			allTimer = setTimeout("currentMenu.hideTree()",50); 
		}
	} else if(ie) {
		theEvent = menuLoc.event;
	   	if (theEvent.srcElement.contains(theEvent.toElement) || (theEvent.fromElement.tagName=="IMG" && theEvent.toElement.contains(theEvent.fromElement)))
	       return;
		this.style.backgroundColor = this.container.menuBGColor;
		this.style.color = this.container.menuFontColor;
	}
}

function moveTo(xPos,yPos) {
	this.style.pixelLeft = xPos;
	this.style.pixelTop = yPos;
}

function showIt(on) {
	if(ie)
		this.style.visibility = (on) ? "visible" : "hidden";
	else if(nn)
		this.visibility = (on) ? "show" : "hide";
	this.currentItem = null;
}

function linkIt() {
//	if(isNumeric(this.linkText))
//		Action(this.linkText, this.cat, true);
//	else
		location.href = this.linkText;
}

function Action(linkText, cat, param) {
	// alert("Action #" + linkText + " for Cat #" + cat + " is " + param);
}

function popDown(menuName) { // alert("popDown");
	if (!isLoaded || !areCreated) return;
	whichEl = eval(menuName);
	whichEl.isOn = false;
	whichEl.hideTop();
}

function hideAll() {
	for(i=1; i<topCount; i++) {
		temp = eval("elMenu" + i + ".startChild");
		temp.isOn = false;
		temp.showIt(false);
	}	
}

function hideTree() { // alert("hideTree");
	allTimer = null;
	if (isOverMenu) return;
	this.hideParents();
}

function hideTop() { // alert("hideTop");
	whichEl = this;
	(this.hideTimer = setTimeout("whichEl.hideSelf()",0));
}

function hideSelf() { // alert("hideSelf");
	this.hideTimer = null;
	if (!this.isOn && !isOverMenu) { 
		this.showIt(false);
	}
}

function hideParents() { // alert("hideParents");
	tempMenu = this;
	while (tempMenu.hasParent) {
		tempMenu.showIt(false);
		tempMenu.parentMenu.isOn = false;		
		tempMenu = tempMenu.parentMenu;
	}
	tempMenu.hideTop();
}

function cancelSelect(){
	return false;
}

function reDo() {
	if (loader.innerWidth==origWidth && loader.innerHeight==origHeight)
		return;
	initVars();
	if(nn) 
		NSresized=true;
	menuLoc.location.reload();
}

function makeDisp(dispText, hasMore, iconSrc) {
	return dispText;
//
//	retVal = dispText;
//	if(iconSrc)
//		retVal += "<img src=" + iconSrc + 
//		" hspace=" + iconHSpace + " vspace=" + iconVSpace + " height=" + iconHeight + " width=" + iconWidth + " align=absright border=0>";
//	return retVal;			
//
}

function makeRight(hasMore) {
	return "<img src=" + (hasMore ? hasMenuSrc : hasnotMenuSrc) + 
	" height=" + haserHeight + " width=" + haserWidth + " hspace=" + haserHSpace + " vspace=" + haserVSpace + " align=right valign=bottom border=0>";
}

menu_loaded=1;
