var FrontMenu = new Class({
	Implements: [Chain, Options],

	options: {		
		'timerDelay':        3000,
		'selectedLinkClass': 'selected',
		'fadeDuration':      500,
    'blockType':         'block'
	},		
			
	initialize: function( slidesCSSPath, options ) {
		this.slides = $$( slidesCSSPath );
		this.setOptions( options );				

		this.run();
	},
	
	run: function() {				
		this.currentSlide = 0;
		this.hideAllSlides();
		this.selectSlide( false );				
		this.runTimer();
	},
	
	hideAllSlides: function() {
		this.slides.each( function( slide, index ) {
			slide.setStyles( {
				'display': 'none',
				'opacity': 0
			} );				
		} );		
	},
	
	runTimer: function() {
		this.timer = function() {
			this.hidePreviousSlide();
			this.selectSlide( true );	
		}.periodical( this.options.timerDelay + this.options.fadeDuration, this );
	},
	
	stopTimer: function() {
		$clear( this.timer );		
	},
	
	selectSlide: function( withFade ) {
		var slide = this.slides[this.currentSlide];
		slide.setStyle( 'display', this.options.blockType );
		if( withFade ) {
			slide.get( 'tween', { property: 'opacity', duration: this.options.fadeDuration } ).start( 1 );
		} else {
			slide.setStyle( 'opacity', 1 );	
		}
		 
		this.currentSlide++;
		if( this.currentSlide > ( this.slides.length - 1 ) ) {
			this.currentSlide = 0;	
		}		 		
	},
	
	hidePreviousSlide: function() {
		var previousSlideNumber;
		if( this.currentSlide == 0 ) {
			previousSlideNumber = this.slides.length - 1;
		} else {
			previousSlideNumber = this.currentSlide - 1;
		}
		
		var previousSlide = this.slides[previousSlideNumber];		 					
		previousSlide.get( 'tween', { 'property': 'opacity', 'duration': this.options.fadeDuration } ).start( 0 ).chain(
	        function() {
				previousSlide.setStyle( 'display', 'none' );
	        }
    	);		
	} 
});
