﻿
var news = new Array();
var arrayLength = 0;

function newsArray(newsItems) {
	this.newsItems = newsItems;
	arrayLength = newsItems.length;
	for (p = 0; p < newsItems.length; p++) {
		news[p] = new makeNews(newsItems[p][0], newsItems[p][1], newsItems[p][2], newsItems[p][3]).write();
	}
	rotateNews();
}

function makeNews(i, l, t, c) {
	var photo = new Image();
	photo.src = i;
	this.img = photo;
	this.link = l;
	this.title = t;
	this.caption = c;
	this.write = writeNews;
}

function writeNews() {
	var str = '<a href="' + this.link + '">';
	str += '<img border="0" src="' + 
			this.img.src + '" /></a><br/>';
	str += '<div class="NewsCaption"><a href="' + this.link + '">';
	str += '<span class="NewsTitle">' + this.title + '</span>more</a>';
	if(arrayLength > 1)
		str += '<div style="float:right;margin-top:5px;"><img src="images/leftarrow.png" onclick="prevNews()" style="padding-right:2px;cursor:pointer;" /><img src="images/rightarrow.png" onclick="nextNews()" style="cursor:pointer;" /></div></div>';
	return str;
}

var nIndex = 0;
var timerID = null;

function rotateNews() {
	var len = news.length;
	if (nIndex >= len)
		nIndex = 0;
	document.getElementById('stories').innerHTML = news[nIndex];
	nIndex++;
	timerID = setTimeout('rotateNews()', 6000);
}

function nextNews() {
	if (nIndex == arrayLength - 1)
		nIndex = 0;
	else
		nIndex++;
	document.getElementById('stories').innerHTML = news[nIndex];
	}

function prevNews() {
	if (nIndex == 0)
		nIndex = arrayLength - 1;
	else
		nIndex--;
	document.getElementById('stories').innerHTML = news[nIndex];
}

function pauseNews() {
	if (timerID != null) {
		clearTimeout(timerID);
		timerID = null;
	}
}

function playNews() {
	if (timerID == null) {
		timerID = setTimeout('rotateNews()', 1000);
	}
}
