$(document).ready(function() {
	var dragMin = false, dragMax = false;								// état des curseurs
	var minPos = $('.middle').offset().left;							// limite inférieure du conteneur
	var cx;																// position courante curseur
	var lim;															// limite de mouvement
	var euroMin, euroMax;												// valeur en euros des curseurs
	var gap = Math.round(((valsup-valinf)/156)*10)/10;					// valeur de chaque palier
	var unselect = typeof document.onselectstart;						// texte non sélectionnable sous IE
	if(unselect!='undefined') {
		var module = document.getElementById('page');
		module.onselectstart = function() { return false; }
	}
	
	if(chmin!=0 && chmin > valinf) {
		var ecartInf = chmin - valinf;
		var posInf = Math.round(ecartInf/gap);
		$('.cursorLeft').css('left',posInf).text(chmin + '€');
	}
	if(chmax!=0 &&  chmax < valsup) {
		var ecartSup = valsup - chmax;
		var posSup = 120 - Math.round(ecartSup/gap);
		$('.cursorRight').css('left',posSup).text(chmax + '€');
	}
	
	$('.cursorLeft').mousedown(function(e) {
		dragMin = true;
		cx = (e.clientX - 20) - minPos;
		lim = $('.cursorRight').offset().left - minPos - 41;
		if(cx < 0) {
			euroMin = valinf;
			$(this).css('left',0).text(euroMin + '€');
		}
		else if(cx > lim) {
			euroMin = valinf + Math.round(lim*gap);
			$(this).css('left',lim).text(euroMin + '€');
		}
		else {
			euroMin = valinf + Math.round(cx*gap);
			$(this).css('left',cx).text(euroMin + '€');
		}
		$('input[name=valmin]').val(euroMin);
		e.preventDefault();
	});
	
	$('.cursorRight').mousedown(function(e) {
		dragMax = true;
		cx = (e.clientX - 20) - minPos;
		var newpos = cx - 36;
		lim = $('.cursorLeft').offset().left - minPos + 41;
		if(cx < lim) {
			euroMax = valinf + Math.round(lim*gap);
			$(this).css('left',lim - 36).text(euroMax + '€');
		}
		else if(cx > 156) {
			euroMax = valsup;
			$(this).css('left',120).text(euroMax + '€');
		}
		else {
			euroMax = valinf + Math.round(cx*gap);
			$(this).css('left',newpos).text(euroMax + '€');
		}
		$('input[name=valmax]').val(euroMax);
		e.preventDefault();
	});
	
	$('#page').mousemove(function(e) {
		if(dragMin == true) {
			$(this).css('cursor','pointer');
			cx = (e.clientX - 20) - minPos;
			lim = $('.cursorRight').offset().left - minPos - 41;
			if(cx < 0) {
				euroMin = valinf;
				$('.cursorLeft').css('left',0).text(euroMin + '€');
			}
			else if(cx > lim) {
				euroMin = valinf + Math.round(lim*gap);
				$('.cursorLeft').css('left',lim).text(euroMin + '€');
			}
			else {
				euroMin = valinf + Math.round(cx*gap);
				$('.cursorLeft').css('left',cx).text(euroMin + '€');
			}
			$('input[name=valmin]').val(euroMin);
		}
		if(dragMax == true) {
			$(this).css('cursor','pointer');
			cx = (e.clientX - 20) - minPos;
			var newpos = cx - 36;
			lim = $('.cursorLeft').offset().left - minPos + 41;
			if(cx < lim) {
				euroMax = valinf + Math.round(lim*gap);
				$('.cursorRight').css('left',lim - 36).text(euroMax + '€');
			}
			else if(cx > 156) {
				euroMax = valsup;
				$('.cursorRight').css('left',120).text(euroMax + '€');
			}
			else {
				euroMax = valinf + Math.round(cx*gap);
				$('.cursorRight').css('left',newpos).text(euroMax + '€');
			}
			$('input[name=valmax]').val(euroMax);
		}
	});
	
	$('#page').mouseup(function(e) {
		$(this).css('cursor','default');
		dragMin = false;
		dragMax = false;
	});
});