/**
 * 
 * Javascript Widgets 2000
 * Javascript reusable GUI components.
 * Copyright by ChieftainY2k
 * 
 * Designed, coded and maintained by ChieftainY2k , www.ChieftainY2k.net
 * ChieftainY2k[at]Y2k-design[dot]net
 * ChieftainY2k[at]vip[dot]interia[dot]pl
 * ChieftainY2k[at]anronet[dot]pl
 * 
 * Post all questions regarding licensing to one of the addresses given above.
 *
 */

/*
 Scroll Tab 
 ver 1.0 
 
 Usage example:
 
	<style> @import url(jsw2000/w-scrolltab-dialog.css); </style> 
	<table id="wordListTable" border=0 width='400px' height='70px' cellpadding=0 cellspacing=0
		bgcolor="#efefef" slcolor="#ffffcc" hlcolor="#f6f6f6"
		class=scrollTableBodyTable
		style="behavior:url(jsw2000/gethtc.php?f=table_hlight);
				   xheight:xexpression(Math.max(document.body.clientHeight-document.all.browseControls.offsetHeight,100)+'px');
				   xwidth:xexpression(Math.max(document.body.clientWidth-200,100)+'px')"
		>
		<thead>
			<tr>		<td>title 1</td>		<td>title 2</td>		<td>title 3</td>	</tr>
		</thead>
		<tbody>
			<tr>		<td>body 1</td>		<td>body 2</td>		<td>body 3</td>	</tr>
			<tr>		<td>body 1</td>		<td>body 2</td>		<td>body 3</td>	</tr>
			<tr>		<td>body 1</td>		<td>body 2</td>		<td>body 3</td>	</tr>
			<tr>		<td>body 1</td>		<td>body 2</td>		<td>body 3</td>	</tr>
		</tbody>
	</table>
	<script src="jsw2000/w-scrolltab.js"></script>
	<script>scrolltab_init('wordListTable');</script>
 
 
*/

//global vars
document.scrollTabData={};

//convert regular table into scrollTab component
function scrolltab_init(tableID,theme)
 {
	//theme selection
	themePrefix = theme ? theme+"_" : "";
	
	document.scrollTabData[tableID]={};
	document.scrollTabData[tableID]['theme']=theme;

	var componentCode="\
		<DIV class='"+themePrefix+"scrollTableDiv' id='"+tableID+"_scrollTableDiv' style='overflow:hidden; border: solid 1px #888888'>\
			<SPAN class='"+themePrefix+"scrollTableHeadSpan' id='"+tableID+"_scrollTableHeadSpan'>\
				<TABLE id='"+tableID+"_scrollTableHeadTable' cellpadding=0 cellspacing=1 frame=border width='100%'>\
					<TR id='"+tableID+"_scrollTableHeadRow'>\
					</TR>\
				</TABLE>\
			</SPAN>\
			<SPAN class='"+themePrefix+"scrollTableBodySpan' id='"+tableID+"_scrollTableBodySpan'>\
			</SPAN>\
		</DIV>\
		";

	var tableObj = document.all[tableID];
	tableObj.insertAdjacentHTML('beforeBegin',componentCode);
	

	//clone DIV size params from table size params
	if (tableObj.style.getExpression('height'))  document.all[tableID+"_scrollTableDiv"].style.setExpression('height',tableObj.style.getExpression('height'))
	else  document.all[tableID+"_scrollTableDiv"].style.height = tableObj.height ? tableObj.height : tableObj.clientHeight;

	if (tableObj.style.getExpression('width'))  document.all[tableID+"_scrollTableDiv"].style.setExpression('width',tableObj.style.getExpression('width'))
	else  document.all[tableID+"_scrollTableDiv"].style.width = tableObj.width ? tableObj.width : tableObj.clientWidth;

	//delete old size params
	tableObj.height=""; tableObj.style.height=""; 
	tableObj.width=""; tableObj.style.width="100%"; //tableObj.style.setExpression("width",'');
	
	//Copy header cells
	for(var i=0;i<tableObj.tHead.rows[0].cells.length;i++)  
	 {
	  var td = document.createElement("td");
	  td.innerHTML="<nobr>"+tableObj.tHead.rows[0].cells[i].innerHTML+"</nobr>";
	  td.align=tableObj.tHead.rows[0].cells[i].align;
	  document.all[tableID+"_scrollTableHeadRow"].appendChild(td);
	 };
	document.all[tableID+"_scrollTableHeadRow"].appendChild(document.createElement("td")); //extra TD
	
	//remove original table head
	tableObj.removeChild(tableObj.tHead);
	//remove original table from document srtucture
	tableObj.parentNode.removeChild(tableObj); 

	//attach mouse events
	for(i=0;i<tableObj.rows.length;i++)
	{
		//tableObj.rows[i].attachEvent('onmouseover', scrolltab_tr_mouseover);
		eval("tableObj.rows[i].attachEvent('onmouseover', function () { scrolltab_tr_mouseover('"+tableObj.id+"','"+i+"'); });");
		eval("tableObj.rows[i].attachEvent('onmouseout', function () { scrolltab_tr_mouseout('"+tableObj.id+"','"+i+"'); });");
		tableObj.rows[i].className='xp_scrollTableTr';
	};
	
	//copy table body cells into component body
	document.all[tableID+"_scrollTableBodySpan"].appendChild(tableObj);
	/*
	for(var i=0;i<tableObj.tBodies[0].rows.length;i++)  
	 	document.all[tableID+"_scrollTableBody"].appendChild(tableObj.tBodies[0].rows[i]); 
	*/

	tableObj.style.display="block";
	scrolltab_recalcTableSize(tableID);
	window.attachEvent('onresize',function () { scrolltab_recalcTableSize(tableID); });	
	setInterval("scrolltab_recalcTableSize('"+tableID+"')",1000);
	

 };						 
 

