Effect.DefaultOptions.duration = 0.3;
NewsTicker = Class.create();
Object.extend(NewsTicker.prototype, {
	
	tickerDiv: "ticker", 
	tickerLocation: "billboard", 
	tickerTitle: "news-link",
	tickerLink: "/www/content/news/newsdetail.php?nf=",  // Need to pull the .nitf vlaue from the XML file and pass to the link //
//	tickerLink: "/www/content/news/",
	feedURL: "/www/content/news/FullFeedrss.xml",
	pauseLength: 4000,
	timer: 0,
	currentTitle: 0,
	nitfFile: "",
	items: null,
	titleLength : 140,
	initialize: function() {
		
		this.items = [];
		
		new Ajax.Request(
			this.feedURL, {
				method: "get",
				onSuccess: function(response) {
					this.parseXML(response.responseXML);
					this.buildTicker();
				}.bind(this),
				onFailure: function() {
					console.log("Please visit http://www.fujitsu.com/news for the latest news and information on Fujitsu.");
				},
				onException: function(req, err) {
					// throw(err);
				}
			}
		);
	},
	
	buildTicker: function() {
		// replace the placeholder content with the first news title
		if (this.items[this.currentTitle]) {
			var newsTickerLink = this.buildNewsLink(this.items[this.currentTitle]['pubdate'], this.items[this.currentTitle]['link'])
			newTickerLink = this.tickerLink + newsTickerLink
			$(this.tickerTitle).setAttribute("href", newTickerLink); 
		  	$(this.tickerTitle).innerHTML =this.items[this.currentTitle]['title'] ;
			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) {
			title = item.getElementsByTagName("title")[0].childNodes[0].nodeValue.substr(0,this.titleLength) + "...";
			//var link = NewsTicker.tickerLink; 
			pubDate = item.getElementsByTagName("pubDate")[0].childNodes[0].nodeValue;
			link = item.getElementsByTagName("link")[0].childNodes[0].nodeValue;
			this.items.push({title: title, link: link, pubdate: pubDate });
		}.bind(this));
	},
	
	buildNewsLink: function(pubDt, link) {
		var finalURL = "";
		pubDt="" + pubDt;
		link ="" + link; 
		var newsYr = pubDt.substr(12,2);
		var newsLink = link;
		newsLink = link.substring(parseInt(parseInt(link.indexOf("release_id="))+11), link.indexOf("&tsource")); 
		finalURL = newsYr + "" + newsLink + ".nitf";
		return finalURL;
	},
	
	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)});

	},
	
    switchData: function() {
    		var newsTickerLink = this.buildNewsLink(this.items[this.currentTitle]['pubdate'], this.items[this.currentTitle]['link'])
    		newTickerLink = this.tickerLink + newsTickerLink
		$(this.tickerTitle).setAttribute("href", newTickerLink); // this.tickerLink
		if (this.items[this.currentTitle]) {
			$(this.tickerTitle).innerHTML = this.items[this.currentTitle]['title'];
		}
	}
});

Event.observe(window, 'load', function() {
	var ticker = new NewsTicker();
});

