/// <reference path="jquery-1.3.2-vsdoc2.js" />

jQuery.fn.rotateItems = function(options) {

    var defaults = {
        itemClass: "galleryimg",
        fadeTime: 1000,
        timeout: 2500
    };


    var opts = jQuery.extend(defaults, options);
    var $container = this;
    var $items = $container.find("." + opts.itemClass);
    var itemCount = $items.length;
    var currentItem = itemCount - 1;

    var doRotation = function() {
        $items.eq(currentItem)
            .fadeOut(opts.fadeTime, function() {
                $container.prepend($(this)); //Put at end of pile
                $(this).show(); //Unhide it

                currentItem--;
                if (currentItem == -1)
                    currentItem = itemCount - 1;

                setTimeout(doRotation, opts.timeout);
            });
    }

    //$container.css("position", "relative"); //now done in css
    //$items.css("position", "absolute");

    if ($items.length > 0) {
        $items.eq(currentItem).fadeIn("fast", function() {
            $items.css("display", "block"); //display the rest
            if (itemCount > 1)
                setTimeout(doRotation, opts.timeout);
        });
    }
}