﻿var scrollUp = false;
var scrollDown = false;

var oldY = null;
var gScrollDiv = null;
var gContentDiv = null;

function MouseOver(dir, id, sId)
{
	if (dir == 0 && scrollDown == false)
	{
		scrollDown = true;
		setTimeout('ScrollDown("' + id + '", "' + sId + '")', 10);
	}
	else if (dir == 1 && scrollUp == false)
	{
		scrollUp = true;
		setTimeout('ScrollUp("' + id + '", "' + sId + '")', 10);
	}
}

function MouseOut(dir, id, sId)
{
	var div = document.getElementById(id);
	if (dir == 0)
		scrollDown = false;
	else if (dir == 1)
		scrollUp = false;
}

function ScrollUp(id, sId)
{
	var div = document.getElementById(id);
	var sBar = document.getElementById(sId);
	
	if (parseInt(div.offsetTop) >= 0)
		scrollUp = false;
	
	if (scrollUp)
	{
		div.style.top = (parseInt(div.offsetTop) + 1) + "px";
		var sPos = Math.abs((div.offsetTop / div.offsetHeight) * 100);
		sPos = Math.round(( sPos / 100) * sBar.parentNode.offsetHeight);
		
		sBar.style.top = sPos + "px";
		setTimeout('ScrollUp("' + id + '", "' + sId + '")', 10);
	}	
}

function ScrollDown(id, sId)
{
	var div = document.getElementById(id);
	var sBar = document.getElementById(sId);
	
	var pos = parseInt(div.offsetTop) + parseInt(div.offsetHeight);
	if (pos <= parseInt(div.parentNode.offsetHeight))
		scrollDown = false;
		
	if (scrollDown)
	{
		div.style.top = (parseInt(div.offsetTop) - 1) + "px";
		var sPos = Math.abs((div.offsetTop / div.offsetHeight) * 100);
		sPos = Math.round(( sPos / 100) * sBar.parentNode.offsetHeight);
		
		sBar.style.top = sPos + "px";
		setTimeout('ScrollDown("' + id + '", "' + sId + '")', 10);
	}
}

function CalcScrollBar(contentId, scrollId)
{
	var contentDiv = document.getElementById(contentId);
	var scrollDiv = document.getElementById(scrollId);
	var contentParent = contentDiv.parentNode;
	var scrollParent = scrollDiv.parentNode;
	gContentDiv = contentDiv;
	
	scrollDiv.onmousedown = onMouseDown;
	document.onmouseup = onMouseUp;
	//scrollDiv.onmouseout = onMouseUp;
	
	var percent = Math.round((contentParent.offsetHeight / contentDiv.offsetHeight) * 100);
	
	if (percent < 100)
	{
		scrollDiv.style.height = Math.round((percent / 100) * scrollParent.offsetHeight) + "px";
	}
	else
	{
		scrollDiv.style.display = 'none';
	}
}

function onMouseDown(evt)
{
	if (!evt) var evt = window.event;
	gScrollDiv = evt.srcElement || evt.originalTarget;
	document.onmousemove = DragMouse;
}

function onMouseUp(evt)
{
	if (!evt) var evt = window.event;
	oldY = null;
	document.onmousemove = null;
}

function DragMouse(evt)
{
	if (!evt) var evt = window.event;
		
	if (oldY == null)
		oldY = evt.screenY;
	else
	{
		var inc = evt.screenY - oldY;
		if (inc > 0)
		{
			if ((gScrollDiv.offsetTop + gScrollDiv.offsetHeight) < gScrollDiv.parentNode.offsetHeight)
			{
				var newY = gScrollDiv.offsetTop + inc;
				
				if ((newY + gScrollDiv.offsetHeight) <= gScrollDiv.parentNode.offsetHeight)
					gScrollDiv.style.top = newY + "px";
				else
				
					gScrollDiv.style.top = (gScrollDiv.parentNode.offsetHeight - gScrollDiv.offsetHeight) + "px";
					
				var percent = (gScrollDiv.offsetTop / gScrollDiv.parentNode.offsetHeight) * 100;
				gContentDiv.style.top = -Math.round((percent / 100) * gContentDiv.offsetHeight) + "px";
			}
		}
		else
		{
			if (gScrollDiv.offsetTop > 0)
			{
				var newY = gScrollDiv.offsetTop + inc;
				
				if (newY >= 0)
					gScrollDiv.style.top = newY + "px";
				else
					gScrollDiv.style.top = "0px";
					
				var percent = (gScrollDiv.offsetTop / gScrollDiv.parentNode.offsetHeight) * 100;
				gContentDiv.style.top = -Math.round((percent / 100) * gContentDiv.offsetHeight) + "px";
			}
		}
		oldY = evt.screenY;
	}
	
	if (document.selection)
		document.selection.empty();
	else if (window.getSelection)
		window.getSelection().removeAllRanges();
}