/********************************************************/
/*       			FX: Color-Fader						*/
/********************************************************/

// e_id 			= die ID des Elements
// e_style			= das Farbelement zum highlighten (z.B.: color, bgcolor, bordercolor etc.)
// e_color_start	= Startfarbe fürs faden
// e_color_end		= Endfarbe fürs faden
// e_speed			= die Geschwindigkeit in msek. für die Farbübergänge von (10 => sehr schnell | 100 => laaangsam)

function fx_fader_color(e_id, e_style, e_color_start, e_color_end, e_speed){
	
	if(e_id && e_style){
		if(e_color_start){
			var color_start_obj = new get_rgb_color(e_color_start);
						
			if(color_start_obj.ok){
				var rgb_color_start_r = color_start_obj.r;
				var rgb_color_start_g = color_start_obj.g;		
				var rgb_color_start_b = color_start_obj.b;
			}else{
				var rgb_color_start_r = 255;
				var rgb_color_start_g = 255;		
				var rgb_color_start_b = 255;
			}
		}
		else{
			var rgb_color_start_r = 255;
			var rgb_color_start_g = 255;		
			var rgb_color_start_b = 255;
		}
		
		if(e_color_end){
			var color_end_obj = new get_rgb_color(e_color_end);
			
			if(color_end_obj.ok){
				var rgb_color_end_r = color_end_obj.r;
				var rgb_color_end_g = color_end_obj.g;		
				var rgb_color_end_b = color_end_obj.b;
			}else{
				var rgb_color_end_r = 255;
				var rgb_color_end_g = 255;		
				var rgb_color_end_b = 204;
			}			
		}
		else{
			var rgb_color_end_r = 255;
			var rgb_color_end_g = 255;		
			var rgb_color_end_b = 204;
		}
		
		if(!e_speed){
			e_speed = 20;
		}
		
		fade_color(rgb_color_start_r, rgb_color_start_g, rgb_color_start_b, rgb_color_end_r, rgb_color_end_g, rgb_color_end_b, e_speed, 1, e_id, e_style);
	}
}

function setColor(r, g, b, e_id, e_style) {
	var this_one = document.getElementById(e_id);
	var hr = hex(r); var hg = hex(g); var hb = hex(b);

	if(e_style == 'bgcolor'){
		this_one.style.backgroundColor = '#'+hr+hg+hb;
	}else if(e_style == 'bordercolor'){
		this_one.style.borderColor = '#'+hr+hg+hb;
	}else if(e_style == 'color'){
		this_one.style.color = '#'+hr+hg+hb;
	}else{
		this_one.style.backgroundColor = '#'+hr+hg+hb;
	}	
}

function fade_color(sr, sg, sb, er, eg, eb, step, direction, e_id, e_style){
    for(var i = 0; i <= step; i++) {
		window.setTimeout("setColor(Math.floor(" +sr+ " *(( " +step+ " - " +i+ " )/ " +step+ " ) + " +er+ " * (" +i+ "/" +step+ ")),Math.floor(" +sg+ " * (( " +step+ " - " +i+ " )/ " +step+ " ) + " +eg+ " * (" +i+ "/" +step+ ")),Math.floor(" +sb+ " * ((" +step+ "-" +i+ ")/" +step+ ") + " +eb+ " * (" +i+ "/" +step+ ")),'"+e_id+"','"+e_style+"');",i*step);		
    }
}

// Farbberechnungen
/********************************************************/

function makearray(n) {
    this.length = n;
    for(var i = 1; i <= n; i++)
        this[i] = 0;
    return this;
}

hexa = new makearray(16);
for(var i = 0; i < 10; i++)
hexa[i] = i;
hexa[10]="a"; hexa[11]="b"; hexa[12]="c";
hexa[13]="d"; hexa[14]="e"; hexa[15]="f";

function hex(i) {
    if (i < 0)
        return "00";
    else if (i > 255)
        return "ff";
    else
       return "" + hexa[Math.floor(i/16)] + hexa[i%16];
}


function get_rgb_color(color_string){

	this.ok = false;
	
	// strip any leading #
	if (color_string.charAt(0) == '#') { // remove # if any
		color_string = color_string.substr(1,6);
	}
	
	color_string = color_string.replace(/ /g,'');
	color_string = color_string.toLowerCase();
	
	// array of color definition objects
	var color_defs = [
	{
		re: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/,
		example: ['rgb(123, 234, 45)', 'rgb(255,234,245)'],
		process: function (bits){
			return [
				parseInt(bits[1]),
				parseInt(bits[2]),
				parseInt(bits[3])
			];
		}
	},
	{
		re: /^(\w{2})(\w{2})(\w{2})$/,
		example: ['#00ff00', '336699'],
		process: function (bits){
			return [
				parseInt(bits[1], 16),
				parseInt(bits[2], 16),
				parseInt(bits[3], 16)
			];
		}
	},
	{
		re: /^(\w{1})(\w{1})(\w{1})$/,
		example: ['#fb0', 'f0f'],
		process: function (bits){
			return [
				parseInt(bits[1] + bits[1], 16),
				parseInt(bits[2] + bits[2], 16),
				parseInt(bits[3] + bits[3], 16)
			];
		}
	}
	];
	
	// search through the definitions to find a match
	for (var i = 0; i < color_defs.length; i++) {
		var re = color_defs[i].re;
		var processor = color_defs[i].process;
		var bits = re.exec(color_string);
		if (bits) {
			channels = processor(bits);
			this.r = channels[0];
			this.g = channels[1];
			this.b = channels[2];
			this.ok = true;
		}
	
	}
	
	// validate/cleanup values
	this.r = (this.r < 0 || isNaN(this.r)) ? 0 : ((this.r > 255) ? 255 : this.r);
	this.g = (this.g < 0 || isNaN(this.g)) ? 0 : ((this.g > 255) ? 255 : this.g);
	this.b = (this.b < 0 || isNaN(this.b)) ? 0 : ((this.b > 255) ? 255 : this.b);
}

/********************************************************/
/********************************************************/