function scrolltab_tr_mouseover(tableID,rowNum)
{ 
	theme = document.scrollTabData[tableID]['theme'];
	themePrefix = theme ? theme+"_" : "";
	//window.status="scrolltab_tr_mouseover("+tableID+","+rowNum+")";
	document.all[tableID].rows[rowNum].className=themePrefix+'scrollTableTrHover';
};
 
function scrolltab_tr_mouseout(tableID,rowNum)
{ 
	theme = document.scrollTabData[tableID]['theme'];
	themePrefix = theme ? theme+"_" : "";
	//window.status="scrolltab_tr_mouseover("+tableID+","+rowNum+")";
	document.all[tableID].rows[rowNum].className=themePrefix+'scrollTableTr';
};
 
//recalc single table components size
function scrolltab_recalcTableSize(tableID)
 {
	//return;
	var tableDiv = document.all[tableID+"_scrollTableDiv"];
	var head = document.all[tableID+"_scrollTableHeadSpan"];
	var headTable = document.all[tableID+"_scrollTableHeadTable"];
	var body = document.all[tableID+"_scrollTableBodySpan"];
	var bodyTable = document.all[tableID];
	
	body.style.height = Math.max(0, tableDiv.clientHeight - head.offsetHeight);
	
	var scrollBarWidth = body.offsetWidth - body.clientWidth;

	// set width of the table in the head
	headTable.style.width = Math.max(0, Math.max(bodyTable.offsetWidth + scrollBarWidth, tableDiv.clientWidth));

	// go through each cell in the head and resize
	var headCells = headTable.rows[0].cells;
	var bodyCells = bodyTable.rows[0].cells;
	
	//var txt='';
	for (var i = 0; i < bodyCells.length; i++)
	{
		if (bodyCells[i].offsetWidth<1) continue;
		headCells[i].style.width = bodyCells[i].offsetWidth;
		//txt=txt+":"+bodyCells[i].offsetWidth;
	};
	//window.status=txt;
 };


 
 
