﻿var flashnewsScrollers=[];
function flashnewsscrollerStarter(id)
{
	//try
	{
		new flashnewsScroller(id);
	}
	//catch(e)
	{
	//	setTimeout("flashnewsscrollerStarter(\""+id+"\")",30);
	}
}

function flashnewsScroller(id)
{
	this.Container=document.getElementById(id);
	this.Container.Item=this;
	this.List=this.Container.childNodes[0];
	this.GetVisibleHeight();
	this.GetFullHeight();
	flashnewsScrollers[this.Index=flashnewsScrollers.length]=this;
	this.StartTime=new Date().getTime();
	this.timer=setInterval("flashnewsScrollers["+this.Index.toString()+"]._timePasses()",this.TIMEOUT);
	this.Container.onmouseover=function(){this.Item.Paused=true;};
	this.Container.onmouseout=function(){this.Item.Paused=false;};
	if(typeof(this.Container.onmousewheel)!="undefined")
		this.Container.onmousewheel=function(e){return this.Item.OnMouseWheel(e||event);};
	else if(this.Container.addEventListener)
		this.Container.addEventListener('DOMMouseScroll', function(e){return this.Item.OnMouseWheel(e||event);}, false);
}
flashnewsScroller.prototype=
{
	INITIAL_TIMEOUT:4000,
	TIMEOUT:40,
	DELTA:1,
	WHEEL_FACTOR:4,
	GetVisibleHeight:function()
	{
		return parseInt(this.Container.offsetHeight);
	},
	GetFullHeight:function()
	{
		return parseInt(this.List.offsetHeight);
	},
	GetY:function()
	{
		var y;
		return isNaN(y=parseInt(this.List.style.top))?0:y;
	},
	SetY:function(y)
	{
		this.List.style.top=y.toString()+"px";
	},
	_timePasses:function()
	{
		var t=new Date().getTime();

		if((!this.Paused)&&((t-this.StartTime)>=this.INITIAL_TIMEOUT))
			this._scroll(this.DELTA);
	},
	OnMouseWheel:function(e)
	{
		var d=0,Xtra=1;
		if(e)
		{
			if(e.wheelDelta)
			{
				d=-e.wheelDelta/40;
				if(window.opera)
					d=-d;
			}
			else if(e.detail)
				d=e.detail;
		}
		if(!d)
			return true;
		if(e.preventDefault)
			e.preventDefault();
		e.returnValue=false;
		if(e.shiftKey)
			Xtra=3;
		this._scroll(d*this.WHEEL_FACTOR*Xtra);
		return false;
	},
	_scroll:function(delta)
	{
		var h,H,t,y;
		h=this.GetVisibleHeight();
		H=this.GetFullHeight();
		if(h>=H)
		{
			t="";
			y=0;
		}
		else
		{
			t="Fermati qui sopra per fermare lo scorrimento automatico.\nUsa la rotella del mouse per scorrere manualmente le news.";
			y=this.GetY()-delta;
			if(y<-H)
			{
				if(delta>0)
					y=h
				else
					y=-H;
			}
			if(y>h)
			{
				if(delta<0)
					y=-H;
				else
					y=h;
			}
		}
		if(this.Container.title!=t)
			this.Container.title=t;
		this.SetY(y);
	}
}
