var Ticker = Class.create({
	position: 0,
	width: 740,
	newsItems: null,
	initialize: function(url) {
		this.newsItems = [];
    new Ajax.Request(url, {
				method: 'get',
				onSuccess: function(response) {
					this.parseXML(response.responseXML);
					this.initializeTicker();
				}.bind(this)
			}
		);
  },
  initializeTicker: function() {
    var content = '';
    this.newsItems.each(function(item,index) {
      if ( index > 0 ) {
        content += ' <img src="/images/small_flag.gif" width="16" height="7" alt="" /> ';
      }
      content += '<a href="'+item['link']+'">'+item['title']+'</a>';
    });
    $('ticker_content').innerHTML = content;
    this.position = this.width;
		this.draw();
  	this.start();
	},
  parseXML: function(xml) {
    $A(xml.getElementsByTagName('item')).each(function(item) {
      var title = item.getElementsByTagName('title')[0].firstChild.nodeValue;
      var link = item.getElementsByTagName('link')[0].firstChild.nodeValue;
			this.newsItems.push({title: title, link: link});
    }.bind(this));
	},
	start: function() {
		this.interval = setInterval(this.next.bind(this), 10);
	},
	stop: function() {
		clearInterval(this.interval)
	},
	draw: function() {
    $('ticker_content').setStyle({'left':''+this.position+'px'});
	},
	next: function() {
	  this.position--;
	  width = $('ticker_content').getWidth();
	  if ( this.position < -width) {
	    this.position = this.width;
	  }
	  this.draw();
	}
});