BT.ns(function() {with(BT) {
	Carousel = Class.create({
		initialize: function(containerId, options) {
			this.setOptions(options);
			
			this.currentIndex = 0;
			this.liSize = 0;
			this.animDone = true;
			
			this.containerId = containerId;
	
			this.updateCount();
			
			//this.buttonLeft = $(this.options.leftId);
			//this.buttonRight = $(this.options.rightId);
		
			//this.buttonLeft.observe('click', this.moveLeft.bindAsEventListener(this));
			//this.buttonRight.observe('click', this.moveRight.bindAsEventListener(this));

			if(this.options.show_selected)
				this._show_selected();
			else			
				this.moveLeft(); // force the left button to start disabled

            this.automove(8500);
		},

        automove: function(timeout){
            var obj = this;

            clearTimeout(obj.current_timer);

            if(!timeout)
                var timeout = 6000;

            if(obj.currentIndex + 1 == obj.listCount){
                obj.current_timer = setTimeout(function() {obj.allLeft();}, timeout);
            }
            else {
                obj.current_timer = setTimeout(function() {obj.moveRight();}, timeout);
            }
        },
		
		updateCount: function() {
			this.ul = $$('#' + this.containerId + ' ul')[0];
			this.listCount = this.ul.getElementsByTagName("li").length;
			
			if(this.listCount < this.options.numVisible) {
				this.options.numVisible = this.listCount;
			}
		},
		
		moveRight: function() {
	
			if (this.animDone != true) 
				return false;
			
			if (this.currentIndex + this.options.numVisible + this.options.increment <= this.listCount)
				this._scroll(-this.options.increment);
			else 
				this._scroll(-(this.listCount - (this.currentIndex + this.options.numVisible)));
			
			if(this.listCount == this.currentIndex + this.options.numVisible)
				Element.addClassName(this.buttonRight, this.options.disabledClass);
			
			Element.removeClassName(this.buttonLeft, this.options.disabledClass);

            this.automove();

		},
		
		moveLeft: function() {
			if (this.animDone != true)
				return false;
			
			var inc = this.options.increment;
	
			if (this.currentIndex - inc < 0)
				inc = this.currentIndex;
			
			this._scroll(inc);
			
			if (this.currentIndex == 0)
				Element.addClassName(this.buttonLeft, this.options.disabledClass);
			
			Element.removeClassName(this.buttonRight, this.options.disabledClass);

            this.automove();
			
		},
		
		allRight: function() {
			if (this.animDone != true)
				return false;
		
			var inc = this.listCount - this.currentIndex - this.options.numVisible;
			this._scroll(-inc);
			
			Element.addClassName(this.buttonRight, this.options.disabledClass);
			Element.removeClassName(this.buttonLeft, this.options.disabledClass);

            this.automove();
		},
		
		allLeft: function() {
			if (this.animDone != true)
				return false;
		
			this._scroll(this.currentIndex, true);

            this.automove(200);
		},
		
		_show_selected: function() {

			var selected = this.ul.select('.' + this.options.selected_class)[0];
			
			var index = this.ul.childElements().indexOf(selected);
			
			var offset = this.options.show_offset;
			
			if(index > offset){

				if((this.listCount - index) < offset)
					offset = this.options.numVisible - (this.listCount - index);

				if(offset > 0)
					index = -index + offset;

				if (index + this.options.numVisible > this.listCount)
					index = this.listCount - this.options.numVisible;
				
				if (index < 0)
					index = 0;
				
				this._scroll(-1 * index);

			}
		},
		
		_scroll: function(delta, effect_duration) { 
			this.animDone = false;

            if(!effect_duration)
                var effect_duration = 0.5;
            else
                var effect_duration = 0.0
			
			if(this.liSize <= 0) 
				this.liSize = this._getLiElementSize();

            new Effect.MoveBy(this.ul, 0, delta * this.liSize, {duration: effect_duration, afterFinish: function() {this.animDone = true}.bind(this)});
    
            this.currentIndex -= delta;

            this._select_current_nav_item(this.currentIndex);
	
		},
		
		_getLiElementSize: function() {
			var li = $(this.ul.getElementsByTagName("li")[0]);
			return li.getDimensions().width + parseFloat(li.getStyle("margin-left")) + parseFloat(li.getStyle("margin-right"));
		},

        moveTo: function(list_position){
			if (this.animDone != true)
				return false;

            var delta = this.currentIndex - list_position;

            //this will happen if we're at the last element (which is fake)
            if(delta == this.listCount){
                list_position = 0;
                delta = this.currentIndex - this.listCount;
            }

            this._scroll(delta);

            this.automove();

        },

        _select_current_nav_item: function(current_list_position) {

            if(current_list_position + 1 == this.listCount)
                current_list_position = "0";

            $$('#carousel_nav .carousel_nav_item').each(function(nav_item) {
                nav_item.removeClassName('selected');
                nav_item.addClassName('not_selected');
            });

            $('carousel_nav_pos_' + current_list_position).removeClassName('not_selected');
            $('carousel_nav_pos_' + current_list_position).addClassName('selected');
        },
	
		setOptions: function (options) {
			this.options = {
				numVisible: 1,
				slider_element: 'ul',
				increment: 1,
				leftId: 'slide_left',
				rightId: 'slide_right',
				disabledClass: 'slide_disabled',
				show_selected: false,
				selected_class: 'selected',
				show_offset: 0,
                instanceName: 'slideGallery' 
			};
			Object.extend(this.options, options || {});
		}
	});
}});
