var timeout = 3000;

function addFadeOnLoad(divTagId, tagName) {
	window.addEventListener?window.addEventListener("load",function(event){crossFade(divTagId, tagName)}, false):window.attachEvent("onload",function(event){crossFade(divTagId, tagName)});
}

function setOpacity(obj) {
	if(obj.xOpacity>.99) {
		obj.xOpacity = .99;
		return;
	}
	obj.style.opacity = obj.xOpacity;
	obj.style.MozOpacity = obj.xOpacity;
	obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
}

function crossFade(divTagId, tagName) {
	var current = 0;
	var objs = new Array();
	var divTag = document.getElementById(divTagId);
	objs = divTag.getElementsByTagName(tagName);
	for(i=1;i<objs.length;i++) objs[i].xOpacity = 0;
	objs[0].style.display = "block";
	objs[0].xOpacity = .99;

	window.setTimeout(function(){doFade(current,objs)}, timeout);
}
function doFade(current, objs) {
	cOpacity = objs[current].xOpacity;
	nIndex = objs[current+1]?current+1:0;

	nOpacity = objs[nIndex].xOpacity;
	
	cOpacity-=.05; 
	nOpacity+=.05;
	
	objs[nIndex].style.display = "block";
	objs[current].xOpacity = cOpacity;
	objs[nIndex].xOpacity = nOpacity;
	
	setOpacity(objs[current]); 
	setOpacity(objs[nIndex]);
	
	if(cOpacity<=0) {
		objs[current].style.display = "none";
		current = nIndex;
		var o = objs;
		window.setTimeout(function(){doFade(current,o)}, timeout);
	} else {
		var o = objs;
		window.setTimeout(function(){doFade(current,o)}, 50);
	}
}
