var top = 0;
var bottom = 0;
var scrollInterval = null;

function rolldown(divId) {
	scrollInterval = setInterval('down("' + divId + '", 10)', 100);
}

function rollup(divId) {
	scrollInterval = setInterval('up("' + divId + '", 10)', 100);
}

function stoproll() {
	if (scrollInterval != null) {
		clearInterval(scrollInterval);
		scrollInterval = null;
	}
}

function down(divId, scrollBy) {
	initializeScroll(scrollBy);

	var viewport = document.getElementById(divId);

	// do nothing if at bottom of containing box
	if (viewport.scrollHeight + top <= scrollBy){
		return;
	}

	top -= scrollBy;
	bottom -= scrollBy;

	viewport.style.marginTop = top + 'px';
	viewport.style.marginBottom = bottom + 'px';
}

function up(divId, scrollBy) {
	initializeScroll(scrollBy);
	
	// do nothing if at top of containing box
	if (top >= 0) {
		return;
	}

	var viewport = document.getElementById(divId);

	top += scrollBy;
	bottom += scrollBy;

	// the ?: ternary-conditional expressions below work around a Safari bug
	viewport.style.marginTop = (top == 0 ? -1 : top) + 'px';
	viewport.style.marginBottom = (top == 0 ? bottom - 1 : bottom) + 'px';
}

function resetScroll(divId, scrollBy) {
	top = 0;
	bottom = scrollBy;
	
	var viewport = document.getElementById(divId);

	viewport.style.marginTop = '-1px';
	viewport.style.marginBottom = (bottom - 1) + 'px';
}

// initialize 'bottom' if needed
function initializeScroll(scrollBy) {
	if (top == 0 && bottom == 0) {
		bottom = scrollBy < 0 ? -scrollBy : scrollBy;
	}
}

// set style height of scrollable DIV to its scrollHeight IF browser is IE
function setScrollLimit(wrapperId, contentId) {
	if (!document.all) {
		return;		// do nothing, not IE
	}

	viewport = document.getElementById(wrapperId);
	viewport.style.height = (contentId == null ? viewport.scrollHeight : document.getElementById(contentId).scrollHeight) + 'px';
}
