var idImgBarreEchelles=null;
var idImgPoignee=null;
var objPoignee=null;
var objbarre=null;

//var glissementPoignee=false;
var posPoignee=-1;
var echelles=null;
var nbPlacesPoignee=0;

function InitBarreEchelles(idBarreEchelles, idPoignee, echelle, listeEchelles)
{
	echelles = listeEchelles;
	objPoignee = document.getElementById(idPoignee);
	objBarre = document.getElementById(idBarreEchelles);
	nbPlacesPoignee = echelles.length;

	posPoignee = 0;
	for (var i=0; i < echelles.length; i++)
		if (echelle <= echelles[i])
			posPoignee = i;

	y = objBarre.offsetTop + 1 + (posPoignee * Math.round(objBarre.height / nbPlacesPoignee));
	PlacePoignee(y);
}

function PlacePoignee(y)
{
	objPoignee.style.left = objBarre.offsetLeft;
	objPoignee.style.top = y;
	objPoignee.style.cursor = (document.all) ? "hand" : "pointer";
	objPoignee.className = "visible";
}


function SubmitZoom(ech)
{
	document.carto_form.echelle.value = ech;
	Soumettre();
}

function ZoomPlus()
{
	if (posPoignee < nbPlacesPoignee-1)
		SubmitZoom(echelles[posPoignee + 1]);
}

function ZoomMoins()
{
	if (posPoignee > 0)
		SubmitZoom(echelles[posPoignee - 1]);
}

function DebutGlissementPoignee()
{
	document.onmousemove = GlissementPoignee;
	document.onmouseup = FinGlissementPoignee;
}

function FinGlissementPoignee(e)
{
	minY = objBarre.offsetTop - (objPoignee.clientHeight / 2);
	maxY = minY + objBarre.height;

	e = e || event;
	y = e.clientY + document.body.scrollTop - (objPoignee.clientHeight / 2);

	if (y < minY) pos = 0;
	else if (y > maxY) pos = nbPlacesPoignee-1;
	else
	{
		pos = y - objBarre.offsetTop;
		pos = Math.round(pos / Math.round(objBarre.height / nbPlacesPoignee));
	}
	document.onmousemove = null;
	document.onmouseup = null;
	SubmitZoom(echelles[pos]);
}

function GlissementPoignee(e)
{
	minY = objBarre.offsetTop - (objPoignee.clientHeight / 2);
	maxY = minY + objBarre.height;
	e = e || event;
	y = e.clientY + document.body.scrollTop - (objPoignee.clientHeight / 2);
	if (y < maxY && y > minY)
		PlacePoignee(y);
}
