var MooSlide = new Class({
	Implements: [Options],
	
	imageList: [
			"data/home/homeslide1.jpg",
			"data/home/homeslide2.jpg",
			"data/home/homeslide3.jpg",
			"data/home/homeslide4.jpg",
			"data/home/homeslide5.jpg",
			"data/home/homeslide6.jpg"
		],
	imageIndex: 0,
	selectedImage: null,
	slideInterval: null,
	
	options: {
		
	},
	
	initialize: function(options){
		this.setOptions(options);
		if (!$('homeSlide')) {
			return;
		}
		this.imageIndex = Math.min(new Date().getDay(), 6);
		this.loadSlide();
	},
	
	loadSlide: function() {
		var _self = this;
		new Asset.images(_self.imageList, {
			onProgress: function(counter, index){
				var img = new Element('img', {
					'id': 'img'+index,
					'src': _self.imageList[index]
				}).set("opacity", 0).inject('homeSlide');
				img.fx = new Fx.Tween(img, {duration: 'long'});
			}
		});
		
		this.slide();
		this.slideInterval = setInterval(function(){
			_self.slide();
		}, 5000);
	},
	
	slide: function() {
		var img = $('img'+this.imageIndex);
		if (img) {
			if (this.selectedImage) {
				this.selectedImage.fx.cancel().start("opacity", 0);
			}
			img.fx.cancel().start("opacity", 1);
			this.selectedImage = img;
			this.imageIndex++;
			if (this.imageIndex > 5) {
				this.imageIndex = 0;
			}
			if ($('homeSlide').hasClass('loading')) {
				$('homeSlide').removeClass('loading');
			}
		}
	},
	
	pauseSlide: function() {
		clearInterval(this.slideInterval);
	}
});

document.addEvent("domready", function(e){
	new MooSlide();
});

