var SpwSlider = new Class({

	Implements: [Options, Events],
	
	options: {
		startIndex: 0,
		className 	: '',
		interval 		: 5000
	},
	
	initialize: function(items,parent,options){
		this.parent = $(parent);
		this.setOptions(options);
		this.addImages($$(items));
		this.showImage();
	},
	
	images: [],
	
	items: [],
	
	img2destroy: null,
	
	addImages: function(images) {
		var showImage = this.showImage;
		images.each(function(image) {
				this.images.push(image.rel);
				this.items.push(image);
				image.addEvent('click',this.showImage.bind(this,image.id.replace('link','')));
				return false;
		},this);
	},
	
	showImage: function(img2show) {
		if(!img2show) {
			img2show = this.options.startIndex;
		} else {
			this.options.startIndex = img2show;
		}
		var img2destroy = this.img2destroy;
		var images = this.images;
		var parent = this.parent;
		var loadImage = this.loadImage;
		var items = this.items;
		var className = this.options.className;
		if(this.images[img2show]) {
			if($('hebele')) {
				var destroyFx = new Fx.Tween($('hebele'),{
					duration: 500,
					onComplete: function() {
						$('hebele').destroy();
						items.each(function(item) {
							item.removeClass('hover');
						});
						items[img2show].addClass('hover');
						loadImage(img2show,parent,images,className);
					}
				});
				destroyFx.start('opacity','0');
			} else {
				this.loadImage(img2show,parent,images,className);
			}
			
			
			this.options.startIndex++;
			if(this.options.startIndex==this.images.length) {
				this.options.startIndex = 0;
			}
		}
		if(this.images.length>1) {
			$clear(this.timer);
			this.timer = this.showImage.delay(this.options.interval,this);
		}
	},
	
	loadImage: function(img2show,parent,images,className) {
		parent.startWaiting(className);
		var myImage = new Asset.image(images[img2show], {
			onload: function() {
				parent.stopWaiting();
				myImage.set('opacity','0');
				myImage.id = 'hebele';
				myImage.inject(parent);
				new Fx.Tween(myImage,{
					duration: 500
				}).start('opacity',0,1);
			}
		});
	}

});