/*
Fix IE css bug: IE ignores combinations of left/right and top/bottom.

This script tries to mimick Firefox as close as possible.

Changes:

20-8-2006: 
- Initial version

23-8-2006: 
- Dropped necessity to add class stretch
- Dropped necessity to use inline styles (looks at currentStyle)
- Now works on all elements that have:
	* position: absolute
	* display: block
	* left and right in pixels (other units will follow) and width = auto OR
	* top and bottom in pixels (other units will follow) and height = auto

11-09-2006:
- Only triggers on explorer for windows (read navigator object)	

TODO: IFRAMEs Automatically get a width and height in pixels, this makes it hard to
determine wether an iframe should be included. At the moment an Iframe is included when 
position = absolute and display = block.				

*/

strRun();

function strRun()
{	
	
	if ((navigator.appName.match("Explorer")) && (navigator.platform.match("Win"))) {
		var oldOnload = window.onload;
		if (typeof(window.onload) != "function")
		{
			window.onload = initStretch;
		}
		else
		{
			window.onload = function()
			{
				oldOnload();
				initStretch();
			}
		}
	}
}

var vDivs;
var hDivs;

// matches a size measured in pixels (can be extended to include other units)
re = new RegExp("[0-9]*px", "i");

function initStretch()
{	
	// should automatically add body.height = 100%!!!!
	
	//var divs = elementsByTag('*');
	//var divs = strElementsByTag('*');
	var	divs = new Array();
	divs[0] = element('main');

	hDivs = new Array();
	vDivs = new Array();
	
	for (var i=0; i < divs.length; i++) {

		//if ((divs[i].currentStyle.position == "absolute") && (divs[i].currentStyle.display == "block")) {
			
			//if ((divs[i].nodeName == "IFRAME") || ((divs[i].currentStyle.left.match(re)) && (divs[i].currentStyle.right.match(re)) && (divs[i].currentStyle.width == "auto")))
			//{
				hDivs[hDivs.length] = divs[i];
			//}
			
			//if ((divs[i].nodeName == "IFRAME") || ((divs[i].currentStyle.top.match(re)) && (divs[i].currentStyle.bottom.match(re)) && (divs[i].currentStyle.height == "auto")))
			//{
				vDivs[vDivs.length] = divs[i];
			//}
		//}
	} 
	strRefresh();
	window.onresize = strRefresh;
	//setTimeout(strRefresh, 100);
	//alert('test');
	
}

function strRefresh()
{
	var bodyWidth = document.body.clientWidth;
	var bodyHeight = document.body.clientHeight;
//alert(bodyHeight);
	for (var i=0; i < hDivs.length; i++) {
		var div = hDivs[i];

		// calculate and assign new width
		if (div.parentNode.tagName == "BODY") {
			var newWidth = bodyWidth - (parseInt(div.currentStyle.left) + parseInt(div.currentStyle.right));
		} else {
			var newWidth = parseInt(div.parentNode.style.width) - (parseInt(div.currentStyle.left) + parseInt(div.currentStyle.right));
		}
		
		if (newWidth >= 0) { div.style.width = newWidth + "px"; }
	}
	
	for (var i=0; i < vDivs.length; i++) {
		var div = vDivs[i];
		
		//alert(parseInt(div.currentStyle.top) + div.offsetHeight);

		if (bodyHeight > (parseInt(div.currentStyle.top) + div.offsetHeight))
		{
			div.style.position = 'absolute';
			// calculate and assign new heigth		
			//if (div.parentNode.tagName == "BODY") {
				var newHeight = bodyHeight - (parseInt(div.currentStyle.top) + parseInt(div.currentStyle.bottom));
			//} else {
			//	var newHeight = parseInt(div.parentNode.currentStyle.height) - (parseInt(div.currentStyle.top) + parseInt(div.currentStyle.bottom));
			//}
			
			//alert(newHeight + ', ' + div.offsetHeight);
			
			if ((newHeight >= 0) && (newHeight > div.offsetHeight)) { div.style.height = newHeight + "px"; }
		}

	}
}

function strElementsByTag(tagString)
{
	if (document.getElementsByTagName)
		return document.getElementsByTagName(tagString);
	else if (document.all)
		return document.all.tags(tagString);
	else return;
}

