var BackgroundFader = new Class({
	Implements: Options,
	options: {
		parentID: 'backgrounds',
		childrenClass: 'bg_fader',
		duration: 15000,
		_backgrounds: []
	},
	breakAnimation: false,
	timer: null,
	initialize: function (options) {
		this.setOptions(options);
		this.options._backgrounds = $(this.options.parentID).getChildren('.' + this.options.childrenClass);
		this.options._backgrounds.each(function (el) {
			el.setStyles({
				'opacity': '0',
				'visibility': 'hidden'
			});
		});
		
		this.timer = this.animate.delay(this.options.duration, this);
	},
	animate: function () {
		if (!this.breakAnimation) {
			this.options._backgrounds[1].set('tween', {duration: parseInt(this.options.duration / 5, 10)}).tween('opacity', 0, 1);
			this.timer = this.setup.delay(this.options.duration / 2, this);
		}
	},
	setup: function () {
		var _prevBackground = this.options._backgrounds[0];
		var _nextBackground = this.options._backgrounds.getLast();
		_prevBackground.setStyles({
			'opacity': '0',
			'visibility': 'hidden'
		});
		if (!this.breakAnimation) {
			_prevBackground.injectAfter(_nextBackground);
			this.options._backgrounds = $(this.options.parentID).getChildren('.' + this.options.childrenClass);
			this.timer = this.animate.delay(this.options.duration, this);
		}
	}
});

window.addEvent('domready', function (event) {
	var bgFader = new BackgroundFader();
});
