/*
 *	Citkomm | von Dreusche
 */
var CitkommScroller = new Class({
	options: {
		slides: 'citkomm_slide',
		slides_container: 'citkomm_slider',
		duration: 3000,
		delay: 1000,
		transition: Fx.Transitions.Quad.easeInOut
	},
	
	initialize: function( slides_scroller, options )
	{
		this.setOptions( options );
		this.slides_scroller = $( slides_scroller );
		this.slides_container = $( this.options.slides_container );
		
		this.slides = $$( '.' + this.options.slides );
		var slides_container_width = 0;
		var maxsteps = 0;
		
		this.slides.each( function( item, index ){
			slides_container_width += item.getStyle( 'width' ).toInt();
			maxsteps += 1;
		});
		
		this.slides_scroller.setStyles({
			position: 'relative',
			overflow: 'hidden'
		});
		
		this.slides_container.setStyles({
			position: 'relative',
			overflow: 'hidden',
			width: slides_container_width
		});
		
		if( $defined(this.options.buttons ) )
		{
			if( $defined(this.options.buttons.next ) )
			{
				$( this.options.buttons.next ).addEvent( 'click', this.next.bind( this ) );
			}
			if( $defined(this.options.buttons.prev ))
			{
				$( this.options.buttons.prev ).addEvent( 'click', this.prev.bind( this ) );
			}
		}
		this.step = 1;
		this.maxsteps = maxsteps;
		this.isFirst = true;
		this.fxOn = false;
		
		// Buttons on/off setzten
		this.setButtons();
	},
	
	next: function()
	{
		if( this.step + 1 > this.maxsteps || this.fxOn == true ) { return; }
		
		var von = - ( this.step - 1 ) * 426;
		var bis = - this.step * 426;
		this.slides_container.set('morph', {duration: this.options.duration, transition: this.options.transition});
		this.slides_container.morph( { left: [ von, bis ] } );
		this.step += 1;
		
		this.fxOn = true;
		this.fxEnd.delay( this.options.duration + 75, this );
		this.setButtons();
	},
	
	prev: function()
	{
		if( this.step - 2 < 0 || this.fxOn == true ) { return; }
		
		var von = - ( this.step - 1 ) * 426;
		var bis = - ( this.step - 2 ) * 426;
		this.slides_container.set('morph', {duration: this.options.duration, transition: this.options.transition});
		this.slides_container.morph( { left: [ von, bis ] } );
		this.step -= 1;
		
		this.fxOn = true;
		this.fxEnd.delay( this.options.duration + 75, this );
		this.setButtons();
	},
	
	fxEnd: function()
	{
		this.fxOn = false;
	},
	
	setButtons: function()
	{
		if( this.maxsteps == 1 )
		{
			this.setNext( false );
			this.setPrev( false );
		}
		else if( this.step >= this.maxsteps )
		{
			this.setNext( false );
			this.setPrev( true );
		}
		else if( this.step <= 1 )
		{
			this.setNext( true );
			this.setPrev( false );
		}
		else
		{
			this.setNext( true );
			this.setPrev( true );
		}
	},
	
	setNext: function( status )
	{
		if( status == true )
		{
			$( this.options.buttons.next ).set('class', 'button_for');
		}
		else
		{
			$( this.options.buttons.next ).set('class', 'button_for_off');
		}
	},
	
	setPrev: function( status )
	{
		if( status == true )
		{
			$( this.options.buttons.prev ).set('class', 'button_back');
		}
		else
		{
			$( this.options.buttons.prev ).set('class', 'button_back_off');
		}
	}
});
CitkommScroller.implement( new Options, new Events );
