// JavaScript Document
function Scroller(layerRef,visibleArea,objectName,scrollBarLayer,scrollButtonLayer){
	this.objectName=objectName;
	this.layerRef=layerRef;
	this.scrollBar=scrollBarLayer;
	this.scrollButton=scrollButtonLayer;
	this.amountPixelsScroll=4;
	this.initHeight=layerRef.clientHeight;
	this.visibleArea=visibleArea;
	layerRef.style.height=visibleArea;
	this.loop=false;
	this.scrollFlag=false;
	this.getCoordinate=function(value){
		return(parseInt(value.substring(0,value.indexOf('px'))));
	}
	this.scrollButton.style.top=this.getCoordinate(this.scrollBar.style.top)+'px';
	this.layerLowLimit=this.getCoordinate(layerRef.style.top);
	this.layerHighLimit=this.getCoordinate(layerRef.style.top)-this.initHeight;
	this.height=this.getCoordinate(layerRef.style.height);
	this.initTop=this.getCoordinate(layerRef.style.top);
	this.layerRef.style.overflow="hidden";
	this.canScroll=function(){
		if((this.getCoordinate(this.layerRef.style.top)-60>this.layerHighLimit)&&(this.getCoordinate(this.layerRef.style.top)<this.layerLowLimit))
			return true;
		return false;
	}
	this.scrollText=function(dir){
		var top=parseInt(this.getCoordinate(layerRef.style.top)+dir*this.amountPixelsScroll);
		this.height=parseInt(this.height-(dir*this.amountPixelsScroll));
		this.layerRef.style.top=top+'px';
		this.layerRef.style.height=(this.height<0)?0+'px':this.height+'px';
		this.drawScrollBar();
		if((this.loop)&&(this.canScroll()))
			setTimeout(this.objectName+".scrollText("+dir+")",30)
	}
	this.stopScrolling=function(){
		this.loop=false;
	}
	this.scrollUp=function(){
		if(this.getCoordinate(this.layerRef.style.top)-30>this.layerHighLimit){
			this.loop=true;
			this.scrollText(-1);
		}
		else
			this.stopScrolling();
	}
	this.scrollDown=function(){
		if(this.getCoordinate(this.layerRef.style.top)<this.layerLowLimit){
			this.loop=true;
			this.scrollText(1);
		}
		else
			this.stopScrolling();
	}
	this.drawScrollBar=function(){
		var value=this.getCoordinate(this.scrollBar.style.top)+Math.round(((this.getCoordinate(this.layerRef.style.height)-this.visibleArea)*this.getCoordinate(this.scrollBar.style.height))/this.initHeight);
		//alert(value1);
		this.scrollButton.style.top=value+'px';
	}
	this.beginScroll=function(){
		this.scrollFlag=true;
	}
	this.endScroll=function(){
		this.scrollFlag=false;
		this.scrollButton.style.cursor="default";
	}
	this.doScroll=function(){
		var deltaY=window.event.offsetY;		//valoare negativa=>scroll up, valoare pozitiva=>scroll down
		var buttonTop=this.getCoordinate(this.scrollButton.style.top);
		var barTop=this.getCoordinate(this.scrollBar.style.top);
		var buttonHeight=this.getCoordinate(this.scrollButton.style.height);
		var barHeight=this.getCoordinate(this.scrollBar.style.height);
		if(this.scrollFlag){
				this.scrollButton.style.cursor="hand";
				this.scrollButton.style.top=buttonTop+deltaY-Math.round(buttonHeight)/2+'px';
				var height=Math.round((buttonTop-barTop)*this.initHeight/barHeight)+this.visibleArea;
				this.layerRef.style.height=height+'px';
				this.layerRef.style.top=this.initTop+this.visibleArea-height+'px';
		}
	} 
}

