/*
======================================================================

adaptLayout.js


author:		Timothy Groves
			desk [at] brandspankingnew.net
version:	1.0

tested on:	Safari 2.0 Mac / FF 1.0.6 Mac / Opera 8 Mac
			FF 1 PC / IE 6 PC

history:	28.10.2005	-	created

======================================================================
*/





var adaptLayout = {

	rowsArr : new Array(),
	
	init : function ()
	{
		if ( !document.getElementById )
			return false;
			
		window.onresize = adaptLayout.setLayout;
		
		adaptLayout.setLayout();
	},
	
	addRow : function (o)
	{
		adaptLayout.rowsArr.push( o );
	},
	
	setLayout : function ()
	{
		var o, c, cw, tw, rw, i, j, k, e, rows, tmp, vw;
		
		for ( k in adaptLayout.rowsArr ) {
			
			o = adaptLayout.rowsArr[k];
			
			// get container
			c = document.getElementById( o.container );
			
			// get width of container
			cw = c.offsetWidth-5;
			
			
			
	
			// get rows
			//
			//
			rows = new Array();
			
			rows[0] = new Array(); // start first row
			rw=0; // init row width
			
			for (i=0;i<o.columns.length;i++) {
				// calculate width of col
				tmp = (o.columns[i].fw)  ?  o.columns[i].fw  :  o.columns[i].mw;
				tmp -= 0; // convert to int
				if (rw+tmp < cw) {
					rw+=tmp;
				} else {
					// new row
					rows[rows.length] = new Array();
					rw = tmp;
				}
				// add a column to row
				rows[rows.length-1].push( o.columns[i] );
				
			}
			
			
			// set col widths
			//
			//
			
			for (i=0;i<rows.length;i++) {
			
				fww = 0; nfw = 0;
				
				for (j=0;j<rows[i].length;j++) {
					// get all fixed width cols in row
					if ( rows[i][j].fw ) {
						e = document.getElementById( rows[i][j].id );
						e.style.width = rows[i][j].fw+"px";
						e.style.height = "auto";
						nfw++;
						fww += rows[i][j].fw;
					}
				}
				
				if (nfw == rows[i].length) { // no variable-width rows available!
				
					vw = Math.round( cw / nfw );
					for (j=0;j<rows[i].length;j++) {
						e = document.getElementById( rows[i][j].id );
						e.style.width = vw+"px";
						e.style.height = "auto";
					}
							
				} else {
				
					// calculate variable-width col width
					vw = Math.round(  (cw - fww) / (rows[i].length - nfw)  );
				
					for (j=0;j<rows[i].length;j++) {
						if ( rows[i][j].mw ) { // get all fixed width cols in row
							e = document.getElementById( rows[i][j].id );
							e.style.width = vw+"px";
							e.style.height = "auto";
						}
					}
					
				}
			}
			
			
		
		}

	}
	
}