Slideshow = HtmlElement.extend({
	_class: 'slideshow',
	
	initialize: function() {
		var that = this;
		this.index = 0;
		var images = $$('td.image img', this);
		this.image = images[0];
		this.loadingImage = images[1];
		this.image.addEvent('load', function(event) {
			that.loadingImage.modifyClass('hidden', true);
			this.modifyClass('hidden', false);
			if(that.caption)
				that.caption.setHtml($('div.caption', that.images[that.index]).getHtml());
		});
		
		this.image.addEvent('click', function(event) {
			that.nextImage();
		});
		
		this.nextButton = $('a.nextButton', this);
		this.prevButton = $('a.prevButton', this);
		this.caption = $('td.caption div', this);
		
		this.images = [];

		$$('li', this).each(function(li, i) {
			that.images.push(li);
			li.addEvent('click', function(event) {
				if(that.index != i)
					that.changeImage(i);
				event.stop();
			});
		});
		
		this.nextButton.addEvent('click', function(event) {
			that.nextImage();
			event.stop();
		});
		
		this.prevButton.addEvents({
			click: function(event) {
				that.prevImage();
				event.stop();
			},
			mouseEnter: function(event) {
				alert('entered')
				this.modifyClass('notactive', true);
			},
			mouseleave: function(event) {
				this.modifyClass('notactive', false);
			}
		});
		
		
		
		this.hideButtons();
	},
	
	changeImage: function(index) {
		this.images[this.index].modifyClass('active', false);
		this.index = index;
		var image = this.images[this.index];
		this.image.modifyClass('hidden', true);
		this.loadingImage.modifyClass('hidden', false);
		this.image.$.src = $('a', image).$.href;
		image.modifyClass('active', true);
		this.hideButtons();
	},
	
	hideButtons: function() {
		this.nextButton.modifyClass('hideButton', this.index + 1 == this.images.length);
		this.prevButton.modifyClass('hideButton', this.index - 1 < 0);
	},
	
	nextImage: function() {
		if(this.index + 1 < this.images.length)
			this.changeImage(this.index + 1);
	},

	prevImage: function() {
		if(this.index - 1 > -1)
			this.changeImage(this.index - 1);
	}
});
