	// This should be easy
var gallery     = new Array('graphic1', 'graphic2', 'graphic3');
var galleryObj  = new Array();
var collections = new Array();
var fadeTimers  = new Array();
var outTimers   = new Array();
var inTimers    = new Array();
var fading = false;

function getRandomArbitary(min)
{
	rand = (Math.random() + 1);
  	return rand * min;  		
}


function initFade()
{
	if (fading)
		return;
	fading = true;
	// Let's attach ourselves to the graphics collections.
	for (i = 0; i < gallery.length; i++)
	{
		if (document.getElementById(gallery[i]))
			galleryObj[i] = document.getElementById(gallery[i]);
		else
			galleryObj[i] = null;
			
		collections[i] = galleryObj[i].getElementsByTagName('img');
	}
	
	for (i = 0; i < collections.length; i++)
	{
		for (j = 0; j < collections[i].length; j++)
			if (j > 0)
			{
				collections[i][j].style.opacity = 0;
				collections[i][j].style.MozOpacity = 0;
				collections[i][j].style.filter = "alpha(opacity=0)";
			} else {
				collections[i][j].style.opacity = 1;
				collections[i][j].style.MozOpacity = 1;
				collections[i][j].style.filter = "alpha(opacity=100)";
			}
	}

	for (i = 0; i < gallery.length; i++)
		if (collections[i].length > 1)
			fadeTimers[i] = setTimeout("runFade(" + i + ");", parseInt(getRandomArbitary(7500)));
}

function runFade(galleryNo)
{
	var showing = false;
	for (i = 0; i < collections[galleryNo].length; i++)
	{
		if (!document.all)
		{
			if (parseInt(collections[galleryNo][i].style.opacity) == 1)
				showing = i;
			if (parseInt(collections[galleryNo][i].style.MozOpacity) == 1)
				showing = i;
		} else {
			if (parseInt(collections[galleryNo][i].style.filter.substring(14, 17)) > 90)
				showing = i;
		}
	}
	fadeOut(galleryNo, showing);
	next = ((showing + 1) > (collections[galleryNo].length - 1))?0:(showing + 1);
	fadeIn(galleryNo, next);
	if (fadeTimers[galleryNo]) 
	 clearTimeout(fadeTimers[galleryNo]);
	timer = parseInt(getRandomArbitary(7500));
	fadeTimers[galleryNo] = setTimeout("runFade(" + galleryNo + ");", timer);
}

function fadeOut(i, j)
{
	if (outTimers[i])
	 clearTimeout(outTimers[i])
	obj = collections[i][j];
	if (obj)
	{
		var opacity = parseFloat(obj.style.opacity);
		opacity -= .05;
		if (opacity >= -.1)
		{
		  obj.style.opacity = opacity;
			obj.style.MozOpacity = opacity;
			obj.style.filter = "alpha(opacity=" + (opacity * 100) + ")";
			outTimers[i] = setTimeout("fadeOut(" + i + ", " + j + ")", 150);
		}
	}
}

function fadeIn(i, j)
{
	if (inTimers[i])
	 clearTimeout(inTimers[i])
	obj = collections[i][j];
	if (obj)
	{
		var opacity = parseFloat(obj.style.opacity);
		opacity += .05;
		//alert(opacity);
		if (opacity <= 1.1)
		{
		  obj.style.opacity = opacity;
			obj.style.MozOpacity = opacity;
			obj.style.filter = "alpha(opacity=" + (opacity * 100) + ")";
			inTimers[i] = setTimeout("fadeIn(" + i + ", " + j + ")", 50);
		}
	}
}