// FONCTION DE LA BARRE DE SCROLL 


// And now... go to the bottom of the page...

// Browser detection
var dom = document.getElementById ? true:false;
var nn4 = document.layers ? true:false;
var ie4 = document.all ? true:false;
var imax;
var mouseY; // Mouse Y position onclick
var mouseX; // Mouse X position onclick

var timer = setTimeout("",500); // Repeat variable

var clickUp = false; // If click on up-arrow
var clickDown = false; // If click on down-arrow
var clickDrag = new Array(); // If click on scrollbar
var clickAbove = false; // If click above scrollbar
var clickBelow = false; // If click below scrollbar

clickDrag[0]=false;
for (i=1;i<=imax;i++) {
	clickDrag[i]=false;
}

var upL=new Array(); // Up-arrow X
var upT=new Array(); // Up-arrow Y
var downL=new Array(); // Down-arrow X
var downT=new Array(); // Down-arrow Y
var dragL=new Array(); // Scrollbar X
var dragT=new Array(); // Scrollbar Y
var rulerL=new Array(); // Ruler X
var rulerT=new Array();// Ruler Y
var contentT=new Array(); // Content layer Y;
var contentH=new Array(); // Content height
var contentClipH=new Array(); // Content clip height
var scrollLength=new Array(); // Number of pixels scrollbar should move
var startY=new Array(); // Keeps track of offset between mouse and span

// Mousedown
function down(e){
	if((document.layers && e.which!=1) || (document.all && event.button!=1)) return true; // Enables the right mousebutton
	getMouse(e);

	for (i=1;i<=imax;i++) {
	
		startY[i] = (mouseY - dragT[i]);
		
		//alert(document.all["up"+i].style.pixelLeft);
		//alert((upL[i] + eval("upW"+i)));
		// If click on up-arrow
		if(mouseX >= upL[i] && (mouseX <= (upL[i] + eval("upW"+i))) && mouseY >= upT[i] && (mouseY <= (upT[i] + eval("upH"+i)))){

			clickUp = true;
			return scrollUp(i);
		}	
		// Else if click on down-arrow
		else if(mouseX >= downL[i] && (mouseX <= (downL[i] + eval("downW"+i))) && mouseY >= downT[i] && (mouseY <= (downT[i] + eval("downH"+i)))){
			clickDown = true;
			return scrollDown(i);
		}
		// Else if click on scrollbar
		else if(mouseX >= dragL[i] && (mouseX <= (dragL[i] + eval("dragW"+i))) && mouseY >= dragT[i] && (mouseY <= (dragT[i] + eval("dragH"+i)))){
			clickDrag[i] = true;
			return false;
		}
		else if(mouseX >= dragL[i] && (mouseX <= (dragL[i] + eval("dragW"+i))) && mouseY >= rulerT[i] && (mouseY <= (rulerT[i] + eval("scrollH"+i)))){
			// If click above drag
			if(mouseY < dragT[i]){
				clickAbove = true;
				clickUp = true;
				return scrollUp(i);
			}
			// Else click below drag
			else{
				clickBelow = true;
				clickDown = true;
				
				return scrollDown(i);
			}
		}
		// If no scrolling is to take place
		else{
			//return true;
		}
	} //end for
}

// Drag function
function move(e){

for (i=1;i<=imax;i++) {
	if(clickDrag[i] && contentH[i] > contentClipH[i]){
		getMouse(e);
		dragT[i] = (mouseY - startY[i]);
		
		if(dragT[i] < (rulerT[i]))
			dragT[i] = rulerT[i];		
		if(dragT[i] > (rulerT[i] + eval("scrollH"+i) - eval("dragH"+i)))
			dragT[i] = (rulerT[i] + eval("scrollH"+i) - eval("dragH"+i));
		
		contentT[i] = ((dragT[i] - rulerT[i])*(1/scrollLength[i]));
		contentT[i] = eval('-' + contentT[i]);

		moveTo(i);
		
		// So ie-pc doesn't select gifs
		if(ie4)
			return false;
	}
}
}

function up(){
	clearTimeout(timer);
	// Resetting variables
	
		clickUp = false;
		clickDown = false;
		for (i=1;i<=imax;i++) {
			clickDrag[i]=false;
		}
		clickAbove = false;
		clickBelow = false;
	return true;
}

// Reads content layer top
function getT(i){

	if(ie4)
		contentT[i] =  parseInt(document.all["content"+i].style.pixelTop);
	else if(nn4)
		contentT[i] = parseInt(document["contentClip"+i].document["content"+i].top);
	else if(dom)
		contentT[i] = parseInt(document.getElementById("content"+i).style.top);

}

// Reads mouse X and Y coordinates
function getMouse(e){
	if(ie4){
		mouseY = event.clientY + document.body.scrollTop;
		mouseX = event.clientX + document.body.scrollLeft;
	}
	else if(nn4 || dom){
		mouseY = e.pageY;
		mouseX = e.pageX;
	}
}

