var initialised;
var container;
var content;
var haut;
var bas;
var posContentT;
var posContentB;
var posMax;
var posMin;
var speed=50;
var loop;
var timer;


function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function findPosHeight(obj)
{
	var curtop = 0;
	if (obj.offsetHeight)
		curtop = obj.offsetHeight
	else if (obj.height)
		curtop += obj.height;
	return curtop;
}


function initScrollbar(cer,cent,topS,bottomS, scrollbar, hide) {
	container = document.getElementById(cer);
	content = document.getElementById(cent);
	haut = document.getElementById(topS);
	bas = document.getElementById(bottomS);
	scrollbar = document.getElementById(scrollbar);
	posMin = findPosY(container);
	posContentT = findPosY(haut)-posMin;
	posContentB = findPosY(bas)-posMin;
	posMin = 0;
	posMax = findPosHeight(container);

	initialised=true;

	if(hide && posContentB<=posMax) {
	  // on cache le scroll si le contenu est plus petit que le maximum
	  scrollbar.style.visibility = "hidden";
	}
}

function stopScroll() {
	loop=false;
  if(timer) clearTimeout(timer);
}

function doUpScroll() {
    if(initialised){
	    loop = true;
      loopUpScroll();
    }
}
function loopUpScroll() {
	if(initialised){
		if(posContentT<posMin){
			posContentT += 1;
			posContentB += 1;
			content.style.top = posContentT+"px";
    			if(loop) setTimeout('loopUpScroll()',20);
    			else if(timer) clearTimeout(timer);
		}
	}
}

function doDownScroll() {
   if(initialised){
		loop=true;
		loopDownScroll();
  }
}

function loopDownScroll() {
	if(initialised){
		if(posContentB>posMax){
			posContentT -= 1;
			posContentB -= 1;
			content.style.top = posContentT+"px";
    			if(loop) setTimeout('loopDownScroll()',20);
    			else if(timer) clearTimeout(timer);
		}
	}
}
