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

  options: {
    'timerDelay':        3000,
    'selectedLinkClass': 'selected',
    'fadeDuration':      500,
    'blockType':         'block',
    'chSlWhenClick':     true
  },

  initialize: function( slidesCSSPath, linksCSSPath, options ) {
    this.slides = $$( slidesCSSPath );

    if( linksCSSPath ) {
    		this.links  = $$( linksCSSPath );
		}
    this.setOptions( options );

    if( this.links ) {
		    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.runTimer();
    this.selectSlide( false );
    changeSlideWhenClick = this.options.chSlWhenClick;

		if( this.links ) {
		    this.links.each( function( link, linkNumber ) {
		      link.addEvents( {
		          'mouseover': function(){
		            if( changeSlideWhenClick ) {
		              this.stopTimer();
		                 this.currentSlide = linkNumber;
		                 this.hideAllSlides();
		                 this.selectSlide( false );
		                }
		          }.bind( this ),
		          'mouseout': function(){
		            if( changeSlideWhenClick ) {
		                this.runTimer();
		              }
		          }.bind( this ),
		          'click': function(){
		               this.currentSlide = linkNumber;
		               this.hideAllSlides();
		               this.selectSlide( false );
		          }.bind( this )
		      } );
		    }, this );
		}

    this.slides.each( function( slide ) {
      slide.addEvents( {
          'mouseover': function(){
            this.stopTimer();
          }.bind( this ),
          'mouseout': function(){
          // Not flash slide
          if( slide.getElements( '.swiff-container' ).length == 0 ) {
            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];
    var slideFlashContainer = slide.getElements( '.swiff-container' );
    if( slideFlashContainer.length > 0 && slideFlashContainer != '' ) {
			var slideFlashFunction = slideFlashContainer.get('id');
      eval( slideFlashFunction + '()' );
    }
    slide.setStyle( 'display', this.options.blockType );
    if( withFade ) {
      slide.set( 'tween', { property: 'opacity', duration: this.options.fadeDuration } );
      slide.get( 'tween' ).start( 1 );
    } else {
      slide.setStyle( 'opacity', 1 );
    }

    // Flash slide
    if( slideFlashContainer.length > 0 ) {
      this.stopTimer();
    }

		if( this.links ) {
	    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.set( 'tween', { 'property': 'opacity', 'duration': this.options.fadeDuration } );
    previousSlide.get( 'tween' ).start( 0 ).chain(
          function() {
              previousSlide.setStyle( 'display', 'none' );
          }
      );
  }
});

