var KontentImageFade = new Class({

	Implements: [Options],

	options: {
		'container': 'kontent_image_fade',
		'sleep': 2000,
		'duration': 2000
	},

	initialize: function (options) {
		this.setOptions(options);
		this.container = $(this.options.container);

		if (!$defined(this.container)) {
			return;
		}

		this.images = this.container.getChildren();
		this.container.empty();
		this.container.setStyle('visibility', 'visible');

		this.container.addEvent('click', this.click.bind(this));

		this.image = this.images.pop();
		this.images.unshift(this.image);
		this.container.adopt(this.image);

		if (this.images.length > 1) {
			this.fade.bind(this).periodical(this.options.sleep + this.options.duration);
		}
	},

	fade: function () {
		var image = this.images.pop();
		this.images.unshift(image);
		this.container.setStyle('background', 'url('+ image.get('src') +') 0 0 no-repeat');

		this.image.set('tween', {
			'duration': this.options.duration,
			'onComplete': function () {
				image.replaces(this.image);

				this.image = image;
				this.image.set('opacity', 1);
			}.bind(this)
		}).tween('opacity', 0);
	},

	click: function (event) {
		var target = $(event.target);
		var href = target.get('rel');

		if ($defined(href)) {
			document.location.href = href;
		}
	}
});