// waits for specified frames or iframes to load
// 
// EXAMPLE USAGE
//
// fLoadHandler = function() { alert('loaded'); }
// fTimeout = function() { alert('timed out while loading'); }
// frameLoad.watch(window.top.frames.myTop, fLoadHandler, fTimeout, 15000);

var frameLoad = {
	watchInterval: 13,
	watchIntervalObject: null,
	currentTime: 0,
	timeout: 15000,
	frames: new Array(),
	loadHandler: function() {},
	timeoutHandler: function() {}, 
	watch: function(aFrames, fLoadHandler, fTimeout, iTimeout) {
		if(aFrames.length) { this.frames = aFrames; }
		else { this.frames = [aFrames]; }
		this.loadHandler = fLoadHandler;
		this.timeoutHandler = fTimeout;
		if(typeof(iTimeout)=='number') { this.timeout = iTimeout; }
		
		// do the watch
		this.currentTime = 0;
		this.watchIntervalObject = setInterval(function() { frameLoad.allFramesReady(); }, this.watchInterval);
	},
	allFramesReady: function() {
		// check for timeout
		this.currentTime += this.watchInterval;
		if(this.currentTime > this.timeout) {
			this.timedOut();
			return false;
		}
		
		// check loaded frames
		for(var i=0; i<this.frames.length; i++) {
			var oFrame = this.frames[i];

			// make sure we have a DOM to work with
			if (!oFrame || !oFrame.document || !oFrame.document.body) {
				return false;
			}
		}
		
		// all DOMs showed up so all frames must be loaded
		this.loaded();
		return true;
	},
	loaded: function() {
		clearInterval(this.watchIntervalObject);
		this.watchIntervalObject = null;
		this.loadHandler();	
	},
	timedOut: function() {
		clearInterval(this.watchIntervalObject);
		this.watchIntervalObject = null;
		this.timeoutHandler();
	}
}
