$(function() {
	imagerotators = new Array();
	$('.imagerotator').each(setup_image_rotator);
    setInterval("rotate_images(imagerotators)", 5000); // Set the rotate
});


function setup_image_rotator()
{
	firstimage = $(this).find('div.rotatingimage:first');

	otherimages = firstimage.siblings('div.rotatingimage');

	if (otherimages.length > 0)
	{
		otherimages.hide(); // Hide all other rotating_items

		$(this).data('currentimage', firstimage);

		imagerotators.push($(this));
	}
}

// Below is the code that picks an item at random to display
jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
    random: function(a, i, m, r) {
        if (i == 0) {
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
        };
        return i == jQuery.jQueryRandom;
    }
});

function rotate_images(rotators)
{
	$(rotators).each(show_next_rotating_item);
}

// The below function repeatedly gets called, to do the rotating
function show_next_rotating_item(t,u) {

	var currentimage = u.data('currentimage');

	currentimage.fadeOut('slow');

    var nextimage = currentimage.siblings('div.rotatingimage:random');

    nextimage.fadeIn('slow');

	u.data('currentimage', nextimage);
} 

