/*
 * onDOMLoaded - crossbrowser boot event
 *
 * Author: Diego Perini <dperini@nwbox.com>
 * Updated: 22/06/2006
 * Version: 0.98-debug
 * Project: IPORT <http://www.iport.it>
 *
 * arguments:
 * @w window (Object)
 * 
 * history of changes last to first:
 *
 * - fix Opera < 9 not having scripts included in innerHTML
 * - changed method of detecting DOMLoaded, getSize of URL
 *
 *
 * Note:
 *
 * a fix for Opera < 9 not having styles included in innerHTML is not yet available
 * use externally linked stylesheets as an alternative (you should however, really)
 *
 * this is still an experimental approach to a known WEB problem
 * the method used still does not guarantee 100% that the last
 * element of the body is referenced in the DOM, but it is
 * the nearest aproximation I could build up to now...
 *
 * Another solution would be to always use GET and with clever
 * RegExp find out the last elements at the end of the BODY,
 * then loop until that part is found in the DOM.
 *
 * I do not like using innerHTML, so please suggest a better way.
 *
 * Again, it may be redundant to say this but sounds
 * to me like we could avoid all this code by inserting:
 *
 * <img src="" onerror="fireEvents(this)" width="1" height="1">
 *
 * as the very last line before the BODY closes.
 *
 * I know/understand not all the situations are the same though.-
 * So please test and report on the blogs/forums.
 *
 */

function onDOMLoaded(w, n) {
	var d = w.document;
	if (typeof n == 'undefined') {
		this.HtmlSize = getSize(d.location.href) - 200;
		// remove after debug
		window.status += ' | Minimum size to match ' + this.HtmlSize;
		var s = document.scripts;
		n = 0;
	}
	var size = 0;
	if (typeof window.opera != 'undefined') {
		var i, l, s;
		s = document.getElementsByTagName('script');
		l = s.length;
		for (i = 0; l > i; i++) {
			if (s[i].src.length == 0) {
				size += s[i].text.length;
			}
		}
	}
	size += d.documentElement.innerHTML.length;
	if (size >= this.HtmlSize) {
		this.HtmlSize = void(0);
		// remove after debug
		window.status += ' | innerHTML size is ' + size;
		init();
	} else if (1000 > n) {
		// remove after debug
		window.status += ' | innerHTML size is ' + size;
		setTimeout(function() { onDOMLoaded(w, n); }, n);
		if (10 > n) { n++; } else { n += 10; }
	}
};

function getSize(url) {
	var r = null;
	try { r = new XMLHttpRequest();	}
	catch(e) { r = new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') != -1) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'); }
	r.open('HEAD', url, false);
	r.send(null);
	var cl = r.getResponseHeader('Content-Length');
	if (cl > 0) {
		return cl;
	} else {
		r.open('GET', url, false);
		r.send(null);
		return r.responseText.length;
	}
}
