var scrollSpeed = 200;
var headerRows = 2;

var engaged = false;
var scrollObj = null;
		
function engage(type) {
	engaged = true;
	if(type=='btm') {
		 scrollBottom();
	} else {
		 scrollTop();
	}
}

function unengage() {
	engaged = false;
}

var rem = new Array();
var t = null;

function scrollBottom() {	
	if(t) clearTimeout(t);
	if(engaged) doScrollBtm();
	
	function doScrollBtm() {
		var tblObj = document.getElementById('data');
		if(tblObj.rows.length - headerRows - 1 > rem.length) {						
			var trArr = tblObj.rows;
			for(var i=2; i<trArr.length-1; i++) {
				if(trArr[i].style.display != 'none') {
					trArr[i].style.display = 'none';
					rem[rem.length] = trArr[i];
					break;
				}
			}			
		}	
		t = setTimeout(scrollBottom, scrollSpeed);
	}
}

function scrollTop() {
	if(t) clearTimeout(t);
	if(engaged) doScrollTop();
		
	function doScrollTop() {
		if(rem.length > 0) {						
			var oldRow = rem[rem.length-1];
			var nowData = oldRow.cells;
			var newCell = null;
			var newRow = oldRow.parentNode.insertRow(headerRows);
			newRow.bgColor = oldRow.bgColor;
			newRow.className = 'content';		
			for (var i=0; i<nowData.length; i++) {
				newCell = newRow.insertCell(i);
				if(i==0) {
					newCell.className = 'name';
				}
				newCell.className = nowData[i].className;
				newCell.colSpan = nowData[i].colSpan;
				newCell.innerHTML = nowData[i].innerHTML
			}
			rem.length = rem.length - 1;
		}
		t = setTimeout(scrollTop, scrollSpeed);
	}
}

function initScroll() {
	if(document.getElementById('scroll')) {
		scrollObj = document.getElementById('scroll');
		var main = document.getElementById('main');
		var dataObj = document.getElementById('data');		
		if(dataObj.offsetHeight - main.offsetHeight > 0) {
			scrollObj.style.display = 'block';
		}
	}	
}
