var ZNFader = new Class({

	Implements: Options,

	options: {
		duration: 2000,
		transition: 200,
		autostart: false
	},

	initialize: function(element, images, options){
		this.element = element;
		this.images = images;
		this.idx = 0;
		this.setOptions(options);

		this.element2 = document.createElement('div');
		this.element2 = $(this.element2);
		
		this.element2.className = this.element.className;
		this.element2.setStyle('opacity', 0);

		this.element.getParent().insertBefore(this.element2, this.element);
	
		this.imagesAsset = new Asset.images(images);

		this.element.set('tween', {duration: this.options.transition, wait: false});
		this.element2.set('tween', {duration: this.options.transition, wait: false});

		if (this.options.autostart == true) {
			this.play();
		}
	},

	play: function () {
		//this.element.setStyle('background-image', 'url("'+this.images[this.idx]+'")');
		this.element.adopt(this.imagesAsset[this.idx]);

		if (this.options.duration > 0) {
			this.slideFunction = function () {
				this.idx = (this.idx+1) % this.images.length;

				if (this.element.get('opacity') == 0) {
					//this.element.setStyle('background-image', 'url("'+this.images[this.idx]+'")');
					this.element.innerHTML = "";
					this.element.adopt(this.imagesAsset[this.idx]);
					this.element2.fade('out');
					this.element.fade('in');
				} else {
//					this.element2.setStyle('background-image', 'url("'+this.images[this.idx]+'")');
					this.element2.innerHTML = "";
					this.element2.adopt(this.imagesAsset[this.idx]);
					this.element.fade('out');
					this.element2.fade('in');
				}
			}

			this.slideFunction.periodical(this.options.duration, this);
			
		}
	}
});

shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};
