var data = [];

function getdata() {
	var lastid = 0;
	var url = '/default.plm';
	var rand = Math.random(9999);
	var lastarray = data.first();
	if (lastarray) {
		lastid = lastarray.postdate;
	}
	var pars = 'action=fetch&lastid=' + lastid + '&rand=' + rand;

	var ajaxreq = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: showdata} );
}

function showdata(originalRequest) {

	eval(originalRequest.responseText);

	// go through all the new items and add them to the list
	var newitems = 0;
	newdata.reverse();
	newdata.each(function(item, index) {
		item.isnew = 1;
		data.unshift(item);
		newitems = 1;
	});

	// cut it to 12
	data = data.slice(0,12);
	if (newitems) {
		fill();
	}
}

function startfetch() {
	getdata();
	new PeriodicalExecuter(getdata, 30);
}

function fill() {

	data.each(function(item, index) {
		var holder = 'hold_' + index;
		if (item.isnew == 1) { 		
			Element.hide(holder);
			new Effect.Appear(holder, { afterFinish:fadeend });
			new Effect.Highlight(holder);
			item.isnew = 0;
		}
		if ($(holder)) {
			$(holder).innerHTML = '<h2><a href="' + item.url + '">' + item.title + '</a></h2>' + item.source + ' - ' + item.nicedate;
		}
	});

}

function fadeend() {

	new Effect.Opacity('hold_8', { duration: 0.0, from: 1, to: 0.7 });
	new Effect.Opacity('hold_9', { duration: 0.0, from: 1, to: 0.5 });
	new Effect.Opacity('hold_10', { duration: 0.0, from: 1, to: 0.3 });
	new Effect.Opacity('hold_11', { duration: 0.0, from: 1, to: 0.1 });

}
