var AjaxAutoScrollerTon = Class.create();
AjaxAutoScrollerTon.prototype = {
	initialize: function(){
		//Parametri di default + aggiornamento da arguments[0]	
		this.options = Object.extend({
			idContainer: 'scrollContainer',
			idContent: 'scrollContent',
			scrollValue: 50,
			interval: 5
		}, arguments[0] ||
		{});
		
		// Get elements
		this.container = $(this.options.idContainer);
		this.content = $(this.options.idContent);
		this.direction = -1;
		this.scrollValue = this.options.scrollValue;
		this.interval = this.options.interval;
		this.scrollOk = 1;
		
		//Ferma il movimento quando il mouse ci va sopra
		this.fMouseOver = this._mouseOver.bindAsEventListener(this);
		this.fMouseOut = this._mouseOut.bindAsEventListener(this);
		
		this.content.observe('mouseover', this.fMouseOver);
		this.content.observe('mouseout', this.fMouseOut);

		// Install timer
		this.timer = new PeriodicalExecuter(this.scrollContent.bind(this), this.interval);
  	},
	terminate: function(){

		Event.stopObserving(this.content,'mouseover', this.fMouseOver);
		Event.stopObserving(this.content,'mouseout', this.fMouseOut);

	},
	scrollContent: function()
	{
		if(this.scrollOk == 1) {
			if(this.content.offsetHeight > this.container.offsetHeight)
			{
				if (this.direction == 1 && (this.content.offsetTop+this.scrollValue) > 0 ) {
					this.direction = -1;
				}
				else if (this.direction == -1 && (this.content.offsetTop-this.scrollValue) < -(this.content.offsetHeight - this.container.offsetHeight)) {
					this.direction = 1;
				}

				new Effect.Move(this.content, { x: 0, y: this.direction*this.scrollValue });
			}
		}
	},
	showError: function()
	{
		this.errorMessage = new Element('p',{ className: 'error'}).update('Impossibile recuperare i dati!');
		this.idContainer.insert(this.errorMessage);
	},
	_mouseOver: function(e)
	{
		this.scrollOk = 0;
	},
	_mouseOut: function(e)
	{
		this.scrollOk = 1;
	}
}


