NbMooSlider = new Class({
	itemWidth: 0,
	totalWidth: 0,
	visibleItems: 4,
	slideVars: new Array,
	wrapper: null,
	innerWrapper: null,
	initialize:  function(wrapper,items,controls)
	{
		this.wrapper = wrapper;


		this.innerWrapper = new Element('div');
		this.innerWrapper.id = 'nb-teaserslider-innerwrap';
		this.innerWrapper.set('tween',{link:'wait'});

		$(this.wrapper).adopt(this.innerWrapper);

		this.totalWidth = 0;

		items.each(function(item,index){
			this.innerWrapper.adopt(item);
			this.itemWidth =  item.getSize().x;
			this.totalWidth += item.getSize().x;
		},this);

		this.innerWrapper.setStyle('width',this.totalWidth+'px');
		this.innerWrapper.setStyle('margin-left',0);

		this.initControls(controls);
		this.autoSlide();
	},
	initControls: function(controls)
	{
		this.controls = controls;

		$(this.controls.next).addEvent('click',this.next.bind(this));
		$(this.controls.prev).addEvent('click',this.prev.bind(this));

		this.controls.disableFn(controls.prev);

		if(this.totalWidth < 920)
		{
			this.controls.disableFn(controls.next);
		}
	},
	autoSlide: function()
	{
		pos = parseInt(this.innerWrapper.getStyle('margin-left'));

		this.period = (function() {
			if ((pos-this.itemWidth) >= parseInt('-'+(this.totalWidth-(this.itemWidth*this.visibleItems)))) {
				this.innerWrapper.set('tween', {
					'onComplete' : function() {
						pos = parseInt(this.innerWrapper.getStyle('margin-left'));
					}.bind(this)
				});
				this.innerWrapper.tween('margin-left', (pos-this.itemWidth)+'px');

				if((pos-this.itemWidth) <= parseInt('-'+(this.totalWidth-(this.itemWidth*this.visibleItems))))
				{
					this.controls.disableFn(this.controls.next);
				}

				if((pos-this.itemWidth) >= parseInt('-'+(this.totalWidth-(this.itemWidth*this.visibleItems))))
				{
					this.controls.enableFn(this.controls.prev);
				}
			} else {
				//$clear(this.period);
				this.innerWrapper.tween('margin-left', '0px');
				this.controls.disableFn(this.controls.prev);
				this.controls.enableFn(this.controls.next);
			}
		}).periodical(7000,this);
	},
	next: function(e)
	{
		e = new Event(e);
		e.stop();

		pos = parseInt(this.innerWrapper.getStyle('margin-left'));



		this.innerWrapper.tween('margin-left', (pos-this.itemWidth)+'px');

		if((pos-this.itemWidth) <= parseInt('-'+(this.totalWidth-(this.itemWidth*this.visibleItems))))
		{
			this.controls.disableFn(this.controls.next);
		}

		if((pos-this.itemWidth) >= parseInt('-'+(this.totalWidth-(this.itemWidth*this.visibleItems))))
		{
			this.controls.enableFn(this.controls.prev);
		}
	},
	prev: function(e)
	{
		e = new Event(e);
		e.stop();

		pos = parseInt(this.innerWrapper.getStyle('margin-left'));

		this.innerWrapper.tween('margin-left', (pos+this.itemWidth)+'px');

		if(parseInt(pos+this.itemWidth) >= parseInt('-'+(this.totalWidth-(this.itemWidth*this.visibleItems))))
		{
			this.controls.enableFn(this.controls.next);
		}

		if(parseInt(pos+this.itemWidth) == 0)
		{
			this.controls.disableFn(this.controls.prev);
		}
	}
});

var slider = null;

window.addEvent('domready',function(){

	var disableFunc = function(el){
		$(el).fade('out')
	};
	var enableFunc = function(el){
		$(el).fade('in')
	};

	slider = new NbMooSlider(
		'nb-teaserslider',
		$$('.teaser-box'),
		{
			'prev':'sliderBack',
			'next':'sliderForward',
			'disableFn':disableFunc,
			'enableFn':enableFunc
		}
	);
});