var to = 0, dzeta = 0;
var scrollTimer;
var actualHeight = 0;
var cn = document.getElementById('content');
var tmp = document.getElementById('temp');
var ie = false;

if (navigator.appName == 'Microsoft Internet Explorer') { ie = true; }

var dragapproved=false
var ev, evobj, x, temp1;
var limit = 290;		//scrollbarheight-dragheight
var nijniylimit = 445;  //do kudi doljen spuskatsya memezok
var minus = 380;

function moveTo(sd)
{
	if (sd >=0 && sd <= limit)
	{
		if (ie) { evobj.style.pixelTop = sd; }
		else { evobj.style.top = sd+'px'; }

		speed = -actualHeight/limit;
		if (ie) { cn.style.pixelTop = sd*speed; }
		else { cn.style.top = sd*speed+'px'; }

		to=sd*speed;
		return false;
	}
}

function move(ev)
{
	ev  = ev || window.event;
	evY = mouseCoords(ev);

	if ( (ev.button == 1 || ev.which == 1) && dragapproved)
	{
		if (evY > nijniylimit)
		{
			n = limit;
			moveTo(n);
		}else
		{
			n = temp1+evY-x;
			moveTo(n);
		}
		return false
	}
}

function drags(ev)
{
	if (ie) { evobj = event.srcElement; }
	else { evobj = ev.target; }

	if (evobj.className == 'drag')
	{
		if (actualHeight<=0) { dragapproved=false; }
		else { dragapproved=true; }

		if (ie) { temp1 = evobj.style.pixelTop; x=mouseCoords(event);}
		else { temp1 = parseInt(evobj.style.top); x=mouseCoords(ev); }

		if (isNaN(temp1))
		{
			temp1 = 0;
		}
		
		document.onmousemove=move;
		return false
	}
}

function mouseCoords(ev){
	if(ev.pageY){ return eval(ev.pageY-380); }
	return eval(ev.clientY + document.documentElement.scrollTop  - document.documentElement.clientTop-380);
}

document.onmousedown=drags;
document.onmouseup=new Function("dragapproved=false");

function Load()
{
	actualHeight=cn.offsetHeight-tmp.offsetHeight+100;  // divheight-otstupsnizu(opredlyayetsya mnoy)
}

function vverx()
{
	if (ie) { to = cn.style.pixelTop; }
	else { to = parseInt(cn.style.top); }

	if (to>-actualHeight) {
		to-=2;
		if (ie) { cn.style.pixelTop = to; }
		else { cn.style.top = to+'px'; }
		scrollTimer=setTimeout("vverx()", 20);

		var speed2 = -limit/actualHeight;
		if (to*speed2 > limit)
		{
			if (ie) { document.images.dgar.style.pixelTop = limit; }
			else { document.images.dgar.style.top = limit+'px'; }
		}else
		{
			if (ie) { document.images.dgar.style.pixelTop = to*speed2; }
			else { document.images.dgar.style.top = to*speed2+'px'; }		
		}
	}
}


function vniz()
{
	if (ie) { to = cn.style.pixelTop; }
	else { to = parseInt(cn.style.top); }

	if (to<0) {
		to+=2;
		if (ie) { cn.style.pixelTop = to; }
		else { cn.style.top = to+'px'; }
		scrollTimer=setTimeout("vniz()", 20);

		var speed2 = -limit/actualHeight;
		if (ie) { document.images.dgar.style.pixelTop = to*speed2; }
		else { document.images.dgar.style.top = to*speed2+'px'; }
	}
}

function stopScroll()
{
	clearTimeout(scrollTimer);
}

window.onload = Load;
