var NXC = NXC || {};

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

	options: {
		'transitionDuration': 500,
		'startIndex':         0,
		'selectedLinkStyle':  false,
		'selectedTabStyle':   false
	},

	tabs         : [],
	links        : [],
	currentIndex : 0,

    initialize: function( linktabsSelector, tabsSelector, options ) {
    	this.linktabs = document.getElements( linktabsSelector );
    	this.links    = document.getElements( linktabsSelector + ' a' );
    	this.tabs     = document.getElements( tabsSelector );
    	if (this.links.lenght!=this.tabs.lenght){
            alert('Links and tabs qty are not the same');
            return false;
        };

    	this.setMaxWrapperHeight();
        this.setOptions( options );
    	var anchor_name=window.location.toString().split('#')[1];
    	var anchor_selector = 'a[name='+anchor_name+']';
    	var selIndex = false;
    	this.tabs.each( function( tab, index ) {
    	   if( tab.getElement( anchor_selector ) ){
               selIndex = index;
               tab.getElement( anchor_selector ).addClass('aaa');
               //break();
           }
    	}.bind( this ) );
    	
    	this.currentIndex = ( selIndex ? selIndex : this.options.startIndex );

    	this.install();
    },

    install: function() {
    	this.showStartTab( this.currentIndex );

		this.links.each( function( link, index ) {
			link.addEvent( 'click', function( e ) {
				//e.stop();

				if( index !== this.currentIndex ) {
					this.showTab( index );
				}
			}.bind( this ) );
		}.bind( this ) );
    },

	showStartTab: function( index ) {
    	this.linktabs.removeClass( this.options.selectedLinkStyle );
    	
		this.tabs.setStyle( 'display', 'none' );
    	this.tabs.setStyle( 'opacity', 0 );
    	this.tabs.removeClass( this.options.selectedTabStyle );
    	this.tabs[ index ].setStyles( {
    		'display': 'block',
			'opacity': '1'
    	} );
		
		if( this.options.selectedLinkStyle !== false ) {
	    	this.linktabs[ index ].addClass( this.options.selectedLinkStyle );
		}
		if( this.options.selectedTabStyle !== false ) {
	    	this.tabs[ index ].addClass( this.options.selectedTabStyle );
		}

		this.currentIndex = index;
	},

    showTab: function( index ) {
    	var linktab    = this.linktabs[ index ];
    	var showTab    = this.tabs[ index ];
    	var currentTab = this.tabs[ this.currentIndex ];

		if( this.options.selectedLinkStyle !== false ) {
            this.linktabs.removeClass( this.options.selectedLinkStyle );
	    	linktab.addClass( this.options.selectedLinkStyle );
		}

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

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

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

    	this.currentIndex = index;
    },

    setMaxWrapperHeight: function() {

		var maxHeight=0;
		var height=0;
        this.tabs.each( function( tab ) {
            //tab_inner = tab.getElement('div.tab-content');
            height = tab.getStyle( 'height' ).toInt() + tab.getStyle( 'padding-top' ).toInt() + tab.getStyle( 'padding-bottom' ).toInt() + tab.getStyle( 'margin-top' ).toInt() + tab.getStyle( 'margin-bottom' ).toInt();
			if( maxHeight < height ) {
				maxHeight = height;
			}
		}.bind( this ) );
        
        this.tabs[0].getParent().setStyle( 'height', maxHeight );
    }
    
} );

