String.prototype.ltrim = function() {
	return this.replace(/^#+/,"");
}

$(document).ready(function() {
	circle = $.farbtastic('#colorpicker',function(color) {
		$('#preview-color').css('background-color',color);
		$('#color').val(color.ltrim());
		$('#hexa').val(color.ltrim());
		$('#color').focus();
		
	});

	//alert(window.location.hash); 
	//alert(typeof hash);
	//alert(hash);
	circle.setColor(window.location.hash);
	

	function searchColor()
	{
		window.location.hash = '#'+$('#color').val();
		//alert($('#radius').val());
		$.ajax({
			type:'GET',
			url:'/search/result/picker/'+$('#color').val()+'/radius/'+$('#radius').val()+'/format/html/',
			dataType:"html",
			success: function(html) {
				if(html == "<span>0</span> couleur "){
					$('#titre_form').show();
				}else{
					$('#titre_form').hide();
					$('#form-contact').hide();
				}
				$('#resultTotal').empty();
				$('#resultTotal').append(html);
				$('#resultTotal').stop();
				$('#resultTotal').css('background-color','#FFFBCC');
				$('#resultTotal').animate({ backgroundColor: "#ffffff" }, 2000,function(){$(this).removeAttr('style');});
			}
		});
	}
	
	$('#colorpicker').mouseup(searchColor);
	$('#radius').change(searchColor);
	$('#form-contact').hide();
	$('#titre_form').click(function(){
		$('#form-contact').slideDown("slow");
	});
	$('#titre_form').hide();
  });
