if (!document.easing) {
	document.write('<scr' + 'ipt language="javascript" type="text/javascript" src="/global/js/classes/easing.js"></scr' + 'ipt>');
}

if (!String.pad) {
	document.write('<scr' + 'ipt language="javascript" type="text/javascript" src="/global/js/classes/string_extended.js"></scr' + 'ipt>');
}

var Story = function (newsTitle, newsDate, newsLink, parent) {

	if (newsTitle && newsTitle.length && newsDate && newsDate.length && newsLink && newsLink.length) {
		this.title = newsTitle;
		this.date = newsDate;
		this.link = newsLink;
		this.parent = parent;
	}

	this._is_valid = function () {
		if (this.title.length > 0) {
			return true;
		}
		return false;
	};

	this.output = function (node) {
		if (this._is_valid() && node) {
			node.lastChild.nodeValue = this.title;
			node.setAttribute("title", "Read more about '" + this.title + "'.");
			node.setAttribute("href", this.link);
		}
	};

	return this;

};

var StoryList = function () {

	if (document.getElementById && document.createElement) {

		this.node = null;
		this.stories = new Array();
		this.currentStory = 0;
		this.displayTime = 4.0; // in decimal seconds
		this.interval = null;
		this.isRunning = false;
		this.direction = 'next';
		this.useTransition = false;
		if (document.css && document.easing) {
			this.useTransition = true;
			this.transitionInTime = 0.81; // in decimal seconds
			this.transitionOutTime = 0.33; // in decimal seconds
		}

	}

	this._is_valid = function () {
		if (this.node && this.stories.length >= 0) {
			return true;
		}
		return false;
	};

	this.add = function (newsTitle, newsDate, newsLink) {
		if (this.stories.length >= 0) {
			var key = this.stories.length;
			this.stories[key] = new Story(newsTitle, newsDate, newsLink, this);
			if (this.stories[key]._is_valid()) {
				return true;
			}
			this.stories.pop();
		}
		return false;
	};

	this.button = function (direction) {
		if (direction == 'previous' || direction == 'next') {
			var	node = document.createElement("a"),
				nodeImage = document.createElement("img");
			nodeImage.setAttribute("alt", direction);
			nodeImage.setAttribute("src", "../images/spacer.gif");
			nodeImage.setAttribute("height", "10");
			nodeImage.setAttribute("width", "6");
			node.appendChild(nodeImage);
			node.setAttribute("id", "news-" + direction);
			node.setAttribute("title", "View the " + direction + " news item");
			node.setAttribute("href", "#news");
			node.onmouseover = function () { document.news.hold(); };
			node.onmouseout = function () { document.news.resume(); };
			eval("node.onclick = function () { document.news.shift('" + direction + "', true); return false; }");
			return node;
		}
		return null;
	};

	this.init = function (id) {
		if (id && id.length && (nodeDiv = document.getElementById(id))) {
			if ((newsItem = document.getElementById("news-item"))) {
				this.node = newsItem;
				this.stories[this.currentStory].output(this.node);
				this.node.onmouseover = function () { document.news.hold(); };
				this.node.onmouseout = function () { document.news.resume(); };
				if ((buttonP = this.button('previous')) && (buttonN = this.button('next'))) {
					this.node.parentNode.appendChild(buttonP);
					this.node.parentNode.appendChild(buttonN);
				}
				this.show();
				this.interval = setInterval("document.news.shift('next');", Math.floor((this.displayTime + this.transitionOutTime + this.transitionInTime) * 1000));
			}
		}
	};

	this.shift = function (direction, clicked) {
		if (this._is_valid() && !this.isRunning) {
			this.isRunning = true;
			this.direction = direction;
			if (this.useTransition && !clicked) {
				setTimeout("document.easing.begin(document.news.node, 'opacity', 0, function () { document.news.changeStory(); }, document.news.transitionOutTime);", 5);
			} else {
				document.css.setStyleValue(this.node, 'opacity', 0);
				this.changeStory();
			}
		}
	};

	this.changeStory = function () {
		if (this._is_valid()) {
			if (this.direction == 'next') {
				this.currentStory++;
				if (this.currentStory == this.stories.length) {
					this.currentStory = 0;
				}
			} else {
				this.currentStory--;
				if (this.currentStory < 0) {
					this.currentStory = this.stories.length - 1;
				}
			}
			this.stories[this.currentStory].output(this.node);
			this.show();
		}
	};

	this.show = function () {
		if (this._is_valid()) {
			if (this.useTransition) {
				setTimeout("document.easing.begin(document.news.node, 'opacity', 1, function () { document.news.isRunning = false; }, document.news.transitionInTime);", 5);
			} else {
				document.css.setStyleValue(this.node, 'opacity', 1);
				this.isRunning = false;
			}
		}
	};

	this.hold = function () {
		if (this.interval) {
			clearInterval(this.interval);
		}
	};

	this.resume = function () {
		if (this._is_valid() && !this.isRunning && this.wait == null) {
			this.interval = setInterval("document.news.shift('next');", Math.floor((this.displayTime + this.transitionOutTime + this.transitionInTime) * 1000));
		}
	};

	return this;

};

if (!document.all || (document.all && (pos = navigator.appVersion.search(/msie/i)) != -1 && parseInt(navigator.appVersion.substring(pos + 5, navigator.appVersion.indexOf(';',pos))) >= 7)) {
	if (window.onload) {
		var currentFunction = window.onload;
		window.onload = function () {
			currentFunction();
			document.news.init("news");
		};
	} else {
		window.onload = function () { document.news.init("news"); };
	}
}
