
function NewsFeedManager(newsItems) {
	
	this.newsItems = newsItems;
	this.currentItemIndex = 0;
	this.startNewsFeed();
	
	var tempObj = this;
	
	$("#prevNewsItem").click(function() {
		clearInterval(tempObj.interval);
		tempObj.displayPreviousItem();
		tempObj.startNewsFeed();
		return false;
	});
	
	$("#nextNewsItem").click(function() {
		clearInterval(tempObj.interval);
		tempObj.displayNextItem();
		tempObj.startNewsFeed();
		return false;
	});
	
}

NewsFeedManager.prototype.startNewsFeed = function() {
	
	var tempObj = this;
	this.interval = setInterval(function() {
		tempObj.displayNextItem();
	},5000);
	
}

NewsFeedManager.prototype.displayNextItem = function() {
	
	$(this.newsItems[this.currentItemIndex]).fadeOut(500);
	this.currentItemIndex++;
	if (this.currentItemIndex >= this.newsItems.length) {
		this.currentItemIndex = 0;
	}
	$(this.newsItems[this.currentItemIndex]).fadeIn(500);
	
}

NewsFeedManager.prototype.displayPreviousItem = function() {
	
	$(this.newsItems[this.currentItemIndex]).fadeOut(500);
	this.currentItemIndex--;
	if (this.currentItemIndex < 0) {
		this.currentItemIndex = this.newsItems.length -1;
	}
	$(this.newsItems[this.currentItemIndex]).fadeIn(500);
	
}

$(document).ready(function(){
		
		var newsItems = $(".newsitem");
		var newsFeedMgr = new NewsFeedManager(newsItems);
		
	});