//clarity slider widget
//clarity slider widget.
var handleLeftclarityNum  = 0;
var handleRightclarityNum = 7;
var currentHandleclarity = '';
var timeout_clarity = null;

function updateResultsClarity(){
	change_filter = true;
	updateResults();
}

function moveClarity(pos,handle)
{
	if (null != timeout_clarity)
	{
		clearTimeout(timeout_clarity);
	}
	
	if (handle == 'left')
	{
		if (handleRightclarityNum < pos)
		{
			return;
		}
		
		switch(pos)
		{
			case 0: //Si2
				$('#slide_iz_clarity').css('padding-left','0px');
				handleLeftclarityNum = 0;
				if (!blocked)
				{
					showUpdateResultsLogin();
					timeout_clarity = setTimeout(updateResultsClarity,600);
				}
				break;
				
			case 1://Si1
				$('#slide_iz_clarity').css('padding-left','29px');
				handleLeftclarityNum = 1;
				if (!blocked)
				{
					showUpdateResultsLogin();
					timeout_clarity = setTimeout(updateResultsClarity,600);
				}
				break;
			case 2://VS2
				$('#slide_iz_clarity').css('padding-left','58px');
				handleLeftclarityNum = 2;
				if (!blocked)
				{
					showUpdateResultsLogin();
					timeout_clarity = setTimeout(updateResultsClarity,600);
				}
				break;
				
			case 3://VS1
				$('#slide_iz_clarity').css('padding-left','88px');
				handleLeftclarityNum = 3;
				if (!blocked)
				{
					showUpdateResultsLogin();
					timeout_clarity = setTimeout(updateResultsClarity,600);
				}
				break;
				
			case 4://VVS2
				$('#slide_iz_clarity').css('padding-left','118px');
				handleLeftclarityNum = 4;
				if (!blocked)
				{
					showUpdateResultsLogin();
					timeout_clarity = setTimeout(updateResultsClarity,600);
				}
				break;
				
			case 5://VVS1
				$('#slide_iz_clarity').css('padding-left','147px');
				handleLeftclarityNum = 5;
				if (!blocked)
				{
					showUpdateResultsLogin();
					timeout_clarity = setTimeout(updateResultsClarity,600);
				}
				break;
				
			case 6://IF
				$('#slide_iz_clarity').css('padding-left','177px');
				handleLeftclarityNum = 6;
				if (!blocked)
				{
					showUpdateResultsLogin();
					timeout_clarity = setTimeout(updateResultsClarity,600);
				}
				break;
				
			case 7://FL
				$('#slide_iz_clarity').css('padding-left','206px');
				handleLeftclarityNum = 7;
				if (!blocked)
				{
					showUpdateResultsLogin();
					timeout_clarity = setTimeout(updateResultsClarity,600);
				}
				break;
		}
	}
	else if ('right' == handle)
	{
		if (handleLeftclarityNum > pos)
		{
			return;
		}
	
		switch(pos){
			case 0://Si2
				$('#slide_de_clarity').css({'margin-left':'18px','padding-right':'206px'});
				handleRightclarityNum = 0;
				showUpdateResultsLogin();
				timeout_clarity = setTimeout(updateResultsClarity,600);
				break;
				
			case 1://Si1
				$('#slide_de_clarity').css({'margin-left':'47px','padding-right':'177px'});
				handleRightclarityNum = 1;
				showUpdateResultsLogin();
				timeout_clarity = setTimeout(updateResultsClarity,600);
				break;
				
			case 2://VS2
				$('#slide_de_clarity').css({'margin-left':'77px','padding-right':'147px'});
				handleRightclarityNum = 2;
				showUpdateResultsLogin();
				timeout_clarity = setTimeout(updateResultsClarity,600);
				break;
				
			case 3://VS1
				$('#slide_de_clarity').css({'margin-left':'107px','padding-right':'118px'});
				handleRightclarityNum = 3;
				showUpdateResultsLogin();
				timeout_clarity = setTimeout(updateResultsClarity,600);
				break;
				
			case 4://VVS2
				$('#slide_de_clarity').css({'margin-left':'136px','padding-right':'88px'});
				handleRightclarityNum = 4;
				showUpdateResultsLogin();
				timeout_clarity = setTimeout(updateResultsClarity,600);
				break;
				
			case 5://VVS1
				$('#slide_de_clarity').css({'margin-left':'166px','padding-right':'58px'});
				handleRightclarityNum = 5;
				showUpdateResultsLogin();
				timeout_clarity = setTimeout(updateResultsClarity,600);
				break;
				
			case 6://IF
				$('#slide_de_clarity').css({'margin-left':'195px','padding-right':'29px'});
				handleRightclarityNum = 6;
				showUpdateResultsLogin();
				timeout_clarity = setTimeout(updateResultsClarity,600);
				break;
				
			case 7://FL
				$('#slide_de_clarity').css({'margin-left':'225px','padding-right':'0px'});
				handleRightclarityNum = 7;
				showUpdateResultsLogin();
				timeout_clarity = setTimeout(updateResultsClarity,600);
				break;
		}
	}
}

