/************************************************
*  viewports.js - CSS-p that works for IE6  :P  *
************************************************/

/* 
* functions to create the view.  vView and hView can be nested inside one another.
* Usage:  
* initializeViewport ( 
* 			vView( 
* 				pane(<div ID>, <size (optional)>, 
* 				hView( pane(<div ID>, <size (optional)> ) 
* 			) 
* ) 
*/

function vView() { return new vViewObj(arguments); }
function hView() { return new hViewObj(arguments); }
function pane ( id , size ) { return new paneObj(id,size); }

function initializeViewport(main)
{			
	// if the main is valid, and is a renderable object, cache it
	if (main && (main instanceof vViewObj || main instanceof hViewObj)) 
		mainViewport = main;
	
	resizeViewport();
}

// re-configure the view to match the client
function resizeViewport()
{
	// if mainViewport is valid, then render it
	if (mainViewport)
	{				
		mainViewport.render(
			document.body.clientHeight,
			document.body.clientWidth,
			0, 0
		);
	}
}
// initialize the window resize event to the resize viewport method
window.onresize = resizeViewport;


var mainViewport;

// vView constructor
function vViewObj(args)
{
	if (args && (args[0] || args[0]==0) && typeof args[0] == 'number')
	{
		this.size = args[0];
	}
	this.panes = new Array();
	this.render = renderV;
	_initView(this, args);
}

// hView constructor
function hViewObj(args)
{
	if (args && (args[0] || args[0]==0) && typeof args[0] == 'number')
	{
		this.size = args[0];
	}
	this.panes = new Array();
	this.render = renderH;
	_initView(this, args);
}

function _initView(view, args)
{
	for (i = 0; i < args.length; ++i)
	{					
		if (args[i] instanceof paneObj
				|| args[i] instanceof vViewObj
				|| args[i] instanceof hViewObj)
			view.panes.push(args[i]);
	}
}

// pane constructor
function paneObj(id, size)
{
	this.div = document.getElementById(id);
	this.size = (size || size == 0 ? size : 'default');
	
	if (this.size == 'default')
		this.div.style.overflow = 'auto';
}

function renderDiv(div, newHeight, newWidth, newTop, newLeft)
{
	div.style.top = newTop;
	div.style.left = newLeft;
	
	if (newHeight > 0)
	{
		div.style.display = 'block';
		div.style.height = newHeight;
	}
	else 
		div.style.display = 'none';
		
	if (newWidth > 0)
	{
		div.style.display = 'block';
		div.style.width = newWidth;
	}
	else 
		div.style.display = 'none';
}

// render a vertical view
function renderV(height, width, currentTop, currentLeft)
{
	var newTops = new Array(), resizables = 0, remaining = height, i;
	for (i = 0; i < this.panes.length; ++i)
	{
		var currentPane = this.panes[i];
		if (typeof currentPane.size == 'number')
		{
			newTops.push(currentPane.size);
			remaining -= currentPane.size;
		}
		else if (currentPane.size == 'actual')
		{
			newTops.push(currentPane.div.offsetHeight);
			remaining -= currentPane.div.offsetHeight;
		}
		else // this is default
		{
			newTops.push(-1);
			resizables++;
		}
	}
	
	if (resizables) remaining /= resizables;
	
	var newTop = currentTop, newHeight = 0;
	for (i = 0; i < newTops.length; ++i)
	{
		newHeight = newTops[i];
		
		if (newHeight == -1) newHeight = remaining;
		
		if (this.panes[i] instanceof paneObj)
		{
			renderDiv(this.panes[i].div, newHeight, width, newTop, currentLeft);
		}
		else
		{
			this.panes[i].render(newHeight, width, newTop, currentLeft);
		}
		
		newTop += newHeight;
	}
	return;
}

// render a horizontal view
function renderH(height, width, currentTop, currentLeft)
{
	var newLefts = new Array(), resizables = 0, remaining = width, i;
	for (i = 0; i < this.panes.length; ++i)
	{
		var currentPane = this.panes[i];
		if (typeof currentPane.size == 'number')
		{
			newLefts.push(currentPane.size);
			remaining -= currentPane.size;
		}
		else if (currentPane.size == 'actual')
		{
			newTops.push(currentPane.div.offsetWidth);
			remaining -= currentPane.div.offsetWidth;
		}
		else // is a "default"
		{
			newLefts.push(-1);
			resizables++;
		}
	}
	
	if (resizables)
		remaining /= resizables;
	
	var newLeft = 0, newWidth = 0;
	for (i = 0; i < newLefts.length; ++i)
	{
		newWidth = newLefts[i];
		
		if (newWidth == -1) newWidth = remaining;
		
		if (this.panes[i] instanceof paneObj)
		{
			renderDiv(this.panes[i].div, height, newWidth, currentTop, newLeft);
		}
		else
		{
			this.panes[i].render(height, newWidth, currentTop, newLeft);
		}
		
		if (newWidth == -1) newWidth = remaining;
		newLeft += newWidth;
	}
	return;
}
