//Versão:1.2 - 14/10/2011
var SA = Class.create({
	scrollingDown:false,
	scrollingUp:false,
	scrollingLeft:false,
	scrollingRight:false,
	scrollingUpSpeed:0,
	scrollingDownSpeed:0,
	scrollingLeftSpeed:0,
	scrollingRightSpeed:0,
	speedTotal:4,
	speedBreak:.5,
	initialize: function(e, speedTotal, speedBreak){
		if(!SA.prototype.started){
			SA.prototype.started = true;
			SA.prototype.start();
		}
		if(speedTotal)
			this.speedTotal = speedTotal;
		if(speedBreak)
			this.speedBreak = speedBreak;
		var self = this;
		this.e=$(e);
		$$('.'+e+'_control').each(function(ce){
			ce.observe('mouseover', function(){
				if(this.hasClassName('up'))
					self.scrollingUp = true;
				else if(this.hasClassName('down'))
					self.scrollingDown = true;
					
				if(this.hasClassName('left'))
					self.scrollingLeft = true;
				else if(this.hasClassName('right'))
					self.scrollingRight = true;
			}).observe('mouseout', function(){
				if(this.hasClassName('up'))
					self.scrollingUp = false;
				else if(this.hasClassName('down'))
					self.scrollingDown = false;
				if(this.hasClassName('left'))
					self.scrollingLeft = false;
				else if(this.hasClassName('right'))
					self.scrollingRight = false;
			});
		});
		SA.prototype.scrollingInstances.push(this);
	},
	scroll: function(){
		if(this.scrollingDownSpeed>0)
			if(this.e.scrollTop>=(this.e.scrollHeight-this.e.getHeight())){
				this.scrollingDown=false;
				this.scrollingDownSpeed=0;
			}
		if(this.scrollingUpSpeed<0)
			if(this.e.scrollTop<=0){
				this.scrollingTop=false;
				this.scrollingTopSpeed=0;
			}
		if(this.scrollingRightSpeed>0)
			if(this.e.scrollRight>=(this.e.scrollWidth-this.e.getWidth())){
				this.scrollingRight=false;
				this.scrollingRightSpeed=0;
			}
		if(this.scrollingLeftSpeed<0)
			if(this.e.scrollLeft<=0){
				this.scrollingLeft=false;
				this.scrollingLeftSpeed=0;
			}
		if(this.scrollingUpSpeed<0||this.scrollingDownSpeed>0)
			this.e.scrollTop+=(this.scrollingDownSpeed+this.scrollingUpSpeed);
		if(this.scrollingRightSpeed>0||this.scrollingLeftSpeed<0){
			this.e.scrollLeft+=(this.scrollingRightSpeed+this.scrollingLeftSpeed);
		}
	}
});
SA.prototype.interval			= 30;
SA.prototype.scrollingInstances = [];
SA.prototype.start = function(){
	SA.prototype.timer = setInterval(function(){
		SA.prototype.scrollingInstances.each(function(saInstante){
			if(saInstante.scrollingDown){
				if(saInstante.scrollingDownSpeed<saInstante.speedTotal)
					saInstante.scrollingDownSpeed+=saInstante.speedBreak;
			}
			else
				saInstante.scrollingDownSpeed=saInstante.scrollingDownSpeed>0?saInstante.scrollingDownSpeed-saInstante.speedBreak:0;
			if(saInstante.scrollingUp){
				if(saInstante.scrollingUpSpeed> -saInstante.speedTotal)
					saInstante.scrollingUpSpeed+= -saInstante.speedBreak;
			}
			else
				saInstante.scrollingUpSpeed=saInstante.scrollingUpSpeed<0?saInstante.scrollingUpSpeed+saInstante.speedBreak:0;
			
			if(saInstante.scrollingRight){
				if(saInstante.scrollingRightSpeed<saInstante.speedTotal)
					saInstante.scrollingRightSpeed+=saInstante.speedBreak;
			}
			else
				saInstante.scrollingRightSpeed=saInstante.scrollingRightSpeed>0?saInstante.scrollingRightSpeed-saInstante.speedBreak:0;
			if(saInstante.scrollingLeft){
				if(saInstante.scrollingLeftSpeed> -saInstante.speedTotal)
					saInstante.scrollingLeftSpeed+= -saInstante.speedBreak;
			}
			else
				saInstante.scrollingLeftSpeed=saInstante.scrollingLeftSpeed<0?saInstante.scrollingLeftSpeed+saInstante.speedBreak:0;
			
			saInstante.scroll();
		});
	}, SA.prototype.interval);
};
