//Рисуем меню
/////////////
content1 = content2 = '';
key1=key2=0;
GLOBAL_id = null;
GLOBAL_parent = null;
cleanable = false;
setInterval('clearDiv()',100);
bud1 = false;
function get_menu(){
	var i = 0;
	myparent = 0;
	while (menu[i] != null) {
			if (menu[i].parent != 0 ) {
				if (menu[i].parent != myparent ) {
					myparent = menu[i].parent;
					divArray[divArray.length] = new myDiv(menu[i]);
				} else {
					addDiv(menu[i],divArray[divArray.length-1]);
				}
			}
		i++;
	}
	drawDiv();
}
function getALink(id1) {
	var alink;
	var elem;
	alink = getElement('a'+id1);
	if (!alink) {
		elem = getParentDiv(id1);
		if(elem) { 
			if (elem.parent) {
				alink = getElement('a'+elem.parent.id);
			} else {
				alink = getElement('a'+elem.id);
			} 
		}
	}
	if (alink) alink.style.color = '#F69200';
	return null;
}
function removeALink() {
	var alink;
	alink = document.getElementsByName('amenu');
	if (alink) {
		i = 0;
		while (alink.item(i) != null) {
			alink.item(i).style.color = '#000000';
			i++;
		}
	
	}
	return null;
}
function mainIn(id,parent){
	GLOBAL_id = id;
	GLOBAL_parent = (parent != null) ? parent : null;
	if (parent) {
		if (bud1) clearTimeout(bud1);
		mainIn1();
	} else {
		bud1 =  setTimeout('mainIn1()',500);
	}
}

function mainIn1(){
	var parent;
	id = GLOBAL_id ;
	if (GLOBAL_parent == null) parent = null;
	else parent = GLOBAL_parent;
	
	hideAll();
	getALink(id);
	i = 0;
	if (!parent ) {
		while (menu[i] != null) {
			td = getElement('td'+menu[i].id);
			if (td) removeBg(td);
			i++;
		}
		div = getDivOnId(id);
		if (div && div.parent) {
			activateTd(getElement('td'+div.parent.id));
			if (div.parent.parent)
				activateTd(getElement('td'+div.parent.parent.id));
		} 
	}
	else {
		while (menu[i] != null) {
			td = getElement('td'+menu[i].id);
			if (td) removeBg(td);
			i++;
		}
		div = getParentDiv(id);
		activateTd(parent);
		if (div) activateTd(getElement('td'+div.id));
		if (div && div.parent) {
			activateTd(getElement('td'+div.parent.id));
			if (div.parent.parent)
				activateTd(getElement('td'+div.parent.parent.id));
		} 

	}
	td = getElement('td'+id);
	if (td) {
		td.style.cursor = "hand";
		activateTd(td);
	} else {
		if (parent) {
			parent.style.cursor = "hand";
			activateTd(parent);
		}
	}
		if (myElement = getElement('Selector1')) {
		if (key1 !=1) {
			content1 =  myElement.innerHTML;
			key1 = 1;
		}
		myElement.innerHTML = '<img src="/images/select3.gif" border="0">';
		myElement = getElement('Selector2')
		if (key2 !=1) {
			content2 =  myElement.innerHTML;
			key2 = 1;
		}
		
		myElement.innerHTML = '<img src="/images/select4.gif" border="0">';

	}
	//activateTd(tdObj);
	div = getDivOnId(id);
	//if (budilnik) clearTimeout(budilnik);
	if (div) {
		closeActiveLayer(activeLayer,id);
		activeLayer = id;
		showDiv(div);
	}
	return true;
}
function mainOut(id){
	cleanable = true;
	removeALink();
	budilnik = setTimeout('closeAll('+id+');',1500);
	return true;
}

function drawDiv(){
	var i = 0;
	while(divArray[i] != null) {
		content = '<div id="second'+divArray[i].id+'" style="top:0px;left:0px;position:absolute;width:155px;background-color:#AEAEAE;">';
		content += '<table width="100%" cellpadding="3" cellspacing="0" style="" onmouseover="clearBudilnik();" onmouseout="mainOut(\''+divArray[i].id+'\');">';
		//content += '<tr><td height="5"></td></tr>';
		for (j=0 ; j<divArray[i].menuArray.length ; j++){
			if (divArray[i].menuArray[j].href == "#") {
				content += '<tr><td id="td'+divArray[i].menuArray[j].id+'" onmouseover="mainIn(\''+divArray[i].menuArray[j].id+'\');"><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td><div id="main'+divArray[i].menuArray[j].id+'"></div>&nbsp;<a href="#" onclick="return false;" class="subsubmenu">'+divArray[i].menuArray[j].name+'&nbsp;</a></td><td align="right">&raquo;</td></tr></table></td></tr>';
			}
			else {
				content += '<tr><td id="td'+divArray[i].menuArray[j].id+'" name="mytd'+divArray[i].id+'" onmouseover="mainIn(\''+divArray[i].menuArray[j].id+'\',this);clearBudilnik();" onmouseout="removeALink();removeBg(this);" onclick="replaceHref(\''+divArray[i].menuArray[j].href+'\')"><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td><div id="main'+divArray[i].menuArray[j].id+'"></div>&nbsp;<a href="'+divArray[i].menuArray[j].href+'" class="subsubmenu">'+divArray[i].menuArray[j].name+'</a></td></tr></table></td></tr>';
			}
		}
		//content += '<tr><td height="5"></td></tr>';
		content += '</table>';
		content += '</div>';
		d.write(content);
		divArray[i].type = divArray[i].menuArray[j-1].type;
		divArray[i].obj = getElement('second'+divArray[i].id);
		divArray[i].obj.style.visibility ='hidden';
		divArray[i].parent = (divArray[i].id != 0 ) ? getParentDiv(divArray[i].id) : null;
		setZindex(divArray[i]);
		changeContent(divArray[i].obj,content);
		i++;
	}
	
}
function replaceHref(href) {
	document.location.href = href;
}
function closeAll(id){
	i = 0;
	removeALink()
	if (myElement = getElement('Selector1')) {
		myElement.innerHTML = content1;
		myElement = getElement('Selector2');
		myElement.innerHTML = content2;
	}
	div = getDivOnId(id);
	hideDiv(divArray[i]);
	return true;
}
function closeActiveLayer(curentId,id) {
	if (curentId == 0 || getDivOnId(curentId) == null)return '';
	div = getDivOnId(id);
	if (!div) return '';
	if (div.parent && div.parent.id == curentId) {
	}
	else {
		hideDivWithoutParent(getDivOnId(curentId));
	}
	return '';
}

function clearBudilnik()
{
	cleanable = false;
	if (budilnik) clearTimeout(budilnik);
}
function activateTd(tdObj){
	if (tdObj) tdObj.style.backgroundColor='#cccccc';
}
function removeBg(tdObj){
	if (budilnik) clearTimeout(budilnik);
	if (tdObj) tdObj.style.backgroundColor='#AEAEAE';
}
function removeBg1(tdId){
	tdObj = getElement('td'+tdId);
	if (budilnik) clearTimeout(budilnik);
	if (tdObj) tdObj.style.backgroundColor='#AEAEAE';
	//if (tdObj) tdObj.style.backgroundColor='#AEAEAE';
}
function clearDiv(){
	if (cleanable) closeAll(null);
}

