var SimpleGallery = new Class({
	initialize: function(container, prev, next) {
		this.prev = $(prev);
		this.next = $(next);
		this.items = $(container).getChildren();
		this.current = 0;
		this.hidden_class = 'hidden';
		this.allow_switch = true;
		
		this.setActionHandlers();
	}
});

SimpleGallery.implement({
	setActionHandlers: function() {
		
		var obj = this;
		
		this.prev.addEvent('click', function(e){
			new Event(e).stop();
			if(obj.allow_switch) {
				obj.allow_switch = false;
				var prev_ind = obj.current;
				if(obj.current <= 0) {
					obj.current = obj.items.length - 1;
				}
				else {
					obj.current--;
				}
				obj.displayItem(obj.items[prev_ind]);
			}
		});
		
		this.next.addEvent('click', function(e){
			new Event(e).stop();
			if(obj.allow_switch) {
				obj.allow_switch = false;
				var prev_ind = obj.current;
				if(obj.current >= obj.items.length - 1) {
					obj.current = 0;
				}
				else {
					obj.current++;
				}
				obj.displayItem(obj.items[prev_ind]);
			}
		});
	}, 
	
	displayItem: function(prev) {
		
		var obj = this;
		
		new Fx.Morph(prev, {
			duration: 100, 
			onComplete: function(){
				prev.addClass(obj.hidden_class);
				new Asset.image(obj.items[obj.current].getFirst('a').getFirst('img').src, {onload: function(){
					obj.items[obj.current].setStyle('opacity', 0);
					obj.items[obj.current].removeClass(obj.hidden_class);
					new Fx.Morph(obj.items[obj.current], {
						duration: 500, 
						onComplete: function(){
							obj.allow_switch = true;
						}
					}).start({
						opacity: [0, 1]
					});
				}});
			}
		}).start({
			opacity: [1, 0]
		});
	}
});