
var used        =   [];
var current     =   0;

var CrossFader  =  Class.create({

    current:        0

    , pool:         []
    , poolLinks:    []
    , used:         []
    , lastChild:    null
    , newChild:     null
    , poolItem:     null

    , interval:     null
    , speed:        500
    , fadeSpeed:    1000
    , imageClass:   null

    , init:         function ($imageClass, $speed, $fadeSpeed, $pool, $used, $poolLinks){
        this.pool           =   $pool;
        this.used           =   $used;
        this.poolLinks      =   $poolLinks;
        this.fadeSpeed      =   $fadeSpeed;

        this.imageClass     =   $imageClass;
        this.speed          =   $speed;

        this.lastChild      =   $( $imageClass + ' a img:last-child');
        this.newChild       =   $( $imageClass + ' a' );
    }

    , fade:         function(){
        var me = this;
        this.poolItem       =   this._getFromPool();

        this._setNewLink();

        this.lastChild      =   $( this.imageClass + ' a img:last-child');

        this.newChild.prepend('<img src="'+ this.poolItem +'" />');
        this.lastChild.fadeOut(
            this.fadeSpeed,
            function(){
                me._onFadeOut_handler(me);
            }
        );
    }

    /**
     * start
     *
     * Starts the interval to fade images with
     *
     * @access  public
     * @param   boolean  $fadeOnStart   Fade as soon as called
     * @param   boolean  $delayStart    MS delay before starting interval
     * @return  void
     */
    , start:        function($fadeOnStart, $delayStart){
        var me = this;

        if( $fadeOnStart )
            me.fade();

        if( $delayStart ){
            setTimeout( function(){
                me.interval   =   setInterval(
                    function(){ me.fade() },
                    me.speed
                );
            }, $delayStart);
        }else{
            this.interval   =   setInterval(
                function(){ me.fade() },
                this.speed
            );
        }
    }


    /**
     * stop
     *
     * SClear Interval
     *
     * @access  public
     * @return  void
     */
    , stop:         function(){
        clearInterval( this.interval );
    }



    , _onFadeOut_handler:  function _onFadeOut_handler () {
        this._removeImage();
        //this._setNewLink();
     }

    , _setNewLink:   function(){
        this.newChild.attr('href', this.poolLinks[current] );
    }

    , _removeImage:  function(){
        var lastImage       =   $( this.imageClass + ' a img:last-child');
        lastImage.remove();
    }

    //  =============================

    , _addToUsedPool:function($index){
        this.used[$index]    =   true;
    }

    , _getFromPool:  function(){
        var total   =   current + this.pool.length;
        var ii      =   0;

        for( var i = current; i < total; i++ ){

            // remove the length and start over if we're over
            ii = i;
            if( i >= this.pool.length ){
                ii =    i - this.pool.length ;
            }

            // if the image hasn't been used yet, return it.
            if( !this.used[ii] ){

                this._removeFromPool(current);
                current     =   ii;
                this._addToUsedPool( ii );

                return this.pool[ii];
            }
        }
    }

    , _removeFromPool: function ($index){
        this.used[$index]    =   false;
    }

    , _removeAllFromPool: function (){
        this.used            =   [];
    }


});

