// Velocità di scroll
var scrollSpeed = 150;

// Passo di scroll
var step = 1; 

// Elemento contenitore
var theContainer;

// Elemento da scrollare
var theContent;

//Altezza dell'elemento da scrollare
var contentHeight;

// Altezza dell'elemento contenitore
var containerHeight;

// Flag di controllo del timer
var timer = false;

// Timer per lo scrolling
var timerName;

// Funzione di inizializzazione
function initializeScroll(){
	theContainer = $('news');
	theContent = $('newsContent');
	if (theContainer && theContent){
		containerHeight = theContainer.offsetHeight;
		contentHeight = theContent.offsetHeight;
		if (contentHeight > containerHeight){
			timer = true;
			timerName = setTimeout("scrollUp()", scrollSpeed);
		}
	}
}

// Funzione di scroll
function scrollUp() {
	if(parseInt(theContent.style.top) <= 0){
		theContent.style.top = (parseInt(theContent.style.top) - step) + 'px';
		if (parseInt(theContent.style.top) + contentHeight < containerHeight - 10){
			timer = false;
			shiftToTop();
			
		}
	} else { 
		theContent.style.top = '0px';
	}
	if (timer) timerName = setTimeout("scrollUp()", scrollSpeed);
}

// Ferma lo scroll
function stop() {
	clearTimeout(timerName);
}

// Riposiziona l'oggetto
function shiftToTop(){
	new Effect.Fade(theContent, {duration: 1.0, queue: 'end', afterFinish: function(){theContent.setStyle({ top: '0px', delay: 0.5, queue: 'end'});}});
	new Effect.Appear(theContent, {duration: 1.0, delay: 0.5, queue: 'end', afterFinish: function(){timer = true;
		timerName = setTimeout("scrollUp()", scrollSpeed);}});
}

// Funzione per lo stacking di funzioni per l'evento window.onload
// Serva anche agli altri script
function addLoadEvent(func) { 
	var oldonload = window.onload; 
	if (typeof window.onload != 'function') { 
		window.onload = func; 
	} else { 
		window.onload = function() { 
			if (oldonload) { 
				oldonload(); 
			} 
			func(); 
		} 
	} 
} 

// Aggiunge l'inizializzatore dello scroll all'evento window.onload
addLoadEvent(initializeScroll);
