//---------------------------------------//
//
//	Scroll class.
//
//---------------------------------------//
Scroll=function(id,mask,content,btup,btdw,scrub,bar)
{
	this.init(id,mask,content,btup,btdw,scrub,bar);
}
Scroll.prototype.init=function(id,mask,content,btup,btdw,scrub,bar)
{
	//save the id.
	this.id=id;

	//define all as sprites.
	if(mask!=null) this.mask=new Sprite(this.id+'.mask',null,mask,null);
	if(content!=null) this.content=new Sprite(this.id+'.content',null,content,120);
	if(btup!=null) this.btup=new Sprite(this.id+'.btup',null,btup,null);
	if(btdw!=null) this.btdw=new Sprite(this.id+'.btdw',null,btdw,null);
	if(scrub!=null) this.scrub=new Sprite(this.id+'.scrub',null,scrub,null);
	if(bar!=null) this.bar=new Sprite(this.id+'.bar',null,bar,null);
	
	//sinc the elements size and proportions. 
	this.sinc();
}
Scroll.prototype.start=function()
{
	//define all the events necessary for the scroll.
	this.scrub._ref=this;
	this.scrub.onDrag=function()
	{
		var y=(((this._y()-this._ref._norm)*this._ref._p2)*-1)+1;
		this._ref.content._y(y); 
	}
	this.scrub._ref=this;
	this.scrub.startDrag(this.bar._x(),this.bar._y(),this.bar._x(),this.bar._y()+this.bar._height()-this.scrub._height());
	this.btup._ref=this;
	this.btup.onMouseDown=function()
	{
		this.onEnterFrame=function()
		{	
			var y=this._ref.content._y()-this._ref.getIncrement();
			if(y<((this._ref.content._height()-this._ref.mask._height())*-1)) y=((this._ref.content._height()-this._ref.mask._height())*-1);
			this._ref.content._y(y);
			//dinamicly adjust the scrubber.
			this._ref.scrub._y(this._ref._norm-y/this._ref._p2);
		}
	}
	this.btup.onMouseUp=function()
	{
		this.onEnterFrame=null;
	}
	this.btdw._ref=this;
	this.btdw.onMouseDown=function()
	{
		this.onEnterFrame=function()
		{
			var y=Math.round(this._ref.content._y()+this._ref.getIncrement());
			if(y>0) y=0;
			this._ref.content._y(y+1);
			//dinamicly adjust the scrubber.
			this._ref.scrub._y(this._ref._norm-y/this._ref._p2);
		}
	}
	this.btdw.onMouseUp=function()
	{
		this.onEnterFrame=null;
	}
}
//sinc the elements with each other.
Scroll.prototype.sinc=function()
{
	//sinc elements proportions.
	this._p1=this.mask._height()/this.content._height();
	var h=this.mask._height()*this._p1;
	if(h>this.bar._height())
	{
		h=this.bar._height();
		//dispara callback definindo-o como false, ou seja scroll desnecessário;
		this.onAutoScroll(false);
	}
	//dispara callback definindo-o como true, ou seja scroll necessário;
	else {this.onAutoScroll(true);}
	//adjust the scrubber to the correct size for the content.
	this.scrub._height(h);
	//find the second proportion.
	this._p2=(this.content._height()-this.mask._height())/(this.bar._height()-this.scrub._height());
	//find the normalizer value.
	this._norm=this.bar._y();
}
Scroll.prototype.getIncrement=function(){ return this._increment; };
Scroll.prototype.setIncrement=function(prm){ this._increment=prm; }; 
//callback.
Scroll.prototype.onAutoScroll=function(prm){
	if(prm==false){
		this.btup._visible(0)
		this.btdw._visible(0)
		this.scrub._visible(0)
		this.bar._visible(0)
	}
}
Scroll.prototype.anchorAt=function(prm){
	var y=prm;
	this.content._y(y+1);
	this.scrub._y(this._norm-y/this._p2);
}