Effect.DefaultOptions.duration = 0.3;
NewsTicker = Class.create();
Object.extend(NewsTicker.prototype, {
	tickerDiv: "ticker",
	tickerTitle: "news-link",
	feedURL: "/feeds/news.rss",
	originalContent: '<span id="ticker-headline">NEWS:</span> <a id="news-link"></a><br style="clear:both;" />',
	pauseLength: 3500,
	timer: 0,
	currentTitle: 0,
	items: null,
	initialize: function() {
		this.items = [];
		new Ajax.Request(this.feedURL, {
			method: "get",
			onSuccess: function(a) {
				this.parseXML(a.responseXML);
				this.buildTicker()
			}.bind(this),
			onFailure: function() {},
			onException: function(b, a) {}
		})
	},
	buildTicker: function() {
		if (this.items[this.currentTitle]) {
			$(this.tickerDiv).innerHTML = this.originalContent;
			$(this.tickerTitle).innerHTML = this.items[this.currentTitle]["title"];
			$(this.tickerTitle).href = this.items[this.currentTitle]["link"];
			this.start();
		}
	},
	parseXML: function(a) {
		$A(a.getElementsByTagName("item")).each(function(c) {
			title = c.getElementsByTagName("title")[0].childNodes[0].nodeValue;
			link = c.getElementsByTagName("link")[0].childNodes[0].nodeValue;
			this.items.push({
				title: title,
				link: link
			})
		}.bind(this))
	},
	start: function() {
		this.interval = setInterval(this.showNext.bind(this), this.pauseLength)
	},
	stop: function() {
		clearInterval(this.interval)
	},
	showNext: function() {
		if (this.currentTitle < this.items.length - 1) {
			this.currentTitle = this.currentTitle + 1
		} else {
			this.currentTitle = 0
		}
		new Effect.Fade("news-link", {
			afterFinish: function() {
				this.switchData();
				new Effect.Appear("news-link")
			}.bind(this)
		})
	},
	switchData: function() {
		$(this.tickerTitle).setAttribute("href", this.tickerLink);
		if (this.items[this.currentTitle]) {
			$(this.tickerTitle).innerHTML = this.items[this.currentTitle]["title"];
			$(this.tickerTitle).href = this.items[this.currentTitle]["link"];
		}
	}
});
Event.observe(window, "load",
function() {
	var a = new NewsTicker()
});
