// JavaScript Document
/**
	@author		Mauro Panzeri
	@date		2009-12-19
	@revision	1.0
*/
function NewsFader(){
//------------ PARAMETRI CONFIGURABILI ------------//
	this.interval = 10; // interval in seconds
	this.fadeSpeed = 30; //higher this value, slower the fade.
//-------------------------------------------------//
	this.list; 
	this.tickerObj; 
	this.fermaNews=false;
	this.opacity = 0;
	this.count=0;
	//queste variabili servono per poter richiamare le setTimeOut senza perdere il riferimento all'oggetto 
	this.id = NewsFader.Instances.length;
	NewsFader.Instances[this.id] = this;
	
	this.fadeText = function () {
		if(this.tickerObj)
		{
			if(this.opacity<95) {
				this.opacity+=5; // increase color darkness
				this.setOpacity();
				setTimeout('NewsFader.Instances['+this.id+'].fadeText()', this.fadeSpeed); 
			} else
				this.opacity=0; //reset hex value
		}
	}
	
	this.setOpacity = function () {
		if(document.all) // se sto usando internet explorer
			this.tickerObj.style.filter = "alpha(opacity=" + this.opacity + ")";
		else
			this.tickerObj.style.opacity = this.opacity / 100;	
	}
	
	this.initialiseList= function (divId) {
		this.count=0;
		this.tickerObj = document.getElementById(divId);
		if(!this.tickerObj)
			reportError("Could not find a div element with id \"" + divId + "\"");
		this.list = this.tickerObj.childNodes;
		if(this.list.length <= 0)
			reportError("The div element \"" + divId + "\" does not have any children");
		for (var i=0; i<this.list.length; i++) {
			var node = this.list[i];
			if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) 
				this.tickerObj.removeChild(node);
		}
		this.run();
	}
	
	this.run = function () {
		if(!this.fermaNews){
			this.fadeText();
			this.list[this.count].style.display = "block";
			if(this.count > 0)
				this.list[this.count-1].style.display = "none";
			else
				this.list[this.list.length-1].style.display = "none";
			this.count++;
			if(this.count == this.list.length)
				this.count = 0;
		}
		window.setTimeout('NewsFader.Instances['+this.id+'].run()', this.interval*1000);
	}
	
	this.reportError = function (error) {
		//alert("The script could not run because you have errors:\n\n" + error);
		return false;
	}
}
NewsFader.Instances = new Array();
