var defaultScrollingStep = 2;
var defaultScrollingTiming = 50;
var currentSteppings = new Object();

function scrollUp(id) {
	currentSteppings[id] = -defaultScrollingStep;
	window.setTimeout('scrolling(\''+id+'\')', defaultScrollingTiming);
}

function scrollDown(id) {
	currentSteppings[id] = defaultScrollingStep;
	window.setTimeout('scrolling(\''+id+'\')', defaultScrollingTiming);
}

function stopScrolling(id) {
	currentSteppings[id] = 0;
}

function scrollFaster(id) {
	var stepping = currentSteppings[id];
	if (stepping > 0) stepping += defaultScrollingStep;
	else if (stepping < 0) stepping -= defaultScrollingStep;
	currentSteppings[id] = stepping;
}

function scrolling(id) {
	var stepping = currentSteppings[id];
	var nodeBox = document.getElementById(id);
	var nodeData = document.getElementById(id+'data');
	if (!nodeBox || !nodeData) {
		if (!nodeBox) alert('scrolling() could not find id ('+id+')');
		if (!nodeData) alert('scrolling() could not find id ('+id+'data)');
		currentSteppings[id] = 0;
		return;
	}
	var visibleHeight = parseInt(nodeBox.offsetHeight);
	var contentHeight = parseInt(nodeData.offsetHeight);
	var contentTopStr = nodeData.style.top;
	if (contentTopStr == '') contentTopStr = '0px';
	var contentTop = parseInt(contentTopStr);
	contentTop -= stepping;
	
	var lastStep = false;
	if (contentTop > 0) {
		contentTop = 0;
		lastStep = true;
	} else if (contentTop < visibleHeight - contentHeight) {
		contentTop = visibleHeight - contentHeight;
		lastStep = true;
	}
	nodeData.style.top = contentTop+'px';
	if (lastStep) currentSteppings[id] = 0;
	else if (stepping != 0) window.setTimeout('scrolling(\''+id+'\')', defaultScrollingTiming);
}

function initScrollarea(id) {
	var oldonresize = window.onresize;
	if (window.onresize == null || typeof window.onresize != 'function') {
		window.onresize = function() { updateScrollarea(id); };
	} else {
		window.onresize = function() {
			oldonresize();
			updateScrollarea(id);
		}
	}
	updateScrollarea(id);
}

function updateScrollarea(id) {
	var nodeBox = document.getElementById(id);
	if (nodeBox) nodeBox.style.overflow = 'hidden';
	
	var nodeData = document.getElementById(id+'data');
	if (nodeBox && nodeData) {
		var visibleHeight = parseInt(nodeBox.offsetHeight);
		var contentHeight = parseInt(nodeData.offsetHeight);
		var hide = (contentHeight <= visibleHeight);
		if (hide) {
			nodeData.style.top = '0px';
		} else {
			var contentTopStr = nodeData.style.top;
			if (contentTopStr == '') contentTopStr = '0px';
			var contentTop = parseInt(contentTopStr);
			if (contentTop < visibleHeight - contentHeight) nodeData.style.top = (visibleHeight - contentHeight)+'px';
		}
		var node = document.getElementById(id+'up');
		if (node) {
			node.style.display = (hide ? 'none' : 'block');
			node.onmouseover = function() { scrollUp(id); };
			node.onmouseout = function() { stopScrolling(id); };
			node.onclick = function() { scrollFaster(id); return false; };
		}
		node = document.getElementById(id+'down');
		if (node) {
			node.style.display = (hide ? 'none' : 'block');
			node.onmouseover = function() { scrollDown(id); };
			node.onmouseout = function() { stopScrolling(id); };
			node.onclick = function() { scrollFaster(id); return false; };
		}
	}
}
