var rch=0;
var top=0;
var tmp=0;
var qwe=0;
var myTop=0;
var myTopCoaching=0;
var myTopComent=0;
var myTopAnsoknings=0;
var flag=0;
var flag_coaching=0;
var flag_ansoknings=0;
var flag_coment=0;


function gettop(id)
{
	height=window.document.getElementById(id).offsetHeight;
	width=window.document.getElementById(id).offsetWidth;
	posit=window.document.getElementById(id).style.position;

    window.document.getElementById(id).style.position="absolute";

    var top = window.document.getElementById(id).offsetTop;

	window.document.getElementById(id).style.position=posit;
	window.document.getElementById(id).style.width = width + "px";
	window.document.getElementById(id).style.height = height + "px";

	return top;
}

function move(event,myTop,id)
{
	if(flag==1)
    {
        window.document.getElementById('scroller_'+id).style.top = (event.clientY - myTop)+ "px";
		if(gettop('scroller_'+id)<0) window.document.getElementById('scroller_'+id).style.top = 0 + "px";
		if(gettop('scroller_'+id) + window.document.getElementById('scroller_'+id).offsetHeight> window.document.getElementById('scrollbox_'+id).offsetHeight)
        	window.document.getElementById('scroller_'+id).style.top = window.document.getElementById('scrollbox_'+id).offsetHeight -window.document.getElementById('scroller_'+id).offsetHeight + "px";

        rch = window.document.getElementById('scrollbox_'+id).offsetHeight - window.document.getElementById('scroller_'+id).offsetHeight;
        window.document.getElementById('scroll_'+id).scrollTop=(qwe/rch)*gettop('scroller_'+id);
        
        
    }

}

function move_coaching(event,myTopCoaching)
{
    var id = 'coaching';
	if(flag_coaching==1)
    {
        window.document.getElementById('scroller_'+id).style.top = (event.clientY - myTopCoaching)+ "px";
		if(gettop('scroller_'+id)<0) window.document.getElementById('scroller_'+id).style.top = 0 + "px";
		if(gettop('scroller_'+id) + window.document.getElementById('scroller_'+id).offsetHeight> window.document.getElementById('scrollbox_'+id).offsetHeight)
        	window.document.getElementById('scroller_'+id).style.top = window.document.getElementById('scrollbox_'+id).offsetHeight -window.document.getElementById('scroller_'+id).offsetHeight + "px";

        rch = window.document.getElementById('scrollbox_'+id).offsetHeight - window.document.getElementById('scroller_'+id).offsetHeight;
        window.document.getElementById('scroll_'+id).scrollTop=(qwe/rch)*gettop('scroller_'+id);
        
//        window.document.getElementById('scroller_'+id).style.height = '18px';
//        window.document.getElementById('scroller_'+id).style.width = '10px';
    }

}

function move_ansoknings(event,myTopAnsoknings)
{
    var id = 'ansoknings';
	if(flag_ansoknings==1)
    {
        window.document.getElementById('scroller_'+id).style.top = (event.clientY - myTopAnsoknings)+ "px";
		if(gettop('scroller_'+id)<0) window.document.getElementById('scroller_'+id).style.top = 0 + "px";
		if(gettop('scroller_'+id) + window.document.getElementById('scroller_'+id).offsetHeight> window.document.getElementById('scrollbox_'+id).offsetHeight)
        	window.document.getElementById('scroller_'+id).style.top = window.document.getElementById('scrollbox_'+id).offsetHeight -window.document.getElementById('scroller_'+id).offsetHeight + "px";

        rch = window.document.getElementById('scrollbox_'+id).offsetHeight - window.document.getElementById('scroller_'+id).offsetHeight;
        window.document.getElementById('scroll_'+id).scrollTop=(qwe/rch)*gettop('scroller_'+id);
        
//        window.document.getElementById('scroller_'+id).style.height = '18px';
//        window.document.getElementById('scroller_'+id).style.width = '10px';
    }

}

function move_coment(event,myTopComent)
{
    var id = 'coment';
	if(flag_coment==1)
    {
        window.document.getElementById('scroller_'+id).style.top = (event.clientY - myTopComent)+ "px";
		if(gettop('scroller_'+id)<0) window.document.getElementById('scroller_'+id).style.top = 0 + "px";
		if(gettop('scroller_'+id) + window.document.getElementById('scroller_'+id).offsetHeight> window.document.getElementById('scrollbox_'+id).offsetHeight)
        	window.document.getElementById('scroller_'+id).style.top = window.document.getElementById('scrollbox_'+id).offsetHeight -window.document.getElementById('scroller_'+id).offsetHeight + "px";

        rch = window.document.getElementById('scrollbox_'+id).offsetHeight - window.document.getElementById('scroller_'+id).offsetHeight;
        window.document.getElementById('scroll_'+id).scrollTop=(qwe/rch)*gettop('scroller_'+id);
    }

}


