var NXC = NXC || {};

NXC.tabs = new Class( {
	Implements: [Options],

	options: {		
		'transitionDuration': 500,
		'startIndex':         0,
		'selectedLinkStyle':  false,
		'selectedTabStyle':   false,
		'selectedContentStyle':   false
	},
	
    initialize: function( links, tabs, options ) {
    	this.links = $$( links );
    	this.tabs  = $$( tabs );

    	this.setOptions( options );

    	this.currentIndex = this.options.startIndex;
    	
    	this.run();
    },

    run: function() {
    	this.tabs.setStyle( 'display', 'none' );
    	this.tabs.setStyle( 'opacity', 0 );
    	this.tabs[this.currentIndex].setStyles( {
    		'display': 'block',
			'opacity': '1'	
    	} );
		if( this.options.selectedLinkStyle != false ) {
	    	this.links[this.currentIndex].addClass( this.options.selectedLinkStyle );
		}
		if( this.options.selectedTabStyle != false ) {
	    	this.tabs[this.currentIndex].addClass( this.options.selectedTabStyle );
		}
    	
    	
    	this.showTab( this.currentIndex, false );

		this.links.each( function( link, index ) {
			link.addEvent( 'click', function( e ) {
				e.stop();
				this.showTab( index, true );
			}.bind( this ) );
		}.bind( this ) );
    },

    showTab: function( index, checkIndex ) {
    	if( index == this.currentIndex ) {
    		return false;
    	}

    	var link       = this.links[index];
    	var nextTab    = this.tabs[index];
    	var currentTab = this.tabs[this.currentIndex];

		if( this.options.selectedLinkStyle != false ) {
	    	this.links.removeClass( this.options.selectedLinkStyle );
	    	link.addClass( this.options.selectedLinkStyle );
		}

		if( this.options.selectedTabStyle != false ) {
	    	this.tabs.removeClass( this.options.selectedTabStyle );
	    	nextTab.addClass( this.options.selectedTabStyle );
		}

		if( this.options.selectedContentStyle != false ) {
				document.id( document.body ).getElements( this.options.selectedContentStyle ).setStyle( 'background-color', link.getStyle('background-color') );
		}

		currentTab.set( 'tween', { property: 'opacity', duration: this.options.transitionDuration } );
		currentTab.get( 'tween' ).start( 0 ).chain( function() {
			currentTab.setStyle( 'display', 'none' );
		}.bind( this ) );

    	nextTab.setStyle( 'display', 'block' );
    	nextTab.set( 'tween', { property: 'opacity', duration: this.options.transitionDuration } );
    	nextTab.get( 'tween' ).start( 1 );

    	this.currentIndex = index;
    }
} );
