function findobj(n, d)
{
	var p,i,x;

	if(!d)
  		d=document;
	if((p=n.indexOf("?"))>0&&parent.frames.length)
	{
		d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all)
	{
		x=d.all[n];
	}
	for (i=0;!x&&i<d.forms.length;i++)
	{
		x=d.forms[i][n];
	}
	for(i=0;!x&&d.layers&&i<d.layers.length;i++)
	{
		x=MM_findObj(n,d.layers[i].document);
	}
	if(!x && d.getElementById)
	{
		x=d.getElementById(n);
	}
	return x;
}

function findPosX(obj) {
	var curleft = 0;
	if(obj.offsetParent)
		while(1) {
			curleft += obj.offsetLeft;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if(obj.offsetParent) {
		while(1) {
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	}else if(obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

function no(){return false;}

var draggedObj = null;
var timerArrow;
var timer;

document.onmousemove = function(ev) {
    if(draggedObj){
		draggedObj.move(ev);
    } 
}

document.onmouseup = function(ev) {
	draggedObj = null;
	js_focus = findobj('focus');
	if(js_focus)
		js_focus.focus();
}

function ScrollBar(pContainer, pContent, pScrollBar) {
	// Attributes
	// Div Objects
	this.id = 'truc';
	this.step = 1;
	this.bigStep = 10;
	this.arrowTimerInterval = 10;
	this.container = findobj(pContainer);
	this.content = findobj(pContent);
	this.scrollBar = findobj(pScrollBar);
	this.handler;
	this.arrowUp;
	this.arrowUpHeight = 0;
	this.arrowDown;
	this.arrowDownHeight = 0;
	this.maxHandler;
	this.minHandler;
	this.initHandler;
	this.initMouse;
	
	if (navigator.appName == "Microsoft Internet Explorer") {
		this.unity = '';
	}else {
		this.unity = 'px';
	}
	
	// Config
	this.scrollBarWidth;
	this.scrollBarBgColor;
	this.scrollBarHandlerColor;
	this.scrollBarArrowTopPath;
	this.scrollBarArrowBottomPath;
	
	// Creation of a focus div, that get the focus on drop of the handler
	js_focus_test = findobj('focus');
	if(!js_focus_test) {
		js_focus_div = document.createElement('Div');
		js_focus_div.id = 'focus';
		document.body.appendChild(js_focus_div);
	}
	
	// Methods
	// Set the configuration variables of the scrollBar
	this.config = function(pScrollBarWidth, pScrollBarBgColor, pScrollBarHandlerColor, pScrollBarArrowTopPath, pScrollBarArrowTopWidth, pScrollBarArrowTopHeight, pScrollBarArrowBottomPath, pScrollBarArrowBottomWidth, pScrollBarArrowBottomHeight) {
		this.scrollBarWidth = pScrollBarWidth;
		this.scrollBarBgColor = pScrollBarBgColor;
		this.scrollBarHandlerColor = pScrollBarHandlerColor;
		this.scrollBarArrowTopPath = pScrollBarArrowTopPath;
		this.scrollBarArrowBottomPath = pScrollBarArrowBottomPath;
		this.arrowTopWidth = pScrollBarArrowTopWidth;
		this.arrowTopHeight = pScrollBarArrowTopHeight;
		this.arrowBottomWidth = pScrollBarArrowBottomWidth;
		this.arrowBottomHeight = pScrollBarArrowBottomHeight;
	}
	
	// Load the scrollBar
	this.loadScrollBar = function() {
		// Resize the content width
		this.content.style.width = (this.container.clientWidth - this.scrollBarWidth) + this.unity;
		this.content.style.position = 'relative';
		this.content.style.top = '0';
		
		// Creation of the arrows
		if(this.scrollBarArrowTopPath != null) {
			this.arrowUp = document.createElement('Div');
			this.arrowUp.parent = this;
			this.arrowUp.id = 'arrowUp';
			this.arrowUp.width = this.scrollBarWidth;
			this.scrollBar.appendChild(this.arrowUp);
			
			var js_arrow = document.createElement('Img');
			js_arrow.src = this.scrollBarArrowTopPath;
			js_width = this.arrowTopWidth;
			js_arrow.width = this.scrollBarWidth;
			js_arrow.height = parseInt((js_arrow.width * this.arrowTopHeight) / js_width);
			this.arrowUp.appendChild(js_arrow);
			
			this.arrowUp.height = js_arrow.height;
			this.arrowUpHeight = js_arrow.height;
			
			// Creating arrows events
			this.arrowUp.onmousedown = function(ev) {
				var obj = this.parent;
				timer = setInterval(function(){obj.moveUp();}, this.parent.arrowTimerInterval);
			}
			
			this.arrowUp.onmouseout = function(ev) {
				if(timer)
					clearInterval(timer);
			}
			
			this.arrowUp.onmouseup = function(ev) {
				if(timer)
					clearInterval(timer);
			}
		}
		
		if(this.scrollBarArrowBottomPath != null) {
			this.arrowDown = document.createElement('Div');
			this.arrowDown.parent = this;
			this.arrowDown.id = 'arrowDown';
			this.arrowDown.style.width = this.scrollBarWidth + this.unity;

			var js_arrow2 = document.createElement('Img');
			js_arrow2.src = this.scrollBarArrowBottomPath;
			js_width = this.arrowBottomWidth;
			js_arrow2.width = this.scrollBarWidth;
			js_arrow2.height = (js_arrow2.width * this.arrowBottomHeight) / js_width;
			
			this.arrowDown.height = js_arrow2.height;
			this.arrowDownHeight = js_arrow2.height;

			// Creating arrows events
			this.arrowDown.onmousedown = function(ev) {
				var obj = this.parent;
				timer = setInterval(function(){obj.moveDown();}, this.parent.arrowTimerInterval);
			}
			
			this.arrowDown.onmouseout = function(ev) {
				clearInterval(timer);
			}
			
			this.arrowDown.onmouseup = function(ev) {
				clearInterval(timer);
			}
		}
		
		// Creation of the scrollBar
		this.scrollBar.style.width = this.scrollBarWidth + this.unity;
		this.scrollBar.style.height = this.container.clientHeight + this.unity;
		this.scrollBar.style.backgroundColor = this.scrollBarBgColor;
		this.scrollBar.parent = this;
		
		this.handler = document.createElement('Div');
		this.handler.id = this.scrollBar.id + 'Handler';
		this.handler.style.height = (((this.container.clientHeight * this.scrollBar.clientHeight) / this.content.clientHeight) - this.arrowUpHeight - this.arrowDownHeight) + this.unity;
		this.handler.style.width = this.scrollBar.style.width + this.unity;
		this.handler.style.backgroundColor = this.scrollBarHandlerColor;
		this.handler.style.position = 'relative';
		this.handler.style.top = '0' + this.unity;
		this.handler.parent = this;
		this.scrollBar.appendChild(this.handler);
		this.maxHandler = parseInt(this.container.clientHeight) - parseInt(this.handler.style.height) - this.arrowUpHeight - this.arrowDownHeight;
		this.minHandler = 0;
		
		// Positionning the bottom arrow
		if(this.scrollBarArrowBottomPath != null) {
			this.arrowDown.style.position = 'relative';
			this.arrowDown.style.top = parseFloat(this.scrollBar.style.height) - parseFloat(this.handler.style.height) - parseFloat(this.arrowUpHeight) - parseFloat(this.arrowDownHeight) + this.unity;
			this.scrollBar.appendChild(this.arrowDown);
			this.arrowDown.appendChild(js_arrow2);
		}
		
		this.scrollBar.onmousedown = function(ev) {
			ev = ev || window.event;
			var mousePos = mouseCoords(ev);
			var targ = null;
			if(ev.target) {
				targ = ev.target;
			}else if(ev.srcElement) {
				targ = ev.srcElement;
			}
			if(targ.id == this.id) {
				var clic = (mousePos.y - findPosY(this.parent.handler));
				
				if(clic < 0) {
					js_handler = findobj(this.parent.scrollBar.id + 'Handler');
					if((parseInt(js_handler.style.top) - this.parent.bigStep) > this.parent.minHandler ) {
						js_handler.style.top = parseInt(js_handler.style.top) - this.parent.bigStep + this.parent.unity;
						this.parent.content.style.top = -(parseInt(this.parent.handler.style.top) * this.parent.content.clientHeight / this.parent.scrollBar.clientHeight) + this.parent.unity;
					}else {
						js_handler.style.top = this.parent.minHandler + this.parent.unity;
						this.parent.content.style.top = -(parseInt(this.parent.minHandler) * this.parent.content.clientHeight / this.parent.scrollBar.clientHeight) + this.parent.unity;
					}
				}else {
					js_handler = findobj(this.parent.scrollBar.id + 'Handler');
					if((parseInt(js_handler.style.top) + this.parent.bigStep) < this.parent.maxHandler ) {
						js_handler.style.top = parseInt(js_handler.style.top) + this.parent.bigStep + this.parent.unity;
						this.parent.content.style.top = -(parseInt(this.parent.handler.style.top) * this.parent.content.clientHeight / this.parent.scrollBar.clientHeight) + this.parent.unity;
					}else {
						js_handler.style.top = this.parent.maxHandler + this.parent.unity;
						this.parent.content.style.top = -(parseInt(this.parent.maxHandler) * this.parent.content.clientHeight / this.parent.scrollBar.clientHeight) + this.parent.unity;
					}
				}
			}
		}
		
		this.handler.onmousedown = function(ev) {
			document.onmousedown=no;
			if(typeof document.onselectstart!="undefined"){
				document.onselectstart=no;
			}
			
			if (navigator.appName == "Microsoft Internet Explorer") {
				draggedObj = this.parent;
				this.parent.initHandler = (this.style.top).substr(0, (this.style.top).length - 2);
				ev = ev || window.event;
				var mousePos = mouseCoords(ev);
				this.parent.initMouse = mousePos.y;
			}else {
				draggedObj = ev.target.parent;
				ev.target.parent.initHandler = (this.style.top).substr(0, (this.style.top).length - 2);
				ev = ev || window.event;
				var mousePos = mouseCoords(ev);
				ev.target.parent.initMouse = mousePos.y;
			}
		};
		
		this.handler.onmouseup = function() {
			draggedObj = null;
			document.onmousedown = null;
			if(typeof document.onselectstart!="undefined"){
				document.onselectstart=null;
			}
		};
	}
	
	this.moveUp = function() {
		js_handler = findobj(this.scrollBar.id + 'Handler');
		if((parseInt(js_handler.style.top) - this.step) > this.minHandler ) {
			js_handler.style.top = parseInt(js_handler.style.top) - this.step + this.unity;
			this.content.style.top = -(parseInt(this.handler.style.top) * this.content.clientHeight / this.scrollBar.clientHeight) + this.unity;
		}else {
			js_handler.style.top = this.minHandler + this.unity;
			this.content.style.top = -(parseInt(this.minHandler) * this.content.clientHeight / this.scrollBar.clientHeight) + this.unity;
		}
	}

	this.moveDown = function() {
		js_handler = findobj(this.scrollBar.id + 'Handler');
		if((parseInt(js_handler.style.top) + this.step) < this.maxHandler ) {
			js_handler.style.top = parseInt(js_handler.style.top) + this.step + this.unity;
			this.content.style.top = -(parseInt(this.handler.style.top) * this.content.clientHeight / this.scrollBar.clientHeight) + this.unity;
		}else {
			js_handler.style.top = this.maxHandler + this.unity;
			this.content.style.top = -(parseInt(this.maxHandler) * this.content.clientHeight / this.scrollBar.clientHeight) + this.unity;
		}
	}
	
	//
	this.move = function(ev) {
		ev = ev || window.event;
		var mousePos = mouseCoords(ev);
		delta = mousePos.y - this.initMouse;
		currentPos = parseInt(delta) + parseInt(this.initHandler);
		if( (currentPos < parseInt(this.maxHandler)) ) {
			if( (currentPos > parseInt(this.minHandler)+3) ) {
				this.handler.style.top = currentPos + this.unity;
				this.content.style.top = -(parseInt(this.handler.style.top) * this.content.clientHeight / this.scrollBar.clientHeight) + this.unity;
			}else {
				this.handler.style.top = parseInt(this.minHandler);
				this.content.style.top = -(parseInt(this.minHandler) * this.content.clientHeight / this.scrollBar.clientHeight) + this.unity;
			}
		}else {
			this.handler.style.top = parseInt(this.maxHandler);
			this.content.style.top = -(parseInt(this.maxHandler) * this.content.clientHeight / this.scrollBar.clientHeight) + this.unity;
		}
	}
	
	// Show the values of the attributes
	this.showObject = function() {
		alert(this.scrollBarWidth + ' ' +
			  this.scrollBarBgColor + ' ' +
			  this.scrollBarHandlerColor + ' ' +
			  this.scrollBarArrowTopPath + ' ' +
			  this.scrollBarArrowBottomPath);
	}
}

function mouseCoords(ev){ 
   if(ev.pageX || ev.pageY){ 
        return {x:ev.pageX, y:ev.pageY}; 
    } 
    return { 
        x:ev.clientX + document.body.scrollLeft - document.body.clientLeft, 
        y:ev.clientY + document.body.scrollTop  - document.body.clientTop 
    }; 
}