/*
Gets new data from uri with ajax request

id: 			elem#id (jQuery style), HTML DOM element
uri: 			base uri of the ajax url ending in ? or &
fade_time: 		fade time
cycle_time: 	timeout between each new ajax request
current_value: 	first item typcially 0
increment:		number of items to get
reply_was_bad:	last reply was bad, do not set or set as false on default
*/

function cycle_me(id, uri, fade_time, cycle_time, current_value, increment, nothing_found, reply_was_bad) {
	$.ajax({
		type: "GET",
		url: uri + "limit=" + current_value + "&increment=" + increment,
		error: function() { set_status(id, fade_time, nothing_found); },
		success: function(msg) {
			if(msg == "false" && reply_was_bad) {
				/* if reply_was_bad and reply is bad: probably means there are no articles or the user lost his internet connection */
				set_status(id, fade_time, nothing_found);
			} else if(msg == "false") {
				/* reply is bad, there are no more items, go back to 0 */
				setTimeout( function(){ cycle_me(id, uri, fade_time, cycle_time, 0, increment, nothing_found, true); }, 0);
			} else {
				/* all is well, fade, then schedule the next request */
				current_value += increment;
				$(id).fadeOut(fade_time, function(){
					set_status(id, fade_time, msg);
					setTimeout(function() { cycle_me(id, uri, fade_time, cycle_time, current_value, increment, nothing_found, false); }, cycle_time);
				});
			}
		}
	});
}
function set_status(id, fade_time, str) {
	$(id).empty();
	$(id).append(str);
	$(id).fadeIn(fade_time);
}