function update_status() {
	$.get("/dispatcher/messages/status-ajax/", function(data) {
		try {
			_status = eval(data)[0];
		} catch(SyntaxError) {
			return;
		}

		messages_link = $("#nav-messages span");
		messages_link.get(0).innerHTML = "Messages ("+_status.total_pending_messages+"/"+_status.total_messages+")"
		
		
		// Refresh message list						
		m = $("#msg-list-area");
		if(m.length > 0){
			$.get("/dispatcher/messages/part/", {page: inbox_page},function(data) {
				area = m.get(0);
				area.innerHTML = data	
				
				init_msg_list_sort()
			})
			
		}
		
		if (_status.high_priority_exists) {
			start_blink(messages_link);
		} else {
			stop_blink(messages_link);
		}
	});
}

function toogle_visibility(obj) {
	return function() {
		if ($(obj).css("color") == "red")
			$(obj).css("color", "#000099");
		else
			$(obj).css("color", "red");
	}
}

function init_blink() {
	$(".hint").each(function(index, obj) {
		$(obj).everyTime("500ms", "hint", toogle_visibility($(obj)));
	});
}

function start_blink(obj) {
	if ($(obj).hasClass("hint"))
		return;

	$(obj).addClass("hint");
	$(obj).everyTime("500ms", "hint", toogle_visibility($(obj)));	
}

function stop_blink(obj) {
	$(obj).removeClass("hint");
	$(obj).stopTime("hint");
	$(obj).css("color", "");
}

function init_msg_list_sort() {
	if ($("#msg-list tbody td").get(0).colSpan == 1) {
		
		$.tablesorter.addParser({
			id: 'priority',
			is: function(s) {
				return false;
			},
			format: function(s) {
				return s.toLowerCase().replace(/low/,0).replace(/medium/,1).replace(/high/,2); 
			},
			type: 'numeric'
		});
		
		$.tablesorter.addParser({
			id: 'status',
			is: function(s) {
				return false;
			},
			format: function(s) {
				return s.toLowerCase().replace(/new/,0).replace(/in progress/,1).replace(/completed/,2); 
			},
			type: 'numeric'
		});	
		
		$("#msg-list").tablesorter({
			headers: { 
				5: {sorter: 'priority'},
				6: {sorter: 'status'},
				7: {sorter: false}  
			},
			widgets: ['zebra', 'cookie'],
	        widgetCookie: {sortList: [[0,0]]}			
		});
		
		
		
	}
}