function InstallMenu()
{
	var menu=document.getElementById("menutable");
	var hover=document.getElementById("menu");
	var tds=menu.getElementsByTagName("TD");
	for(var i=tds.length; i--;)tds[i].onmouseover=Over2;
	hover.onmouseout=new Function("this.style.display='none'");
/*
	var menu=document.getElementById("menutable");
	var tds=menu.getElementsByTagName("TD");
	for(var i=tds.length; i--;)
	{
//		tds[i].onmouseover=Over;
		tds[i].onmousemove=Over;
	}
	var hover=document.getElementById("menu");
	hover.onmouseout=new Function("this.style.display='none'");
	hover.onmousemove=new Function("if(showed){showed=false; setTimeout('Hide()', 10)}");
	menu.onmouseout=new Function("if(timeout)clearTimeout(timeout);");
//*/
}
function Hide()
{
	if(!showed)
	{
		var hover=document.getElementById("menu");
		hover.style.display='none';
	}
}
var showed=false;
function SetItem(obj)
{
	try
	{
		showed=true;
		var hover=document.getElementById("menu");
		var span=obj.getElementsByTagName("SPAN")[0];
		var a=obj.getElementsByTagName("a")[0];
		hover.innerHTML='<a href="'+a.href+'">'+span.innerHTML+'</a>';
	}
	catch (e){}
}
var curobjct=false, timeout=false, firstover=0;
function Over2()
{
	var hover=document.getElementById("menu");
	var table=document.getElementById("menutable");
	hover.style.width=table.offsetWidth;
	SetItem(this);
	hover.style.display="block";
}
function Over()
{
	if(firstover!=2)firstover++;
	else
	{
		if(this.innerHTML!='')
		{
			curobjct=this;
			if(timeout)clearTimeout(timeout);
			timeout=setTimeout("Show()", 250);
		}
	}
}
function Show()
{
	timeout=false;

	var hover=document.getElementById("menu");
	var table=document.getElementById("menutable");
	hover.style.width=table.offsetWidth;
	SetItem(curobjct);
	hover.style.display="block";
}
var picpos=0;
function AddPhoto()
{
	var cont=document.getElementById("gallery");
	if(Defined(cont))
	{
		var pics=cont.getElementsByTagName("SPAN");
		if(pics.length>picpos)
		{
			var pic=pics[picpos];
			var cont=pic.parentNode;
			var arr=pic.title.split("preview2/");
			cont.innerHTML+='<a href="javascript:OpenPicture(\''+arr[0]+arr[1]+'\')"><img alt="'+(Defined(pic.alt)?pic.alt:'')+'" src="'+pic.title+'" height="146" border="0" onload="AddPhoto()" onmouseover="ShowTitle()" onmouseout="HideTitle()" '+(picpos?'style="margin-left:10px"':'')+'></a>';
			picpos++;
		}
		InstallScrollBars();
	}
}

