//var Node;
var Div;
var RunnerHeight = 20;
var maxScrollMargin = 495 - RunnerHeight;
var IsScrolling = false;
var maxScrollHeight = 0;
var oldPosY = 0;
var Runner;

function DoScrolling(e)
{
	if (!e || e == undefined)
	{
		e = window.event;
	}
	if (IsScrolling)
	{
		//
		// Изменяем позицию бегунка
		//
		var nY = (e.pageY == undefined) ? e.y : e.pageY;
		var nDelta = nY - oldPosY;
		var currentDivPosY = parseInt(Runner.style.marginTop);
		var newDivPosY = currentDivPosY + nDelta;
		if (newDivPosY >= 0 && newDivPosY <= maxScrollMargin)
		{
			//
			// Изменение прокрутки слоя и положения бегунка
			//
			Runner.style.marginTop = newDivPosY + "px";
			//document.write(Runner.style.marginTop);
			var nDeltaOfScroll = maxScrollMargin / newDivPosY;
			var nDeltaToScroll = Math.floor((Div.scrollHeight - maxScrollMargin - RunnerHeight) / nDeltaOfScroll);
			Div.scrollTop = nDeltaToScroll;
			//Node.innerHTML = nDeltaToScroll;
		}
		if (newDivPosY < 0)
		{
			Runner.style.marginTop = "0px";
			//IsScrolling = false;
		}
		if (newDivPosY > maxScrollMargin)
		{
			Runner.style.marginTop = maxScrollMargin + "px";
			//IsScrolling = false;
		}
		oldPosY = (e.pageY == undefined) ? e.y : e.pageY;
	}
}

//document.onmousemove = DoScrolling;
document.onmouseup = StopScrolling;

function EnableScrolling(e)
{
	if (!e || e == undefined)
	{
		e = window.event;
	}
	if (Div == null)
	{
		Div = document.getElementById("Div1");
		Runner = document.getElementById("Runner");
		maxScrollHeight = Div.scrollHeight - Div.clientHeight;
	}
	IsScrolling = true;
	oldPosY = (e.pageY == undefined) ? e.y : e.pageY;
}

function StopScrolling()
{
	if (IsScrolling)
	{
		IsScrolling = false;
	}
}

function ValidateRunner()
{
	if (Div == null)
	{
		//
		// Убираем бегунок
		//
		Div = document.getElementById("Div1");
		Runner = document.getElementById("Runner");
		if (Div != undefined && Div != null)
		{
			maxScrollHeight = Div.scrollHeight - Div.clientHeight;
			if (maxScrollHeight <= 0)
			{
				var Parent = Runner.parentNode.parentNode.parentNode.parentNode;
				Parent.style.visibility = "hidden";
			}
		}
	}
}

function InitImageViewer(img, title)
{	
	window.open('viewer.init.php?img=' + img + '&title=' + escape(title), 
	'', 'width=640,height=480,resizable=1,left=1,top=1,scrollbars=1');
}

function SetDocumentHeight(bIndex)
{
	var nHeight = 90;
	if (screen.width != undefined && screen.width > 1024)
	{
		if (bIndex)
		{
			if (document.documentElement.clientHeight > document.body.clientHeight)
			{
				nHeight = (document.documentElement.clientHeight - 340) / 2;
			}
			else
			{
				nHeight = (document.body.clientHeight - 340) / 2;
			}
		}
		else
		{
			if (document.documentElement.clientHeight > document.body.clientHeight)
			{
				nHeight = (document.documentElement.clientHeight - 300) / 2;
			}
			else
			{
				nHeight = (document.body.clientHeight - 300) / 2;
			}
		}
		document.getElementById("MainBody").style.height = nHeight + "px";
	}
}	