
function TopNews () {
	
	/*********************************
		CUSTOMISE CLASS PROPERTIES
	*********************************/
	
	this._NormalCss = "normal"; // CSS to use when there is no mouse over any of the news stories
	this._RolloverCss = "rollover"; // CSS to use when the user mouse is over one of the news stories
	this._StoryTotal = 6; // The total number of news item that is allowed to be shown to a user
	this._TimeToNextStory = 5000; // in milliseconds
	
	/*********************************
		DON'T EDIT BELOW THIS POINT
	*********************************/
	
	this._HeadlineId = "headline";
	this._TopStoriesId = "topstories";
	this._CurrentStory = 1;
	this._Interval = null;
	this._NewsItems = new Array();
	this._Counter = 0;
	
	this.Initialize = function () {
		
		// Check that we have all the required class properties defined before running the class
		if(this._NormalCss == "" || this._RolloverCss == "" || this._StoryTotal < 1) {
			alert("You need to define some class properties before you can use this class");
			return;
		}
		
		this.SetHeadline(1);
		this.RollOut();
		document.getElementById(this._TopStoriesId + 1).className = this._RolloverCss;
		
	}
	
	this.SetHeadline = function (headlineNum) {
		
		this.HideAll();
		document.getElementById(this._HeadlineId + headlineNum).style.display = "block";
		
		this.ResetCssNewStory();
		document.getElementById(this._TopStoriesId + headlineNum).className = this._RolloverCss;
		
	}
	
	this.HideAll = function () {
		for (var i = 1; i <= this._StoryTotal; i++)
			document.getElementById(this._HeadlineId + i).style.display = "none";
	}
	
	this.PickStory = function () {
		
		this._CurrentStory++;
		
		if(this._CurrentStory > this._StoryTotal)
			this._CurrentStory = 1;
		
		this.SetHeadline(this._CurrentStory);
		
	}
	
	this.RollOut = function () {
		this._Interval = window.setInterval("news.PickStory();", this._TimeToNextStory);
	}
	
	this.RollOver = function (num) {
		
		if(num == 0)
			return;
		
		this._CurrentStory = num;
		this.SetHeadline(num);
		window.clearInterval(this._Interval);
		
	}
	
	this.ResetCssNewStory = function () {
		for (var i = 1; i <= this._StoryTotal; i++)
			document.getElementById(this._TopStoriesId + i).className = this._NormalCss;
	}
	
	this.AddNewsStory = function (headline, story, url, linkText, image) {
		
		var intLen = this._NewsItems.length;
		if((this._StoryTotal - 1) >= intLen) {
			
			this._NewsItems[intLen] = new Array();
			this._NewsItems[intLen]["Headline"] = headline;
			this._NewsItems[intLen]["Story"] = story;
			this._NewsItems[intLen]["Url"] = url;
			this._NewsItems[intLen]["LinkText"] = linkText;
			this._NewsItems[intLen]["Image"] = image;
			
		}
		
	}
	
	this.DisplayHeadlines = function (num) {
		
		var template = "<div id=\"topstories{NEWS_COUNTER}\" onMouseOver=\"news.RollOver({NEWS_COUNTER}); return false;\" onMouseOut=\"news.RollOut(); return false;\" onclick=\"news.GetUrl({NEWS_COUNTER});\"><div class=\"headline\">{HEADLINE}</div><div class=\"storyText\">{STORY} {LINK}</div></div>";
		
		for(var i = 0; i < this._NewsItems.length && i < num; i++, this._Counter++) {
			
			var tmpTemplate = template;
			
			if(this._NewsItems[this._Counter]["Url"] != null && this._NewsItems[this._Counter]["LinkText"] != null && 
				this._NewsItems[this._Counter]["Url"] != "null" && this._NewsItems[this._Counter]["LinkText"] != "null")
				tmpTemplate = tmpTemplate.replace(/{LINK}/gi, "<a href=\"" + this._NewsItems[this._Counter]["Url"] + "\">" + this._NewsItems[this._Counter]["LinkText"] + "</a>");
			else
				tmpTemplate = tmpTemplate.replace(/{LINK}/gi, "");
			
			tmpTemplate = tmpTemplate.replace(/{HEADLINE}/gi, this._NewsItems[this._Counter]["Headline"]);
			tmpTemplate = tmpTemplate.replace(/{STORY}/gi, this._NewsItems[this._Counter]["Story"]);
			tmpTemplate = tmpTemplate.replace(/{NEWS_COUNTER}/gi, (this._Counter + 1));
			
			document.write(tmpTemplate);
			
		}
		
	}
	
	this.DisplayStoryListing = function () {
		
		var template = "<div id=\"headline{NEWS_COUNTER}\" onclick=\"news.GetUrl({NEWS_COUNTER});\"><div class=\"headline\">{HEADLINE}</div>{IMAGE}</div>";
		
		for(var i = 0, newsCounter = 1; i < this._NewsItems.length; i++, newsCounter++) {
			
			var tmpTemplate = template;
			
			if(this._NewsItems[i]["Image"] != null && this._NewsItems[i]["Image"] != "null")
				tmpTemplate = tmpTemplate.replace(/{IMAGE}/gi, "<div class=\"image\"><img src=\"" + this._NewsItems[i]["Image"] + "\" alt=\"{HEADLINE}\" /></div>");	
			else
				tmpTemplate = tmpTemplate.replace(/{IMAGE}/gi, "");
			
			if(this._NewsItems[i]["Url"] != null && this._NewsItems[i]["LinkText"] != null && this._NewsItems[i]["Url"] != "null" && this._NewsItems[i]["LinkText"] != "null")
				tmpTemplate = tmpTemplate.replace(/{LINK}/gi, "<a href=\"" + this._NewsItems[i]["Url"] + "\">" + this._NewsItems[i]["LinkText"] + "</a>");
			else
				tmpTemplate = tmpTemplate.replace(/{LINK}/gi, "");
			
			tmpTemplate = tmpTemplate.replace(/{HEADLINE}/gi, this._NewsItems[i]["Headline"]);
			tmpTemplate = tmpTemplate.replace(/{STORY}/gi, this._NewsItems[i]["Story"]);
			tmpTemplate = tmpTemplate.replace(/{NEWS_COUNTER}/gi, newsCounter);
			
			document.write(tmpTemplate);
			
		}
		
	}
	
	this.GetUrl = function(num) {
		
		if(num < 0 || num > this._CurrentStory)
			return;
		
		window.location = this._NewsItems[--num]["Url"];
		
	}
	
}