function getclarityPosOver(mousePosition){
	if (mousePosition >= 0 && mousePosition <= 26)
	{
		return 0;//Si2
	}
	else if (mousePosition >= 27 && mousePosition <= 56)
	{
		return 1; //Si1
	}
	else if (mousePosition >= 57 && mousePosition <= 86)
	{
		return 2; //VS2
	}
	else if (mousePosition >= 87 && mousePosition <= 114)
	{
		return 3; //VS1
	}
	else if (mousePosition >= 115 && mousePosition <= 144)
	{
		return 4;//VVS2
	}
	else if (mousePosition >= 145 && mousePosition <= 175)
	{
		return 5;//VVS1
	}
	else if (mousePosition >= 176 && mousePosition <= 203)
	{
		return 6;//IF
	}
	else if (mousePosition >= 204 && mousePosition <= 233)
	{
		return 7;//FL
	}
	
	//invalid position value;
	return -1;
}

$(document).ready(function()
{
	$('#claritySliderContainer').bind('mousedown',function(e)
	{
		currentDragElement = 'claritySlider';
		e.preventDefault();
		initMousePosition = [e.pageX,e.pageY];
		
		e.preventDefault();
		var pos  = findPos($('#claritySliderContainer')[0]);
		var initX = pos[0];
		var relativeMousepos = e.pageX - initX;
		
		var overPosition = getclarityPosOver(relativeMousepos);
		
		if (handleLeftclarityNum == handleRightclarityNum)
		{
			currentHandleclarity = 'both';
		}
		else
		{
			if (overPosition == handleRightclarityNum)
			{
				currentHandleclarity = 'right';
				//console.log('use right handle');
			}
			else if (overPosition == handleLeftclarityNum)
			{
				currentHandleclarity = 'left';
				//console.log('use left handle');
			}
			else
			{
				currentHandleclarity = '';
			}
		}
	});
	
	$('#claritySliderContainer').bind('mousemove',function(e){
		
		e.preventDefault();
		var pos  = findPos($('#claritySliderContainer')[0]);
		var initX = pos[0];
		var endX  = initX + 240;
		
		var relativeMousepos = e.pageX - initX;
		
		if (currentDragElement == 'claritySlider')
		{
			//console.log('current handle: ' + currentHandleclarity);
			
			if ('both' == currentHandleclarity)
			{
				//si el usuario mueve el mouse hacia la izquierda se usa el handle left.
				//sino se usa el handle right.
				var deltaX = e.pageX - initMousePosition[0];
				if (deltaX > 0)
				{
					currentHandleclarity = 'right';
				}
				else
				{
					currentHandleclarity = 'left';
				}
			}
			
			var overPosition = getclarityPosOver(relativeMousepos);
			moveClarity(overPosition,currentHandleclarity);
		}
		
		initMousePosition = [e.pageX,e.pageY];
	});
});

