var baseColor = {r:255,g:0,b:0};
var currentColor = {r:255,g:255,b:255};
var lastValues = [0,0];
$(document).ready(
	function()
	{
		$('#wc_coul').Slider(
			{
				accept : '#wc_coul_sel',
				onSlide : function( cordx, cordy,x, y)
				{
					lastValues = [parseInt(cordx * 255/100),parseInt(cordy * 255/100)];
					setGradientColor();
				},
				onChange : function()
				{
					document.getElementById('wc_coul_anc').style.backgroundColor = 'rgb(' + currentColor.r + ',' + currentColor.g + ',' + currentColor.b + ')';
				}
			}
		);
		$('#wc_degra').Slider(
			{
				accept : '#wc_degra_sel',
				onSlide : function( cordx, cordy,x, y)
				{
					setVertColor(parseInt(cordy * 255 / 100));
					setGradientColor();
				},
				onChange : function()
				{
					document.getElementById('wc_coul_anc').style.backgroundColor = 'rgb(' + currentColor.r + ',' + currentColor.g + ',' + currentColor.b + ')';
				}
			}
		);
	}
);
function setVertColor(indic){
	var n=256/6, j=256/n, C=indic, c=C%n;

	baseColor = {
		r : parseInt(C<n?255:C<n*2?255-c*j:C<n*4?0:C<n*5?c*j:255),
		g : parseInt(C<n*2?0:C<n*3?c*j:C<n*5?255:255-c*j),
		b : parseInt(C<n?c*j:C<n*3?255:C<n*4?255-c*j:0)
	};
	document.getElementById('wc_coul').style.backgroundColor = 'rgb(' + baseColor.r + ',' + baseColor.g + ',' + baseColor.b + ')';
}

function setGradientColor(){
	var r =	Math.round((1-(1-(baseColor.r/255))*(lastValues[0]/255))*(255-lastValues[1]));
	var g =	Math.round((1-(1-(baseColor.g/255))*(lastValues[0]/255))*(255-lastValues[1]));
	var b =	Math.round((1-(1-(baseColor.b/255))*(lastValues[0]/255))*(255-lastValues[1]));
	var hexa = "#"+(toHex(r) + toHex(g) + toHex(b)).toUpperCase();
	var rgb	= 'rgb(' + r + ',' + g + ',' + b + ')';
	document.getElementById('wc_coul_nouv').style.backgroundColor = rgb;
	document.getElementById('wc_rgbR').value = r;
	document.getElementById('wc_rgbG').value = g;
	document.getElementById('wc_rgbB').value = b;
	document.getElementById('wc_hexa').value = hexa;
	document.getElementById("wc_hextex").style.color=rgb;
	currentColor = {r:r,g:g,b:b};
}

function toHex(color){
	color=parseInt(color).toString(16);
	return color.length<2?"0"+color:color;
}