/**
*
*  Scrollable HTML table
*  http://www.webtoolkit.info/
*
**/

function ScrollableTable (tableEl, tableHeight, tableWidth) {
	function initIEengine() {

		containerEl.style.overflowY = 'auto';
		if (tableEl.parentElement.clientHeight - tableEl.offsetHeight < 0) {
			tableEl.style.width = newWidth - scrollWidth +'px';
		} else {
			containerEl.style.overflowY = 'hidden';
			tableEl.style.width = newWidth +'px';
		}

		if (thead) {
			var trs = thead.getElementsByTagName('tr');
			for (x=0; x<trs.length; x++) {
				trs[x].style.position ='relative';
				trs[x].style.setExpression("top",  "parentElement.parentElement.parentElement.scrollTop + 'px'");
			}
		}

		if (tfoot) {
			var trs = tfoot.getElementsByTagName('tr');
			for (x=0; x<trs.length; x++) {
				trs[x].style.position ='relative';
				trs[x].style.setExpression("bottom",  "(parentElement.parentElement.offsetHeight - parentElement.parentElement.parentElement.clientHeight - parentElement.parentElement.parentElement.scrollTop) + 'px'");
			}
		}

		if (!tableEl.id) {
			tableEl.id = 'id' + Math.floor(Math.random()*1000);
		}
		eval("window.attachEvent('onresize', function () { document.getElementById('" + tableEl.id + "').style.visibility = 'hidden'; document.getElementById('" + tableEl.id + "').style.visibility = 'visible'; } )");
	}


	function initFFengine() {
		containerEl.style.overflow = 'hidden';
		tableEl.style.width = newWidth + 'px';

		var headHeight = (thead) ? thead.clientHeight : 0;
		var footHeight = (tfoot) ? tfoot.clientHeight : 0;
		var bodyHeight = tbody.clientHeight;
		var trs = tbody.getElementsByTagName('tr');
		if (bodyHeight >= (newHeight - (headHeight + footHeight))) {
			tbody.style.overflow = '-moz-scrollbars-vertical';
			for (x=0; x<trs.length; x++) {
				var tds = trs[x].getElementsByTagName('td');
				tds[tds.length-1].style.paddingRight += scrollWidth + 'px';
			}
		} else {
			tbody.style.overflow = '-moz-scrollbars-none';
		}

		var cellSpacing = (tableEl.offsetHeight - (tbody.clientHeight + headHeight + footHeight)) / 4;
		tbody.style.height = (newHeight - (headHeight + cellSpacing * 2) - (footHeight + cellSpacing * 2)) + 'px';

	}

	tableEl = tableEl;
	scrollWidth = 17;

	originalHeight = tableEl.clientHeight;
	originalWidth = tableEl.clientWidth;

	newHeight = parseInt(tableHeight);
	newWidth = tableWidth ? parseInt(tableWidth) : originalWidth;

	tableEl.style.height = 'auto';
	tableEl.removeAttribute('height');

	containerEl = tableEl.parentNode.insertBefore(document.createElement('div'), tableEl);
	containerEl.appendChild(tableEl);
	containerEl.style.height = newHeight + 'px';
	containerEl.style.width = newWidth + 'px';


	var thead = tableEl.getElementsByTagName('thead');
	thead = (thead[0]) ? thead[0] : null;

	var tfoot = tableEl.getElementsByTagName('tfoot');
	tfoot = (tfoot[0]) ? tfoot[0] : null;

	var tbody = tableEl.getElementsByTagName('tbody');
	tbody = (tbody[0]) ? tbody[0] : null;

	if (!tbody) return;

	if (document.all && document.getElementById && !window.opera) initIEengine();
	if (!document.all && document.getElementById && !window.opera) initFFengine();


}

