/*
 * Code to handle loading pixels with a timeout.   
 */ 
function loadPixelWithTimeout(pixelUrl, timeout) {
	if (document.pixels == null) {
		document.pixels = new Array();
	}
	if (document.pixelInitTimes == null) {
		document.pixelInitTimes = new Array();
	}
	if (document.pixelTimeouts == null) {
		document.pixelTimeouts = new Array();
	}
	
	idx = document.pixels.length;
	document.pixels[idx] = new Image(1,1);
	document.pixelInitTimes[idx] = new Date();	
	document.pixelTimeouts[idx] = timeout;
	document.pixels[idx].src = pixelUrl;
	
	pixelLoader_startChecking();
}

function pixelLoader_startChecking() {
	if (document.pixelLoaderStarted == null) {
		document.pixelLoaderStarted = "yes";
		setTimeout('pixelLoader_setSrcToNull()', 2500);
	}
}

function pixelLoader_setSrcToNull(pixelUrl) {
	now = new Date();
	callAgain = false;
	
	if (document.pixels != null) {
		for (var i = 0; i < document.pixels.length; i++) {
			if (document.pixels[i] != null) {
				callAgain = true;
				if (document.pixelInitTimes[i].getTime() + document.pixelTimeouts[i] < now.getTime()) {
					// needed to not force another request to the page your loading.
					document.pixels[i].src = "/skylights/images/pixelLoaderBlank.gif";
					document.pixels[i] = null;
				}
			}
		}
	}
	
	// iframe loading timeouts held in the loadTimeout attribute in the iframe tag
	var iframeObjs = document.getElementsByName("pixelFrame");
	if (iframeObjs != null) {
		for (var i = 0; i < iframeObjs.length; i++) {
		    var anIFrame = iframeObjs[i];
			if (anIFrame.initTime == null) {
				anIFrame.initTime = new Date();
				callAgain = true;
			}
			else if (anIFrame.finishedLoading == "true")
			{
				continue;
			}
			else if (anIFrame.src.indexOf("pixelLoaderBlank.html") == -1) {
				 var timeout = Number(anIFrame.getAttribute("loadTimeout"));
				 if (timeout < 1000 || timeout > 120000) {
				 	// sanity check on the timeout; set to 30 seconds if insane
				 	timeout = 30000;
				 }
				 
				 if (anIFrame.initTime.getTime() + timeout < now.getTime()) {
				 	anIFrame.src = "/global/pixelLoaderBlank.html";
				 }
				 else {
				 	callAgain = true;
				 }
			}
		}	
	}
	  
	if (callAgain) {
		setTimeout('pixelLoader_setSrcToNull()', 2500);
	}
}