var timestamp=0;
var busy = false;
var animating = false;

var items = Array();
var new_items = 0;
var animation_colors = Array("#ffc891", "#ffd3a6", "#ffd7ae", "#ffdcb9", "#FFE2C5", "#ffe3c8", "#ffe9d3", "#ffeddb", "#fff0e1", "#fff6ee", "#fffaf6", "#fffdfa", "#ffffff");
var colors_max = animation_colors.length - 1;
var current_colors = Array();
var animation_timer;
var max_items = 20;
var min_update = 5000;
var next_update = 5000;
var requests = 0;
var max_requests = 3000;

function start() {
	for (i=0; i<max_items; i++) {
		items[i] = document.getElementById('live2-'+i);
	}
	get_data();
}

function get_data() {
	$.get('/esi/live_data.php?CACHED', null, function(result) {
		received_data(result);
	});
requests++;
	
	return false;
}

function set_initial_color(i) {
	var j;
	if (i >= colors_max)
		j = colors_max - 1;
	else j = i;
	current_colors[i] = j;
	document.getElementById('live2-'+i).style.backgroundColor = animation_colors[j];
}

function animate_background() {
	if (animating) {
		return;
	}
	if (current_colors[0] == colors_max) {
		clearInterval(animation_timer);
		return;
	}
	animating = true;
	for (i=new_items-1; i>=0; i--) {
		if (current_colors[i] < colors_max) {
			current_colors[i]++;
			document.getElementById('live2-'+i).style.backgroundColor = animation_colors[current_colors[i]];
		} else 
			new_items--;
	}
	animating = false;
}


function received_data(reda) {
		timestamp = reda.timestamp;
		var new_data = reda.new_data;
		new_items= new_data.length;
		if(new_items > 0) {
			
			clearInterval(animation_timer);
			next_update = Math.round(0.5*next_update + 0.5*min_update/(new_items*2));
			shift_items(new_items);
			
			for (i=0; i<new_items && i<max_items; i++) {
				
				document.getElementById('live2-'+i).innerHTML = to_html(new_data[i]);
				set_initial_color(i);
			}
			animation_timer = setInterval('animate_background()', 100)
		} else next_update = Math.round(next_update*1.25);
	
	if (next_update < 5000) next_update = 5000;
	if (next_update > min_update) next_update = min_update;
	if (requests > max_requests) {
		if ( !confirm('Timeout: Would you like to try to reconnect?') ) {
			mnm_banner_reload = 0;
			return;
		}
		requests = 0;
		next_update = 100;
	}
	timer = setTimeout('get_data()', next_update)
	
}

function shift_items(n) {
	
	for (i=max_items-1;i>=n;i--) {
		document.getElementById('live2-'+i).innerHTML = document.getElementById('live2-'+Math.round(i-n)).innerHTML;
	}
}

function to_html(data) {
	var ts=new Date(data.ts*1000);
	var timeStr;

	var hours = ts.getHours();
	var minutes = ts.getMinutes();
	var seconds = ts.getSeconds();

	timeStr  = ((hours < 10) ? "0" : "") + hours;
	timeStr  += ((minutes < 10) ? ":0" : ":") + minutes;
	timeStr  += ((seconds < 10) ? ":0" : ":") + seconds;
	
	var Xts = new Date();
	var diff = (Xts - ts) / 60000; // how manys minutes since
	if(diff > 1440){timeStr = '> 25 Hours';} // 1440 = 60 mins/hr * 24 hrs/day

	html = '<div class="live2-ts">'+timeStr+'</div>';

	if (data.type == 'problem')
		html += '<div class="live2-type"><span class="live2-problem">'+data.type+'</span></div>';
	else if (data.type == 'new')
		html += '<div class="live2-type"><strong>Tilføjet</strong></div>';
	else if (data.type == 'published')
		html += '<div class="live2-type"><strong><a href="/loggen/udgivede">Udgivet</a></strong></div>';	
	else if (data.type == 'comment')
		html += '<div class="live2-type"><strong><a href="/loggen/kommentarer">Kommentar</a></strong></div>';		
		else if (data.type == 'vote')
		html += '<div class="live2-type"><strong>Stemme op</strong></div>';	
	else if(data.type=='report')
	html += '<div class="live2-type"><strong>Stemme ned</strong></div>';	
	else
		html += '<div class="live2-type">'+data.type+'</div>';

	html += '<div class="live2-votes"><center>'+data.votes+'</center></div>';
	html += '<div class="live2-story"><a href="'+data.link+'">'+data.title+'</a></div>';
	if (data.type == 'problem')
		html += '<div class="live2-who"><span class="live2-problem">'+data.who+'</span></div>';
	else if (data.uid > 0) 
		html += '<div class="live2-who"><a href="/profil/'+data.who+'"><center>'+data.who2+'</center></a></div>';
	else 
		html += '<div class="live2-who">'+data.who2+'</div>';
	if(data.status=='queued')
		html += '<div class="live2-status">Bobler</div>';
	
	else if(data.status=='published')
		html += '<div class="live2-status">Udgivet</div>';
	
	else if(data.status=='discarded')
		html += '<div class="live2-status">Slettet</div>';
		
	else if(data.status=='report')
	html += '<div class="live2-status">Stemme ned</div>';
	
	else
			html += '<div class="live2-status">Stemme ned '+data.status+'</div>';
	
	
	return html;

}