// Moves the layer
function moveTo(i){
	if(ie4){
		document.all["content"+i].style.top = contentT[i];
		document.all["ruler"+i].style.top = dragT[i];
		document.all["drag"+i].style.top = dragT[i];
	}
	else if(nn4){
		document["contentClip"+i].document["content"+i].top = contentT[i];
		document["ruler"+i].top = dragT[i];
		document["drag"+i].top = dragT[i];
	}
	else if(dom){
		document.getElementById("content"+i).style.top = contentT[i] + "px";
		document.getElementById("drag"+i).style.top = dragT[i] + "px";
		document.getElementById("ruler"+i).style.top = dragT[i] + "px";
	}
	//document["Console"+i]["valeurTop"+i].value = contentT[i];// Indicateur de la position Y du contenu à scroller
}

// Scrolls up
function scrollUp(i){
	getT(i);
	
	if(clickAbove){
		if(dragT[i] <= (mouseY-(eval("dragH"+i)/2)))
			return up();
	}
	
	if(clickUp){
		if(contentT[i] < 0){		
			dragT[i] = dragT[i] - (eval("speed"+i)*scrollLength[i]);
			
			if(dragT[i] < (rulerT[i]))
				dragT[i] = rulerT[i];
				
			contentT[i] = contentT[i] + eval("speed"+i);
			if(contentT[i] > 0)
				contentT[i] = 0;
			
			moveTo(i);
			timer = setTimeout("scrollUp("+i+")",25);
		}
	}
	return false;
}

// Scrolls down
function scrollDown(i){
	getT(i);
	
	if(clickBelow){
		if(dragT[i] >= (mouseY-(eval("dragH"+i)/2)))
			return up();
	}

	if(clickDown){
		if(contentT[i] > -(contentH[i] - contentClipH[i])){			
			dragT[i] = dragT[i] + (eval("speed"+i)*scrollLength[i]);
			if(dragT[i] > (rulerT[i] + eval("scrollH"+i) - eval("dragH"+i)))
				dragT[i] = (rulerT[i] + eval("scrollH"+i) - eval("dragH"+i));
			
			contentT[i] = contentT[i] - eval("speed"+i);
			if(contentT[i] < -(contentH[i] - contentClipH[i]))
				contentT[i] = -(contentH[i] - contentClipH[i]);
			
			moveTo(i);
			timer = setTimeout("scrollDown("+i+")",25);
		}
	}
	return false;
}

// reloads page to position the layers again
function reloadPage(){
	location.reload();
}

// Preload
function eventLoader(){

	for (i=1;i<=imax;i++) {
		if(ie4){
			// Up-arrow X and Y variables
			upL[i] = document.all["up"+i].style.pixelLeft;
			upT[i] = document.all["up"+i].style.pixelTop;
			// Down-arrow X and Y variables
			downL[i] = document.all["down"+i].style.pixelLeft;
			downT[i] = document.all["down"+i].style.pixelTop;
			// Scrollbar X and Y variables
			dragL[i] = document.all["drag"+i].style.pixelLeft;
			dragT[i] = document.all["drag"+i].style.pixelTop;
			// Ruler Y variable
			rulerT[i] = document.all["ruler"+i].style.pixelTop;
			// Height of content layer and clip layer
			contentH[i] = parseInt(document.all["content"+i].scrollHeight);
			contentClipH[i] = parseInt(document.all["contentClip"+i].style.height);
			//var MinH = contentH[i] + 2;
			if (contentH[i] <= eval("contHeight"+i)){
				document.all["up"+i].style.visibility ='hidden';
				document.all["down"+i].style.visibility ='hidden';
				document.all["drag"+i].style.visibility ='hidden';
				document.all["ruler"+i].style.visibility ='hidden';
			}
		}
		else if(nn4){
			// Up-arrow X and Y variables
			upL[i] = document["up"+i].left;
			upT[i] = document["up"+i].top;
			// Down-arrow X and Y variables
			downL[i] = document["down"+i].left;
			downT[i] = document["down"+i].top;	
			// Scrollbar X and Y variables
			dragL[i] = document["drag"+i].left;
			dragT[i] = document["drag"+i].top;
			// Ruler Y variable
			rulerT[i] = document["ruler"+i].top;
			// Height of content layer and clip layer
			contentH[i] = document["contentClip"+i].document["content"+i].clip.bottom;
			contentClipH[i] = document["contentClip"+i].clip.bottom;
			if (contentH[i] <= eval("contHeight"+i)){
				document["up"+i].visibility ='hidden';
				document["down"+i].visibility ='hidden';
				document["drag"+i].visibility ='hidden';
				document["ruler"+i].visibility ='hidden';
			}
		}
		else if(dom){
			// Up-arrow X and Y variables
			upL[i] = parseInt(document.getElementById("up"+i).style.left);
			upT[i] = parseInt(document.getElementById("up"+i).style.top);
			// Down-arrow X and Y variables
			downL[i] = parseInt(document.getElementById("down"+i).style.left);
			downT[i] = parseInt(document.getElementById("down"+i).style.top);
			// Scrollbar X and Y variables
			dragL[i] = parseInt(document.getElementById("drag"+i).style.left);
			dragT[i] = parseInt(document.getElementById("drag"+i).style.top);
			// Ruler Y variable
			rulerT[i] = parseInt(document.getElementById("ruler"+i).style.top);
			// Height of content layer and clip layer
			contentH[i] = parseInt(document.getElementById("content"+i).offsetHeight);
			contentClipH[i] = parseInt(document.getElementById("contentClip"+i).offsetHeight);
			document.getElementById("content"+i).style.top = 0 + "px";
			if (contentH[i] <= eval("contHeight"+i)){
				document.getElementById("up"+i).style.visibility ='hidden';
				document.getElementById("down"+i).style.visibility ='hidden';
				document.getElementById("drag"+i).style.visibility ='hidden';
				document.getElementById("ruler"+i).style.visibility ='hidden';
			}
		}
		// Number of pixels scrollbar should move
		scrollLength[i] = ((this["scrollH"+i]-this["dragH"+i])/(contentH[i]-contentClipH[i]));
		//document["Console"+i]["valeurHeight"+i].value = contentH[i];// Indicateur de la hauteur totale à scroller
	}
	// Initializes event capturing
	if(nn4){
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
		window.onresize = reloadPage;
	}
	document.onmousedown = down;
	document.onmousemove = move;
	document.onmouseup = up;
	
}

