var Slideshow = Class.create();
Object.extend(Slideshow.prototype, {
	initialize:    function(elements) {
		var args = $A(arguments);
		this.options = Object.extend({
			wrap: false
		}, typeof(args.last) == 'String' ? {} : args.pop());
		this.picture = $(elements.picture);
		this.current_num = $(elements.current_num);
		this.left = $(elements.left);
		this.right = $(elements.right);
		var pictures = args;
		pictures.shift();
		this.register_pics.apply(this, pictures);
		this.register_events();
		if( !this.options.wrap ) this.left.hide();
		if( !this.options.wrap && (pictures.length < 2) )
			this.right.hide();
	},
	next:          function() {
		this.current(this.current()+1);
	},
	previous:      function() {
		this.current(this.current()-1);
	},
	current:       function(val) {
		if( this.piclist.length == 0 ) return;
		var current = parseInt(this.current_num.innerHTML);
		if( typeof(val) != 'undefined' ) {
			if( val > this.piclist.length ) val = 1;
			if( val < 1 ) val = this.piclist.length;
			this.current_num.innerHTML = val;
			this.picture.setAttribute('src',
				this.piclist[val-1].src);
			this.left.show();
			this.right.show();
			if( !this.options.wrap ) {
				if(val == 1) this.left.hide();
				if(this.piclist.length == 1) this.right.hide();
				if(val == this.piclist.length) this.right.hide();
			}
			return val;
		}
		return current;
	},
	register_pics: function() {
		this.piclist = $A(arguments);
		this.piclist = this.piclist.collect(function(img) {
			var i = new Image();
			i.src = img;
			return i;
		});
	},
	register_events: function() {
		this.left.style.cursor = 'pointer';
		Event.observe(this.left, 'click',
			this.previous.bindAsEventListener(this));
		this.right.style.cursor = 'pointer';
		Event.observe(this.right, 'click',
			this.next.bindAsEventListener(this));
	}
});