/**
 *  Plugin which make flush message with fade effect
 *  @author:  Tee++; 
 *  @version: 1.0 (25/12/2007)
 *  @website http://www.jQueryTips.com/
 */
(function($) {

jQuery.fn.flushMsg = function(url, options) {
	// Stop anything if no url
	if (url == null) return false;

	// Set options
	options = options || {};
	options.url = url; 
	var settings = {
		max: 0,
		delay: 3000,
		fspeed: 500
	};

	if (options)
		$.extend(settings, options);

	var me = $(this);
	var msgs = Array();
	var i = 0;

	// Flush Message
	function flushMsg() {
		if (i == msgs.length) {
			// Loop end, start again
			i = 0; getData() 
		} else { 
			// Flush data with delay
			$(me).html(msgs[i]).hide().fadeIn(settings.fspeed);
			setTimeout(function() { flushMsg() }, settings.delay);
			i++;
		}
	}
	
	// Get data from server side script with JSON format
	function getData() {
		$.getJSON(settings.url, function(n) {
			if (settings.max != 0) // Slice array with option
				msgs = $.grep(n, function(n, i) {	return i < settings.max; });
			else // Unlimited array size
				msgs = n;
			// Start flush messages
			flushMsg();
		});
	}
	
	// Hide message to prepare fade in
	$(this).hide();
	// Get first data
	getData();
};

})(jQuery);