function class_color_set_rgb(x_r, x_g, x_b) {

	// Set color
	this.r = x_r;
	this.g = x_g;
	this.b = x_b;
	
}

function class_color_get_rgb() {

	// Return color as CSS string
	return 'rgb(' + parseInt(this.r) + ',' + parseInt(this.g) + ',' + parseInt(this.b) + ')';
	
}

function class_color_change_to(x_r, x_g, x_b, x_step_count, x_oncomplete) {
	
	// Set target color
	this.r_target = x_r;
	this.g_target = x_g;
	this.b_target = x_b;
	
	// Set steps to target
	this.step_count = x_step_count;
	
	// Set statement to evaluate on completion of change_to method
	this.oncomplete = x_oncomplete;
	
	// Expose object using unique associative index in global array (necessary for object to be used with setTimeout function)
	var x_object_index = Math.random().toString();
	private_class_color_array[x_object_index] = this;
	
	// Begin steps to target
	private_class_color_change_step(x_object_index);
	
}

function class_color_init(x_div_id, x_style) {
	
	// Set default properties
	this.element = document.getElementById(x_div_id);
	this.element_style = x_style;
	this.r = null;
	this.g = null;
	this.b = null;
	this.r_target = null;
	this.g_target = null;
	this.b_target = null;
	this.step_count = null;
	this.oncomplete = null;
	
	// Set default methods
	this.set_rgb = class_color_set_rgb;
	this.get_rgb = class_color_get_rgb;
	this.change_to = class_color_change_to;
	
}

// This global array is used to expose/retrieve objects for use with setTimeout function
var private_class_color_array = new Array();

function private_class_color_change_step(x_object_index) { 
	
	// Retrive object using unique associative index in global array (necessary for object to be used with setTimeout function)
	var x = private_class_color_array[x_object_index];
	
	// If not yet at target...
	if ( x.step_count > 0 ) {
		
		// Step to target
		x.r = x.r + ((x.r_target - x.r) / x.step_count);
		x.g = x.g + ((x.g_target - x.g) / x.step_count);
		x.b = x.b + ((x.b_target - x.b) / x.step_count);
		x.step_count = x.step_count - 1;
		
		// Set style
		switch (x.element_style) {
			case 'backgroundColor':
				x.element.style.backgroundColor = x.get_rgb();
				break;
			case 'color':
				x.element.style.color = x.get_rgb();
				break;
		}
		
		// Repeat with timeout
		window.setTimeout("private_class_color_change_step('" + x_object_index + "')", 40);
		
	}
	
	// If at target...
	else {
		
		// Take care of rounding errors
		x.r = x.r_target;
		x.g = x.g_target;
		x.b = x.b_target;
		
		// Clear object properties used for the change_to method
		x.r_target = null;
		x.g_target = null;
		x.b_target = null;
		x.step_count = null;
		private_class_color_array[x_object_index] = undefined;
		
		// change_to method is complete... evaluate next statement
		//alert(x.oncomplete);
		eval(x.oncomplete);
		
	}
}
