var buh_Scroller = new Class({
	version: '1.0',
	options: {
		'scroll': {
			'duration': 500,
			'itemsPerClick': 2,
			'transition': Fx.Transitions.Quad.easeOut
		},
		'arrows': {
			'effect': false,
			'opacity': 0.85,
			'align': 'center'
		}
	},
	initialize: function (el, options) {
		this.element = $(el);
		if (!this.element) return this;
		this.setOptions(options);
		this.container = new Element('div', {
			'class': 'buh_scroller-container'
		}).inject(this.element, 'before').setStyle('position', 'relative');
		this.wrapper = new Element('div', {
			'class': 'buh_scroller-wrapper'
		}).inject(this.container).adopt(this.element);
		if (window.ie6) this.wrapper.setStyle('width', this.element.getCoordinates().width);
		var wrapperWidth = this.element.getCoordinates().width;
		this.elements = this.element.getChildren().filter(function (div) {
			return div.getTag() == 'div'
		});
		this.elements[0].addClass('first');
		this.elements[this.elements.length - 1].addClass('last');
		this.size = this.getSize(this.elements);
		this.size.x += wrapperWidth;
		this.current = 0;
		this.element.setStyles({
			'width': this.size.x,
			'height': this.size.y
		});
		var wrapperSize = this.wrapper.getStyle('width').toInt() + this.wrapper.getStyle('padding-left').toInt() + this.wrapper.getStyle('padding-right').toInt() + this.wrapper.getStyle('margin-left').toInt() + this.wrapper.getStyle('margin-right').toInt() + this.wrapper.getStyle('border-left').toInt() + this.wrapper.getStyle('border-right').toInt();
		if (this.size.x > wrapperSize) {
			this.scroller = new Fx.Scroll(this.wrapper, {
				'duration': this.options.scroll.duration,
				'transition': this.options.scroll.transition,
				'wait': false
			}).set([0, 0]);
			this.makeArrows()
		}
		return this
	},
	makeArrows: function () {
		this.leftArrow = new Element('div', {
			'class': 'buh_scroller-leftarrow'
		}).inject(this.container, 'top');
		this.rightArrow = new Element('div', {
			'class': 'buh_scroller-rightarrow'
		}).inject(this.container);
		if (window.ie6) {
			this.leftArrow.inject(this.container.getParent(), 'top');
			this.rightArrow.inject(this.container.getParent())
		};
		var arrows = $$(this.leftArrow, this.rightArrow);
		arrows.setStyles({
			'position': 'absolute',
			'top': 0,
			'cursor': 'pointer'
		});
		if (this.options.arrows.align == 'center') {
			var wrapperHeight = this.wrapper.getCoordinates().height;
			var leftHeight = this.leftArrow.getCoordinates().height;
			var rightHeight = this.rightArrow.getCoordinates().height;
			var substract = {
				'left': (wrapperHeight - leftHeight) / 2,
				'right': (wrapperHeight - rightHeight) / 2
			};
			this.leftArrow.setStyle('top', substract.left);
			this.rightArrow.setStyle('top', substract.right)
		} else if (this.options.arrows.align == 'bottom') {
			arrows.setStyles({
				'top': '',
				'bottom': 0
			})
		};
		this.leftArrow.setStyle('left', 0);
		this.rightArrow.setStyle('right', 0);
		if (this.options.arrows.effect) {
			var opacity = this.options.arrows.opacity,
			scroller = this.scroller;
			arrows.each(function (arrow) {
				var fx = new Fx.Style(arrow, 'opacity', {
					duration: 200,
					wait: false
				}).set(1);
				arrow.addEvents({
					'mouseenter': function () {
						fx.start(opacity)
					},
					'mouseleave': function () {
						fx.start(1)
					},
					'click': function () {
						var x = scroller.now[0];
						if (arrow.hasClass('buh_scroller-rightarrow')) {
							this.current = this.setCurrent('right');
							scroller.toElement(this.elements[this.current])
						} else {
							this.current = this.setCurrent('left');
							scroller.toElement(this.elements[this.current])
						}
					}.bind(this)
				})
			},
			this)
		}
	},
	setCurrent: function (direction) {
		var tmp = this.current,
		amount = this.options.scroll.itemsPerClick;
		if (direction == 'left') {
			if (tmp - amount >= 0) this.current -= amount;
			else this.current = 0
		} else {
			if (tmp + amount < this.elements.length) this.current += amount
		}
		return this.current
	},
	getSize: function (els) {
		var size = {
			'x': 0,
			'y': 0
		};
		els.each(function (el) {
			var tmp = el.getSize().size;
			var padding = {
				'x': el.getStyle('padding-left').toInt() + el.getStyle('padding-right').toInt(),
				'y': el.getStyle('padding-top').toInt() + el.getStyle('padding-bottom').toInt()
			};
			var margin = {
				'x': el.getStyle('margin-left').toInt() + el.getStyle('margin-right').toInt(),
				'y': el.getStyle('margin-top').toInt() + el.getStyle('margin-bottom').toInt()
			};
			var border = {
				'x': el.getStyle('border-left-width').toInt() + el.getStyle('border-right-width').toInt(),
				'y': el.getStyle('border-top-width').toInt() + el.getStyle('border-bottom-width').toInt()
			};
			size.x += tmp.x + margin.x + padding.x + border.x;
			if (tmp.y > size.y) size.y = tmp.y + padding.y + margin.y + border.y
		});
		return size
	}
});
buh_Scroller.implement(new Options);


