Effect.DefaultOptions.duration = 0.3;
NewsTicker = Class.create();
Object.extend(NewsTicker.prototype, {
    tickerBlock: "latestNews",
    tickerTitle: "latestNewsHeadline",
    tickerPubDate: "latestNewsPubDate",
    feedURL: "/rssite/action/Feed?ForumName=RS_SITE_NEWS",
    feedLimit: 5,
    pauseLength: 3500,
    timer: 0,
    currentTitle: 0,
    items: [],
    initialize: function() {
        new Ajax.Request(
            this.feedURL, {
                method: "get",
                onSuccess: function(response) {
                    this.parseXML(response.responseXML);
                    this.buildTicker();
                }.bind(this)
            }
        );
    },
	
    buildTicker: function() {
        if(this.items[this.currentTitle]) {
            this.switchData();
            this.start();
        }
    },
	
    parseXML: function(xml) {
        var months = new Array();
        var days = new Array();
        
        months[0] = "January";
        months[1] = "February";
        months[2] = "March";
        months[3] = "April";
        months[4] = "May";
        months[5] = "June";
        months[6] = "July";
        months[7] = "August";
        months[8] = "September";
        months[9] = "October";
        months[10] = "November";
        months[11] = "December";

        days[0] = "Sunday";
        days[1] = "Monday";
        days[2] = "Tuesday";
        days[3] = "Wednesday";
        days[4] = "Thursday";
        days[5] = "Friday";
        days[6] = "Saturday";
        
        $A(xml.getElementsByTagName("item")).each(function(item) {
            if(this.items.length < this.feedLimit) {
                var title = item.getElementsByTagName("title")[0].childNodes[0].nodeValue;
                var link = item.getElementsByTagName("link")[0].childNodes[0].nodeValue;
                var pubDate = new Date(item.getElementsByTagName("pubDate")[0].childNodes[0].nodeValue);

                pubDate.setUTCHours(pubDate.getUTCHours() - 6); // CST
                var formattedDate = days[pubDate.getDay()] + ', ' + months[pubDate.getMonth()] + ' ' + pubDate.getDate() + ', ' + pubDate.getFullYear();

                this.items.push({title: title, link: link, pubDate: formattedDate});
            }
        }.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('latestNews', {
                afterFinish: function() {
                    this.switchData();
                    new Effect.Appear('latestNews');
                }.bind(this)
            }
        );
    },
	
    switchData: function() {
        var item = this.items[this.currentTitle];
        $(this.tickerTitle).setAttribute("href", item['link']);
    	$(this.tickerTitle).innerHTML = item['title'];
    	$(this.tickerPubDate).innerHTML = "Posted on: " + item['pubDate'];
    }
});

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