//  Monday, 2 Jan. 2006 - Beak f(x) Colorshit.js v1.0

// OK, may 26th, 2005 added some stuff, changed all step values to 1, and added a list box (select object)
// to pick the interval of the color change, in miliseconds. This is cool, but I really don't know what I'm doing
// so more to be done later, but the idea getting closer.

// Friday, 30 Dec. 2005 -- This is getting messy (how could it not?) so time to move everything into sperate compartments.

// Monday, 2 Jan. 2006 -- OK, I'll leave the inverse color stuff in, but just comment it out to save time.
// May use it someday. Oh, shit...the last thing is to seperate the js file...uhg.


var panel = new Array;
      panel[0] = new Image();
      panel[0].src = "/images/flat_switchOFF_60.gif";
      panel[1] = new Image();
      panel[1].src = "/images/flat_switchON_60.gif";      

var stopCycle = 0;
var statusBarFlag = 1;
var clampDown = 51;
var clampUp = 204;
var globalCount = 0;
var isPaused;
var isGoing;

function makeColorObj(name) {

	this.name = name;
	this.hex = "CC";
	this.hexInv = "33"
	this.dec = 204;
	this.max = 1;
	this.goingUp = 1;
	this.goingDown = 1;
	return this;
}

redObj = new makeColorObj("red");
greenObj = new makeColorObj("green");
blueObj = new makeColorObj("blue");

colorObj = new Object();
colorObj.red = redObj; colorObj.green = greenObj; colorObj.blue = blueObj;

// main control function
function swCon(item, state) {

	if (item == 0) { //this is image1, the main switch
		if (state) {
			if (isGoing && !isPaused) {return 0;} // avoid getting stuck on
			document.images[item].src = panel[1].src; //image to ON
			isPaused = 0;  //we're running
			main(); // run the main
			//window.status="Running";
			return;  //get the fuck out
		}
		
		document.images[item].src = panel[0].src; // image to OFF
		if (isGoing) {
			clearInterval(isGoing); //pause main if started
			//window.status="Paused: Last Color " + eval(hexformat);
		}
		isPaused = 1;  //leave a mark because I'm confused...
		return; //leave now
	}
	
	if (item == 1) {
		if (state) { // state is ON
			document.images[item].src = panel[1].src; //image to ON
			statusBarFlag = 1; // set the flag for status update
			window.status="Status On"
			return;  //leave
			}
		//otherwise state is OFF
		document.images[item].src = panel[0].src; //click!
		statusBarFlag=0;
		window.status="Status Off"; // try this
		return; // do I need to do this?
	}
	if (item == 2) {  // here we clamp the color range from 0x33 to 0xCC (16-0x33) for nicer colors
		if (state) {  // as is ON
			document.images[item].src = panel[1].src; //image to ON	
			clampDown = 51;
			clampUp = 204;
			init_page(204, 204, 204);
			return;
		} // clamp switch turned on
		document.images[item].src = panel[0].src;
		clampDown = 1;
		clampUp = 254;
		init_page(255, 255, 255);
		return;
	}
	
}

function speedChange(speed) {
	coreTiming = speed;
	if (isGoing) {
		clearInterval(isGoing); //pause main	 
		updateText ();
		if (isPaused) {
			return;
		}
		main(); //hopefully re-start loop where it left off.
		return true;
	}
	 isGoing=0;
} // that was a narrrow escape, I realize that should be cleaner, but I'm so close to 'live'.

