var DEF_DIR_UP = 1;
var DEF_DIR_DOWN = -1;
var DEF_DIR_LEFT = 2;
var DEF_DIR_RIGHT = -2;
var DEF_MODE_SLIDE = 0;
var DEF_MODE_BOUNCE = 1;

function Marquee(divName, spanName, varName)
{
	DEF_INTERVAL = 50;				//milliseconds
	DEF_STOPTIME = 3000;			//milliseconds
	DEF_PXSHIFT = 5;				//pixels
	DEF_MARGIN = 20; 				//pixels
	DEF_DIR = DEF_DIR_LEFT;
	DEF_MODE = DEF_MODE_SLIDE;
	DEF_LOOPS = 1;
	DEF_TARGET = "_blank";
	DEF_CSSCLASS = null;
	DEF_BLINKINTERVAL = 0;
	DEF_ONMOUSESTOP = true;

	this.Get_DEF_INTERVAL = function() { return DEF_INTERVAL;}
	this.Get_DEF_STOPTIME = function() { return DEF_STOPTIME;}
	this.Get_DEF_PXSHIFT = function() { return DEF_PXSHIFT;}
	this.Get_DEF_MARGIN = function() { return DEF_MARGIN;}
	this.Get_DEF_DIR = function() { return DEF_DIR;}
	this.Get_DEF_MODE = function() { return DEF_MODE;}
	this.Get_DEF_LOOPS = function() { return DEF_LOOPS;}
	this.Get_DEF_TARGET = function() { return DEF_TARGET;}
	this.Get_DEF_CSSCLASS = function() { return DEF_CSSCLASS;}
	this.Get_DEF_BLINKINTERVAL = function() { return DEF_BLINKINTERVAL;}
	this.Get_DEF_ONMOUSESTOP = function() { return DEF_ONMOUSESTOP;}
	
	this.Set_DEF_INTERVAL = function(value) { DEF_INTERVAL = value; }
	this.Set_DEF_STOPTIME = function(value) { DEF_STOPTIME = value; }
	this.Set_DEF_PXSHIFT = function(value) { DEF_PXSHIFT = value; }
	this.Set_DEF_MARGIN = function(value) { DEF_MARGIN = value; }
	this.Set_DEF_DIR = function(value) { DEF_DIR = value; }
	this.Set_DEF_MODE = function(value) { DEF_MODE = value; }
	this.Set_DEF_LOOPS = function(value) { DEF_LOOPS = value; }
	this.Set_DEF_TARGET = function(value) { DEF_TARGET = value; }
	this.Set_DEF_CSSCLASS = function(value) { DEF_CSSCLASS = value; }
	this.Set_DEF_BLINKINTERVAL = function(value) { DEF_BLINKINTERVAL = value; }
	this.Set_DEF_ONMOUSESTOP = function(value) { DEF_ONMOUSESTOP = value; }	
	
	var _myName = varName;
	var _div = null;
	var _divName = divName;
	var _span = null;
	var _spanName = spanName;
	var _messages = new Array();
	var _curMsg = 0;
	var _timer = null;
	var _scrollPos = null;
	var _running = false;
	var _firstRun = true;
	var _blinkTimeout = null;
	
	//PSEUDO-EVENTS
	this.OnChangeMessage = null;	//function(oldMsg, newMsg)
	this.OnEndLoops = null;			//function()
	
	//FUNCTIONS
	this.Start = function()
	{
		_running = true;
		if (_div == null)
			_div = document.getElementById(_divName);
		if (_span == null)
		{
			_span = document.getElementById(_spanName);
			if (DEF_ONMOUSESTOP)
			{
				_span.onmouseover = function() {eval(_myName + ".Pause();");}
				_span.onmouseout = function() {eval(_myName + ".Start();");}
			}
		}
		HandleShow();
	}
	
	this.Pause = function()
	{
		_running = false;
		clearTimeout(_timer);
		_timer = null;
		if (_blinkTimeout != null)
			StopBlinking();
	}

	this.Stop = function()
	{
		Pause();
		_curMsg = 0;
		InitSpan(_messages[_curMsg], false);
		for (var i = 0; i < _messages.length; i++)
			_messages[i].ClearLoopsDone();
		_firstRun = false;
	}
	
	var InitSpan = function(msg, setHTML)
	{
		if (setHTML)
		{
			if (msg.HRef == null)
				_span.innerHTML = msg.Msg;
			else
			{
				_span.innerHTML = "<a href='" + msg.HRef + "'"
								+ " target='" + msg.Target + "'"
								+ (msg.CssClass == null ? "" : " class='" + msg.CssClass) + "'>"
								+ (msg.Msg == null ? msg.HRef : msg.Msg) + "</a>"
			}
		}
		switch(msg.Dir())
		{
			case DEF_DIR_UP:
				_span.style.top =  (_div.offsetHeight + DEF_MARGIN) + "px";
				_span.style.left = "0px";
				_scrollPos = _span.offsetTop;
				break;
			case DEF_DIR_DOWN:
				_span.style.top = (-_span.offsetHeight - DEF_MARGIN) + "px";
				_span.style.left = "0px";
				_scrollPos = _span.offsetTop;
				break;
			case DEF_DIR_LEFT:
				_span.style.top = "0px";
				_span.style.left = (_div.offsetWidth + DEF_MARGIN) + "px";
				_scrollPos = _span.offsetLeft;
				break;
			case DEF_DIR_RIGHT:
				_span.style.top =  "0px";
				_span.style.left = (-_span.offsetWidth - DEF_MARGIN) + "px";
				_scrollPos = _span.offsetLeft;
				break;
		}
	}
	
	Blink = function()
	{
		if (_span.style.visibility == "visible")
			_span.style.visibility = "hidden";
		else
			_span.style.visibility = "visible";
	}	

	StopBlinking = function()
	{
		clearTimeout(_blinkTimeout);
		_blinkTimeout = null;
		_span.style.visibility = "visible";
	}	
	
	HandleShow = function()
	{
		if (_messages.length > 0)
		{
			msg = _messages[_curMsg];
			
			if (_blinkTimeout != null)
				StopBlinking();
			
			//Senza forzatura IE si perde via...why??
			_span.style.visibility = "visible";

			if (msg.HasEnded())
			{
				_curMsg = (_firstRun || _curMsg >= _messages.length - 1) ? 0 : _curMsg + 1;
				msg = _messages[_curMsg];
				InitSpan(msg, true);
				msg.LoopsDone = 0;
				_firstRun = false;
			}
			
			switch(msg.Dir())
			{
				case DEF_DIR_UP:
					if (_scrollPos == null)
						InitSpan(msg, false);
					else
						_scrollPos -= msg.PxShift;
					_span.style.top = _scrollPos + "px";
					if (_scrollPos <= -_span.offsetHeight - DEF_MARGIN)
					{
						if (msg.HasEnded() || msg.Mode != DEF_MODE_BOUNCE)
							_scrollPos = null;
						if (msg.Mode == DEF_MODE_BOUNCE)
							msg.scrollingDir = -msg.scrollingDir;
						msg.LoopsDone++;
					}
					break;
				case DEF_DIR_DOWN:
					if (_scrollPos == null)
						InitSpan(msg, false);
					else
						_scrollPos += msg.PxShift;
					_span.style.top = _scrollPos + "px";
					if (_scrollPos >= _div.offsetHeight + DEF_MARGIN)
					{
						if (msg.HasEnded() || msg.Mode != DEF_MODE_BOUNCE)
							_scrollPos = null;
						if (msg.Mode == DEF_MODE_BOUNCE)
							msg.scrollingDir = -msg.scrollingDir;
						msg.LoopsDone++;
					}
					break;
				case DEF_DIR_LEFT:
					if (_scrollPos == null)
						InitSpan(msg, false);
					else
						_scrollPos -= msg.PxShift;
					_span.style.left = _scrollPos + "px";
					if (_scrollPos <= -_span.offsetWidth - DEF_MARGIN)
					{
						if (msg.HasEnded() || msg.Mode != DEF_MODE_BOUNCE)
							_scrollPos = null;
						if (msg.Mode == DEF_MODE_BOUNCE)
							msg.scrollingDir = -msg.scrollingDir;
						msg.LoopsDone++;
					}
					break;
				case DEF_DIR_RIGHT:
					if (_scrollPos == null)
						InitSpan(msg, false);
					else
						_scrollPos += msg.PxShift;
					_span.style.left = _scrollPos + "px";
					if (_scrollPos >= _div.offsetWidth + DEF_MARGIN)
					{
						if (msg.HasEnded() || msg.Mode != DEF_MODE_BOUNCE)
							_scrollPos = null;
						if (msg.Mode == DEF_MODE_BOUNCE)
							msg.scrollingDir = -msg.scrollingDir;
						msg.LoopsDone++;
					}
					break;
			}
			if (_running)
			{
				var hasToStop = (_scrollPos != null && msg.scrollingDir == 1 && 
								(msg.StopTime > 0 || msg.StopTime == -1) && _scrollPos >= 0 
							  && _scrollPos < msg.PxShift);
				if (hasToStop && msg.BlinkInterval > 0)
					_blinkTimeout = setInterval("Blink()", msg.BlinkInterval);
				if (hasToStop && msg.StopTime == -1)
					return;
				_timer = setTimeout("HandleShow()",  hasToStop ? msg.StopTime : msg.Interval);
			}
		}
	}
	
	this.AddMessage = function(str, blinkinterval, dir, mode, loops, pxshift, interval, stoptime, href, target, cssclass)
	{
		_messages[_messages.length] = new Message(this, str, blinkinterval, dir, mode, loops, pxshift, interval, 
														stoptime, href, target, cssclass);
	}
}

