var NXC = NXC || {};
NXC.FrontMenu = new Class( {

	Implements: [Options, Events],

	options:{
		'slidesSelector'     : 'div.nxc-front-menu-slide-container',
		'navigationSelector' : 'div.nxc-front-menu-navigation-element',
		'selectedLinkClass'  : 'nxc-front-menu-navigation-element-selected',
		'timerDelay'         : 3000,
		'fade'               : true,
		'fadeDuration'       : 500,
		'slideShow'          : true
	},

	container: false,
	slides: [],
	navigationLinks: [],
	currentSlide: 0,
	timer: $empty,

	initialize: function( containerID, options ) {
		this.setOptions( options );

		this.container       = document.id( containerID )
		this.slides          = this.container.getElements( this.options.slidesSelector );
		this.navigationLinks = this.container.getElements( this.options.navigationSelector );

		if( this.slides.length != this.navigationLinks.length ) {
			console.log( 'Slides and naviagtion links quantity dosen`t the same' );
			return false;
		}

		this.hideAllSlides();
		this.showSlide( this.currentSlide, false );

		this.installEvents();
		this.installSlideShow();
	},

	installEvents: function() {
		this.navigationLinks.each( function( navigationLink, index ) {
			navigationLink.addEvents( {
				'click': function(event){
					event.stop();
					this.currentSlide = index;
					this.hideAllSlides();
					this.showSlide( this.currentSlide, false );
				}.bind( this )
			} );
		}.bind( this ) );

		this.slides.addEvents( {
			'mouseover': function(){
				this.stopTimer();
			}.bind( this ),
			'mouseout': function(){
				this.startTimer();
			}.bind( this )
		} );
	},

	installSlideShow: function() {
		if( this.options.slideShow ) {
			this.startTimer();
		}
	},

	startTimer: function() {
		if( this.options.slideShow ) {
			this.timer = function() {
				this.showNextSlide( this.options.fade );
			}.periodical( this.options.timerDelay + this.options.fadeDuration, this );
		}
	},

	stopTimer: function() {
		$clear( this.timer );
	},

	showNextSlide: function( fade ) {
		this.hideSlide( this.getPreviousIndex(), fade);
		this.showSlide( this.currentSlide, fade );
	},
	
	showPreviousSlide: function( fade ) {
		var prevIndex = this.getPreviousIndex();
		var showIndex = ( prevIndex == 0 ) ? this.slides.length - 1 : prevIndex - 1;

		this.hideSlide( prevIndex, fade );
		this.showSlide( showIndex, fade );
	},

	showSlide: function( index, fade ) {
		var index = $defined( index ) ? index : this.currentSlide;
		var fade  = $defined( fade ) ? fade : true;
		var slide = this.slides[ index ];

		slide.setStyle( 'display', 'block' );
		if( fade ) {
			slide.get( 'tween', { property: 'opacity', duration: this.options.fadeDuration } ).start( 1 );
		} else {
			slide.get( 'tween' ).cancel();
			slide.setStyle( 'opacity', 1 );
		}

		this.navigationLinks.removeClass( this.options.selectedLinkClass );
		this.navigationLinks[ index ].addClass( this.options.selectedLinkClass );

		if( index == ( this.slides.length - 1 ) ) {
			this.currentSlide = 0;
		} else {
			this.currentSlide = index + 1;
		}
	},

	hideSlide: function( index, fade ) {
		var fade  = $defined( fade ) ? fade : true;
		var index = $defined( index ) ? index : this.getPreviousIndex();
		var slide = this.slides[index];

		if( fade ) {
			slide.get( 'tween', { 'property': 'opacity', 'duration': this.options.fadeDuration } ).start( 0 ).chain(
				function() {
					slide.setStyle( 'display', 'none' );
				}
			);
		} else {
			slide.setStyles( {
				'display': 'none',
				'opacity': 0
			} );
		}
	},

	hideAllSlides: function() {
		this.slides.each( function( slide, index ) {
			this.hideSlide( index, false );
		}.bind( this ) );
	},

	getPreviousIndex: function() {
		return ( this.currentSlide == 0 ) ? this.slides.length - 1 : this.currentSlide - 1;
	}
} );
