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

	options: {		
		'timerDelay':        3000,
		'selectedLinkClass': 'selected',
		'fadeDuration':      500,
    'blockType':         'block'
	},		
			
	initialize: function( slidesCSSPath, linksCSSPath, options ) {    	
		this.slides = document.getElements( slidesCSSPath );
		this.links  = document.getElements( linksCSSPath );		
		this.setOptions( options );				
		
		if( this.slides.length != this.links.length ) {
			alert( 'Images and links qty must be same' );
			return false;
		}	

		this.run();
	},
	
	run: function() {				
		this.currentSlide = 0;
		this.hideAllSlides();
		this.selectSlide( false );				
		this.runTimer();
		this.links.each( function( link, linkNumber ) {
			link.addEvents( {
			    'mouseover': function(){
			    	this.stopTimer();
       				this.currentSlide = linkNumber;
       				this.hideAllSlides();
       				this.selectSlide( false );
			    }.bind( this ),
			    'mouseout': function(){
			        this.runTimer();
			    }.bind( this )
			} );			
		}, this );		
		this.slides.each( function( slide ) {
			slide.addEvents( {
			    'mouseover': function(){
			    	this.stopTimer();
			    }.bind( this ),
			    'mouseout': function(){
			        this.runTimer();
			    }.bind( this )
			} );			
		}, this );		
	},
	
	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 );	
		}		
		
		var link  = this.links[this.currentSlide];
		this.links.each( function( link ) {
			link.removeClass( this.options.selectedLinkClass );
		}, this );
 		link.addClass( this.options.selectedLinkClass );
		 
		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' );	            
	        }
    	);		
	} 
});

