/**
 * @author Administrator
 */
var mNewsFeed = new Class({
	options: {
		itemClass: 'item',
		auto: true,
		anim: {
			duration: 500,
			interval: 5000
		}
	},
	initialize: function(container, options) {
		this.setOptions(options);
		this.itemsFx = [];
		
		this.container = $(container);
		this.container.setStyle('display', 'block');
		
		this.items = this.container.getElementsBySelector('.'+this.options.itemClass);
		
		this.items.each(function(item, i) {
			this.itemsFx[i] = new Fx.Style(item, 'opacity', {duration:this.options.anim.duration});
		}, this);
		
		this.prepNewsFeed();
	},
	prepNewsFeed: function() {
		// Short hand
		var opts = this.options;
		this.shownItem = 0;
		
		this.items.each(function(item, i) {
			if(i != this.shownItem)
				item.setStyle('display', 'none');
		}, this);
		
		if(opts.auto) {
			this.nextItem.periodical(opts.anim.interval, this);
		}
	},
	nextItem: function () {
		var curItem = this.shownItem;
		var nextItem = curItem+1;
		
		if(this.items.length == nextItem)
			nextItem = 0;	
			
		this.animate(curItem, nextItem);

		this.shownItem = nextItem;
	},
	animate: function(from, to) {
		this.itemsFx[from].start(1, 0).chain(function() {
			this.items[from].setStyle('display', 'none');
			
			this.items[to].setStyles({
				visibility: 'hidden',
				opacity: 0,
				display: 'block'
			});
			
			this.itemsFx[to].start(0, 1);
		}.bind(this));
	}
});

mNewsFeed.implement(new Options);