var t = null;
var time = null;
var border = [0,0]; // value of border-left and border-right in cells
var tHeadHeight = 0; // height of table head
var rows = 10; // visible rows

function initScroll(name) {	

	var c = document.getElementById(name);	
	var t = c.getElementsByTagName('TABLE')[0];	
	
	//not suppoted browsers
	if(navigator.appName == "Netscape" && parseInt(navigator.appVersion) <= 6 ) {
		showBody(getOrigContentRow().parentNode);
		return false;
	}	
	
	border = getBorders();
	if((border + "").indexOf('Error') != -1) {
		alert(border);
		return false;
	}	
	
	rows = getRows();
	if((rows + "").indexOf('Error') != -1) {
		alert(rows);
		return false;
	}	
	
	tHeadHeight = getHeaderHeight();
	if((tHeadHeight + "").indexOf('Error') != -1) {
		alert(tHeadHeight);
		return false;
	}
	
	var tmpRow = getOrigContentRow();
	
	if((tmpRow + "").indexOf('Error') != -1) {
		alert(tmpRow);
		return false;
	}
	
	if(!setMainDivHeight()) {
		return false;
	}
	
	// duplicating table and positioning here over original table
	var tcopy = t.cloneNode(true);
	var content = document.createElement('DIV');	
	content.id = "sDiv";
	content.className = "sDiv";
	content.style.height = c.offsetHeight - tHeadHeight;
	content.appendChild(tcopy)	
	c.appendChild(content);
	content.style.width = c.offsetWidth + "px";
	c.style.paddingRight = (content.offsetWidth - content.firstChild.offsetWidth) + "px";
	
	var row = null;
	
	//time = setTimeout(fixWidth, 1);	// dancing rows
	fixWidth();
	
	function fixWidth() {
		//clearTimeout(time);
		
		row = getOrigContentRow();		
		
		var dupRow = getDuplicatedContentRow();
		if((dupRow + "").indexOf('Error') != -1) {
			alert(dupRow);
			return false;
		}
		
		for( var i = 0; i < row.cells.length; i++) {
			border[1] = (i == 0) ? 0 : 1; // in first row border-right = 0
			setWidth(dupRow, i);
		}	
	}
		
	function setWidth(dupRow, i) {			
		var sCell = dupRow.cells[i];
		sCell.style.paddingLeft = '0';
		sCell.style.paddingRight = '0';				
		sCell.style.width = ( row.cells[i].offsetWidth - (border[0] + border[1]) ) + "px";		
	}
	
	function getOrigContentRow() {
		try {
			for(var i = 0; i < t.tBodies.length; i++ ) {
				if(t.tBodies[i].className == 'sContent' && t.tBodies[i].rows.length > 0) {
					return t.tBodies[i].rows[0];
				}
			}			
		} catch(e) { return "Error: Can't find TBODY in original table!\n" + e.message; }		
	}
	
	function getDuplicatedContentRow() {
		try {
			for(var i = 0; i < tcopy.tBodies.length; i++ ) {
				if(tcopy.tBodies[i].className == 'sContent') {
					return tcopy.tBodies[i].rows[0];
				}
			}
		} catch(e) { return "Error: Can't find TBODY tag in duplicated table!\n" + e.message; }
	}
	
	function setMainDivHeight() {
		height = 0;
		for(var i = 1; i <= rows && tmpRow; i++) {															
			height += tmpRow.offsetHeight;
			if(tmpRow) tmpRow = tmpRow.nextSibling;
		}		

		if(i < rows) { // scrolling dont need
			try {
				showBody(getOrigContentRow().parentNode);			
			}			
			catch(e) {} // no rows
			return false;
		} else {
			c.style.height = (height + tHeadHeight) + "px";
			return true;
		}
	}
	
	function getHeaderHeight() {
		var h = 0;
		try {
			var hrows = t.tHead.rows;		
			for(var i = 0; i < hrows.length; i++ ) {
				h += hrows[i].offsetHeight;
			}			
			return h;
		} catch(e) { return "Error: Can't calculate head height!\n" + e.message; }		
	}
	
	function getRows() {
		try {
			return parseInt(c.getAttribute('ROWS'));
		} catch(e) { return "Error: Can't get value of 'ROWS' attribute in main div!\n" + e.message; }
	}
	
	function getBorders() {
		try {
			return c.getAttribute('BORDERS').split(',');
		} catch(e) { return "Error: Can't get value of 'BORDERS' attribute in main div!\n" + e.message; }
	}
	function showBody(node) {
		node.style.visibility = "visible";
		node.style.position = "relative";
	}
}
