var Slider = new Class(
{
	Implements: [Options, Chain],
	options: {
		slider: '.AC-Slider',
		slides: '.AC-Slide',
		sliderPrev: '.AC-SliderPrev',            
		sliderNext: '.AC-SliderNext',
		navContainer: '.AC-SliderNav',
		transition: Fx.Transitions.Sine.easeInOut,
		transitionDuration: 900,
		textBlock: '.AC-Slide .AC-BlockText',
		textBlockTransition: Fx.Transitions.Sine.easeInOut,
		textBlockTransitionDuration: 450,
		speed: false
	},

	initialize: function(options) {
	
		this.setOptions(options);
		if($$(this.options.slides).length > 0){
			slidesPerSet = ($$(this.options.slider)[0].getParent().getSize().x / $$(this.options.slides)[0].getScrollSize().x).toInt();
			$$(this.options.slider).set('styles', { 'width': ($$(this.options.slides)[0].getScrollSize().x) * $$(this.options.slides).length });	


			if($$(this.options.slides).length <= slidesPerSet) { $$(this.options.sliderNext)[0].addClass('disabled'); }
			$$(this.options.sliderPrev)[0].addEvent('click',function(){$clear(this.timer); this.scrollLeft();}.bind(this));	
			$$(this.options.sliderNext)[0].addEvent('click',function(){$clear(this.timer); this.scrollRight();}.bind(this));
			
			slideFx = new Fx.Tween($$(this.options.slider)[0], {duration:this.options.transitionDuration, transition:this.options.transition});
			$$(this.options.textBlock).set('tween',{duration:this.options.textBlockTransitionDuration, transition:this.options.textBlockTransition});
			$$(this.options.textBlock).setStyle('display', 'none');
		    $$(this.options.textBlock)[0].setStyle('display', 'block').get('tween').start('opacity', '0', '1.0');
			
			if (this.options.speed) { this.timer = this.scrollRight.periodical(this.options.speed, this); }; 
		}
	},
	
	scrollLeft: function(e){
		slideFx.cancel();
		$$(this.options.textBlock)[0]
			.get('tween')
			.start('opacity',1,0)
			.chain(function(){
				$$(this.options.textBlock)[0].setStyle('display', 'none');
			}.bind(this));
	
		moveSlide = $$(this.options.slides)[($$(this.options.slides).length)-1].dispose();
		$$(this.options.slider)[0].setStyle('margin-left',-($$(this.options.slides)[0].getScrollSize().x * slidesPerSet));
		moveSlide.inject($$(this.options.slider)[0],'top')
		slideFx.start('margin-left', 0).chain(function() {
			$$(this.options.textBlock)[0].setStyle('display', 'block').get('tween').start('opacity', '0', '1.0');
			
		}.bind(this));
		
	},
	scrollRight: function(e){
	    slideFx.cancel();
	   
		$$(this.options.textBlock)[0]
			.get('tween')
			.start('opacity',1,0)
			.chain(function(){
				$$(this.options.textBlock)[0].setStyle('display', 'none');
			}.bind(this));

		
		slideFx.start('margin-left', -(($$(this.options.slides)[0].getScrollSize().x * slidesPerSet))).chain(function() {
			moveSlide = $$(this.options.slides)[0].dispose();
			$$(this.options.slider)[0].setStyle('margin-left','0');
			moveSlide.inject($$(this.options.slider)[0])
			
			$$(this.options.textBlock)[0].setStyle('display', 'block').get('tween').start('opacity', '0', '1.0');
			
		}.bind(this));

    }         
});

window.addEvent('domready',function(){var carousel = new Slider({'speed':'5000'});});