bgcolor = "#666677"; // match initial bgcolor in body tag- lowercase!
currentcolor = bgcolor;
changeSpeed = 8; // the color increment/decrement - not time
fadetimer = 30;// timer interval - not implementd
nuClr ="";
i=0;// for dev

function fadeToWhite() {
	nuClr="#ffffff";
	fadeToNuClr(nuClr);
}

function fadeToBlack() {
	nuClr="#000000";
	fadeToNuClr(nuClr);	
}
///////////////////////////////////////////////////////////

function fadeToNuClr(nuClr) {
// in hex $123abc


	nuClr = nuClr.toLowerCase();
	currentcolor = currentcolor.toLowerCase();
	if(nuClr != currentcolor) {
	nuClrFade_int = setInterval(doFadeNuClr,30);
	}// end test
}// end fade to nu clr


function doFadeNuClr() {
//alert(nuClr + " and current color is " + currentcolor);

	if (nuClr != currentcolor) {		
	// get nuClr in rgb hex values as the targetRGBs"
	targetR = HexToR(nuClr);
	targetG = HexToG(nuClr);
	targetB = HexToB(nuClr);	
	// get currentcolor in rgb hex values:
	currentR = HexToR(currentcolor);
	currentG = HexToG(currentcolor);
	currentB = HexToB(currentcolor);
	
	processColors(targetR,targetG,targetB,currentR,currentG,currentB);
			
	} else {
		clearInterval(nuClrFade_int);

	}
}

//////////////                 /////////////////

function fadeToBGClr() {

// in hex $123abc
	nuClr = nuClr.toLowerCase();
	currentcolor = currentcolor.toLowerCase();	
	if(bgcolor != currentcolor) {	
	bgFade_int = setInterval(doFadeBG,45);// time
	}// end test
}

function doFadeBG() {
//alert(bgcolor + " and current color is " + currentcolor);

	if (bgcolor != currentcolor) {
	
	//alert("not equall - doing doFadeBG by " + changeSpeed);
	
	// get bgcolor in rgb hex values as the targetRGBs
	targetR = HexToR(bgcolor);
	targetG = HexToG(bgcolor);
	targetB = HexToB(bgcolor);
	
	// get currentcolor in rgb hex values
	currentR = HexToR(currentcolor);
	currentG = HexToG(currentcolor);
	currentB = HexToB(currentcolor);
	
	processColors(targetR,targetG,targetB,currentR,currentG,currentB);
	
	} else {
		clearInterval(bgFade_int);
		
	}
}// end bgfade


function processColors(targetR,targetG,targetB,currentR,currentG,currentB) {
	
	//need hex to rgb greyscale  equivalents for all 3 colors
	//RED:
	if(currentR > targetR) {
	//alert ("current is bigger " + currentR);
	currentR -= changeSpeed;
	currentR = currentR < targetR ? targetR : currentR;
	}
	
	if(currentR < targetR) {
	//alert ("currentR is smaller " + currentR);
	currentR += changeSpeed;
	currentR = currentR > targetR ? targetR : currentR;
	}
	//GREEEN:	
	if(currentG > targetG) {
	//alert ("currentG is bigger " + currentG);
	currentG -= changeSpeed;
	currentG = currentG < targetG ? targetG : currentG;
	}
	
	if(currentG < targetG) {
	//alert ("currentG is smaller " + currentG);
	currentG += changeSpeed;
	currentG = currentG > targetG ? targetG : currentG;
	}	
	//BLUE:
	if(currentB > targetB) {
	//alert ("current is bigger " + currentB);
	currentB -= changeSpeed;
	currentB = currentB < targetB ? targetB : currentB;
	}
	
	if(currentB < targetB) {
	//alert ("currentB is smaller " + currentB);
	currentB += changeSpeed;
	currentB = currentB > targetB ? targetB : currentB;
	}
	
	changeBg(rgbToHex(currentR,currentG,currentB));

}// end process colors

function HexToR(h) { return parseInt((cutHex(h)).substring(0,2), 16) }
function HexToG(h) { return parseInt((cutHex(h)).substring(2,4), 16) }
function HexToB(h) { return parseInt((cutHex(h)).substring(4,6), 16) }
function cutHex(h) { return (h.charAt(0)=="#") ? h.substring(1,7) : h}

/////////////////////
function changeBgColor(clr) {
// changes attribute then fades to it
clr= clr.toLowerCase();
	bgcolor=clr;
	fadeToBGClr();	
}
/////////////////////
function changeBg(color) {
color=color.toLowerCase();
	document.bgColor=color;
	currentcolor=color;
}
//////////////////////
function rgbToHex(r,g,b) {
	var tempR;
	var tempG;
	var tempB;
	tempR = r.toString(16);
	tempG = g.toString(16);
	tempB = b.toString(16);
	tempR = tempR.length > 1 ? tempR : "0" + tempR;
	tempG = tempG.length > 1 ? tempG : "0" + tempG;
	tempB = tempB.length > 1 ? tempB : "0" + tempB;
	return "#" + tempR.toString() + tempG.toString() + tempB.toString();
}// end rgbtohex


