var Ticker = Class.create({
	initialize: function (o) {
		this.options = o || {
			duration: 0.3,
			tickerID: 'ticker',
			shift: 50,
			intervalSpeed: 7000
		};
	},

	setOptions: function (o) {
		o = o || {};
		this.initialize(o);
	},

	scroll: function () {
		var yShift = this.options.shift;
		var currentItem = null;
		var nextItem = null;

		for(var i=0; i<this.items.length; i++)
		{
			if(this.items[i].getStyle('display') != 'none')
			{
				currentItem = this.items[i];
				nextItem = this.items[(i == this.items.length - 1 ? 0 : i + 1)];
				break;
			}
		}

		if(!currentItem || !nextItem)
		{
			if(this.items.length == 0)
			{
				throw "No items found to animate";
			}

			currentItem = this.items[0];
			nextItem = this.items[(this.items.length == 1 ? 0 : 1)];
		}

		new Effect.Opacity(
			currentItem,
			{
				from: 1,
				to: 0,
				duration: this.options.duration,
				afterFinish: function ()
				{
					currentItem.setOpacity(0);
					currentItem.setStyle({ display: 'none' });

					new Effect.Opacity(
						nextItem,
						{
							from: 0,
							to: 1,
							duration: this.options.duration,
							beforeStart: function()
							{
								nextItem.setOpacity(0);
								nextItem.setStyle({ display: '' });
							}.bind(this),
							afterFinish: function ()
							{
								nextItem.setOpacity(1);
							}.bind(this)
						}
					);
				}.bind(this)
			}
		);
	},

	startScroll: function () {
		this.items = $(this.options.tickerID).childElements();

		this.interval = window.setInterval("ticker.scroll()", this.options.intervalSpeed);
	}
});
var ticker = new Ticker;