function Message(marq, msg, blinkinterval, dir, mode, loops, pxshift, interval, stoptime, href, target, cssclass)
{
	this.Msg = msg;
	this.BlinkInterval = (blinkinterval == undefined || blinkinterval == null) 
					   ? marq.Get_DEF_BLINKINTERVAL() : blinkinterval;
	this._dir = (dir == undefined || dir == null) ? marq.Get_DEF_DIR() : dir;
	this.Mode = (mode == undefined || mode == null) ? marq.Get_DEF_MODE() : mode;
	this.Loops = (loops == undefined || loops == null) ? marq.Get_DEF_LOOPS() : loops;
	this.PxShift = (pxshift == undefined || pxshift == null) ? marq.Get_DEF_PXSHIFT() : pxshift;
	this.Interval = (interval == undefined || interval == null) ? marq.Get_DEF_INTERVAL() : interval;
	this.StopTime = (stoptime == undefined || stoptime == null) ? marq.Get_DEF_STOPTIME() : stoptime;
	this.HRef = (href == undefined) ? null : href;
	this.Target = (target == undefined || target == null) ? marq.Get_DEF_TARGET() : target;
	this.CssClass = (cssclass == undefined) ? marq.Get_DEF_CSSCLASS() : cssclass;
	
	this.LoopsDone = -1;	// -1 => Non inizializzato
	this.scrollingDir = 1;
	
	this.Dir = function()
	{
		return this._dir * this.scrollingDir;
	}
	
	this.HasEnded = function()
	{
		if (this.LoopsDone >= 0 && (this.Loops <= 0 || (this.Loops > 0 && this.LoopsDone < this.Loops)))
			return false;
		return true;
	}
	
	this.ClearLoopsDone = function()
	{
		this.LoopsDone = -1;
		this.scrollingDir = 1;
	}
}