function addCommas(nStr) {
var x, x1, x2;
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function updateText () {
var globalformat = addCommas(globalCount);
	document.getElementById("hexOutDiv").childNodes[0].nodeValue = hexformat;
	document.getElementById("decOutDiv").childNodes[0].nodeValue = decformat;
	document.getElementById("totalOutDiv").childNodes[0].nodeValue = globalformat;
	document.getElementById("timingOutDiv").childNodes[0].nodeValue = coreTiming;
}  //ui updater

function getHexColor (decColor) {
var hexdig01, hexdig10, hexColor;
var hexnumbers = "0123456789ABCDEF";

	hexColor = Math.floor(decColor / 16);
	hexdig10 = hexnumbers.substring(hexColor,hexColor+1);
	hexColor *=16;
	hexColor = decColor - hexColor;
	hexdig01 = hexnumbers.substring(hexColor,hexColor+1);
	hexColor = hexdig10 + hexdig01;
	return hexColor;

} //get hex

// try a new approach here, one that allows reversing and dynamic speed changes

function init_page(Rval, Gval, Bval){

	//init red object @ given value
	colorObj.red.dec = Rval;
	colorObj.red.hex = getHexColor(Rval);
	colorObj.red.hexInv = getHexColor (255 - Rval);
	
	//init green object @ given value
	colorObj.green.dec = Gval;
	colorObj.green.hex = getHexColor(Gval);
	colorObj.green.hexInv = getHexColor (255 - Gval);	
	
	//init blue object @ given value
	colorObj.blue.dec = Bval;
	colorObj.blue.hex = getHexColor(Bval);
	colorObj.blue.hexInv = getHexColor (255 - Bval);
	
	// OK, let's set the bg color and hope for the best...
	hexformat= "\"\#" + colorObj.red.hex + colorObj.green.hex  + colorObj.blue.hex + "\"";
	document.bgColor= eval(hexformat);
	return true;
} // init done

function colorRamp (color, dir) {  // this here is the clever bit..

if (dir == "down") {
	colorObj[color].dec--;
	if (colorObj[color].dec < clampDown) {  // oops, gone too far
		colorObj[color].max = 0;
		colorObj[color].dec = clampDown;  // reset dec and proceed.
		return;
	}
}
if (dir == "up") {
	colorObj[color].dec++;
	if (colorObj[color].dec > clampUp) {
		colorObj[color].max = 1;
		colorObj[color].dec = clampUp;
		return;  //done
	}
}

colorObj[color].hex = getHexColor(colorObj[color].dec);
colorObj[color].hexInv = getHexColor(255 - (colorObj[color].dec) );
hexformat= "\"\#" + colorObj.red.hex + colorObj.green.hex  + colorObj.blue.hex + "\"";
//invformat =  "\"\#" + colorObj.red.hexInv + colorObj.green.hexInv  + colorObj.blue.hexInv + "\"";
decformat = " " + colorObj.red.dec + ", " + colorObj.green.dec + ", " + colorObj.blue.dec;
if (statusBarFlag) {updateText();}
document.bgColor= eval(hexformat);
//document.fgColor=eval(invformat);
globalCount++;
return true;
}  // ho-rah

function main () {
	if (!window.coreTiming) { coreTiming = 100 };
	isGoing = setInterval("figureLoop()", coreTiming);
}

function figureLoop() {

	if (stopCycle) {
		//if (!colorObj.blue.max) {colorRamp('blue', "down"); return; }
		colorRamp('green', "up");
		colorRamp('blue', "up");
		colorRamp('red', "up");
		// any 'done' messages go here
		return;
	}
	if (colorObj.red.max && colorObj.green.max && colorObj.blue.max) {	//start cycle on red
		colorRamp('green', "down");
		colorRamp('blue', "down");
	}
	if ( colorObj.red.max && !colorObj.green.max && !colorObj.blue.max) {	//orange - yellow
		colorRamp('green', "up")
	}
	if (colorObj.red.max && colorObj.green.max && !colorObj.blue.max) {	//green
		colorRamp ('red', "down")
	}
	if (!colorObj.red.max && colorObj.green.max && !colorObj.blue.max) {		//green - cyan
		colorRamp('blue', "up")
	}
	if (!colorObj.red.max && colorObj.green.max && colorObj.blue.max) {	//blue
		colorRamp('green', "down")
	}
	if (!colorObj.red.max && !colorObj.green.max && colorObj.blue.max) {		//indigo violet
		colorRamp('red', "up")
		
	}
	//finish up after this
	if (colorObj.red.max && !colorObj.green.max && colorObj.blue.max) {		// green only, blue up
		colorRamp('blue', "down");
		stopCycle=1;  //that triggers the stopping bit at the top. Though it's also blocking blue fro going away.
	}
}//fun