function setpos(top,id)
{
    
    bottom = window.document.getElementById('scrollbox_'+id).offsetHeight - window.document.getElementById('scroller_'+id).offsetHeight;
    if(top < bottom)
    {
        window.document.getElementById('scroller_'+id).style.top = top + "px"; 
    }
    
    loaded(id);
}

function divscroll(event,id)
{
		flag=1;
		myTop = event.clientY - gettop('scroller_'+id);

        window.document.onmouseup = function()
        {
        		flag=0;
                window.document.onselectstart = "";
        }
        document.onselectstart = function()
        {
        	return false;
        }
}

function divscroll_coaching(event)
{
        var id = 'coaching';
		flag_coaching=1;
		myTopCoaching = event.clientY - gettop('scroller_'+id);

        window.document.onmouseup = function()
        {
        		flag_coaching=0;
                window.document.onselectstart = "";
        }
        document.onselectstart = function()
        {
        	return false;
        }
}

function divscroll_ansoknings(event)
{
        var id = 'ansoknings';
		flag_ansoknings=1;
		myTopAnsoknings = event.clientY - gettop('scroller_'+id);

        window.document.onmouseup = function()
        {
        		flag_coaching=0;
                window.document.onselectstart = "";
        }
        document.onselectstart = function()
        {
        	return false;
        }
}

function divscroll_coment(event)
{
        var id = 'coment';
		flag_coment=1;
		myTopComent = event.clientY - gettop('scroller_'+id);

        window.document.onmouseup = function()
        {
        		flag_coment=0;
                window.document.onselectstart = "";
        }
        document.onselectstart = function()
        {
        	return false;
        }
}

function hv()
{
    /*
	if(window.document.getElementById('scroll').scrollTop==0)
		window.document.getElementById('upbutton').style.display="none";
    else window.document.getElementById('upbutton').style.display="block";

	if(window.document.getElementById('scroll').scrollTop==qwe)
		window.document.getElementById('downbutton').style.display="none";
    else window.document.getElementById('downbutton').style.display="block";
    */
}
function down(id)
{
	window.document.getElementById('scroll_'+id).scrollTop=window.document.getElementById('scroll_'+id).scrollTop+30;
    hv();
}

function up(id)
{
	window.document.getElementById('scroll_'+id).scrollTop=window.document.getElementById('scroll_'+id).scrollTop-30;
    hv();
}

function loaded(id)
{
    
	tmp=window.document.getElementById('scroll_'+id).scrollTop;
	
	for(i=0;i<20;i++)
	    window.document.getElementById('scroll_'+id).scrollTop=window.document.getElementById('scroll_'+id).scrollTop+1;
	qwe=window.document.getElementById('scroll_'+id).scrollTop;
	window.document.getElementById('scroll_'+id).scrollTop=tmp;
	window.document.getElementById('scroll_'+id).onscroll = function()
	{
        rch = window.document.getElementById('scrollbox_'+id).offsetHeight - window.document.getElementById('scroller_'+id).offsetHeight;
        newtop = window.document.getElementById('scroll_'+id).scrollTop/qwe*rch

        if( newtop < rch)
            window.document.getElementById('scroller_'+id).style.top = newtop + "px";
        hv();
	}
}

function scrollfind(event,id)
{
	st = event.clientY - gettop('scrollbox_'+id);
    window.document.getElementById('scroller_'+id).style.top = st- window.document.getElementById('scroller_'+id).offsetHeight/2
    if(gettop("scroller_"+id)<0) window.document.getElementById('scroller_'+id).style.top = 0 + "px";
    if(gettop("scroller_"+id) + window.document.getElementById('scroller_'+id).offsetHeight> window.document.getElementById('scrollbox_'+id).offsetHeight)
        window.document.getElementById('scroller_'+id).style.top = window.document.getElementById('scrollbox_'+id).offsetHeight -window.document.getElementById('scroller_'+id).offsetHeight + "px";

    rch = window.document.getElementById('scrollbox_'+id).offsetHeight - window.document.getElementById('scroller_'+id).offsetHeight;
    window.document.getElementById('scroll_'+id).scrollTop=(qwe/rch)*gettop("scroller_"+id);


    window.status=st;
}

