// Routines for scrolling content in a window.

function ScrollContent(name,url,stillTime)
	{
	this.name		= name
	this.url		= url;
	this.stillTime	= stillTime;
	};
	
function scrollTimer(interval)
	{
	this.index=0;
	this.scrollers=new Array();
	this.interval=interval;
	this.add = function( object )
		{
		this.scrollers[this.index]=object;
		this.index++;
		}
	};
	
var theScrollTimer=new scrollTimer(50);

function scrollTimeout() {
	var i;
	for(i=0;i<theScrollTimer.scrollers.length;i++) {
		theScrollTimer.scrollers[i].timeout(theScrollTimer.interval);
	}
	setTimeout(scrollTimeout,theScrollTimer.interval);
};
	
function ScrollView(name,top,left,height,width)
	{
	this.name=name;
	this.divName=name+"-scrollViewDiv";
	this.contentList=new Array();
	this.next=0;
	this.top=top;
	this.left=left;
	this.height=height;
	this.width=width;
	this.offset=0;
	this.currentArticle = 0;
	var scrolldiv = document.createElement("div");
	scrolldiv.id = this.divName;
	document.getElementById(name).appendChild(scrolldiv);
	setSize(this.divName,height,width);
	setPosition(this.divName, top, left);
	scrolldiv.className = "lltScroll";
	document.getElementById(this.name).appendChild(scrolldiv);
	

	// This adds the next article on the list to the scroll view
	this.showArticle=function( startIndex)
		{
		this.currentArticle	= startIndex;
		
		document.getElementById(this.divName).innerHTML="";

		var i;
		var newDiv;
		var index;
		for(i=0;i<3;i++)
			{
			index=(startIndex+i) % this.contentList.length;
			newDiv =document.createElement("DIV");
			newDiv.id=this.contentList[index].name+"-articleDiv";
			document.getElementById(this.divName).appendChild(newDiv);
			loadFile(this.contentList[index].url,newDiv.id);
		
			// This initialization of newDiv.style.height is to account for the differences between IE & Chrome...
			if(newDiv.clientHeight>0)newDiv.style.height=null;
			else newDiv.style.height=0;
			}
		this.countDown=this.contentList[startIndex].stillTime;
		};

	//adds content. Content must be a ScrollContent object.
	this.add=function(content)
		{
		this.contentList[this.next]=content;
		this.next++;
		};
	
	//this gets called at each timer increment
	this.timeout=function(increment)
		{
		if(this.countDown<=0)
			{
			this.scroll();
			}
		this.countDown-=increment;
		};
	
	//this gets called when we scroll
		this.scroll = function() {
		    var currentDiv = this.currentObject();
		    this.offset++;
		    if (currentDiv != null && this.offset >= currentDiv.clientHeight) {
		        this.offset = 0;
		        // remove last Article
		        if (currentDiv != null) document.getElementById(this.divName).removeChild(currentDiv);
		        this.currentArticle++;
		        this.currentArticle = (this.currentArticle) % this.contentList.length;
		        this.showArticle(this.currentArticle);
		    }
		    currentDiv = this.currentObject();
		    currentDiv.style.marginTop = -this.offset;
		};
	

	// Function to get the current article object
	this.currentObject=function(){return document.getElementById(this.contentList[this.currentArticle].name+"-articleDiv");};
	
	// this starts the scrolling at a random article
	this.startRandom = function()
		{
		this.currentArticle=Math.floor(Math.random()*this.contentList.length);
		this.showArticle(this.currentArticle);
		
		}
	}

// This is the function called to run the scroller.
function initScroll()
{
  var scroll=new ScrollView("scroll-articles",0,0,600,260);
  scroll.add(new ScrollContent("abbey","abbey.htm",5000));
  scroll.add(new ScrollContent("annie","annie.htm",5000));
  scroll.add(new ScrollContent("ryan","ryan.htm",5000));
  scroll.add(new ScrollContent("jean-anne","jean-anne.htm",5000));
  scroll.add(new ScrollContent("batman","fatman.htm",5000));
  scroll.add(new ScrollContent("greek-slps","angelique.htm",5000));
  scroll.add(new ScrollContent("celestia","celestia.htm",5000));
  scroll.add(new ScrollContent("addie", "addie.htm", 5000));
  scroll.add(new ScrollContent("alfie", "alfie.htm", 5000));
  scroll.startRandom();
  theScrollTimer.add(scroll);
  scrollTimeout();
}
