/** 
 * Form addons
 */
 
Element.implement(
{
    enable: function()
    {
        this.set( 'disabled', false );
        
        this.fireEvent( 'enable' );
    },
    
    disable: function()
    {
        this.set( 'disabled', true );
        
        this.fireEvent( 'disable' );
    }
} );
 
window.addEvent( 'domready', function()
{
    // center pagination
    $each( $$( '.ecPagination.ecCenter' ), function( element )
    {
        var size = 0;
        
        $each( element.getElements( 'li' ), function( li )
        {
            var a = li.getElement( 'a' );
            
            if( a )
            {
                size += a.getSize().x;
            }
            else
            {
                size += li.getSize().x;
            }
        } );
        
        element.setStyle( 'margin-left', element.getParent().getSize().x/2 - size/2 );
    } );
    // table limit selection
    $each( $$( '.ecTableLimit' ), function( element )
    {
        var select = element.getElement( 'select' );
        
            if( !select )
            {
                return;
            }
            
            select.addEvent( 'change', function()
            {
                $( this ).getParent( 'fieldset' ).addClass( 'ecDisabled' );
            
                $( this ).getParent( 'form' ).submit();    
            } );
    } );
    // set select redirect event
    $each( $$( '.ecSelectRedirect'), function( select )
    {
        select.addEvent( 'change', function()
        {
            window.location = this.value;
            
            this.disabled();    
        } );
        
    } );
    // disabled
    $each( $$( 'input, select, textarea' ), function( element )
    {                           
        var label  = null;
        var parent = element.getParent();
        
        while( parent && parent.get( 'tag' ) != 'fieldset' )
        {                  
            if( parent.get( 'tag' ) == 'label' )
            {   
                label = parent;
                
                parent = null;
            }
            else if( parent.getElement( 'label' ) )
            {
                label = parent.getElement( 'label' );
                
                parent = null;
            }
         
            if( parent )
            {   
                parent = parent.getParent();
            }
        }
        
        if( !label )
        {
            return;
        }
        
        element.addEvents(
        {
            enable: function()
            {
                label.removeClass( 'disabled' );   
            },
            
            disable: function()
            {
                label.addClass( 'disabled' );   
            }
        } );
        
        if( element.get( 'disabled' ) )
        {
            element.fireEvent( 'disable' );
        }
    } );
    // tabs
    $each( $$( '.ecTabs' ), function( tabs )
    {
        new ecTabs( tabs );
    } );
} );

var ecTabs = new Class(
{
    btns: [],
    config: [],
    id: '',
    tabs: [],
    
    initialize: function( container )
    {
        container.store( 'tabs', this );
        
        this.btns = container.getChildren( 'ul' )[0].getElements( 'li' );
        this.tabs = container.getChildren( 'div.ecTab' );
        
        this.setEvents();
        
        this.config = JSON.decode( Cookie.read( 'ecTabs' ) );
        
        if( this.config == null )
        {
            this.config = {};
        }
        
        this.id = container.get( 'id' );
        
        if( this.id && eval( 'this.config.'+this.id ) )
        {
            this.display( eval( 'this.config.'+this.id ) );    
        }
        else
        {
            this.display( 0 );
        }
    },
    
    setEvents: function()
    {
        var $this = this;
        
        $each( this.btns, function( btn, index )
        {
            if( !$this.tabs[index] )
            {
                return;
            }
            
            btn.addEvent( 'click', function()
            {
                if( this.hasClass( 'ecDisabled' ) )
                {
                    return;
                }
                
                $this.display( index );
            } );
        } );
    },
    
    display: function( id )
    {
        var dock = null;
        
        var $this = this;
        
        $each( this.tabs, function( tab, index )
        {
            $this.btns[index].removeClass( 'ecSelected' );
            
            if( index == id )
            {
                dock = tab;
            }
            
            tab.hide();
            
            fireEvent( 'hideTab' );
        } );
        
        if( dock )
        {
            if( this.id )
            {
                eval( 'this.config.'+this.id+' = id' );
                
                Cookie.write( 'ecTabs', JSON.encode( this.config ) );
            }
            
            $this.btns[id].addClass( 'ecSelected' );
            
            dock.show();
            dock.fireEvent( 'showTab' );
        }
    }
} );

var EnixCore = new Class( 
{
    
} );
