function ScrollBarsResize() {
	if(window.removeEventListener){
		window.removeEventListener("resize", ScrollBarsResize, false);
	}else if(window.attachEvent){
		window.detachEvent("onresize", ScrollBarsResize);
	} 
	for(var i=0; i<ScrollBars.length; i++){
		if(!ScrollBars[i].resizing && !ScrollBars[i].drawing) ScrollBars[i].Resize();
	}
	setTimeout("ReAttachResize()", 400);
}
function ReAttachResize(){
	if(window.addEventListener){
		window.addEventListener("resize", ScrollBarsResize, false);
	}else if(window.attachEvent){
		window.attachEvent("onresize", ScrollBarsResize);
	} 
}
var ScrollBars = new Array();

/*
HELP ON ScrollBar.ScrollerStyle, default is ScrollBar.AutoResize
ScrollBar.ScrollerStyle = ScrollBar.AutoResize or 
ScrollBar.ScrollerStyle = ScrollBar.Fixed

can be 
- ScrollBar.AutoResize
	The Scroller width (the button) is computed from the content to display and the container.
	The more scroll you have to do, the smaller the scroller is. (windows style)

- ScrollBar.Fixed
	The Scroller width is fixed to a size.
	The scroll for each move is computed from the content to display and the container.
	The more scroll you have to do, the less you have to move the scroll. (no example style)
	The button width (in pixel) can be set up be setting this variable :
		MyScrollBar.ButtonWidth = 250;

*/

var isFF=false;
var isIE6=false;
var isIE7=false;
if(typeof document.all != "undefined"){
	if (typeof window.XMLHttpRequest != "undefined") {
	  isIE7 = true;
	} else {
	  isIE6 = true;
	}
}else{
	isFF = true;
}



function ScrollBar(Name, imgScrollLeft, imgScrollRight, imgScrollBackground, DisplayElement, ContentElement, ScrollBarHeight){
	this.Obj = null;
	this.ParentObj = null;
	this.DisplayElement = DisplayElement;
	this.ContentElement = ContentElement;
	this.enabled = true;
	this.drawing = false;
	this.resizing = false;
	
	this.Name = Name;
	
	this.down = false;
	
	this.objX = 0;
	this.initX = 0;
	this.parentX = 0;
	
	this.imgScrollLeft = imgScrollLeft;
	this.imgScrollRight = imgScrollRight;
	this.imgScrollBackground = imgScrollBackground;
	
	this.Min = 0;
	this.Max = 0;

	//dynamically computed value from resize
	this.xMin = 0;
	this.xMax = 0;
	this.magnetMax = 0;
	this.scrollableAmount = 0;
	
	this.Width = 0;
	this.Height = ScrollBarHeight;
	
	this.moveCallback = null;
	this.Position = 0;
	
	this.oldMouseMove = "";
	this.oldMouseUp = "";
	this.oldSelectStart = "";
	this.oldBodyClick = "";
	
	this.ScrollerStyle = ScrollBar.AutoResize;
	
	ScrollBars[ScrollBars.length] = this;
}

ScrollBar.AutoResize = 1;
ScrollBar.Fixed = 2;

/************************************************/
/* methodes de deplacement de la scrollbar      */
ScrollBar.prototype.getPosition = function(e){
	var X = 0;
	var Y = 0;
	if(e.pageX){
		X = e.pageX;
		Y = e.pageY;
	}else if(e.clientX){
		X = e.clientX;
		Y = e.clientY;
	}else{
		X = e.x;
		Y = e.y;
	}
	return new Array(X, Y);
}

ScrollBar.prototype.MouseDown = function (e){
	if(!this.enabled) return false;
	this.down = true;
	
	var Pos = this.getPosition(e);
	this.initX = Pos[0];
	this.objX = this.Obj.offsetLeft;
	
	this.oldMouseMove = document.onmousemove;
	this.oldMouseUp = document.onmouseup;
	var obj = this;

	if(document.all){
		document.onmousemove = function(){ obj.Drag(); return false; };
	}else{
		document.onmousemove = function(e){ obj.Drag(e); return false; };
	}
	
	document.onclick = function(){ obj.MouseUp(); };
	document.onmouseup = function(){ obj.MouseUp(); };
	
	return false;
}

ScrollBar.prototype.MouseUp = function (){
	if(!this.enabled) return;
	this.down = false;
	
	document.onmousemove = this.oldMouseMove;
	document.onmouseup = this.oldMouseUp;
}

