/****************************************************************************************************

	Copyright (C) 2004 -2005 Daniel Kauffman. All rights reserved.
	
	THIS SOFTWARE MAY NOT BE USED EXCEPT UNDER LICENSE.
	
	ABSOLUTELY NO WARRANTY IS PROVIDED.
	
	For licensing information, you may contact:
		"Daniel Kauffman"
		Daniel (dot) Kauffman (at) rocksolidsolutions (dot) org
	
	This software is not in the public domain and may not be used for any purpose,
	except as provided under license.
	
****************************************************************************************************/

function class_color_set_rgb(x_r, x_g, x_b) {
	
	/* Clear interval */
	if(this.interval) {
		window.clearInterval(this.interval);
		this.interval = null;
	}
	
	/* Set color */
	this.r = x_r;
	this.g = x_g;
	this.b = x_b;

	/* Set style */
	switch (this.element_style) {
		case 'backgroundColor':
			this.element.style.backgroundColor = this.get_rgb();
			break;
		case 'color':
			this.element.style.color = this.get_rgb();
			break;
	}

}

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) {
	
	/* Clear interval */
	if(this.interval) {
		window.clearInterval(this.interval);
		this.interval = null;
	}
	
	/* 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 setInterval function) */
	var x_object_index = Math.random().toString();
	private_class_color_array[x_object_index] = this;
	
	/* Begin steps to target */
	this.interval = window.setInterval("private_class_color_change_step('" + x_object_index + "')", 40);

}

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;
	this.interval = 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 setInterval 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 setInterval 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;
		}
		
	}
	
	/* If at target... */
	else {
		
		/* Clear interval */
		if(x.interval) {
			window.clearInterval(x.interval);
			x.interval = null;
		}
		
		/* 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;
		
		/* Once change_to method is complete... evaluate next statement */
		eval(x.oncomplete);
		
	}
}
