/* $Id: fading.js,v 1.1 2002/10/02 18:49:42 shaggy Exp $ */

/*
Copyright (c) 2001, 2002 by Martin Tsachev. All rights reserved.
http://www.mt-dev.com

Redistribution and use in source and binary forms,
with or without modification, are permitted provided
that the conditions available at
http://www.opensource.org/licenses/bsd-license.html
are met.
*/

var r = g = b = 0;
var chR = 30;
var chG = chB = 0;
var fader = null;
var timer = null;

function setColor(R, G, B) {
	chR = R;
	chG = G;
	chB = B;
	// this is needed because often colors get out of range
	r = g = b = 0;
}

function fadeIn(obj) {
	if ( obj.style ) { // browser ok
		fader = obj;
		if ( timer )
			clearTimeout(timer);
		fadeReal(chR, chG,chB)
	}
}

function fadeOut(obj) {
	if ( obj.style ) { // browser ok
		fader = obj;
		if ( timer )
			clearTimeout(timer);
		fadeReal(-chR,-chG,-chB)
	}
}

function fadeReal(chR, chG, chB) {
	r += chR; // update color values
	g += chG;
	b += chB;

	if ( ( r >= 0 ) && ( r < 200 ) && ( g >= 0 ) && ( g < 256 ) && ( b >= 0 ) && ( b < 256 ) ) {
		fader.style.color = "rgb(" + r + "," + g + "," + b + ")";
		timer = setTimeout("fadeReal(" + chR + "," + chG + "," + chB + ")", 100);
	}
}

