
var ColorCompare = function (set) {
	this.set = set;
	
	var self = this,
		leftInput  = this.leftInput  = this.set.leftInput,
		rightInput = this.rightInput = this.set.rightInput;
	
	leftInput.change(function () {
		if (this.value.CSS()) {
			self.compare();
			self.set.onLeftChange(this.value);
			$(this).valid(true);			
		} else {
			$(this).valid(false);
		}
	})
	rightInput.change(function () {
		if (this.value.CSS()) {
			self.compare();
			self.set.onRightChange(this.value);
			$(this).valid(true);
		} else {
			$(this).valid(false);
		}
	})
	
	this.set.onStart(this);
	this.compare();
}
ColorCompare.prototype = {
	compare : function () {
		var rgb1 = COLOR.css2rgb( this.leftInput.attr('value')  ),
			rgb2 = COLOR.css2rgb( this.rightInput.attr('value') ),
			cont = COLOR.contrast(rgb1, rgb2),
			diff = COLOR.clrDiff(rgb1, rgb2),
			bri  = COLOR.briDiff(rgb1, rgb2);
			
		this.set.contrast.output.text(cont + ' %');
		this.set.difference.output.text(diff + ' %');
		this.set.brightness.output.text(bri + ' %');
		
		this.set.contrast.tip.text( this.contrastAdvice(cont) );
		this.set.brightness.tip.text( this.brightnessAdvice(bri) );
		this.set.difference.tip.text( this.differenceAdvice(diff) );
	},
	leftValue : function (css) {
		this.leftInput.valid(true);
		this.leftInput.attr('value', css);
		this.compare();
	},
	rightValue : function (css) {
		this.rightInput.valid(true);
		this.rightInput.attr('value', css);
		this.compare();
	},
	contrastAdvice : function (val) {
		this.set.contrast.output.valid( val >= 40 );
		if (val < 20) return s('absent-he');
		if (val < 40)  return s('weak-he');
		if (val < 70)  return s('notable-he');
		if (val < 92)  return s('optimal-he');
		return s('high-he')
	},
	differenceAdvice : function (val) {
		this.set.difference.output.valid( val <= 75 );
		if (val < 50) return s('absent-she');
		if (val < 70)  return s('weak-she');
		if (val < 80)  return s('medium-she');
		return s('strong-she')
	},
	brightnessAdvice : function (val) {
		this.set.brightness.output.valid( val >= 20 );
		if (val < 20) return s('absent-she');
		if (val < 40)  return s('weak-she');
		if (val < 60)  return s('notable-she');
		if (val < 80)  return s('optimal-she');
		return s('high-she')
	}
}


String.prototype.CSS = function () {
	return (/[0-9abcdef]{6}/i).test(this) 
}

Number.prototype.ro = function (accuracy) {
	var acc = accuracy ? Math.pow(10,accuracy) : 1;
	return Math.round( this * acc ) / acc
}

$.fn.valid = function (val) {
	return val ? this.css('color', '') : this.css('color', '#E00')
}

