var tabSlider = new Class({
	options: {
		btPrev: 'a.link-prev',
		btNext: 'a.link-next',
		tabLinks: 'ul.accordion ul a',
		holder: 'div.visual-holder',
		mover: 'div.slide-visual',
		scrollEl: 'div.case-study',
		duration : 1000,
		effect: Fx.Transitions.Expo.easeOut
	},

	// create class
	initialize: function(element, options){
	    this.setOptions(options);
		var _this = this;
		this.holder = element.getElement(this.options.holder);
		
		if (this.options.btNext)
				this.next = element.getElement(this.options.btNext);
			else this.next = false;
		if (this.options.btPrev)
				this.prev = element.getElement(this.options.btPrev);
			else this.prev = false;
		if (this.options.tabLinks)
				this.links = element.getElements(this.options.tabLinks);
			else this.links = false;
			
		this.mover = this.holder.getElement(this.options.mover);
		this.scrollEl = this.holder.getElements(this.options.scrollEl);
		this.size = this.holder.getSize();
		this.step = this.size.x;
		
		this.animated = false;
		this.duration = this.options.duration;
		this.length = this.scrollEl.length;
		this.current = 0;
		
		this.mover.fx = new Fx.Tween(this.mover, {
			duration:_this.duration,
			transition: _this.options.effect,
			onStart: function(){
				_this.animated = true;
			},
			onComplete: function(){
				_this.animated = false;
			}
		});
		if (this.links) {
			this.links.each(function(link, i){
				link.addEvent('click', function(){
					if (!_this.animated) {
						_this.links.getParent().removeClass('active');
						_this.mover.fx.start('marginLeft', -(_this.step*i));
						this.getParent().addClass('active');
					}		
					return false;
				});
			});
		}
		if (this.next) {
			this.next.addEvent('click', function(){
				if (!_this.animated) {
					_this.nextSlide();
				}
				return false;
			});
		}
		if (this.prev) {
			this.prev.addEvent('click', function(){
				if (!_this.animated) {
					_this.prevSlide();
				}
				return false;
			});
		}
	},
	
	nextSlide: function(){
		this.current += 1;
		if (this.current > this.length-1) this.current = 0;
		this.mover.fx.start('marginLeft', -(this.step*this.current));
		this.setActive();
	},
	prevSlide: function(){
		this.current -= 1;
		if (this.current < 0) this.current = this.length-1;
		this.mover.fx.start('marginLeft', -(this.step*this.current));
		this.setActive();
		
	},
	setActive: function(){
		if (this.links) {
			this.links.getParent().removeClass('active');
			this.links[this.current].getParent().addClass('active');
		}
	},

	// add options and events
	Implements : [Options, Events]
});

window.addEvent('domready', function(){
	if ($$('div.promo-box').length) {
		var _tabSlider = new tabSlider($$('div.promo-box')[0]);
	}
	
	if ($$('div.gallery').length) {
		$$('div.gallery').each(function(_gal, i){
			var _tabSlider = new tabSlider(_gal,{
				btPrev: 'a.link-prev',
				btNext: 'a.link-next',
				tabLinks: false,
				holder: 'div',
				mover: 'ul',
				scrollEl: 'li',
				duration : 600
			});
		})
	}
});
