// derived from: http://www.warpgear.com/developer
// use single quotes to delimit attribute values
var bgcolor = "#e2ece2"; // background color, must be valid browser hex color (not color names)
var fcolor = "#306033";  // foreground or font color
var loop = true; // true = continue to display messages, false = stop at last message

var texts = new Array("<font size='6' color='{COLOR}'><b>One World...</b></font>");
var subtxt = 0;
var steps = 20; // number of steps to fade
var colors = new Array(steps);
getFadeColors(bgcolor,fcolor,colors);
var speed = 50;  // time for each fade step (when fading in or out)
var show = 7000; // milliseconds to display message
var sleep = 1800; // milliseconds to pause between messages
var subcolor = 0;
var step = 1;

var texts2 = new Array("<font size='5' color='{COLOR}'><b>endless opportunities</b></font>");
var subtxt2 = 0;
var steps2 = 20; // number of steps to fade
var colors2 = new Array(steps2);
getFadeColors(bgcolor,fcolor,colors2);
var speed2 = 50;  // time for each fade step (when fading in or out)
var show2 = 7000; // milliseconds to display message
var sleep2 = 2000; // milliseconds to pause between messages
var subcolor2 = 0;
var step2 = 1;

function startfade() {
	if (document.all) { // IE
		fader.innerHTML = "<font size='6'>&nbsp;</font>";
		fader2.innerHTML = "<font size='5'>&nbsp;</font>";
	}
	if (document.layers) { // Netscape
		document.fader.document.write("<font size='6'>&nbsp;</font>"); document.fader.document.close();
		document.fader2.document.write("<font size='5'>&nbsp;</font>"); document.fader2.document.close();
	}
	setTimeout("fadein()", 0);
	}
// fade: magic fader function
function fadein() {
	// insert fader color into message
	// texts should be defined in user script, e.g.: var texts = new Array("<font color='{COLOR}' sized='+3' face='Arial'>howdy</font>");
	var text_out = texts[subtxt].replace("{COLOR}", colors[subcolor]);
	// actually write message to document
	if (document.all) { fader.innerHTML = text_out; } // document.all = IE only
	if (document.layers) { document.fader.document.write(text_out); document.fader.document.close(); } // document.layers = Netscape only
	// select next fader color
	subcolor += step;
	// completely faded in?
	if (subcolor>=colors.length-1) { // loop should be defined in user script, e.g.: var loop=true;
		step = -1; // traverse colors array backward to fade out
		if (!loop && subtxt >= texts.length-1) return; // stop at last message if loop=false
	}
	// completely faded out?
	if (subcolor == 0) {
		step = 1; // traverse colors array forward to fade in again
		subtxt += 1; // select next message
		if (subtxt == texts.length) subtxt = 0; // loop back to first message
	}
	// timing logic
	// sleep and show should be defined in user script, e.g.: var sleep=30; var show=500;
	//setTimeout("fadein()", (subcolor==colors.length-2 && step==-1) ? show : ((subcolor == 1 && step == 1) ? sleep : speed));
	var pausetime = speed;
	if (subcolor == colors.length-2 && step == -1) { setTimeout("fadein2()", 1000);	}
	else {
		if (subcolor == 1 && step == 1) { pausetime = sleep; }
		setTimeout("fadein()", pausetime);
	}
}
function fadein2() {
	var text_out2 = texts2[subtxt2].replace("{COLOR}", colors2[subcolor2]);
	if (document.all) { fader2.innerHTML = text_out2; } // document.all = IE only
	if (document.layers) { document.fader2.document.write(text_out2); document.fader2.document.close(); } // document.layers = Netscape only
	subcolor2 += step2; // select next fader color
	if (subcolor2>=colors2.length-1) { // completely faded in?
		step2 = -1; // traverse colors array backward to fade out
		if (!loop && subtxt2 >= texts2.length-1) return; // stop at last message if loop=false
	}
	if (subcolor2 == 0) { // completely faded out?
		step2 = 1; // traverse colors array forward to fade in again
		subtxt2 += 1; // select next message
		if (subtxt2 == texts2.length) subtxt2 = 0; // loop back to first message
	}
	// timing logic
	var pausetime = speed2;
	if (subcolor2 == colors2.length-2 && step2 == -1) {
		setTimeout("fadeout()", show2);
	}
	else {
		if (subcolor2 == 1 && step2 == 1) { pausetime = sleep2; }
		setTimeout("fadein2()", pausetime);
	}
}
function fadeout() {
	var text_out = texts[subtxt].replace("{COLOR}", colors[subcolor]);
	var text_out2 = texts2[subtxt2].replace("{COLOR}", colors2[subcolor2]);
	if (document.all) { // document.all = IE only
		fader.innerHTML = text_out;
		fader2.innerHTML = text_out2;
	}
	if (document.layers) { // document.layers = Netscape only
		document.fader.document.write(text_out); document.fader.document.close();
		document.fader2.document.write(text_out2); document.fader2.document.close();
	}
	subcolor += step; // select next fader color
	subcolor2 += step2; // select next fader color
	if (subcolor == 0 || subcolor2 == 0) { // completely faded out?
		step = 1; // traverse colors array forward to fade in again
		step2 = 1;
	}
	// timing logic
	var pausetime = speed2;
	if (subcolor2 == 1 && step2 == 1) { pausetime = sleep2; setTimeout("fadein()", pausetime); }
	else { setTimeout("fadeout()", pausetime); }
}
// getFadeColors: fills Colors (predefined Array)
// with color hex strings fading from ColorA to ColorB
// note: Colors.length equals the number of steps to fade
function getFadeColors(ColorA, ColorB, Colors) {
	len = Colors.length;
	// strip '#' signs if present
	if (ColorA.charAt(0)=='#') ColorA = ColorA.substring(1);
	if (ColorB.charAt(0)=='#') ColorB = ColorB.substring(1);
	// substract rgb compents from hex string
	var r = HexToInt(ColorA.substring(0,2));
	var g = HexToInt(ColorA.substring(2,4));
	var b = HexToInt(ColorA.substring(4,6));
	var r2 = HexToInt(ColorB.substring(0,2));
	var g2 = HexToInt(ColorB.substring(2,4));
	var b2 = HexToInt(ColorB.substring(4,6));
	// calculate size of step for each color component
	var rStep = Math.round((r2 - r) / len);
	var gStep = Math.round((g2 - g) / len);
	var bStep = Math.round((b2 - b) / len);
	// fill Colors array with fader colors
	for (i=0; i<len-1; ++i) { Colors[i] = "#" + IntToHex(r) + IntToHex(g) + IntToHex(b); r+=rStep; g+=gStep; b+=bStep; }
	Colors[len-1]=ColorB; // make sure we finish exactly at ColorB
}
// IntToHex: converts integers between 0-255 into a two digit hex string.
function IntToHex(n) {
	var result = n.toString(16);
	if (result.length==1) result = "0"+result;
	return result;
}
// HexToInt: converts two digit hex strings into integer.
function HexToInt(hex) {
	return parseInt(hex, 16);
}
// body tag must include: onload="fade()" bgcolor="#000000"  where bgcolor equals bgcolor in javascript above