ScrollBar.prototype.Drag = function (e){
	if(!this.enabled) return false;
	if(!this.down) return false;

	if(!e) e = window.event;

	var Pos = this.getPosition(e);
	var nX = Pos[0] - this.initX + this.objX;
	
	/* magnetisme */
	if(nX < this.xMin)
		nX = this.parentX;
	else if(nX > this.xMax)
		nX = this.magnetMax;	
	nX = nX - this.parentX;
		
	this.Position = Math.floor((nX / (this.scrollableAmount) * this.Max) + 1);
		
	if(this.Position < this.Min) this.Position = this.Min;
	else if(this.Position > this.Max) this.Position = this.Max;
	
	this.Obj.style.left = nX + "px";
	
	if(this.moveCallback){
		this.moveCallback(this);
	}
	return false;
}

ScrollBar.prototype.Enable = function (){
	this.enabled = true;
	this.Draw();
}

ScrollBar.prototype.Disable = function (){
	this.enabled = false;
	this.Draw();
}

/***********************************************/
/* methode de fabrication de la scrollbar      */
ScrollBar.prototype.SetInterval = function (Min, Max){
	this.Min = Min;
	this.Max = Max;
}

ScrollBar.prototype.SetCallBackMove = function (callback){
	this.moveCallback = callback;
}

ScrollBar.prototype.Display = function (){

	document.write("<div id='"+this.Name+"_parent' style='text-align:left'>");
	document.write("</div>");
	this.ParentObj = document.getElementById(this.Name + "_parent");

	this.Draw();

}

ScrollBar.prototype.Draw = function (){
	if(this.drawing) return;
	this.drawing = true;
	
	this.ParentObj.innerHTML = "";
	this.ParentObj.style.display = (this.enabled && this.ContentElement.offsetWidth >= this.DisplayElement.offsetWidth)? "block" : "none";
	
	if(this.ContentElement.offsetWidth < this.DisplayElement.offsetWidth){
		this.drawing = false;
		return;
	}

	if(!this.enabled){
		this.drawing = false;
		return;
	}
	
	var HTML = "";
	
	HTML += "<div style='height:"+this.Height+";position:relative' id='"+this.Name+"' ";
	HTML += " onMouseDown='return "+this.Name+".MouseDown(event)' ";
	HTML += " onMouseUp='return "+this.Name+".MouseUp()' ";
	HTML += ">";
	
		HTML += "<img src='"+this.imgScrollLeft+"' class='scroll_left' id='"+this.Name+"_leftarrow'";
		HTML += " onMouseDown='return "+this.Name+".MouseDown(event)' ";
		HTML += " onMouseUp='return "+this.Name+".MouseUp()' ";
		HTML += ">";
	
		HTML += "<div style='background:url(\""+this.imgScrollBackground+"\") repeat-x bottom;' ";
		HTML += " onMouseDown='return "+this.Name+".MouseDown(event)' ";
		HTML += " onMouseUp='return "+this.Name+".MouseUp()' ";
		HTML += ">&nbsp;";
		HTML += "</div>";
	
		HTML += "<img src='"+this.imgScrollRight+"' class='scroll_right' id='"+this.Name+"_rightarrow'";
		HTML += " onMouseDown='return "+this.Name+".MouseDown(event)' ";
		HTML += " onMouseUp='return "+this.Name+".MouseUp()' ";
		HTML += ">";

	HTML += "</div>";

	this.ParentObj.innerHTML = HTML;

	this.Width = this.ParentObj.offsetWidth;
	this.parentX = this.ParentObj.offsetLeft;

	if(this.ScrollerStyle == ScrollBar.AutoResize){
		this.ButtonWidth = this.DisplayElement.offsetWidth / (this.ContentElement.offsetWidth/this.DisplayElement.offsetWidth);
		this.ButtonWidth *= this.Width / this.DisplayElement.offsetWidth;
	}else{
		//this.ButtonWidth += (document.getElementById(this.Name+"_leftarrow").offsetWidth + document.getElementById(this.Name+"_rightarrow").offsetWidth);
	}

	this.Obj = document.getElementById(this.Name);
	this.Obj.style.height = (this.Height-2) + "px";
	this.Obj.style.width = this.ButtonWidth + "px";
	
	this.SetInterval(1, this.ContentElement.offsetWidth - this.DisplayElement.offsetWidth);
	
	//compute values used in the Drag method
	this.magnetMax = this.parentX + this.Width - this.ButtonWidth;
	this.xMin = this.parentX + 10;
	this.xMax = this.magnetMax - 10;
	this.scrollableAmount = this.Width - this.ButtonWidth;
	
	this.drawing = false;
}

ScrollBar.prototype.Resize = function (){
	//if(!this.enabled) return;
	if(this.resizing) return;
	this.resizing = true;
	//alert("resize");
	this.Draw();
	this.Position = 0;
	if(this.moveCallback){
		this.moveCallback(this);
	}
	this.resizing = false;
}