window.addEvent( 'domready', function()
{
    new ( new Class(
    {
        locked: false,
        played: true,
        pauseBtn: null,
        projects: [],
        display: 0,

        initialize: function()
        {
            var container = $$( '.lastproject' );

            if( !container.length )
                {
                return;
            }

            this.projects = container[0].getElement( 'ul.projects' ).getElements( 'li' );

            if( this.projects.length < 2 )
                {
                return;
            }

            var $this = this;

            container[0].getElement( 'li.btnPrev' ).getElement( 'span' ).addEvent( 'click', function()
            {
                $this.prev();
            } );
            
            this.pauseBtn = container[0].getElement( 'li.btnPause' );
            this.pauseBtn.getElement( 'span' ).addEvent( 'click', function()
            {
                $this.pause();
            } );

            container[0].getElement( 'li.btnNext' ).getElement( 'span' ).addEvent( 'click', function()
            {
                $this.next();
            } );
            
            ( function() { $this.play() } ).periodical( 5000 );
        },

        animate: function( project, startZIndex, endZIndex, side, endDisplay )
        {
            var $this = this;
            
            project.setStyles(
            {
                'z-index': startZIndex,
                'display': 'block'
            } );

            new Fx.Morph( project,
            {
                duration: 250,
                link: 'chain',
                onStart: function()
                {
                    project.getElement( 'a.name' ).setStyle( 'opacity', '0.1' );
                },
                onChainComplete: function()
                {
                    project.setStyle( 'display', endDisplay );
                    
                    project.getElement( 'a.name' ).setStyle( 'opacity', '1' );
                    
                    $this.unlock();
                }
            } ).start(
            {
                'left': ( project.getSize().x / 2 ) * side
            } ).start(
            {
                'z-index': endZIndex  
            } ).start(
            {
                'left': 0  
            } );  
        },
        
        lock: function()
        {
            this.locked = true;
        },
        
        unlock: function()
        {
            this.locked = false;
        },

        next: function()
        {
            this.pause();
            
            this.open( this.display + 1 );
        },

        prev: function()
        {
            this.pause();
            
            this.open( this.display - 1 );
        },

        open: function( show )
        {
            if( this.locked )
            {
                return;
            }
            
            this.lock();
            
            if( show < 0 )
                {
                show = this.projects.length - 1;
            }
            else if( show > this.projects.length - 1 )
                {
                show = 0;
            }

            var hidden  = this.projects[this.display];
            this.display = show;
            var display = this.projects[this.display];

            this.animate( hidden, 10000, 9999, -1, 'none' );
            this.animate( display, 9999, 10000, 1, 'block' );
        },
        
        play: function()
        {
            if( !this.played )
            {
                return;
            }
            
            this.open( this.display + 1 );
        },
        
        pause: function()
        {
            this.pauseBtn.destroy();
            
            this.played = false;
        }
    } ) );
} );
