/*

 IMAGE PRELOADER WITH GRADIENT FADE

 taken from trashionArt (trashion.cjb.net)
 author: unknown

 adapted for DOM-Browers and modified for Ivory Gate by Danny Rehs (danny@ivorygate.de)
 version 1.0 (19.09.02)

*/

function initPreload(myImages, myRedirect)
{
	// GLOBAL STUFF:

	myImg = myImages;
	myRed = myRedirect;
	gap = 4; // Play arround with to avoid javascript errors

	preloadbarWidth = 328; // Should be greater than total amount of images you want to preload!
	preloadbarHeight = 20;
	preloadbarBorder = "#ffffff";
	preloadbarBackgr = "#444444";

	// Color the preloadbar is starting with - enter 1st, 3rd and 5th numbers/letters of color code
	startingColor = new Array();
	startingColor[0] = "4"; startingColor[1] = "4"; startingColor[2] = "4";
	
	// Color the preloadbar is going to end up with - enter the 1st, 3rd and 5th numbers/letters of color code
	endingColor = new Array();
	endingColor[0] = "f"; endingColor[1] = "f"; endingColor[2] = "f";
	
	a = 10, b = 11, c = 12, d = 13, e = 14, f = 15;
	convert = new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
	ones = new Array(), sixteens = new Array(), diff = new Array();
	imgLen = myImg.length, loaded = new Array(), preImages = new Array();
	currCount = 0, pending = 0, h = 0, hilite = new Array(), cover = new Array();
	num = Math.floor(preloadbarWidth/gap);

	// GRADIENT STUFF:

	var i, j;
	for (i = 0; i < 3; i++)
	{
		startingColor[i] = startingColor[i].toLowerCase();
		endingColor[i] = endingColor[i].toLowerCase();
		startingColor[i] = eval(startingColor[i]);
		endingColor[i] = eval(endingColor[i]);
		diff[i] = (endingColor[i]-startingColor[i])/num;
		ones[i] = Math.floor(diff[i]);
		sixteens[i] = Math.round((diff[i] - ones[i])*15);
	}
	endingColor[0] = 0; endingColor[1] = 0; endingColor[2] = 0;
	i = 0, j = 0;
	while (i <= num)
	{
		hilite[i] = "#";
		while (j < 3)
		{
			hilite[i] += convert[startingColor[j]];
			hilite[i] += convert[endingColor[j]];
			startingColor[j] += ones[j];
			endingColor[j] += sixteens[j];
			if (endingColor[j] > 15)
			{
				endingColor[j] -= 15;
				startingColor[j]++;
			}
			j++;
		}
		j = 0;
		i++;
	}
}

// LOADING STUFF:

function loadImages() // Start loading
{
	for (i = 0; i < imgLen; i++)
	{
		preImages[i] = new Image();
		preImages[i].src = myImg[i];
		loaded[i] = 0;
		cover[i] = Math.floor(num/imgLen)*(i+1);
	}
	cover[cover.length-1] += num%imgLen;
	checkLoad();
}
function checkLoad() // Redirect if done
{
	if (pending && document.getElementById) { changeto(); return; }
	if (currCount == imgLen) { location.replace(myRed); return; }
	for (i = 0; i < imgLen; i++)
	{
		if (!loaded[i] && preImages[i].complete)
		{
			loaded[i] = 1; pending++; currCount++;
			checkLoad();
			return;
		}
	}
	setTimeout("checkLoad()",10);
}
function changeto() // Update gradientbar (DOM only!)
{
	if (h+1 > cover[currCount-1])
	{
		var percent = Math.round(100/imgLen)*currCount;
		if (percent > 100) while (percent != 100) percent--;
		if (currCount == imgLen && percent < 100) percent = 100;
		defaultStatus = "Loaded " + currCount + " out of " + imgLen + " images [" + percent + "%].";
		pending--;
		checkLoad();
		return;
	}
	var cell = "cell" + (h+1);
	document.getElementById(cell).style.backgroundColor = hilite[h];
	h++;
	setTimeout("changeto()",10);
}
