var scrollTimer;

var mouseX;
var mouseY;

function mouse(e) {
	if(e) {
		mouseX=e.pageX; mouseY=e.pageY;
	} else {
		mouseX=event.clientX;mouseY=event.clientY;
	}
}

function ObjGetPos(Obj){
	var x = 0; var y = 0;

	if((Obj) && (Obj.offsetParent)) {
		do {
       			x+=Obj.offsetLeft; y+=Obj.offsetTop; // position Parent
		} while (Obj=Obj.offsetParent); // Tant qu'un parent existe
	}
	return([x,y]);
}


function scrollStop() {
	clearTimeout(scrollTimer);
}

function scrollBegin(id,i,id1,id2,sens) {
	obj1=document.getElementById(id1);

	if(parseInt(obj1.scrollTop)+parseInt(sens) <= 0) {
		obj1.scrollTop=0;
		scrollStop();
	} else if (obj1.scrollTop+parseInt(sens) >= parseInt(obj1.scrollHeight)-parseInt(obj1.offsetHeight)) {
		obj1.scrollTop=parseInt(obj1.scrollHeight)-parseInt(obj1.offsetHeight);
		scrollStop();
	} else {
		obj1.scrollTop=parseInt(obj1.scrollTop)+parseInt(sens);
		clearTimeout(scrollTimer);
		scrollTimer=setTimeout("scrollBegin('"+id+"','"+i+"','"+id1+"','"+id2+"','"+sens+"')",30);
	};
	init(id,i,id1,id2);
}

function scrollPlace(id,i,id1,id2) {
	obj1=document.getElementById(id1);
	obj2=document.getElementById(id2);
	if(navigator.appName=="Microsoft Internet Explorer") {
		var tmp=(mouseY-ObjGetPos(obj2)[1]);
	} else {
		var tmp=(mouseY-ObjGetPos(obj2)[1]);
	}

	obj1.scrollTop=parseInt((tmp*parseInt(obj1.scrollHeight))/parseInt(obj2.offsetHeight)-parseInt(obj1.offsetHeight)/2);

	clearTimeout(scrollTimer);

	init(id,i,id1,id2);
	scrollTimer=setTimeout("scrollPlace('"+id+"','"+i+"','"+id1+"','"+id2+"')",30);

}

document.onmousemove=mouse;
document.onmouseup=scrollStop;

function reinit(id,i,id1,id2,up,down) {
	obj1=document.getElementById(id1);
	obj2=document.getElementById(id2);
	if(parseInt(obj1.scrollHeight)>(parseInt(obj1.offsetHeight))) {
		document.getElementById(i).style.height  = (parseInt((parseInt(obj1.scrollTop)   *parseInt(obj2.offsetHeight))/parseInt(obj1.scrollHeight)) + "px");
		document.getElementById(id).style.height = (parseInt((parseInt(obj1.offsetHeight)*parseInt(obj2.offsetHeight))/parseInt(obj1.scrollHeight)) + "px");
	} else {
		obj2.style.display="none";
		document.getElementById(id).style.display="none";
		document.getElementById(i).style.display="none";
		document.getElementById(up).style.display="none";
		document.getElementById(down).style.display="none";
	}
}

function init(id,i,id1,id2) {
	obj1=document.getElementById(id1);
	obj2=document.getElementById(id2);
	if(parseInt(obj1.scrollHeight)>(parseInt(obj1.offsetHeight))) {
		document.getElementById(i).style.height  = (parseInt((parseInt(obj1.scrollTop)   *parseInt(obj2.offsetHeight))/parseInt(obj1.scrollHeight)) + "px");
	} else {
		document.getElementById(id).style.height  = 0;
	}
}

function scroll(droite,up,down,support,barre) {
	var noeud_element2 = document.createElement("div")
	noeud_element2.style.fontSize="1px";
	noeud_element2.style.lineHeight="0";
	noeud_element2.style.border="0";
	noeud_element2.style.padding="0";
	noeud_element2.style.margin="0";
	noeud_element2.id=barre+"_marge_barre";
	noeud_element2.style.marginTop="-1px";
	document.getElementById(support).appendChild(noeud_element2);

	var noeud_element = document.createElement("div")
	noeud_element.style.fontSize="1px";
	noeud_element.style.lineHeight="0";
	noeud_element.style.border="0";
	noeud_element.style.padding="0";
	noeud_element.style.margin="0";
	noeud_element.id=barre;
	document.getElementById(support).appendChild(noeud_element);

	document.getElementById(support).style.overflow="hidden";

	objUp=document.getElementById(up);
	objUp.onmousedown = function() { scrollBegin(barre,barre+"_marge_barre","droite","support",-15); };
	objUp.onmouseout  = function() { scrollStop(); };

	objDown=document.getElementById(down);
	objDown.onmousedown= function() { scrollBegin(barre,barre+"_marge_barre","droite","support",+15); };
	objDown.onmouseout = function() { scrollStop(); };

	objSupport=document.getElementById(support);
	objSupport.onmousedown= function() { scrollPlace(barre,barre+"_marge_barre","droite","support"); };

	reinit(barre,barre+"_marge_barre",droite,support,up,down);
}
