var ColorTimer = null;
var CurrentColor = new Array();
var dr = 1;
var dg = -1;
var db = -1;
var r_done = false;
var g_done = false;
var b_done = false;

function dec2hex (dec)
{
	var hexChars = "0123456789ABCDEF";
	var a = dec % 16;
	var b = (dec - a) / 16;
	hex = '' + hexChars.charAt (b) + hexChars.charAt (a);
	return hex;
}

function setPhoneColor (r, g, b)
{
	var phone = document.getElementById ('phone');
	var color = '#' + dec2hex(r)+dec2hex(g)+dec2hex(b);
	if (phone) phone.style.color = color;
	window.status = CurrentColor.r.toString+" - "+CurrentColor.g.toString+" - "+CurrentColor.b.toString;
}


function initColors()
{
	CurrentColor.r = 0; // 207
	CurrentColor.g = 255; // 61
	CurrentColor.b = 255; // 36
	
	ColorTimer = setInterval (shiftColor, 50);
}

function shiftColor()
{
	var x = Math.floor (10 * Math.random());
	if (x < 4) CurrentColor.r = moveColorComponent(CurrentColor.r, 0, 255, "r");
	else if (x <7) CurrentColor.g = moveColorComponent(CurrentColor.g, 0, 255, "g");
	else CurrentColor.b = moveColorComponent(CurrentColor.b, 0, 255, "b");
	
	setPhoneColor (CurrentColor.r, CurrentColor.g, CurrentColor.b);

	if(r_done&&g_done&&b_done) {
		dr = -dr;
		db = -db;
		dg = -dg;
		r_done = false;
		g_done = false;
		b_done = false;
	}	
}

function moveColorComponent (value, min, max, code)
{
	var step = Math.floor (5 * Math.random())+10;
	
	var d;
	
	if(code=="r") d = dr;
	if(code=="g") d = dg;
	if(code=="b") d = db;
	
	if (d<0) {
		if(value-step>min) value-=step;
		else {
			value = min;
			if(code=="r") r_done = true;
			if(code=="g") g_done = true;
			if(code=="b") b_done = true;
		}
	} else {
		if(value+step<max) value+=step;
		else {
			value = max;
			if(code=="r") r_done = true;
			if(code=="g") g_done = true;
			if(code=="b") b_done = true;
		}
	}

	if(code=="r") dr = d;
	if(code=="g") dg = d;
	if(code=="b") db = d;

	return value;
}

initColors(); 