$(document).ready(function(){
	$('ul.spy').simpleSpy();

			$('ul.spy li').reverseOrder(); 

});

(function ($) {
$.fn.reverseOrder = function() {
	return this.each(function() {
		$(this).prependTo( $(this).parent() );
	});
};

$.fn.simpleSpy = function (limit, interval) {
    limit = limit || 2;
    interval = interval || 5000;

    return this.each(function () {
            // capture a cache of all the list items
            // chomp the list down to limit li elements
        var $list = $(this),
            items = [], // uninitialised
            currentItem = limit,
            total = 0, // initialise later on
            start = 0,//when the effect first starts
            startdelay = 5000;//set the initial delay onload.
            height = $list.find('> li:first').height();

        // capture the cache
        $list.find('> li').each(function () {
            items.push('<li>' + $(this).html() + '</li>');
        });

        total = items.length;

        $list.wrap('<div class="spyWrapper">').parent().css({ height : height * limit });

        $list.find('> li').filter(':gt(' + (limit - 1) + ')').remove();
        function spy() {
            // insert a new item with opacity and height of zero
            var $insert = $(items[currentItem]).css({
                height : 0,
                opacity : 0,
                display : 'none'
            }).prependTo($list);

            // fade the last item out.
            $list.find('> li:last').animate({ opacity : 0}, 1000, function () {
                // increase the height of the new first item.
                 $insert.animate({ height : height }, 1000).animate({ opacity : 1 }, 1000);
                    // fade the first item in and remove the last one
                    $(this).remove();

            });

            currentItem++;
            if (currentItem >= total) {
                currentItem = 0;
            }

            setTimeout(spy, interval)
        }

        if (start < 1) {
               setTimeout(spy,startdelay);
                start++;
            } else {
            spy();
            }

    });
};

})(jQuery);
