Effect.DefaultOptions.duration = 0.3;
NewsTicker = Class.create();
Object.extend(NewsTicker.prototype, {
	
	tickerDiv: "ticker", 
	tickerLocation: "billboard", 
	tickerTitle: "news-link",
	tickerDesc: "news-desc",
	tickerLink: "/newsletter/",
	feedURL: "/newsletter/ticker2.php",
	pauseLength: 10000,
	timer: 0,
	currentTitle: 0,
	items: null,
	initialize: function() {
		this.items = [];
		
		new Ajax.Request(
			this.feedURL, {
				method: "get",
				onSuccess: function(response) {
					this.parseXML(response.responseXML);
					this.buildTicker();
				}.bind(this),
				onFailure: function() {
					window.alert("ERROR");
					console.log("Please visit http://www.apple.com/hotnews for the latest news and information on Apple.");
				},
				onException: function(req, err) {
					//window.alert(req+" "+err);
					// throw(err);
				}
			}
		);
	},
	
	buildTicker: function() {
		// replace the placeholder content with the first news title
		if (this.items[this.currentTitle]) {
			$(this.tickerTitle).innerHTML = this.items[this.currentTitle]['title'];
			$(this.tickerTitle).href = this.items[this.currentTitle]['link'];
			$(this.tickerDesc).innerHTML = this.items[this.currentTitle]['description'];
			this.start();// start the timer if we have valid headlines
		}
	},
	
	parseXML: function(xml) {
		// build the array of news titles
		$A(xml.getElementsByTagName("item")).each(function(item) {
			var title = item.getElementsByTagName("title")[0].childNodes[0].nodeValue;
			var linkx = item.getElementsByTagName("link")[0].childNodes[0].nodeValue;
			var description = item.getElementsByTagName("description")[0].childNodes[0].nodeValue;
			this.items.push({title: title, link: linkx, description: description});
		}.bind(this));
	},
	
	start: function() {
		this.interval = setInterval(this.showNext.bind(this), this.pauseLength);
	},
	
	stop: function() {
		clearInterval(this.interval)
	},
	
	showNext: function() {
		
		//determine next headline
		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)});
		new Effect.Fade('news-desc', {
			afterFinish: function() {
				this.switchData();
				new Effect.Appear('news-desc'); }.bind(this)});

	},
	
    switchData: function() {
		$(this.tickerTitle).setAttribute("href", this.tickerLink);
		//window.alert($(this.tickerTitle).href);
		if (this.items[this.currentTitle]) {
			$(this.tickerTitle).innerHTML = this.items[this.currentTitle]['title'];
			$(this.tickerDesc).innerHTML = this.items[this.currentTitle]['description'];
			if(this.items[this.currentTitle]['title']!="") $(this.tickerTitle).setAttribute("href", this.items[this.currentTitle]['link']);
		}
	}
});

Event.observe(window, 'load', function() {
	var ticker = new NewsTicker();
});
