/* Copyright (c) Creative Spark, 2009 */

var ImageRotater = new Class({
	Implements: Options,
	options: {
		imagePath: '',
		images: [],
		imageDuration: 5000,
		animDuration: 800,
		transition: 'sine:in:out'
	},

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

		if (this.options.images.length <= 1) return;
		this.currentImage = 0;

		this.container = $(container);
		this.image = new Element('img', {
			src: this.container.getStyle('background-image').replace('url(', '').replace(')', '')
		});
		this.container.grab(this.image, 'top');

		this.nextImage.periodical(this.options.imageDuration, this);
	},

	nextImage: function(){
		this.container.setStyle('background', 'url(' + this.options.imagePath + this.options.images[(this.currentImage + 1) % this.options.images.length] + ') 0 0 no-repeat');
		this.image.set('tween', {
			duration: this.options.animDuration,
			onComplete: function(){
				this.image.set({
					src: this.options.imagePath + this.options.images[this.currentImage],
					styles: {
						opacity: 1
					}
				});
			}.bind(this)
		}).tween('opacity', 0);

		this.currentImage++;
		this.currentImage %= this.options.images.length;
	}
});

