/*
################################
# Scrolling news item
# v0.2
# By E.Leitch
#
# v0.3 9/29/06  
#   now you can change the speed of movement and the fps rate
#   also some light repair on the stoping method
#
# Setup
#
# First you need to include this file in the page. You'll want to create the
# object after the page has finished loading. Create the varible you're going
# to use outside the window.onload method. Then inside set the object to the
# function set the variable to the object
#
# <script language="Javascript">
#	var newsFeed;
#	window.onload = function()
#	{
#		newsFeed = new scrollingNews('parentDiv',10);
#
# When creating the object you need to pass in two things. First the name of the
# container div, and the amount of time you want it to wait between news items.
# The next thing is to add items to the object.
# 
# newsFeed.addItem('bit of news');
# 
# To start the newsfeed call the startNews method.
# 
# newsFeed.startNews();
# 
# To control the speed of the scrolling you can change the moveBy variable 
# newsFeed.moveBy = 1(slow) - 5(fast)
# 
# If need be you can also change the fps it animates at 
# newsFeed.speed = 1000 //rate of update
# newsFeed.fps = 30 //frames per second
# 
# 
# NOTES
# It's important to note what styles need to be set on the container div
# 
# height:145px;
# width:210px;
# overflow:hidden;
# position:relative;
# 
#
################################


*/
function scrollingNews(parentDiv,idleTime)
{
	this.idleTime = idleTime; //this is how long the news will wait in seconds
	this.parentDiv = document.getElementById(parentDiv); //this is the main div on the outside
	this.newsItems = new Array(); //this will store all the items we're scrolling through
	
	this.animateInterval = 0; //we move this property out to the div so it can be controlled
	this.startNextStory = 0; //this is used when setting the timeout for the next news item to scroll
	this.newsIndex = 0;
	
	this.newsScrolling = true; //when the news is moving this is true
	this.allowStart = false; //false to begin with. once stop is pressed we can start
	
	this.divHeight = this.parentDiv.offsetHeight;
	
	this.speed = 1000;
    this.rate = 30;
    
    this.moveBy = 1;
    
	this.debug = true; //set this to false to stop the output
	
	//create child inside of parent
	this.childDiv = document.createElement('div'); //name of the div we're feeding into
	this.childDiv.id = "_childDiv";
	
	this.childDiv.style.top = this.divHeight+"px";
	this.childDiv.style.left = "0px";
	this.childDiv.style.position = "relative";
	
	this.childDiv.spaceFromTop = parseInt(this.childDiv.style.top);
	
	this.parentDiv.appendChild(this.childDiv);
	//relink the internal child to the DOM child
	this.childDiv = document.getElementById('_childDiv');
	
	scrollingNews.prototype.addItem = addItem;
	scrollingNews.prototype.buildNews = buildNews;
	scrollingNews.prototype.startNews = startNews;
	scrollingNews.prototype.stopNews = stopNews;
	
	scrollingNews.prototype.moveUp = moveUp;
	scrollingNews.prototype.animate = animate;
	
	scrollingNews.prototype.stopNewsBtn = stopNewsBtn;
	scrollingNews.prototype.startNewsBtn = startNewsBtn;
	
	scrollingNews.prototype.stopScroll = stopScroll;
	scrollingNews.prototype.startScroll = startScroll;
}

function addItem(item)
{
	this.newsItems.push(item);
}

//this starts the news scroll. internally used
function startNews()
{
    //set the speed it moves
    this.moveBy = -(this.moveBy); //changed to negative so it scrolls up
    
    //calculate the fps
    this.fps = this.speed / this.rate;
    
	//set the onMouseOver and onMouse Methods
	this.parentDiv.onmouseover = this.stopScroll;
	this.parentDiv.onmouseout = this.startScroll;
	
	//attach a reference to the whole object so we can access the everything inside the onMouse methods
	this.parentDiv.me = this; 
	
	//build all news items
	this.buildNews();
	
	//alert(this.parentDiv.style.overFlow);
	this.moveUp();
}

function buildNews()
{
	for(var i=0;i<this.newsItems.length;i++)
	{
		var toyDiv = document.createElement('div');
		
		toyDiv.id = "_news"+i;
		toyDiv.name = toyDiv.id;
		toyDiv.style.paddingBottom = "15px";
		
		toyDiv.innerHTML = this.newsItems[i];
		
		//attach toy to child
		this.childDiv.appendChild(toyDiv);
	}
}

//all this needs to do is clear the interval. internally used
function stopNews()
{
	if(this.debug)window.status = 'news has stopped';
}

//this just needs to clear the interval and stop the animation from starting again
function stopNewsBtn()
{
	this.stopNews();
	//this will stop the animation from loading the next item
	this.newsScrolling = false; //don't start the next news item
	this.allowStart = true;
}

//this needs to start the news again, and have multi-click protection
function startNewsBtn()
{
	if(this.allowStart)
	{
		//start the animation gain
		this.newsScrolling=true;
		//this will stop the news from starting over and over and over again
		this.allowStart = false;
		this.startNews();
	}
}

function moveUp()
{
	var me = this;
	this.newsScrolling = true;
	this.animateInterval = setInterval(function(){me.animate();},this.fps);
}

function animate()
{
	var top = parseInt(this.childDiv.spaceFromTop += this.moveBy);
	
    this.childDiv.style.top = (parseInt(this.childDiv.style.top) + this.moveBy) +"px";
	
	if(top<=2)
	{
		clearInterval(this.animateInterval);
		
		//restart the news scroll
		if(this.newsIndex == this.newsItems.length)
		{
			this.childDiv.style.top = this.divHeight+"px";
			this.childDiv.spaceFromTop = parseInt(this.childDiv.style.top);
			this.newsIndex = 0;
			
			var me = this;
			setTimeout(function(){me.moveUp();},1);
		}
		//scroll to the next item
		else if(this.newsScrolling)
		{
			//sets the new stop point
			this.childDiv.spaceFromTop = parseInt(document.getElementById('_news'+this.newsIndex).offsetHeight); 
			
			//set the new timer
			var me = this;
			this.startNextStory = setTimeout(function(){me.moveUp();},this.idleTime*1000);
			
			this.newsIndex++;
			//now the news has stopped moving
			this.newsScrolling = false;
		}
	}
}

function stopScroll()
{
	//this will stop everything
	clearInterval(this.me.animateInterval);
	clearTimeout(this.me.startNextStory);
}

function startScroll()
{
	//if the news has stop scrolling we need to set a timer when it will start again
	if(!this.me.newsScrolling)
	{
		var me = this.me;
		this.me.startNextStory = setTimeout(function(){me.moveUp();},this.me.idleTime*500);
	}
	//if the news is still scrolling this will let it continue
	else
	{
		this.me.moveUp();
	}
}
