/*************************************************************************
 * SI_clearAbsolutes() v1.2
 * Attempts to clear all absolutely positioned elements...
 *************************************************************************/
var SI_frame = false;
function SI_clearAbsolutes() {
	
	var d=document;
	if (!d.getElementsByTagName) return;
	var divs = d.getElementsByTagName('div');
	
	// Reset all divs heights
	for (i=0; i<divs.length; i++) { divs[i].style.height='auto'; }
	
	// Now loop through them all in reverse
	for (i=divs.length-1;i>=0;i--) {
		var div = divs[i];
		var containsDivs = false;
		for (j=0; j<div.childNodes.length; j++) {
			if (div.childNodes[j].nodeName=='DIV') {
				containsDivs = true;
				break;
				}
			}
		if (containsDivs) {
			//w('#'+div.id+'.'+div.className+' at depth: '+SI_getNestingDepth(div)+', source order: '+i);
			var oh = []; 
			for (j=0;j<div.childNodes.length;j++) { c = div.childNodes[j]; 
				if (c.nodeName=='DIV') { 
					// Add this height to our array of rendered heights
					oh[oh.length] = parseInt(c.offsetHeight); 
					}
				}
				
			// Determine the max height
			var h = 0; for (k=0;k<oh.length;k++) { h = (oh[k]>h)?oh[k]:h;}
			// Determine sum of contained elements heights
			var sh = 0; for (l=0;l<oh.length;l++) { sh +=oh[l]; }
			
			/* getComputedStyle is not supported in Safari 1.2-older so we need to work around detecting the positioning of each element
			 * (
			 * div.offsetHeight==0	: if the containing element only has absolutely positioned elments inside of it it will collapse and have no offsetHeight
			 * &&
			 * h!=0					: contains at least one element (in this case `div`) with an offsetHeight
			 * )					: combined expression should detect only an element whose children are all absolutely positioned
			 * ||
			 * (
			 * div.offsetHeight<sh	: if the offset height is less than the sum of the contained elements height then something has been taken out of the flow of content by positioning
			 * )					: this expression should detect when contained elements are of mixed positioning ie. one child absolute and the other static or relative
			 */
			if ((div.offsetHeight==0 && h!=0) || div.offsetHeight<=sh) {
				// Extend shorter elements (Breaks redraw with resizable text)
				for (m=0;m<div.childNodes.length;m++) { c = div.childNodes[m]; if (c.nodeName=='DIV') { c.style.height = h+'px'; }}
				// Set the height of the containing element
				div.style.height = h+'px';
				
				}
			}
		}
	
	
	// First time through set up an invisible iframe to capture resized text
	if (!SI_frame) {
		var f = d.createElement('iframe');
		// For some reason the iframe needs to be in the viewport for 
		// the onresize event to fire in Safari so the position is set
		// to fixed
		f.style.position	= (d.all)?'absolute':'fixed';
		f.style.top		= '0px';
		
		// Setting the left to a negative number results in Safar 
		// ignoring or missing the iframe onresize event
		f.style.visibility	= 'hidden';
		
		// Smaller than 10em and Safari doesn't registe...you get the idea.
		f.style.width		= '10em';
		f.style.height		= '10em';
		f.setAttribute('src','js/iframe.html');
		d.body.appendChild(f);
		
		// Workaround for IE 5.0 PC which doesn't want to load the dynamically 
		// generated iframe. Lucky for us, the broken object still fires an
		// onresize event.
		if (d.all) f.onresize = SI_clearAbsolutes;
		
		// Let future calls to this function know we've already created the iframe
		SI_frame=true;
		}
	}
window.onload=SI_clearAbsolutes;

function SI_getNestingDepth(e) {
	var depth=0;
	do { depth++; e = e.parentNode; }
	while (e.nodeName!='BODY');
	return depth;
	}

/*************************************************************************
 * A simple function that replaces alert()
 * It writes to a div.id='write' creating that div if it doesn't exist
 *************************************************************************/
function w(val) { 
	var d=document;
	if (!d.getElementById) return;
	
	if (!d.getElementById('write')) {
		div = d.createElement('div');
		div.id = 'write'
		d.body.appendChild(div);
		}
	d.getElementById('write').innerHTML += val+'<br />';
	}