
var i,j,mX,mY,pX,pY,ns4,ie4,ns6,ns,ver4,tempArr;
//array of layers (headers, main menu)
headLyrs		=new Array('sinod','dokum','sobit','hist','art','schools','sviatini','bogoslov','eparhii');
//head layers description & sizes 
sinod_size		=new Array('СИНОД',70,14);
dokum_size		=new Array('ДОКУМЕНТЫ',104,14);
sobit_size		=new Array('СОБЫТИЯ',90,14);
hist_size		=new Array('ИСТОРИЯ',70,14);
art_size		=new Array('ИСКУССТВА',100,14);
schools_size	=new Array('ШКОЛЫ',70,14);
sviatini_size	=new Array('СВЯТЫНИ',90,14);
bogoslov_size	=new Array('БОГОСЛОВИЕ',104,14);
eparhii_size	=new Array('ЕПАРХИИ',70,14);
//array of layers that contain menu items
baseLyrs		=new Array('sinodbase','dokumbase','sobitbase','histbase','artbase','schoolsbase','sviatinibase','bogoslovbase','eparhiibase');
//set arrays for menu items
//stat_subLyrs	=new Array('ustav','karta','symbol');
sinod_subLyrs	=new Array('members','poslan','deian','zased');
dokum_subLyrs	=new Array('uch','sog','soccon','inoslav','raskol');
sobit_subLyrs	=new Array('glavn','preds','komment');
hist_subLyrs	=new Array('short','ocherk','prBY');
//art_subLyrs		=new Array('iconpis','music','architect','literature');
art_subLyrs		=new Array('architect','music','literature');
schools_subLyrs	=new Array('minda','minds','bogos','mindu','regent');
sviatini_subLyrs=new Array('cross','icons','saints');
bogoslov_subLyrs	=new Array('komissia','ekzarh');
eparhii_subLyrs	=new Array('minsk','vitebsk','pinsk','gomel','turov','grodno','novogrudok','polozk','mogilov','brest');
//menu items sizes & descriptions (title, link, width, height, left, top)
//ustav_size		=new Array('Устав Церкви','#',101,16,2,2);
//karta_size		=new Array('Карта','#',101,16,2,21);
//symbol_size		=new Array('Символика','#',101,16,2,40);
uch_size		=new Array('Учредительные<BR>&nbsp;&nbsp;документы','http://www.church.by/resource/Dir0009/Dir0048/index.html',101,30,2,2);
sog_size		=new Array('Соглашения','http://www.church.by/resource/Dir0009/Dir0015/index.html',101,16,2,35);
soccon_size		=new Array('Социальная<BR>&nbsp;&nbsp;концепция','http://www.church.by/resource/Dir0009/Dir0031/index.html',101,30,2,54);
inoslav_size	=new Array('Инославие','http://www.church.by/resource/Dir0009/Dir0010/index.html',101,16,2,87);
raskol_size		=new Array('Нестроения и<BR>&nbsp;&nbsp;расколы','http://www.church.by/resource/Dir0009/Dir0012/index.html',101,30,2,106);
members_size	=new Array('Члены Синода','http://www.church.by/resource/Dir0205/Dir0213/Dir0224/index.html',101,16,2,2);
poslan_size		=new Array('Послания','http://www.church.by/resource/Dir0205/Dir0209/Page0210.html',101,16,2,21);
deian_size		=new Array('Деяния','http://www.church.by/resource/Dir0205/Dir0206/index.html',101,16,2,40);
zased_size		=new Array('Заседания','http://www.church.by/resource/Dir0205/Dir0207/Page0208.html',101,16,2,59);
glavn_size		=new Array('Главные<BR>&nbsp;&nbsp;события','http://www.church.by/resource/Dir0301/Dir0302/Page0306.html',101,30,2,2);
preds_size		=new Array('Предстоящие<BR>&nbsp;&nbsp;события','http://www.church.by/resource/Dir0301/Dir0372/Page0380.html',101,30,2,35);
komment_size		=new Array('Комментарии','http://www.church.by/resource/Dir0301/Dir0367/index.html',101,16,2,68);
short_size		=new Array('Краткая справка','http://www.church.by/resource/Dir0176/Dir0194/index.html',101,16,2,2);
ocherk_size		=new Array('Исторический<BR>&nbsp;&nbsp;очерк','http://www.church.by/resource/Dir0176/Dir0177/index.html',101,30,2,21);
prBY_size		=new Array('Православие на<BR>&nbsp;&nbsp;Белой Руси','http://www.church.by/resource/Dir0176/Dir0200/index.html',101,30,2,54);
//iconpis_size	=new Array('Иконопись','#',101,16,2,2);
//music_size		=new Array('Музыка','/искусства/Музыка/Православная музыкально-певческая культура.txt',101,16,2,21);
//architect_size	=new Array('Архитектура','/искусства/Архитектура/Проект духовно-образовательного центра.txt',101,16,2,40);
//literature_size	=new Array('Литература','/искусства/Литература/Православная Церковь в Польше и Литве.txt',101,16,2,59);
music_size		=new Array('Музыка','http://www.church.by/resource/Dir0151/Dir0162/Page0164.html',101,16,2,21);
architect_size	=new Array('Архитектура','http://www.church.by/resource/Dir0151/Dir0152/index.html',101,16,2,2);
literature_size	=new Array('Литература','http://www.church.by/resource/Dir0151/Dir0154/Page0158.html',101,16,2,40);
minda_size		=new Array('Минская духовная<BR>&nbsp;&nbsp;Академия','http://www.church.by/resource/Dir0382/Page0392.html',116,30,2,2);
minds_size		=new Array('Минская духовная<BR>&nbsp;&nbsp;Семинария','http://www.church.by/resource/Dir0382/index.html',116,30,2,35);
bogos_size		=new Array('Институт<BR>&nbsp;&nbsp;теологии БГУ','http://www.church.by/resource/Dir0382/Page0388.html',116,30,2,68);
mindu_size		=new Array('Минское духовное<BR>&nbsp;&nbsp;училище','http://www.church.by/resource/Dir0382/Page0393.html',116,30,2,101);
regent_size		=new Array('Регентская школа','http://www.church.by/resource/Dir0382/Page0404.html',116,16,2,134);
cross_size		=new Array('Полоцкий крест','http://www.church.by/resource/Dir0262/Dir0294/index.html',116,16,2,2);
icons_size		=new Array('Иконы<BR>&nbsp;&nbsp;Божией Матери','http://www.church.by/resource/Dir0262/Dir0291/index.html',116,30,2,21);
saints_size		=new Array('Белорусские<BR>&nbsp;&nbsp;Святые','http://www.church.by/resource/Dir0262/Dir0263/index.html',116,30,2,54);
komissia_size		=new Array('Синодальная<BR>&nbsp;&nbsp;комиссия','http://www.church.by/resource/Dir0000/Dir0007/index.html',101,30,2,2);
ekzarh_size		=new Array('Архив<BR>&nbsp;&nbsp;председателя','http://www.church.by/resource/Dir0000/Dir0001/index.html',101,30,2,35);
minsk_size		=new Array('Минская','http://www.church.by/resource/Dir0056/Dir0083/index.html',101,16,2,2);
vitebsk_size		=new Array('Витебская','http://www.church.by/resource/Dir0056/Dir0067/index.html',101,16,2,21);
pinsk_size		=new Array('Пинская','http://www.church.by/resource/Dir0056/Dir0132/index.html',101,16,2,40);
gomel_size		=new Array('Гомельская','http://www.church.by/resource/Dir0056/Dir0072/index.html',101,16,2,59);
turov_size		=new Array('Туровская','http://www.church.by/resource/Dir0056/Dir0145/index.html',101,16,2,78);
grodno_size		=new Array('Гродненская','http://www.church.by/resource/Dir0056/Dir0078/index.html',101,16,2,97);
novogrudok_size		=new Array('Новогрудская','http://www.church.by/resource/Dir0056/Dir0123/index.html',101,16,2,116);
polozk_size		=new Array('Полоцкая','http://www.church.by/resource/Dir0056/Dir0140/index.html',101,16,2,135);
mogilov_size		=new Array('Могилевская','http://www.church.by/resource/Dir0056/Dir0118/index.html',101,16,2,154);
brest_size		=new Array('Брестская','http://www.church.by/resource/Dir0056/Dir0058/index.html',101,16,2,173);
function init()
{
	if(document.layers)
	{
		ns4=true;
		layerRef="document.layers['";
		styleSwitch="']";
		shortCut="']";
		leftRef=".left";
		topRef=".top";
		screenWidth=innerWidth;
		minWidth=796;
		ns4LyrOffset=8;
	}
	else if(document.all)
	{
		ie4=true;
		layerRef="document.all('";
		styleSwitch="').style";
		shortCut="')";
		leftRef=".pixelLeft";
		topRef=".pixelTop";
		screenWidth=document.body.clientWidth;
		minWidth=780;
		ns4LyrOffset=0;
	}
	else if(document.getElementById)
	{
		ns6=true;
		layerRef="document.getElementById('";
		styleSwitch="').style";
		shortCut="')";
		leftRef=".left";
		topRef=".top";
		screenWidth=innerWidth;
		minWidth=792;
		ns4LyrOffset=7;
	}
	else 
	{
		//other
		bName="ot";
		// что будем делать если браузер не поддерживает слои?
		//eval('self.location.href = "'+self.location.href.substring(0, self.location.href.indexOf(".html"))+'_nondhtml.html"');
	}
	
	ver4 = (ns4 || ie4 || ns6) ? 1 : 0;
	if(ver4)
	{
		//build layers
		
		//build main navigation layers
		var mainMenu=makeLyr('mainmenu');
		var spacer = '<TD width=1 bgcolor=#9CAEBD><IMG src="pics/trans.gif" width=1 height=1></TD>\n'
		var htmlCode = '<TABLE border=0 cellspacing=0 cellpadding=0 height=14>\n' 
		+ '<TR align=center>\n' 
		+ spacer;
		for(i in headLyrs)
		{
			tempArr=eval(headLyrs[i]+'_size');
			
			if(ns4)
			{
				htmlCode += '<TD width=' + tempArr[1] + ' class=topnav><LAYER name=' + headLyrs[i] + ' width=' + tempArr[1] + ' height=' + tempArr[2] + ' class=padd-al><A href="#" class=topnav>' + tempArr[0] + '</A></LAYER></TD>\n'  
				+ spacer;
			}
			else
			{
				htmlCode += '<TD width=' + tempArr[1] + ' class=topnav><DIV id=' + headLyrs[i] + ' style="position:absolute; width:' +tempArr[1] + 'px; height:' + tempArr[2] + 'px"><A href="#" class=topnav>' + tempArr[0] + '</A></DIV></TD>\n' 
				+ spacer;
			}
		}
			 
		htmlCode += '</TR>\n'  
		+ '</TABLE>\n'; 
		if(ns4)
		{
			mainMenu.document.open();
			mainMenu.document.write(htmlCode);
			mainMenu.document.close();
		}
		else
		{
			mainMenu.innerHTML=htmlCode;
		}
		//build menu items layers
		for(i in headLyrs)
		{
			var baseLyr=makeLyr(headLyrs[i]+'base');
			var width=0, height=0;
			tempArr=eval(headLyrs[i]+'_subLyrs');
			
			htmlCode = '<TABLE border=0 cellspacing=1 cellpadding=0 bgcolor=#21416B>\n';
			
			for(j in tempArr)
			{
				var subArr=eval(tempArr[j]+'_size');
				
				if(ns4)
				{
					htmlCode += '<TR><TD width=' + (subArr[2]+2) + ' height=' + (subArr[3]+2) + ' bgcolor=#ADC7E7 class=subnav>&nbsp;<LAYER name=' + tempArr[j] + ' width=' + subArr[2] + ' height=' + subArr[3] + ' left=' +subArr[4] + ' top=' +subArr[5] +' class=padd>&nbsp;&nbsp;<A href="' + subArr[1] + '" class=subnav>' + subArr[0] + '</A></LAYER></TD></TR>\n';
				}
				else
				{
//					htmlCode += '<TR><TD height=' + (subArr[3]+2) + ' bgcolor=#ADC7E7 class=subnav>&nbsp;<DIV id=' + tempArr[j] + ' style="position:absolute; width:' + subArr[2] + 'px; height:' + subArr[3] + 'px; left:' + subArr[4] + 'px; top:' +subArr[5] +'px; padding-bottom:2px">&nbsp;&nbsp;<A href="' + subArr[1] + '" class=subnav>' + subArr[0] + '</A></DIV></TD></TR>\n';
					htmlCode += '<TR><TD width=' + (subArr[2]+2) + ' height=' + (subArr[3]+2) + ' bgcolor=#ADC7E7 class=subnav>&nbsp;<DIV id=' + tempArr[j] + ' style="width:' + subArr[2] + 'px; height:' + subArr[3] + 'px;">&nbsp;&nbsp;<A href="' + subArr[1] + '" class=subnav>' + subArr[0] + '</A></DIV></TD></TR>\n';
					width = subArr[2];
					height += subArr[3];
				}
			}
			htmlCode += '</TABLE>\n';
			//code complete, write it in			
			if(ns4 && baseLyr)
			{
				baseLyr.document.open();
				baseLyr.document.write(htmlCode);
				baseLyr.document.close();
			}
			else if(baseLyr)
			{
				baseLyr.innerHTML=htmlCode;
			}
			//set sublayers coordinates & sizes for ns6 & ie4
			if(ns6 || ie4)
			{
				for(j in tempArr)
				{
					var subArr=eval(tempArr[j]+'_size');
					var tempLyr=makeLyr(tempArr[j]);
					setXCoord(tempLyr,subArr[4]);
					setYCoord(tempLyr,subArr[5]);
					
					ResLyr(tempLyr,subArr[2],subArr[3]);
				}
			}
			//set baseLyr sizes
			ResLyr(baseLyr,width+4,height+tempArr.length*4-(tempArr.length-1));					
		}
			
		// capture events for main menu (head) layers
		for(i in headLyrs)
		{
			var headLyr=nestLyr('mainmenu',headLyrs[i]);
			if(ns4) headLyr.document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
			headLyr.onmouseover = handleHeadLyrOn;
			headLyr.onmouseout  = handleHeadLyrOff;
			// capture events for all menu items (sublyrs) layers
			tempArr=eval(headLyrs[i]+'_subLyrs');
			var baseLyr=headLyrs[i]+'base';
			for(j in tempArr)
			{
				var subLyr=nestLyr(baseLyr,tempArr[j]);
				if(ns4) subLyr.document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
				subLyr.onmouseover = chColor;
				subLyr.onmouseout  = chColor;
			}
		}		
		//capture events for all base layers (containers for menu items)
		for(i in baseLyrs)
		{
			var baseLyr=makeLyr(baseLyrs[i]);
			if(ns4) baseLyr.document.captureEvents(Event.MOUSEOUT);
			baseLyr.onmouseout = checkXY;
			if(ns4) baseLyr.document.releaseEvents(Event.MOUSEOUT);
		}
	
		// set bgcolor for navigation layers for netscape
		for(i in baseLyrs)
		{
			if(ns4)
			{
				document.layers[baseLyrs[i]].bgColor="#21416B";
			}
			else if(ns6)
			{
				document.getElementById(baseLyrs[i]).style.backgroundColor="#21416B";
			}
		}
		//define page inner width 
		pageWidth=(screenWidth>=minWidth)?screenWidth:minWidth;
		//set mainmenu layer coordinates
		setXCoord(mainMenu,pageWidth/2-389-ns4LyrOffset);
		setYCoord(mainMenu,103);
	
		//set base layer coordinates
		setXCoord(makeLyr('sinodbase'),pageWidth/2-389-ns4LyrOffset);
		setXCoord(makeLyr('dokumbase'),pageWidth/2-318-ns4LyrOffset);
		setXCoord(makeLyr('sobitbase'),pageWidth/2-213-ns4LyrOffset);
		setXCoord(makeLyr('histbase'),pageWidth/2-122-ns4LyrOffset);
		setXCoord(makeLyr('artbase'),pageWidth/2-53-ns4LyrOffset);
		setXCoord(makeLyr('schoolsbase'),pageWidth/2+2-ns4LyrOffset);
		setXCoord(makeLyr('sviatinibase'),pageWidth/2+93-ns4LyrOffset);
		setXCoord(makeLyr('bogoslovbase'),pageWidth/2+213-ns4LyrOffset);
		setXCoord(makeLyr('eparhiibase'),pageWidth/2+284-ns4LyrOffset);
		for(i in baseLyrs)
		{
			setYCoord(makeLyr(baseLyrs[i]),124);
		}
		//set head layer coordinates
		//these layers are inside mainmenu layer -- beware!
		setXCoord(nestLyr('mainmenu','sinod'),1)
		setXCoord(nestLyr('mainmenu','dokum'),72);
		setXCoord(nestLyr('mainmenu','sobit'),177);
		setXCoord(nestLyr('mainmenu','hist'),268);
		setXCoord(nestLyr('mainmenu','art'),339);
		setXCoord(nestLyr('mainmenu','schools'),440);
		setXCoord(nestLyr('mainmenu','sviatini'),511);
		setXCoord(nestLyr('mainmenu','bogoslov'),602);
		setXCoord(nestLyr('mainmenu','eparhii'),707);
								
		for(i in headLyrs)
		{
			setYCoord(nestLyr('mainmenu',headLyrs[i]),0);
		}	
		
		Show(makeLyr('mainmenu'));
		
	}
}
function ResLyr(lyr,width,height) 
{
	if(!lyr) return;
	if(ns4) lyr.width=width, lyr.height=height;
	else if(ns6) lyr.style.width=width, lyr.style.height=height;
	else lyr.style.pixelWidth=width, lyr.style.pixelHeight=height;
}
function checkXY(e)
{
	//receives layer object
	
	if(!ns4)
	{
		var eObj=(ns6) ? e : event;
		var XOffset=(ns6) ? window.pageXOffset : document.body.scrollLeft;
		var YOffset=(ns6) ? window.pageYOffset : document.body.scrollTop;
		if(ie4 || ns6)
		{
			mX=eObj.clientX + XOffset;
			mY=eObj.clientY + YOffset;
			var top		= (ns6) ? parseInt(this.style.top) : this.style.pixelTop;
			var bottom	= (ns6) ? parseInt(this.style.top) + parseInt(this.style.height) : this.style.pixelTop + this.style.pixelHeight;
			var left	= (ns6) ? parseInt(this.style.left) : this.style.pixelLeft;
			var right	= (ns6) ? parseInt(this.style.left) + parseInt(this.style.width) : this.style.pixelLeft + this.style.pixelWidth;
		
			//if inside base layer area -> don't hide
			if(	mX >= left + 2 &&
				mX <= right &&
				mY >= top && 
				mY <= bottom)
		
			return;
		}
	}
	
	Hide(this);
}
function Dummy() {}
function handleHeadLyrOn(e)
{
	//receives layer object
	var triggerName=(ns4)? this.name : this.id;
	
	//hide all base layers shown
	for(i in baseLyrs)
	{
		if(triggerName+'base' != baseLyrs[i]) Hide(makeLyr(baseLyrs[i]));
	}
	//change head layer color and show current base layer	
	if(ns4) this.bgColor='#5A7194';
	else this.style.backgroundColor='#5A7194';
	var toShowLyr=makeLyr(triggerName+'base');
	if(toShowLyr) Show(toShowLyr);
}
function handleHeadLyrOff(e)
{
	//receives layer object
	if(ns4) this.bgColor='#21416B';
	else this.style.backgroundColor='#21416B';
}
function Hide(lyr)
{
	//receives layer object
	if(ns4) lyr.visibility='hidden';
	else lyr.style.visibility='hidden';
	
}
function Show(lyr)
{
	//receives layer object
	if(ns4) lyr.visibility='visible';
	else lyr.style.visibility='visible';	
}
function chColor(e)
{
	//receives layer object
	var eObj=(ns4 || ns6) ? e : event;
	if(eObj.type=='mouseover') var bgclr='#7392BD', clr='#FFFFFF';
	else if(eObj.type=='mouseout') var bgclr='#ADC7E7', clr='#21416B';
	if(ns4) this.bgColor=bgclr;
	else this.style.backgroundColor=bgclr;
}
function nestLyr(parentLyrName,childLyrName)
{
	//receives layer names, returns layer object
	if(ns4) return document.layers[parentLyrName].document.layers[childLyrName];
	else return eval(layerRef+childLyrName+shortCut);
	
}
function setXCoord(lyr,pX)
{
	//receives layer object
	if(ns4) lyr.left=pX;
	else if(ie4) lyr.style.pixelLeft=pX;
	else if(ns6) lyr.style.left=pX;
}
function setYCoord(lyr,pY)
{
	//receives layer object
	if(ns4) lyr.top=pY;
	else if(ie4) lyr.style.pixelTop=pY;
	else if(ns6) lyr.style.top=pY;
}
function makeLyr(lyrName)
{
	//receives layer name, returns object
	return eval(layerRef+lyrName+shortCut);
}
