
(function ($) {

    // allow the user to pass in a couple of parameters.
    $.fn.itemScroller = function (options) {

        // For each item in the wrapped set, perform the following. 
        return this.each(function () {

            // Caches this - or the ul/div widget(s) that was passed in (Saves time and improves performance)
            var  
            $this = $(this),

            // If the user doesn't pass in parameters, we'll use this object. 
		    defaults = {
		        speed: 400, // How quickly should the items scroll?
		        delay: 3000, // How long a rest between transitions?
		        itemSelector: '.ui-portlet-content-element',
		        list_item_height: $this.children(".ui-portlet-content-element").outerHeight() // How tall is each list item? If this parameter isn't passed in, jQuery will grab it.
		    },
            
            // Create a new object that merges the defaults and the 
            // user's "options".  The latter takes precedence.
		    settings = $.extend({}, defaults, options);

            // This sets an interval that will be called continuously.
            setInterval(function () {
                // Get the very first list item in the wrapped set.
                $this.children(settings.itemSelector + ':first')
                // Animate it
	  	    	    .animate({
	  	    		    marginTop: '-' + settings.list_item_height // Shift this first item upwards.
	  	    		    ,opacity: 'hide'
	  	    	    }, // Fade the li out.

                    // Over the course of however long is 
                    // passed in. (settings.speed)
	  	    		settings.speed,

                    // When complete, run a callback function.
	  	    		function () {
                        // Get that first list item again. 
	  	    		    $this.children(settings.itemSelector + ':first')
	  	 					    .appendTo($this) // Move it the very bottom of the ul.

	  	    		        // Reset its margin top back to 0. Otherwise, 
	  	    		        // it will still contain the negative value that we set earlier.
	  	 					.css('marginTop', 0)
	  	 					.fadeIn(100); // Fade in back in.
	  	    		}
 	 			); // end animate

	  	  }, settings.delay); // end setInterval

        });
    }

})(jQuery);
