var SCROLLTIMER = 10;
var SCROLLPERCENT = 0.1;
var locked = false;
var activeMargin = 50;

var div;
var mousePosition,heightObject,mousey,mousePercent,scrollableHeight;

////////////////////////////////////////molecule
function scrollTo(div,position) {
	//div.style.top = '-' + position + 'px';
	div.targetValue = position;
}

function scrollToStep(){

  if (!top.thediv)
    return;

  var div = top.thediv;

  var position = div.currentValue + SCROLLPERCENT*(div.targetValue - div.currentValue);
 
  position = Math.max(0,position);


  div.currentValue = position;

  locked = false;
	  
  if (position > 0)
    div.style.top = '-' + position + 'px';
  else
    div.style.top = "0px";

  //if (Math.abs(position - div.targetValue) < 1)
    //killScroll();
}

function findPosYXXX(obj){
	var curtop = 0;
	if(obj.offsetParent) {
		while(1) {
			curtop += obj.offsetTop;
			if(!obj.offsetParent){
				break;
			} 
			obj = obj.offsetParent;
		}
	} else if(obj.y) {
		curtop += obj.y;
	}
	return curtop;
}


function findPosY(obj){
	var curtop = 0;
	if(obj.offsetParent) {
		curtop += obj.offsetTop;
		
		if(obj.offsetParent){
			curtop += findPosY(obj.offsetParent)
		} 
	
	} else if(obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

function getPosition(e) {
	e = e || window.event;
	var cursor = {x:0, y:0};
	if (e.pageX || e.pageY) {
		cursor.x = e.pageX;
		cursor.y = e.pageY;
	} else {
		var de = document.documentElement;
		var b = document.body;
		cursor.x = e.clientX + 
			(de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
		cursor.y = e.clientY + 
			(de.scrollTop || b.scrollTop) - (de.clientTop || 0);
	}
	return cursor;
}

// loosely based on http://sandbox.leigeber.com/contentslider/slider.html
// and http://www.leigeber.com/2008/05/ultimate-javascript-scroller-and-slider/
function scrollMe(e){
	if (!locked){
		locked = true;
		
		if (top.thediv.timer == -1){
			top.thediv.timer = setInterval( 'scrollToStep();', SCROLLTIMER);
		}
		
		if (!e) e = window.event;

		// Caclulate the mouse's position
		mousePosition = getPosition(e);

		// Caclulate the vertical offset of the div
		heightObject = findPosY(top.thediv.parentNode); 

		// Caclulate the mouse's position inside the div
		mousey = mousePosition.y - heightObject - activeMargin;

		// Caclulate the percentage of the div the mouse is currently at, bounded to 0<mp<1
		mousePercent = Math.max(0,Math.min(1,mousey/(top.thediv.parentNode.offsetHeight - 3*activeMargin)));

		// Caclulate the total height of the div?
		scrollableHeight = (top.thediv.offsetHeight) - (top.thediv.parentNode.offsetHeight-4);

		if (scrollableHeight <= 0){
			scrollTo(top.thediv,0);
		}else{
			scrollTo(top.thediv,mousePercent*scrollableHeight);
		}
	} else {
		return;
	}
}


function killScroll()
{
	//return;
	if (top.thediv.timer)
	{	clearTimeout(top.thediv.timer);
		top.thediv.timer = -1;
	}
}

function startScroll()
{
	if (top.thediv.timer == -1)
	{
		//alert("started");
		top.thediv.timer = setInterval( 'scrollToStep();', SCROLLTIMER);
	}
}

function makeAutoScrolling(div)
{
	//return;
	
	top.thediv = div;
	top.thediv.timer = -1;

	div.onmousemove = scrollMe;
	// div.onmouseout = killScroll;
	// div.onmouseover = startScroll;
	div.currentValue = 0;
}
////////////////////molecule
