/******************************************************************* 
* File    : JSFX_VerticalScroller.js  © JavaScript-FX.com
* Created : 2001/09/28 
* Author  : Roy Whittle  (Roy@Whittle.com) www.Roy.Whittle.com 
* Purpose : To create a cross browser news scroller
* History 
* Date         Version        Description 
* 2001-09-28	1.0		I have seen hundreds of news scrollers so I thought
*					I would try and write one that  was easy to install.
***********************************************************************/ 
JSFX.VerticalScroller = function()
{
	this.state 		= "NOT_BUILT";
	this.sx 		= 0;
	this.sy 		= 0;
	this.w 		= 100;
	this.h 		= 100;
	this.outerLayer 	= null;
	this.scrollLayer 	= null;
	this.bgColor 	= null;
	this.bgImage	= null;
	this.theStyleStr  = null;
	this.messages 	= new Array();
	this.currMessage 	= 0;
	this.step 		= 2;
	this.id 		= "JSFXScroll" + JSFX.VerticalScroller.no++;
	this.holder 	= this.id + "_h";
	this.content 	= this.id + "_c";
	this.showTime	= 1000;
	window[this.id]	= this;
}
JSFX.VerticalScroller.no = 0;
JSFX.VerticalScroller.prototype.build = function(x, y, w, h)
{
	this.outerLayer = new JSFX.Layer(" ");
	this.outerLayer.moveTo(x, y);
	this.outerLayer.resizeTo(w, h);
	this.outerLayer.show();
	this.outerLayer.clip(0,0,w, h);
	if(this.bgColor != null)
		this.outerLayer.setBgColor(this.bgColor);

	this.scrollLayer = new JSFX.Layer(" ", this.outerLayer);
	this.scrollLayer.moveTo(0,0);
	this.scrollLayer.resizeTo(w, h);
	this.scrollLayer.show();
	this.scrollLayer.setzIndex(100);

	this.state = "OFF";
}
JSFX.VerticalScroller.prototype.toHtml = function(width, height)
{
	this.w = width;
	this.h = height;
	var str = "";
	if(ns4)
	{
		str =    '<ILAYER WIDTH="'+width+'" HEIGHT="'+height+'" NAME="'+this.holder+'">'
			+  '<LAYER  WIDTH="'+width+'" HEIGHT="'+height+'" NAME="'+this.content+'">'
			+  '</LAYER></ILAYER>';
	}
	else
	{
		str =   '<div style="overflow:hidden;   width:'+width+';height:'+height+';" id="'+this.holder+'">'
			+ '<div style="position:relative; width:'+width+';height:'+height+'; filter:alpha(opacity=0); -moz-opacity:0%;" id="'+this.content+'">'
			+ '</div></div>';
	}

	return str;
}
JSFX.VerticalScroller.prototype.setShowTime = function(theInterval)
{
	this.showTime = theInterval * 1000;
}
JSFX.VerticalScroller.prototype.setBgColor = function(color)
{
	this.bgColor = color;
	if(this.state != "NOT_BUILT")
		this.outerLayer.setBgColor(color);
}
JSFX.VerticalScroller.prototype.setBgImage = function(theImage)
{
	this.bgImage = theImage;
	if(this.state != "NOT_BUILT")
		this.outerLayer.setBgImage(theImage);
}
JSFX.VerticalScroller.prototype.setStyle = function(theStyleStr)
{
	this.styleStr = theStyleStr;
}
JSFX.VerticalScroller.prototype.addMessage = function(message)
{
	this.messages[this.messages.length] = message;
}
JSFX.VerticalScroller.prototype.start = function()
{
	//Initialise the DIV/Layer if not already done so
	if(this.scrollLayer == null)
	{
		this.outerLayer  = new JSFX.Layer(JSFX.findLayer(this.holder));
		this.scrollLayer = new JSFX.Layer(JSFX.findLayer(this.content));
		if(this.bgImage != null)
			this.outerLayer.setBgImage(this.bgImage);
		else if(this.bgColor != null)
			this.outerLayer.setBgColor(this.bgColor);
	}
	this.state = "OFF";
	this.animate();
}

JSFX.VerticalScroller.prototype.animate = function(message)
{
	if(this.state == "OFF")
	{
		var str = "";
		if(this.styleStr != null)
			str += '<span style="'+this.styleStr+'">';

		str += this.messages[this.currMessage];

		if(this.styleStr != null)
			str += '</span>';

		this.sx=0;
		this.sy=this.h;
		this.scrollLayer.moveTo(this.sx, this.sy);
		this.scrollLayer.setContent(str);
		this.scrollLayer.setOpacity(100);
		this.state = "ANIMATING";	
		this.setTimeout("animate()", 40);
	}
	else if(this.state == "ANIMATING")
	{
		this.sy -= this.step;
		this.scrollLayer.moveTo(this.sx, this.sy);
		if(this.sy <= 0)
		{
			this.currMessage = (this.currMessage + 1) % this.messages.length;
			this.state = "FADING";
			this.op=100;
			this.setTimeout("animate()", this.showTime);
		}
		else
			this.setTimeout("animate()", 40);
	}
	else if(this.state == "FADING")
	{
		this.op -= 10;
		if(this.op == 0)
			this.state = "OFF";
		this.scrollLayer.setOpacity(this.op);
		this.setTimeout("animate()", 40);
	}
}
JSFX.VerticalScroller.prototype.setTimeout = function(f, t) 
{
	setTimeout("window."+this.id+"."+f, t);
}

/*** If no other script has added it yet, add the ns resize fix ***/
if(navigator.appName.indexOf("Netscape") != -1 && !document.getElementById)
{
	if(!JSFX.ns_resize)
	{
		JSFX.ow = outerWidth;
		JSFX.oh = outerHeight;
		JSFX.ns_resize = function()
		{
			if(outerWidth != JSFX.ow || outerHeight != JSFX.oh )
				location.reload();
		}
	}
	window.onresize=JSFX.ns_resize;
}
