// JavaScript Document
var scrollpx = 15;
var containerdiv = document.getElementById('scrollcontainer');
var dragging = false;
var dragobject;

function test2(e)
{
    var containerheight = document.getElementById('scrollcontainer').offsetHeight;
    var contentheight = document.getElementById('scrollcontent').offsetHeight;
  
    if (contentheight < containerheight) return;
  
    dragging = true;
	document.onselectstart = function () 
	{
		if (dragging == true)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	document.onmouseup = function ()
	{
	    stopscrolldrag();
	}

 mousescroll(e);
 document.onmousemove = mousescroll;
}

function stopscrolldrag()
{
    document.onmousedown = null;
    document.onmousemove = null;
    dragging = false;
}

function mousescroll(e)
{
    var posx = 0;
    var posy = 0;
    //if (!e) { var e = window.event; }
    var e = e || window.event
    //if (e == undefined) return;
        
    if (e.pageX || e.pageY)
    {
	    posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY)
    {
        posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }
       
    
    var containerheight = document.getElementById('scrollcontainer').offsetHeight;
    var contentheight = document.getElementById('scrollcontent').offsetHeight;   

    var scrollheight = document.getElementById('scroll').offsetHeight;
    var scrollbarheight = document.getElementById('scrollbar').offsetHeight;
    var testt = findPos(document.getElementById('scrollbar'));
    
 
    var ct = (contentheight - containerheight) / 100;
    
    var maxy = (scrollbarheight + testt[1]) - scrollheight;
    
    var newtop = posy - testt[1];
    var pct = (newtop / (scrollbarheight - scrollheight)) * 100;
    if(pct <= 0)
    {
        pct = 0;
        newtop = 0;
    }
    if (pct > 100)
    {
        pct = 100;
        newtop = scrollbarheight - scrollheight;
    }

    document.getElementById('scroll').style.top = newtop + 'px'
        
    document.getElementById('scrollcontent').style.top = "-" + (pct  * ct) + "px";
    
    
    
    //alert(pct);
    
    
    //alert(posy - testt[1]);
}

function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
        do
        {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
        while (obj = obj.offsetParent);

        return [curleft,curtop];
    }
}

function initScrollbar()
{
    dragobject = document.getElementById('scroll');
    //miny = document.getElementById('scrollbar').;
}

function scrolldown()
{
    //var divheight = document.getElementById('scrollcontent').style.height;
    var containerheight = document.getElementById('scrollcontainer').offsetHeight;
    var contentheight = document.getElementById('scrollcontent').offsetHeight;
    if (contentheight < containerheight) return;
    
    var scrollbarheight = document.getElementById('scrollbar').offsetHeight;
    var scrollheight = document.getElementById('scroll').offsetHeight;
    
    var scrollbarpx = (scrollbarheight - scrollheight) / 100;
    
    var contenttop = document.getElementById('scrollcontent').style.top;
    contenttop = contenttop.split('px')[0];
    
    var contentpct = Math.abs((contenttop  / (contentheight - containerheight)) * 100);

    if(contentpct >= 100) return;
    
    contenttop = contenttop - scrollpx;   
    
    document.getElementById('scrollcontent').style.top = contenttop + 'px';
    
    contentpct = Math.abs((contenttop  / (contentheight - containerheight)) * 100);
    if(contentpct >= 100) contentpct = 100;
    
    //calc top for scroll image
    var thetop = contentpct * scrollbarpx;
    
    document.getElementById('scroll').style.top = thetop + 'px'
    
    //alert(thetop);
}

function scrollup()
{
    var contenttop = document.getElementById('scrollcontent').style.top;
    contenttop = contenttop.split('px')[0];
    if(contenttop >= 0) return;

    //var divheight = document.getElementById('scrollcontent').style.height;
    var containerheight = document.getElementById('scrollcontainer').offsetHeight;
    var contentheight = document.getElementById('scrollcontent').offsetHeight;
    if (contentheight < containerheight) return;
    
    var scrollbarheight = document.getElementById('scrollbar').offsetHeight;
    var scrollheight = document.getElementById('scroll').offsetHeight;
    
    var scrollbarpx = (scrollbarheight - scrollheight) / 100;
    
    
    //alert(contenttop);
    
    var contentpct = Math.abs((contenttop  / (contentheight - containerheight)) * 100);

    if(contentpct < 1) return;
    
    contenttop = parseInt(contenttop) + parseInt(scrollpx);
    //alert(contenttop);
    if (contenttop > 0) contenttop = 0;
    document.getElementById('scrollcontent').style.top = contenttop + 'px';
    
    contentpct = Math.abs((contenttop  / (contentheight - containerheight)) * 100);
    if(contentpct <= 1) contentpct = 0;
    
    //calc top for scroll image
    var thetop = contentpct * scrollbarpx;
    if (thetop < 0) thetop = 0;
    
    document.getElementById('scroll').style.top = thetop + 'px'
}






/* Handle the wheel angle change (delta) of the mouse wheel */
function handle(delta)
{
	var change = false;
	switch (delta > 0)
	{
		case true:
			scrollup();
			break;

		default:
		    scrolldown();
			break;
	}
}

/* Event handler for mouse wheel event */
function wheel(event)
{
	var delta = 0;
	if (!event) event = window.event;
	if (event.wheelDelta)
	{
		delta = event.wheelDelta / 120;
	}
	else if (event.detail)
	{
		delta = -event.detail / 3;
	}
	if (delta) handle(delta);
	if (event.preventDefault) event.preventDefault();
	event.returnValue = false;
}

/* Initialize mouse wheel event listener */
function initMouseWheel()
{
	if(window.addEventListener) document.getElementById('scrollcontainer').addEventListener('DOMMouseScroll', wheel, false);
	document.getElementById('scrollcontainer').onmousewheel = wheel;
}

window.onload = function()
{
    initMouseWheel();
}
