function scroll(id){
	this.id=id;
	this.dragInit=false;
	this.documentBody=null;
	var self=this;
	DOMReady(function(){self.init();});
}

scroll.prototype.set= function(){
	if (typeof(this.id)=='object'){
		this.div=DOM(this.id.div);		
		this.line=DOM(this.id.line);
		this.point=DOM(this.id.point);
		this.btnUp=DOM(this.id.up);
		this.btnDown=DOM(this.id.down);
		if (this.id.slider) this.slider=DOM(this.id.slider);
		else this.slider=null;
	}
	else {
		this.div=DOM(this.id);
		this.line=DOM(this.id+'_line');
		this.point=DOM(this.id+'_point');
		this.btnUp=DOM(this.id+'_up');
		this.btnDown=DOM(this.id+'_down');		
	}
}

scroll.prototype.init= function(){
	this.set();
	
	this.delta=parseInt(this.div.scrollHeight)-parseInt(this.div.offsetHeight);
	this.div.style.overflow='hidden';
	if (this.delta<=0){
		if (this.slider) this.slider.style.display='none';
		else this.line.parentNode.style.display='none';
		return;
	}
	
	this.line.style.position='relative';
	
	this.point.style.position='absolute';
	this.point.style.top='0px';
	
	this.maxTop=this.line.offsetHeight-this.point.offsetHeight;
	
	var self=this;
	this.line.listen('click', function(e){self.onClick(e);});
	
	if (this.btnUp) this.btnUp.listen('click', function(){self.up();});
	
	if (this.btnDown) this.btnDown.listen('click', function(){self.down();});
	
	if (browser.msie){
		this.documentBody=DOM(null);
		this.div.listen('scroll',function(e){self.onScroll(e)});
	}
	else {
		this.div.listen('scroll', function(e){self.onScroll(e)});
		this.line.listen('scroll', function(e){self.onScroll(e)});
	}
	
	DOM((browser.mozilla)?window:document.body).listen('keydown', function(e){
		if (e.keyCode==keyCode.up) 		return self.up();
		if (e.keyCode==keyCode.down) 	return self.down();
		if (e.keyCode==keyCode.pgDown) 	return self.scrollTo(self.div.scrollTop+self.div.clientHeight);
		if (e.keyCode==keyCode.pgUp) 	return self.scrollTo(self.div.scrollTop-self.div.clientHeight);
		if (e.keyCode==keyCode.home) 	return self.scrollTo(0);
		if (e.keyCode==keyCode.end)		return self.scrollTo(self.delta);
		
		return true;
	});
	//drag'n'drop:
	this.point.listen('mousedown', function(e){self.dragStart();});
	//selection:
	this.div.listen('mousedown',function(e){
		self.selectStart=true;
		self.div.listen('mousemove', function(e){
			if (self.selectStart){
				var offsetBottom= self.div.offsetTop+self.div.offsetHeight-e.pageY;
				if (offsetBottom<5){self.scrollTo(self.div.scrollTop+20);}
				if (self.div.offsetHeight-offsetBottom<5) {self.scrollTo(self.div.scrollTop-20);}
			}
			return true;
		});
		self.div.listen('mouseup', function(e){self.selectStart=false; return true;});
		return true;
	});
	this.btnActive();
	
}

scroll.prototype.onScroll=function(e){	
	if (e.scrollDelta<0) this.down();
	else this.up();
}

scroll.prototype.onClick= function(e){
	this.moveTo(e.pageY- this.line.offsetTop);
}

scroll.prototype.moveTo= function(xtop){
	var top=Math.min(this.maxTop,xtop);
	if (top<0) top=0;
	var perc=top/this.maxTop;
	this.point.style.top=top+'px';
	this.div.scrollTop=Math.ceil(perc*this.delta);
	this.btnActive();
}

scroll.prototype.up= function(){		
	this.moveTo(parseInt(this.point.style.top)-this.point.offsetHeight);
}

scroll.prototype.down= function(){	
	this.moveTo(parseInt(this.point.style.top)+this.point.offsetHeight);
}

scroll.prototype.scrollTo= function(pos){
	if (pos<0) pos=0;
	else if(pos>this.delta) pos=this.delta;
	this.div.scrollTop=pos;
	var top=Math.ceil(this.maxTop*pos/this.delta);
	this.point.style.top=top+'px';
	this.btnActive();
}

scroll.prototype.btnActive= function(){
	if (this.div.scrollTop>0) this.btnUp.detachClass('disabled');
	else this.btnUp.attachClass('disabled');
	
	if (this.div.scrollTop>=this.delta) this.btnDown.attachClass('disabled');
	else this.btnDown.detachClass('disabled');
}

scroll.prototype.dragStart= function(e){
	if (!this.dragInit){
		var self=this;
		if (!this.documentBody) this.documentBody=DOM(null);
		this.documentBody.listen('mouseup', function(e){self.dragEnd();});
		this.documentBody.listen('mousemove',function(e){self.dragProgress(e);});
//		this.dragInit=true;
	}
	this.drag=true;
}

scroll.prototype.dragProgress= function(e){
	if (this.drag) this.onClick(e);
}

scroll.prototype.dragEnd= function(){
	this.drag=false;
}