var animation=false;
var fps=100;
function StartScrollAnimation(begin, end)
{
	animation=true;
	var time=200, frms=Math.ceil(time/1000*fps), delta=(end-begin)/frms;
	for(var i=0, tmout=0; i<frms; i++, tmout+=1000/fps, begin+=delta)setTimeout('document.getElementById("gallery").scrollLeft='+begin, tmout);
	setTimeout('animation=false; document.getElementById("gallery").scrollLeft='+end+'; InstallScrollBars();', tmout);
}
function DoScroll(direct)
{
	if(!animation)
	{
		var el=document.getElementById("gallery");
		var imags=el.getElementsByTagName("IMG");
		for(var i=0, width=0; i<imags.length&&width<el.scrollLeft; i++)width+=imags[i].width+10;

		var curimg=direct<0?i-1:i;
		if(curimg>=0&&curimg<imags.length)
		{
			var delt=imags[curimg].width;
			StartScrollAnimation(el.scrollLeft, el.scrollLeft+(delt+10)*direct);
		}
	}
}
function ScrollDown()
{
	DoScroll(1);
}
function ScrollUp()
{
	DoScroll(-1);
}
function InstallScrollBars()
{
	var el=document.getElementById("gallery");
	if(Defined(el))
	{
		if(el.scrollLeft)document.getElementById("scrollup").style.visibility="visible";
		else document.getElementById("scrollup").style.visibility="hidden";

		if(el.scrollWidth>el.offsetWidth&&(el.scrollLeft+el.offsetWidth!=el.scrollWidth))document.getElementById("scrolldown").style.visibility="visible";
		else document.getElementById("scrolldown").style.visibility="hidden";
	}
}
function SetGalleryContainer()
{
	var cont=document.getElementById("gallery");
	if(Defined(cont))
	{
		cont.style.display="none";
		cont.style.width=cont.parentNode.offsetWidth;
		cont.style.display="block";
		InstallScrollBars();
	}
}
function ShowScroll(obj)
{
	obj.style.overflowY="scroll";
	obj.style.overflowX="hidden";
	obj.style.overflow="auto";
}
function HideScroll(obj)
{
	obj.style.overflowY="hidden";
	obj.style.overflowX="hidden";
	obj.style.overflow="hidden";
}
function ShowScroll1()
{
	if(this.scrollHeight>parseInt(this.style.height))
	{
		this.style.paddingRight=0;
		ShowScroll(this);
	}
}
function HideScroll1()
{
	HideScroll(this);
	this.style.paddingRight=17;
}
function ShowScroll2()
{
	if(this.scrollHeight>parseInt(this.style.height))
	{
		this.style.paddingLeft=0;
		ShowScroll(this);
	}
}
function HideScroll2()
{
	HideScroll(this);
	if(!isOpera)this.style.paddingLeft=17;
}
function SetForumContainer()
{
	var cont=document.getElementById("forum");
	if(Defined(cont))
	{
		cont.style.display="none";
		var height=cont.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.offsetHeight;
		cont.style.height=height-263;
		cont.style.display="block";
		cont.onmouseover=ShowScroll1;
		cont.onmouseout=HideScroll1;
	}
}
function SetNewsContainer()
{
	var cont=document.getElementById("news");
	cont.style.display="none";
	var height=cont.parentNode.parentNode.offsetHeight;
	if(height<670)height=670;
	cont.style.height=height-519;
	cont.style.display="block";
	cont.onmouseover=ShowScroll2;
	cont.onmouseout=HideScroll2;
	if(isOpera)cont.style.paddingLeft=0;
}
function ShowTitle()
{
	if(event.srcElement.alt!='')
	{
		var hover=document.getElementById("menu");
		var table=document.getElementById("menutable");
		hover.style.width=table.offsetWidth;
		hover.innerHTML='<a href="#">'+event.srcElement.alt+'</a>';
		hover.style.display="block";
	}
}
function HideTitle()
{
	var hover=document.getElementById("menu");
	hover.style.display="none";
}
function SetImageTitles()
{
	var imgs=document.body.getElementsByTagName("IMG");
	for(var i=imgs.length; i--;)if(imgs[i].alt!='')
	{
		imgs[i].attachEvent('onmouseover', ShowTitle);
		imgs[i].attachEvent('onmouseout', HideTitle);
	}
}
function SetTitle()
{
	previewwindow.document.title=oldevent.alt;
}
var oldevent;
function SetPreviewTitles()
{
	var as=document.body.getElementsByTagName("A");
	for(var i=as.length; i--;)if(as[i].href.indexOf('javascript:OpenPicture(')!=-1)
	{
		as[i].onclick=new Function('oldevent=event.srcElement; setTimeout("SetTitle()", 10);');
	}
}
function Load()
{
	AddPhoto();
	setTimeout("AddPhoto();", 200);
	SetGalleryContainer();
	SetNewsContainer();
	SetForumContainer();
	SetImageTitles();
	SetPreviewTitles();
}
function Resize()
{
	SetGalleryContainer();
	SetNewsContainer();
	SetForumContainer();
}
function CorrectPosition()
{
	var frame=document.getElementById("frame");
	var img=document.getElementById("preview");
	if(img.width<150)
	{
		img.parentNode.innerHTML='<img src="'+img.src+'" border="'+img.border+'" width="150" id="preview">';
		img=document.getElementById("preview");
		var height=img.height;
		img.parentNode.innerHTML='<img src="'+img.src+'" border="'+img.border+'" height="'+height+'" id="preview">';
		img=document.getElementById("preview");
		ApplyEvents(img);
	}
	if(img.height<150)img.height=150;
	if(parseInt(frame.style.marginLeft)<14)frame.style.marginLeft=14;
	if(parseInt(frame.style.marginTop)<14)frame.style.marginTop=14;
	if(parseInt(frame.style.marginLeft)>img.width-150+14)frame.style.marginLeft=img.width-150+14;
	if(parseInt(frame.style.marginTop)>img.height-150+14)frame.style.marginTop=img.height-150+14;
}
function MoveFrame()
{
	var frame=document.getElementById("frame");
	frame.style.marginLeft=event.offsetX+14-150/2;
	frame.style.marginTop=event.offsetY+14-150/2;
	CorrectPosition();
}
function ResizePreview()
{
	var img=document.getElementById("preview");
	var delta=event.wheelDelta>0?1:-1;
	delta*=event.shiftKey?1:20;
	img.height+=delta;
	CorrectPosition();
}
function SendImage()
{
	var img=document.getElementById("preview");
	var frame=document.getElementById("frame");
	var form=document.FormName;
	form.width.value=img.width;
	form.height.value=img.height;
	form.left.value=parseInt(frame.style.marginLeft)-14;
	form.top.value=parseInt(frame.style.marginTop)-14;
	frame.style.display="none";
	img.parentNode.style.display="none";
	form.submit();
}
function ApplyEvents(img)
{
	img.onmouseover=MoveFrame;
	img.onmousemove=MoveFrame;
	img.onclick=SendImage;
	img.onmousewheel=new Function('ResizePreview(); return false;');
	img.style.cursor="hand";
	img.id="preview";
}
function PreparePhoto2()
{
	var maxwidth=1000, maxheight=1000;
	var img=event.srcElement;
	if(img.width>maxwidth||img.height>maxheight)
	{
		alert("Размер фотографии не должен привышать "+maxwidth+" точек по ширине и "+maxheight+" точек по высоте!");
		img.parentNode.style.display="block";
		img.parentNode.innerHTML='';
		document.FormName.reset();
	}
	else
	{
		img.height="400";
		ApplyEvents(img);
		var frame=document.getElementById("frame");
		setTimeout('document.getElementById("photo").style.display="block";', 5);
		frame.style.display="block";
	}
}
function PreparePhoto()
{
	var cont=document.getElementById("photo");
	var src=event.srcElement.value;
	cont.innerHTML='<img src="file:///'+src+'" border="14" onload="PreparePhoto2()">';
	setTimeout('document.getElementById("photo").style.display="none";', 2);
}
if(isOpera)
{
	document.getElementById("correction").innerHTML='#news { padding-left:0px; }';
}