/* ******************************************************
** *** FONCTION DE RESETING DES PARAMETRES
** ************
*/
function eventReseter(){

	for (i=1;i<=imax;i++) {
		if(ie4){
			// Up-arrow X and Y variables
			upL[i] = document.all["up"+i].style.pixelLeft;
			// Down-arrow X and Y variables
			downL[i] = document.all["down"+i].style.pixelLeft;
			// Scrollbar X and Y variables
			dragL[i] = document.all["drag"+i].style.pixelLeft;
			// Height of content layer and clip layer
			contentH[i] = parseInt(document.all["content"+i].scrollHeight);
			contentClipH[i] = parseInt(document.all["contentClip"+i].style.height);
			if (contentH[i] <= eval("contHeight"+i)){
				document.all["up"+i].style.visibility ='hidden';
				document.all["down"+i].style.visibility ='hidden';
				document.all["drag"+i].style.visibility ='hidden';
				document.all["ruler"+i].style.visibility ='hidden';
			}
		}
		else if(nn4){
			// Up-arrow X and Y variables
			upL[i] = document["up"+i].left;
			// Down-arrow X and Y variables
			downL[i] = document["down"+i].left;
			// Scrollbar X and Y variables
			dragL[i] = document["drag"+i].left;
			// Height of content layer and clip layer
			contentH[i] = document["contentClip"+i].document["content"+i].clip.bottom;
			contentClipH[i] = document["contentClip"+i].clip.bottom;
			if (contentH[i] <= eval("contHeight"+i)){
				document["up"+i].visibility ='hidden';
				document["down"+i].visibility ='hidden';
				document["drag"+i].visibility ='hidden';
				document["ruler"+i].visibility ='hidden';
			}
		}
		else if(dom){
			// Up-arrow X and Y variables
			upL[i] = parseInt(document.getElementById("up"+i).style.left);
			// Down-arrow X and Y variables
			downL[i] = parseInt(document.getElementById("down"+i).style.left);
			// Scrollbar X and Y variables
			dragL[i] = parseInt(document.getElementById("drag"+i).style.left);
			// Height of content layer and clip layer
			contentH[i] = parseInt(document.getElementById("content"+i).offsetHeight);
			contentClipH[i] = parseInt(document.getElementById("contentClip"+i).offsetHeight);
			document.getElementById("content"+i).style.top = 0 + "px";
			if (contentH[i] <= eval("contHeight"+i)){
				document.getElementById("up"+i).style.visibility ='hidden';
				document.getElementById("down"+i).style.visibility ='hidden';
				document.getElementById("drag"+i).style.visibility ='hidden';
				document.getElementById("ruler"+i).style.visibility ='hidden';
			}
		}
		// Number of pixels scrollbar should move
		scrollLength[i] = ((this["scrollH"+i]-this["dragH"+i])/(contentH[i]-contentClipH[i]));
		//document["Console"+i]["valeurHeight"+i].value = contentH[i];// Indicateur de la hauteur totale à scroller
	}
	// Initializes event capturing
	if(nn4){
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
		window.onresize = reloadPage;
	}
	document.onmousedown = down;
	document.onmousemove = move;
	document.onmouseup = up;
	
}