var slideshow = new Class({
	cursor: 0,
	delay: 5000,
	transition: 500,
	transitioning: false,
	status: "stopped",
	images: [],
	loaded: 0,
	onChange: function(n) {},
	
	ready: function() {
		if(this.images.length == this.loaded) return true
		return false
	},
	
	initialize: function(container) {
		p = this.parentContainer = container
		this.parentContainer.style.backgroundRepeat = "no-repeat"
		
		c = this.childContainer = document.createElement("div")
		c.style.backgroundRepeat = "no-repeat"
		c.style.width = (p.offsetWidth)+"px"
		c.style.height = (p.offsetHeight)+"px"
		
		p.appendChild(c)
		this.loadImage()
	},
	
	play: function() {
		if(!this.images) return false
		this.next()
		this.period = (function() {this.next()}).periodical(this.delay,this)
		this.status = "playing"
	},
	
	rewind: function() {	
		if(!this.images) return false
		this.period = (function() {this.previous()}).periodical(this.delay,this)
	},
	
	stop: function() {
		$clear(this.period)
		this.status = "stopped"
	},
	
	previous: function() {
		if(!this.images || !this.ready) return false
		if(this.transitioning == "transitioning") return false
		if(this.cursor > 0) {
			this.cursor--
			this.loadImage()
		} else {
			this.cursor = this.images.length-1
			this.loadImage()
		}
	},
	
	next: function() {
		if(!this.images || !this.ready) return false
		if(this.transitioning == "transitioning") return false
		if(this.cursor < this.images.length-1) {
			this.cursor++
			this.loadImage()
		} else {
			this.cursor = 0
			this.loadImage()
		}
	},
	
	loadImage: function() {
		if(!this.images) return false
		this.transitioning = "transitioning";
		$(this.childContainer).setOpacity(0);
		(function(){this.childContainer.style.backgroundImage = 'url("'+this.images[this.cursor]+'")'}).delay(100,this);
		
		this.current = this.images[this.cursor]
		tFx = new Fx.Style(this.childContainer, 'opacity',{
			onComplete: (function(self) {
				this.transitioning = "standby"
				this.onChange(this.cursor)
				self.parentNode.style.backgroundImage = self.style.backgroundImage
			}).bind(this),
			duration: this.transition
		})
		tFx.custom(0,1)
	},
	
	addImage: function(image) {
		new Asset.image(image, {onload: (function() {this.loaded++}).bind(this)});
		this.images[this.images.length] = image
		if(!this.current) this.loadImage()
	}
})