////////////////////////////////////////////////////////////////
///// Developpement Michel Pereira - MRM WorldWide
///// Scroll v1.0
////////////////////////////////////////////////////////////////

Scroll = function(idToScroll, idContainer)
{
	this.objToScroll = document.getElementById(idToScroll);
	this.container = document.getElementById(idContainer);
	this.objToScroll.style.top = "0px";
	
	this.sens = 1;
	
	//Contenu scroll
	this.maxScroll = this.objToScroll.offsetTop;
	this.minScroll = this.objToScroll.offsetTop - (this.objToScroll.offsetHeight - this.container.offsetHeight);
	this.heightScroll = this.maxScroll - this.minScroll;
	

	// drag
	this.heightDraging = this.container.offsetHeight;
	this.init_YDrag = 0;
	this.butDrag = null;
	this.onDrag = false;
	this.mouseOffset = null;
	
	this.intervalScroll = -1;

}

Scroll.prototype =
{

	//------------------------------------------------------
	start: function(sens){
	
		if(this.intervalScroll != -1) clearInterval(this.intervalScroll);
		this.sens = sens;
		
		var ref = this;
		this.intervalScroll = setInterval(function(){ref.scrollAnim()}, 10);
	},
	//------------------------------------------------------
	stop: function(){
		if(this.intervalScroll != -1) clearInterval(this.intervalScroll);
		this.intervalScroll = -1;
	},
	//------------------------------------------------------
	addDrag: function(idBut, idFond){
		var item = document.getElementById(idBut);
		var fond = document.getElementById(idFond);
	
		if(this.objToScroll.offsetHeight <= this.container.offsetHeight){
			item.style.display = "none";
			fond.style.display = "none";
			return;
		}

	
		this.butDrag  = item;
		this.butDrag.style.top = "0px";
		
		this.heightDraging = fond.offsetHeight;
		this.init_YDrag = fond.offsetTop;
		this.butDrag.style.top = this.init_YDrag;

		item.ref = this;
		item.onmousedown = function(ev){
			this.ref.onDrag = true;
			this.ref.startDrag();
			return false;
		}

	},
	//------------------------------------------------------
	addButs: function(idButTop, idButBottom){
		var butTop = document.getElementById(idButTop);
		var butBottom = document.getElementById(idButBottom);

		if(this.objToScroll.offsetHeight <= this.container.offsetHeight){
			butTop.style.display = "none";
			butBottom.style.display = "none";
			return;
		}

		butTop.ref = this;
		butTop.onmousedown = function(ev){
			this.ref.start(1);
		}
		butTop.onmouseup = function(ev){
			this.ref.stop();
		}
		
		butBottom.ref = this;
		butBottom.onmousedown = function(ev){
			this.ref.start(-1);
		}
		butBottom.onmouseup = function(ev){
			this.ref.stop();
		}

	},
	//------------------------------------------------------
	startDrag: function(){
		
		// ecouteurs
		document.ref = this;
		document.onmouseup = function(ev){
			if(this.ref.onDrag){
				 this.ref.stopDrag();
			}
		}
		document.onmousemove = function(ev){
			if(this.ref.onDrag) this.ref.dragBut(ev);
		}
		
	},
		
	//------------------------------------------------------
	stopDrag: function(){
		this.onDrag = false;
	},
	//------------------------------------------------------
	dragBut: function(ev){
			ev           = ev || window.event;
		
			this.mouseOffset = this.getMouseOffset(this.butDrag, ev);
			
			var val = this.butDrag.offsetTop;
			val += this.mouseOffset.y;
			
			this.updateDragBut(val - this.butDrag.offsetHeight);

	},
	//------------------------------------------------------
	updateDragBut: function(val){
		if(val < 0){
			this.butDrag.style.top = this.init_YDrag;
			this.objToScroll.style.top = this.maxScroll;
		}else{
			if(val > this.heightDraging - this.butDrag.offsetHeight){
				this.butDrag.style.top = this.heightDraging + this.init_YDrag - this.butDrag.offsetHeight;
				this.objToScroll.style.top = this.minScroll;
			}else{
				this.butDrag.style.top = val + this.init_YDrag;
				this.objToScroll.style.top = (val/(this.heightDraging - this.butDrag.offsetHeight))*this.minScroll;
			}
		}
	},
		
	//------------------------------------------------------
	scrollAnim: function(){
		var val = this.objToScroll.offsetTop;
		val += this.sens*2;
		
		if(val < this.minScroll){
			this.objToScroll.style.top = this.minScroll + "px";
			this.stop();
		}else{
			if(val > this.maxScroll){
				this.objToScroll.style.top = this.maxScroll + "px";
				this.stop();
			}else{
				this.objToScroll.style.top = val + "px";
				this.updateDragBut(-(val/this.heightScroll)*this.heightDraging);
			}
		}
	},
	//------------------------------------------------------
	getPosition: function (e){
		var left = 0;
		var top  = 0;

		while (e.offsetParent){
			left += e.offsetLeft;
			top  += e.offsetTop;
			e     = e.offsetParent;
		}

		left += e.offsetLeft;
		top  += e.offsetTop;

		return {x:left, y:top};
	},
	//------------------------------------------------------	
	getMouseOffset: function (target, ev){
		ev = ev || window.event;

		var docPos    = this.getPosition(target);
		var mousePos  = this.mouseCoords(ev);
		return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
	},
	//------------------------------------------------------
	mouseCoords: function (ev){
		ev           = ev || window.event;
		
		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
		};
	}
		
		
}