(function($){
    $.fn.shuffle = function () {
        return this.each( function () {
            var items = $(this).children().clone(true);
            return ( items.length ) ? $(this).html( $.shuffle(items) ) : this;
        } );
    }
    $.shuffle = function(arr) {
        for (
            var j, x, i = arr.length;
            i;
            j = parseInt( Math.random() * i ), x = arr[--i], arr[i] = arr[j], arr[j] = x
        );
        return arr;
    }
})(jQuery);

function DivRotationSet( container, fade, pause ) {
    $(container)
        .shuffle()
        .children("div")
        .hide()
        .filter(':first')
        .fadeIn(fade)
        .parent()
        .show();

    this.rotationDivs = $(container).children("div").get();

    this._flip = function ( direction, fade ) {
        var divs = this.rotationDivs;;

        $( divs[0] ).fadeOut(
            fade,
            function () {
                if ( direction == -1 ) {
                    divs.unshift( divs.pop() );
                }
                else {
                    divs.push( divs.shift() );
                }

                $( divs[0] ).fadeIn(fade);
            }
        );
    }

    this.flip = function ( direction, fade ) {
        clearTimeout( this.timeout );
        this._flip( direction, fade );
        this.rotate();
    }

    this.rotate = function () {
        var _this    = this;
        this.timeout = setTimeout(
            function () {
                _this._flip( 1, fade );
                _this.rotate();
            },
            pause
        );
    }

    this.rotate();
}

var headerRotation;
$(document).ready( function () {
    headerRotation = new DivRotationSet( "div#page_banner_flash", "medium", 2000 );
} );

