window.addEvent('domready', function() {
	if ($('bi-container')) {
		var BScroller = new Class({
			initialize : function() {
				this.current = 0;
				this.items = $$('#bi-container .bi-item');
				this.timer = null;
				this.interval = 3000;
				this.allow_change = true;
				this.setActionHandlers();
				this.startTimer();
			}
		});
		BScroller.implement({
			setActionHandlers : function() {
				var obj = this;
				$('b_prev').addEvent('click', function(e) {
					new Event(e).stop();
					obj.showPrev();
				});
				$('b_next').addEvent('click', function(e) {
					new Event(e).stop();
					obj.showNext();
				});
			},
			show : function(item_id) {
				var obj = this;
				obj.allow_change = false;
				var hide_ef = new Fx.Morph(obj.items[obj.current], {
					duration : 100,
					onComplete : function() {
						obj.items[obj.current].addClass('hidden');
						obj.current = item_id;
						obj.items[obj.current].setStyle('opacity', 0);
						obj.items[obj.current].removeClass('hidden');
						var show_ef = new Fx.Morph(obj.items[obj.current], {
							duration : 800,
							onComplete : function() {
								obj.allow_change = true;
							}
						});
						show_ef.start({
							opacity : [ 0, 1 ]
						});
					}
				});
				hide_ef.start({
					opacity : [ 1, 0 ]
				});
			},
			showPrev : function() {
				this.stopTimer();
				if (!this.allow_change) {
					return;
				}
				if (this.current == 0) {
					this.show(this.items.length - 1);
				} else {
					this.show(this.current - 1);
				}
			},
			showNext : function() {
				this.stopTimer();
				if (!this.allow_change) {
					return;
				}
				if (this.current == this.items.length - 1) {
					this.show(0);
				} else {
					this.show(this.current + 1);
				}
			},
			stopTimer : function() {
				if (this.timer) {
					clearInterval(this.timer);
				}
			},
			startTimer : function() {
				var obj = this;
				obj.timer = setInterval(function() {
					if (obj.current < obj.items.length - 1) {
						var next = obj.current + 1;
					} else {
						var next = 0;
					}
					obj.show(next);
				}, obj.interval);
			}
		});
		new BScroller;
	}
});

